Skip to content

Rewrite exported function - #21

Merged
lysnikolaou merged 3 commits into
pegenfrom
exported-function-rewrite
Mar 24, 2020
Merged

lysnikolaou merged 3 commits into
pegenfrom
exported-function-rewrite

Conversation

@lysnikolaou

Copy link
Copy Markdown
Member
  • It now initializes the keywords field so that the keywords variable can be static.
  • It also accepts an argument, in order to be able to run the parser with different start rules.

    * It now initializes the keywords field so that the keywords
      variable can be static.
    * It also accepts an argument, in order to be able to
      run the parser with different start rules.

@pablogsal pablogsal left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that the pegen tests are failing because you need to get rid of the extern const int n_keyword_lists in peg_parser/pegen.h:

python3.8 -m pegen -q -c data/python.gram -o peg_parser/parse.c --compile-extension
peg_parser/parse.c:3:18: error: static declaration of ‘n_keyword_lists’ follows non-static declaration
 static const int n_keyword_lists = 9;
                  ^~~~~~~~~~~~~~~
In file included from peg_parser/parse.c:2:0:
peg_parser/pegen.h:84:18: note: previous declaration of ‘n_keyword_lists’ was here
 extern const int n_keyword_lists;
                  ^~~~~~~~~~~~~~~
peg_parser/parse.c:4:22: error: static declaration of ‘reserved_keywords’ follows non-static declaration
 static KeywordToken *reserved_keywords[] = {
                      ^~~~~~~~~~~~~~~~~
In file included from peg_parser/parse.c:2:0:
peg_parser/pegen.h:85:22: note: previous declaration of ‘reserved_keywords’ was here
 extern KeywordToken *reserved_keywords[];
                      ^~~~~~~~~~~~~~~~~

Comment thread Parser/pegen/pegen.h
};
typedef enum INPUT_MODE INPUT_MODE;

enum START_RULE {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this enum of only one field?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll probably add more, we we need to choose between start rules. I'm currently working on maybe changing the start rule for f-string to expressions_rule and for this I added another field EXPRESSIONS to this enum.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Probably for a separate PR.)

Maybe we could pass a string literal instead? By convention we could have NULL -> start and a few other names could be recognized based on metadata given in the .gram file, e.g. [1]

@alternate_starts "single_input file_input eval_input"

which would generate a little table at the end

static struct { char *, mod_ty (*)(Parser *) } [] alternate_starts = {
    {"single_input", single_input_rule},
    ...
    NULL,
};

and then the dispatch function could just look up the argument in the table. (This is a negligible cost given everything else that goes into parsing even one line.)


[1] We'd need to add a few new rules named single_input, file_input, eval_input that correspond to those targets in Grammar/Grammar.

Comment thread Parser/pegen/pegen.c
@lysnikolaou

Copy link
Copy Markdown
Member Author

Seems that the pegen tests are failing because you need to get rid of the extern const int n_keyword_lists in peg_parser/pegen.h

I intentionally chose to ignore these failures, since we are about to remove all the C code from Tools/peg_generator/peg_parser and only rely on the C code in Parser/pegen. See #14 and we-like-parsers/pegen_experiments#235.

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bingo.

Comment thread Grammar/python.gram Outdated

@bytecode True
@modulename 'peg_parser' # Non needed for now, but might be needed later
@modulename 'peg_parser' # Not needed for now, but might be needed later

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I'd take it out until we find we need it again.

Comment thread Parser/pegen/pegen.h
};
typedef enum INPUT_MODE INPUT_MODE;

enum START_RULE {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Probably for a separate PR.)

Maybe we could pass a string literal instead? By convention we could have NULL -> start and a few other names could be recognized based on metadata given in the .gram file, e.g. [1]

@alternate_starts "single_input file_input eval_input"

which would generate a little table at the end

static struct { char *, mod_ty (*)(Parser *) } [] alternate_starts = {
    {"single_input", single_input_rule},
    ...
    NULL,
};

and then the dispatch function could just look up the argument in the table. (This is a negligible cost given everything else that goes into parsing even one line.)


[1] We'd need to add a few new rules named single_input, file_input, eval_input that correspond to those targets in Grammar/Grammar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants