]> git.draconx.ca Git - cdecl99.git/commitdiff
Factor out common parser invocation.
authorNick Bowler <nbowler@draconx.ca>
Thu, 1 Jun 2023 00:29:41 +0000 (20:29 -0400)
committerNick Bowler <nbowler@draconx.ca>
Thu, 1 Jun 2023 00:34:04 +0000 (20:34 -0400)
The two main parsing functions have nearly identical parser invocation
sequences, with the only difference being the flag passed to the scanner
init.  Split that off into a separate function, to simplify the code.

Additionally, if the code is compiled with YYDEBUG, enable parser
debugging.

src/parse-decl.c

index f00442d31712c2dffef54ecf10bb8aa6c0b8d66b..1b38153216e16b588324318c851a3c660f327a5a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Parse and validate C declarations.
- * Copyright © 2011-2012, 2020-2021 Nick Bowler
+ * Copyright © 2011-2012, 2020-2021, 2023 Nick Bowler
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -470,26 +470,36 @@ static bool forall_declarators(struct cdecl *decl,
        return true;
 }
 
-struct cdecl *cdecl_parse_decl(const char *declstr)
+static struct cdecl *do_parse(const char *str, int english_mode)
 {
-       struct cdecl_declspec *norm_specs;
        YY_BUFFER_STATE state;
        yyscan_t scanner;
        struct cdecl *decl;
-       int rc;
 
-       cdecl__init_i18n();
+#if YYDEBUG
+       extern int cdecl__yydebug;
+       cdecl__yydebug = 1;
+#endif
 
-       rc = cdecl__yylex_init(&scanner);
-       if (rc != 0)
+       cdecl__init_i18n();
+       if (cdecl__yylex_init_extra(english_mode, &scanner) != 0)
                return NULL;
 
-       state = cdecl__yy_scan_string(declstr, scanner);
-       rc = cdecl__yyparse(scanner, &decl);
+       state = cdecl__yy_scan_string(str, scanner);
+       if (cdecl__yyparse(scanner, &decl) != 0)
+               decl = NULL;
        cdecl__yy_delete_buffer(state, scanner);
        cdecl__yylex_destroy(scanner);
 
-       if (rc != 0)
+       return decl;
+}
+
+struct cdecl *cdecl_parse_decl(const char *declstr)
+{
+       struct cdecl_declspec *norm_specs;
+       struct cdecl *decl;
+
+       if (!(decl = do_parse(declstr, false)))
                return NULL;
 
        /*
@@ -537,23 +547,9 @@ err:
 
 struct cdecl *cdecl_parse_english(const char *english)
 {
-       YY_BUFFER_STATE state;
-       yyscan_t scanner;
        struct cdecl *decl;
-       int rc;
-
-       cdecl__init_i18n();
-
-       rc = cdecl__yylex_init_extra(true, &scanner);
-       if (rc != 0)
-               return NULL;
-
-       state = cdecl__yy_scan_string(english, scanner);
-       rc = cdecl__yyparse(scanner, &decl);
-       cdecl__yy_delete_buffer(state, scanner);
-       cdecl__yylex_destroy(scanner);
 
-       if (rc != 0)
+       if (!(decl = do_parse(english, true)))
                return NULL;
 
        for (struct cdecl *i = decl; i; i = i->next) {