binpac: Run clang-format on c++ code embedded in pac_parse.{ll,yy}

This commit is contained in:
Tim Wojtulewicz 2025-08-21 11:10:39 -07:00
parent ea0ffd3ec1
commit 64b3265eb8
2 changed files with 125 additions and 152 deletions

View file

@ -1073,33 +1073,32 @@ optlinebreaker : /* nothing */
const ID* current_decl_id = 0; const ID* current_decl_id = 0;
int yyerror(const char msg[]) int yyerror(const char msg[]) {
{ auto n = strlen(msg) + yyleng + 64;
auto n = strlen(msg) + yyleng + 64; char* msgbuf = new char[n];
char* msgbuf = new char[n];
if ( ! yychar || ! yytext || yytext[0] == '\0' ) if ( ! yychar || ! yytext || yytext[0] == '\0' )
snprintf(msgbuf, n, "%s, at end of file", msg); snprintf(msgbuf, n, "%s, at end of file", msg);
else if ( yytext[0] == '\n' ) else if ( yytext[0] == '\n' )
snprintf(msgbuf, n, "%s, on previous line", msg); snprintf(msgbuf, n, "%s, on previous line", msg);
else else
snprintf(msgbuf, n, "%s, at or near \"%s\"", msg, yytext); snprintf(msgbuf, n, "%s, at or near \"%s\"", msg, yytext);
/* /*
extern int column; extern int column;
sprintf(msgbuf, "%*s\n%*s\n", column, "^", column, msg); sprintf(msgbuf, "%*s\n%*s\n", column, "^", column, msg);
*/ */
if ( ! input_filename.empty() ) if ( ! input_filename.empty() )
fprintf(stderr, "%s:%d: ", input_filename.c_str(), line_number); fprintf(stderr, "%s:%d: ", input_filename.c_str(), line_number);
else else
fprintf(stderr, "line %d: ", line_number); fprintf(stderr, "line %d: ", line_number);
fprintf(stderr, "%s", msgbuf); fprintf(stderr, "%s", msgbuf);
fprintf(stderr, " (yychar=%d)", yychar); fprintf(stderr, " (yychar=%d)", yychar);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
delete[] msgbuf; delete[] msgbuf;
return 0; return 0;
} }

View file

@ -45,38 +45,36 @@ int line_number = 1;
int begin_pac_primitive(int tok); int begin_pac_primitive(int tok);
int end_pac_primitive(); int end_pac_primitive();
int string_token(int tok) int string_token(int tok) {
{ yylval.str = copy_string(yytext);
yylval.str = copy_string(yytext); return tok;
return tok; }
}
int char_token(int tok) int char_token(int tok) {
{ yylval.val = yytext[0];
yylval.val = yytext[0]; return tok;
return tok; }
}
void include_file(const char *filename); void include_file(const char* filename);
std::string do_dirname(std::string_view s) std::string do_dirname(std::string_view s) {
{
#ifdef _MSC_VER #ifdef _MSC_VER
return std::filesystem::path(s).parent_path().string(); return std::filesystem::path(s).parent_path().string();
#else #else
std::unique_ptr<char[]> tmp{new char[s.size()+1]}; std::unique_ptr<char[]> tmp{new char[s.size() + 1]};
strncpy(tmp.get(), s.data(), s.size()); strncpy(tmp.get(), s.data(), s.size());
tmp[s.size()] = '\0'; tmp[s.size()] = '\0';
char* dn = dirname(tmp.get()); char* dn = dirname(tmp.get());
if ( !dn ) if ( ! dn )
return ""; return "";
std::string res{dn}; std::string res{dn};
return res; return res;
#endif #endif
} }
%} %}
/* EC -- embedded code state */ /* EC -- embedded code state */
@ -283,133 +281,109 @@ ESCSEQ (\\([^\n]|[0-7]{3}|x[[:xdigit:]]{2}))
%% %%
void begin_RE() void begin_RE() {
{ BEGIN(RE);
BEGIN(RE); }
}
void end_RE() void end_RE() { BEGIN(INITIAL); }
{
BEGIN(INITIAL);
}
// The DECL state is deprecated // The DECL state is deprecated
void begin_decl() void begin_decl() {
{ // BEGIN(DECL);
// BEGIN(DECL); }
}
void end_decl() void end_decl() {
{ // BEGIN(INITIAL);
// BEGIN(INITIAL); }
}
int begin_pac_primitive(int tok) int begin_pac_primitive(int tok) {
{ BEGIN(PP);
BEGIN(PP); return tok;
return tok; }
}
int end_pac_primitive() int end_pac_primitive() {
{ BEGIN(EC);
BEGIN(EC); return TOK_END_PAC;
return TOK_END_PAC; }
}
const int MAX_INCLUDE_DEPTH = 100; constexpr int MAX_INCLUDE_DEPTH = 100;
struct IncludeState { struct IncludeState {
YY_BUFFER_STATE yystate; YY_BUFFER_STATE yystate;
string input_filename; string input_filename;
int line_number; int line_number;
}; };
IncludeState include_stack[MAX_INCLUDE_DEPTH]; IncludeState include_stack[MAX_INCLUDE_DEPTH];
int include_stack_ptr = 0; int include_stack_ptr = 0;
void switch_to_file(FILE *fp) void switch_to_file(FILE* fp) { yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE)); }
{
yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
}
void switch_to_file(const char *filename) void switch_to_file(const char* filename) {
{ if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) fprintf(stderr, "Includes nested too deeply");
{ exit(1);
fprintf( stderr, "Includes nested too deeply" ); }
exit( 1 );
}
IncludeState state = IncludeState state = {YY_CURRENT_BUFFER, input_filename, line_number};
{ YY_CURRENT_BUFFER, input_filename, line_number }; include_stack[include_stack_ptr++] = state;
include_stack[include_stack_ptr++] = state;
FILE *fp = fopen(filename, "r"); FILE* fp = fopen(filename, "r");
if ( ! fp ) if ( ! fp ) {
{ fprintf(stderr, "%s:%d: error: cannot include file \"%s\"\n", input_filename.c_str(), line_number, filename);
fprintf(stderr, "%s:%d: error: cannot include file \"%s\"\n", exit(1);
input_filename.c_str(), line_number,filename); }
exit( 1 );
}
yyin = fp; yyin = fp;
input_filename = string(filename); input_filename = string(filename);
line_number = 1; line_number = 1;
switch_to_file(yyin); switch_to_file(yyin);
if ( !FLAGS_quiet ) if ( ! FLAGS_quiet )
fprintf(stderr, "switching to file %s\n", input_filename.c_str()); fprintf(stderr, "switching to file %s\n", input_filename.c_str());
} }
void include_file(const char *filename) void include_file(const char* filename) {
{ ASSERT(filename);
ASSERT(filename);
string full_filename; string full_filename;
if ( filename[0] == '/' ) if ( filename[0] == '/' )
full_filename = filename; full_filename = filename;
else if ( filename[0] == '.' ) else if ( filename[0] == '.' ) {
{ string dir = do_dirname(input_filename);
string dir = do_dirname(input_filename);
if ( ! dir.empty() ) if ( ! dir.empty() )
full_filename = dir + "/" + filename; full_filename = dir + "/" + filename;
else else {
{ fprintf(stderr, "%s:%d error: cannot include file \"%s\": %s\n", input_filename.c_str(), line_number,
fprintf(stderr, "%s:%d error: cannot include file \"%s\": %s\n", filename, strerror(errno));
input_filename.c_str(), line_number, filename, exit(1);
strerror(errno)); }
exit( 1 ); }
} else {
} int i;
else for ( i = 0; i < (int)FLAGS_include_directories.size(); ++i ) {
{ full_filename = FLAGS_include_directories[i] + filename;
int i; DEBUG_MSG("Try include file: \"%s\"\n", full_filename.c_str());
for ( i = 0; i < (int) FLAGS_include_directories.size(); ++i ) if ( access(full_filename.c_str(), R_OK) == 0 )
{ break;
full_filename = FLAGS_include_directories[i] + filename; }
DEBUG_MSG("Try include file: \"%s\"\n", if ( i >= (int)FLAGS_include_directories.size() )
full_filename.c_str()); full_filename = filename;
if ( access(full_filename.c_str(), R_OK) == 0 ) }
break;
}
if ( i >= (int) FLAGS_include_directories.size() )
full_filename = filename;
}
switch_to_file(full_filename.c_str()); switch_to_file(full_filename.c_str());
} }
int yywrap() int yywrap() {
{ yy_delete_buffer(YY_CURRENT_BUFFER);
yy_delete_buffer(YY_CURRENT_BUFFER); --include_stack_ptr;
--include_stack_ptr; if ( include_stack_ptr < 0 )
if ( include_stack_ptr < 0 ) return 1;
return 1;
IncludeState state = include_stack[include_stack_ptr]; IncludeState state = include_stack[include_stack_ptr];
yy_switch_to_buffer(state.yystate); yy_switch_to_buffer(state.yystate);
input_filename = state.input_filename; input_filename = state.input_filename;
line_number = state.line_number; line_number = state.line_number;
return 0; return 0;
} }