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,8 +1073,7 @@ 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];
@ -1102,4 +1101,4 @@ int yyerror(const char msg[])
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,40 +281,32 @@ 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;
@ -327,81 +317,65 @@ struct IncludeState {
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) {
if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
fprintf(stderr, "Includes nested too deeply");
exit(1);
} }
void switch_to_file(const char *filename) IncludeState state = {YY_CURRENT_BUFFER, input_filename, line_number};
{
if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
{
fprintf( stderr, "Includes nested too deeply" );
exit( 1 );
}
IncludeState state =
{ 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 else {
{
int i; int i;
for ( i = 0; i < (int) FLAGS_include_directories.size(); ++i ) for ( i = 0; i < (int)FLAGS_include_directories.size(); ++i ) {
{
full_filename = FLAGS_include_directories[i] + filename; full_filename = FLAGS_include_directories[i] + filename;
DEBUG_MSG("Try include file: \"%s\"\n", DEBUG_MSG("Try include file: \"%s\"\n", full_filename.c_str());
full_filename.c_str());
if ( access(full_filename.c_str(), R_OK) == 0 ) if ( access(full_filename.c_str(), R_OK) == 0 )
break; break;
} }
if ( i >= (int) FLAGS_include_directories.size() ) if ( i >= (int)FLAGS_include_directories.size() )
full_filename = filename; 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 )
@ -412,4 +386,4 @@ int yywrap()
input_filename = state.input_filename; input_filename = state.input_filename;
line_number = state.line_number; line_number = state.line_number;
return 0; return 0;
} }