-
-
Notifications
You must be signed in to change notification settings - Fork 452
Fixes for PR #1506, which adds the option to cache the grammar definition #1540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| """Tree matcher based on Lark grammar""" | ||
|
|
||
| import re | ||
| from typing import List, Dict | ||
| from collections import defaultdict | ||
|
|
||
| from . import Tree, Token | ||
| from . import Tree, Token, Lark | ||
| from .common import ParserConf | ||
| from .exceptions import ConfigurationError | ||
| from .parsers import earley | ||
| from .grammar import Rule, Terminal, NonTerminal | ||
|
|
||
|
|
@@ -39,7 +41,7 @@ | |
| return list(d.values()) | ||
|
|
||
|
|
||
| def _best_rules_from_group(rules): | ||
| def _best_rules_from_group(rules: List[Rule]) -> List[Rule]: | ||
| rules = _best_from_group(rules, lambda r: r, lambda r: -len(r.expansion)) | ||
| rules.sort(key=lambda r: len(r.expansion)) | ||
| return rules | ||
|
|
@@ -85,12 +87,23 @@ | |
| Initialize with an instance of Lark. | ||
| """ | ||
| rules_for_root: Dict[str, List[Rule]] | ||
| rules: List[Rule] | ||
| parser: Lark | ||
|
|
||
| def __init__(self, parser): | ||
| def __init__(self, parser: Lark): | ||
| # XXX TODO calling compile twice returns different results! | ||
| assert not parser.options.maybe_placeholders | ||
| # XXX TODO: we just ignore the potential existence of a postlexer | ||
| self.tokens, rules, _extra = parser.grammar.compile(parser.options.start, set()) | ||
|
|
||
| if parser.options.postlex and parser.options.postlex.always_accept: | ||
| # If postlexer's always_accept is used, we need to recompile the grammar with empty terminals-to-keep | ||
| if not hasattr(parser, 'grammar'): | ||
| raise ConfigurationError('Source grammar not available from cached parser, use cache_grammar=True' | ||
| if parser.options.cache else "Source grammar not available!") | ||
| self.tokens, rules, _extra = parser.grammar.compile(parser.options.start, set()) | ||
| else: | ||
| self.tokens = list(parser.terminals) | ||
| rules = list(parser.rules) | ||
|
|
||
| self.rules_for_root = defaultdict(list) | ||
|
|
||
|
|
@@ -101,9 +114,9 @@ | |
| self.rules = _best_rules_from_group(self.rules) | ||
|
|
||
| self.parser = parser | ||
| self._parser_cache = {} | ||
| self._parser_cache: Dict[str, earley.Parser] = {} | ||
|
|
||
| def _build_recons_rules(self, rules): | ||
| def _build_recons_rules(self, rules: List[Rule]): | ||
|
||
| "Convert tree-parsing/construction rules to tree-matching rules" | ||
| expand1s = {r.origin for r in rules if r.options.expand1} | ||
|
|
||
|
|
@@ -145,7 +158,7 @@ | |
| yield make_recons_rule_to_term(origin, NonTerminal(alias)) | ||
| yield make_recons_rule_to_term(origin, origin) | ||
|
|
||
| def match_tree(self, tree, rulename): | ||
| def match_tree(self, tree: Tree, rulename: str) -> Tree: | ||
| """Match the elements of `tree` to the symbols of rule `rulename`. | ||
| Parameters: | ||
|
|
@@ -159,7 +172,7 @@ | |
| UnexpectedToken: If no match was found. | ||
| Note: | ||
| It's the callers' responsibility match the tree recursively. | ||
| It's the callers' responsibility to match the tree recursively. | ||
| """ | ||
| if rulename: | ||
| # validate | ||
|
|
@@ -176,11 +189,11 @@ | |
|
|
||
| # TODO pass callbacks through dict, instead of alias? | ||
| callbacks = {rule: rule.alias for rule in rules} | ||
| conf = ParserConf(rules, callbacks, [rulename]) | ||
| conf = ParserConf(rules, callbacks, [rulename]) # type: ignore[arg-type] | ||
| parser = earley.Parser(self.parser.lexer_conf, conf, _match, resolve_ambiguity=True) | ||
| self._parser_cache[rulename] = parser | ||
|
|
||
| # find a full derivation | ||
| unreduced_tree = parser.parse(ChildrenLexer(tree.children), rulename) | ||
| unreduced_tree: Tree = parser.parse(ChildrenLexer(tree.children), rulename) | ||
| assert unreduced_tree.data == rulename | ||
| return unreduced_tree | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Use a consistent container type for
__serialize_fields__(e.g., a list) to match the pattern in other classes and avoid confusion.