Commit deeefeab authored by di68kap's avatar di68kap
Browse files

some refactoring

parent 7946cb56
DHParser Version 0.9.7
......................
- Packaging changed to poetry
DHParser Version 0.9.6 (1.7.2021)
.................................
......
......@@ -3584,7 +3584,7 @@ class Capture(ContextSensitive):
"Cannot capture content from parsers that drop content!"
self.grammar.variables__[self.pname].append(node.content)
self.grammar.push_rollback__(self._rollback_location(text, text_), self._rollback)
# memoizing will be blocked by parser guard (see way above),
# memoizing will be blocked by push_rollback__(),
# because it would prevent recapturing of rolled back captures
return self._return_value(node), text_
else:
......
[match:element]
[ast:element]
[fail:element]
[match:group]
[ast:group]
[fail:group]
[match:unordered]
[ast:unordered]
[fail:unordered]
[match:interleave]
[ast:interleave]
[fail:interleave]
[match:oneormore]
[ast:oneormore]
[fail:oneormore]
[match:repetition]
[ast:repetition]
[fail:repetition]
[match:option]
[ast:option]
[fail:option]
[match:expression]
[ast:expression]
[fail:expression]
[match:term]
[ast:term]
[fail:term]
[match:factor]
[ast:factor]
[fail:factor]
[match:syntax]
[ast:syntax]
[fail:syntax]
[match:definition]
[ast:definition]
[fail:definition]
[match:directive]
[ast:directive]
[fail:directive]
[match:_CHARACTERS]
[ast:_CHARACTERS]
[fail:_CHARACTERS]
[match:PLAIN]
[ast:PLAIN]
[fail:PLAIN]
[match:ESCAPE]
[ast:ESCAPE]
[fail:ESCAPE]
[match:UNICODE]
[ast:UNICODE]
[fail:UNICODE]
[match:HEX]
[ast:HEX]
[fail:HEX]
[match:INT]
[ast:INT]
[fail:INT]
[match:NEG]
[ast:NEG]
[fail:NEG]
[match:FRAC]
[ast:FRAC]
[fail:FRAC]
[match:DOT]
[ast:DOT]
[fail:DOT]
[match:EXP]
[ast:EXP]
[fail:EXP]
[match:_EOF]
[ast:_EOF]
[fail:_EOF]
[match:json]
[ast:json]
[fail:json]
[match:_element]
[ast:_element]
[fail:_element]
[match:object]
[ast:object]
[fail:object]
[match:member]
[ast:member]
[fail:member]
[match:array]
[ast:array]
[fail:array]
[match:string]
[ast:string]
[fail:string]
[match:number]
[ast:number]
[fail:number]
[match:bool]
[ast:bool]
[fail:bool]
[match:null]
[ast:null]
[fail:null]
[match:syntax]
[ast:syntax]
[fail:syntax]
[match:definition]
[ast:definition]
[fail:definition]
[match:directive]
[ast:directive]
[fail:directive]
[match:literals]
[ast:literals]
[fail:literals]
[match:procedure]
[ast:procedure]
[fail:procedure]
[match:FOLLOW_UP]
[ast:FOLLOW_UP]
[fail:FOLLOW_UP]
......@@ -75,13 +75,11 @@ class FixedEBNFGrammar(Grammar):
countable = Forward()
element = Forward()
expression = Forward()
source_hash__ = "8dbc09df6de2f2758e43fc351a3671c7"
source_hash__ = "0da7ed4d4fae3d6fa358ddd7867ccadb"
disposable__ = re.compile('component$|pure_elem$|countable$|FOLLOW_UP$|SYM_REGEX$|ANY_SUFFIX$|EOF$')
static_analysis_pending__ = [] # type: List[bool]
parser_initialization__ = ["upon instantiation"]
error_messages__ = {'definition': [(re.compile(r','), 'Delimiter "," not expected in definition!\\nEither this was meant to be a directive and the directive symbol @ is missing\\nor the error is due to inconsistent use of the comma as a delimiter\\nfor the elements of a sequence.')]}
resume_rules__ = {'definition': [re.compile(r'\n\s*(?=@|\w+\w*\s*=)')],
'directive': [re.compile(r'\n\s*(?=@|\w+\w*\s*=)')]}
COMMENT__ = r'(?!#x[A-Fa-f0-9])#.*(?:\n|$)|\/\*(?:.|\n)*?\*\/|\(\*(?:.|\n)*?\*\)'
comment_rx__ = re.compile(COMMENT__)
WHITESPACE__ = r'\s*'
......@@ -126,9 +124,9 @@ class FixedEBNFGrammar(Grammar):
retrieveop = Alternative(Series(Text("::"), dwsp__), Series(Text(":?"), dwsp__), Series(Text(":"), dwsp__))
flowmarker = Alternative(Series(Text("!"), dwsp__), Series(Text("&"), dwsp__), Series(Text("<-!"), dwsp__), Series(Text("<-&"), dwsp__))
ANY_SUFFIX = RegExp('[?*+]')
literals = OneOrMore(literal)
pure_elem = Series(element, NegativeLookahead(ANY_SUFFIX), mandatory=1)
procedure = Series(SYM_REGEX, Series(Text("()"), dwsp__))
pure_elem = Series(element, NegativeLookahead(ANY_SUFFIX), mandatory=1)
component = Alternative(procedure, expression)
term = Alternative(oneormore, counted, repetition, option, pure_elem)
difference = Series(term, Option(Series(Series(Text("-"), dwsp__), Alternative(oneormore, pure_elem), mandatory=1)))
lookaround = Series(flowmarker, Alternative(oneormore, pure_elem), mandatory=1)
......@@ -136,12 +134,14 @@ class FixedEBNFGrammar(Grammar):
sequence = Series(Option(Series(Text("§"), dwsp__)), Alternative(interleave, lookaround), ZeroOrMore(Series(AND, dwsp__, Option(Series(Text("§"), dwsp__)), Alternative(interleave, lookaround))))
FOLLOW_UP = Alternative(Text("@"), symbol, EOF)
definition = Series(symbol, DEF, dwsp__, Option(Series(OR, dwsp__)), expression, ENDL, dwsp__, Lookahead(FOLLOW_UP), mandatory=1)
component = Alternative(regexp, literals, procedure, Series(symbol, NegativeLookahead(DEF)))
directive = Series(Series(Text("@"), dwsp__), symbol, Series(Text("="), dwsp__), component, ZeroOrMore(Series(Series(Text(","), dwsp__), component)), Lookahead(FOLLOW_UP), mandatory=1)
literals = OneOrMore(literal)
element.set(Alternative(Series(Option(retrieveop), symbol, NegativeLookahead(DEF)), literal, plaintext, regexp, Series(character, dwsp__), any_char, whitespace, group))
countable.set(Alternative(option, oneormore, element))
expression.set(Series(sequence, ZeroOrMore(Series(OR, dwsp__, sequence))))
syntax = Series(dwsp__, ZeroOrMore(Alternative(definition, directive)), EOF)
resume_rules__ = {'definition': [re.compile(r'\n\s*(?=@|\w+\w*\s*=)')],
'directive': [re.compile(r'\n\s*(?=@|\w+\w*\s*=)')]}
root__ = syntax
......@@ -153,6 +153,11 @@ def get_grammar() -> FixedEBNFGrammar:
resume_notices_on(grammar)
elif get_config_value('history_tracking'):
set_tracer(grammar, trace_history)
try:
if not grammar.__class__.python_src__:
grammar.__class__.python_src__ = get_grammar.python_src__
except AttributeError:
pass
return grammar
def parse_FixedEBNF(document, start_parser = "root_parser__", *, complete_match=True):
......@@ -165,71 +170,71 @@ def parse_FixedEBNF(document, start_parser = "root_parser__", *, complete_match=
#
#######################################################################
FixedEBNF_AST_transformation_table = {
# AST Transformations for the FixedEBNF-grammar
"<": flatten,
"syntax": [],
"definition": [],
"directive": [],
"literals": [],
"procedure": [],
"FOLLOW_UP": [],
"expression": [],
"sequence": [],
"interleave": [],
"lookaround": [],
"difference": [],
"term": [],
"countable": [],
"pure_elem": [],
"element": [],
"ANY_SUFFIX": [],
"flowmarker": [],
"retrieveop": [],
"group": [],
"oneormore": [],
"repetition": [],
"option": [],
"counted": [],
"range": [],
"no_range": [],
"multiplier": [],
"symbol": [],
"literal": [],
"plaintext": [],
"regexp": [],
"char_range": [],
"character": [],
"free_char": [],
"any_char": [],
"whitespace": [],
"EOF": [],
"DEF": [],
"OR": [],
"AND": [],
"ENDL": [],
"RNG_OPEN": [],
"RNG_CLOSE": [],
"RNG_DELIM": [],
"TIMES": [],
"RE_LEADIN": [],
"RE_LEADOUT": [],
"CH_LEADIN": [],
"char_range_heuristics": [],
"literal_heuristics": [],
"regex_heuristics": [],
"RE_CORE": [],
"SYM_REGEX": [],
"HEXCODE": [],
"*": replace_by_single_child
EBNF_AST_transformation_table = {
# AST Transformations for the EBNF-grammar
"<":
[remove_children_if(all_of(not_one_of('regexp'), is_empty))],
"syntax":
[],
"directive":
[flatten, remove_tokens('@', '=', ',')],
"procedure":
[remove_tokens('()'), reduce_single_child],
"literals":
[replace_by_single_child],
"definition":
[flatten, remove_children('DEF', 'ENDL'),
remove_tokens('=')], # remove_tokens('=') is only for backwards-compatibility
"expression":
[replace_by_single_child, flatten, remove_children('OR'),
remove_tokens('|')], # remove_tokens('|') is only for backwards-compatibility
"sequence":
[replace_by_single_child, flatten, remove_children('AND')],
"interleave":
[replace_by_single_child, flatten, remove_tokens('°')],
"lookaround":
[],
"difference":
[remove_tokens('-'), replace_by_single_child],
"term, pure_elem, element":
[replace_by_single_child],
"flowmarker, retrieveop":
[reduce_single_child],
"group":
[remove_brackets],
"oneormore, repetition, option":
[reduce_single_child, remove_brackets, # remove_tokens('?', '*', '+'),
forbid('repetition', 'option', 'oneormore'), assert_content(r'(?!§)(?:.|\n)*')],
"counted":
[remove_children('TIMES')],
"range":
[remove_children('BRACE_SIGN', 'RNG_BRACE', 'RNG_DELIM')],
"symbol, literal, any_char":
[reduce_single_child],
"plaintext":
[],
"regexp":
[remove_children('RE_LEADIN', 'RE_LEADOUT'), reduce_single_child],
"char_range":
[flatten, remove_tokens('[', ']')],
"character":
[remove_children('CH_LEADIN'), reduce_single_child],
"free_char":
[],
(TOKEN_PTYPE, WHITESPACE_PTYPE, "whitespace"):
[reduce_single_child],
"EOF, DEF, OR, AND, ENDL, BRACE_SIGN, RNG_BRACE, RNG_DELIM, TIMES, "
"RE_LEADIN, RE_CORE, RE_LEADOUT, CH_LEADIN":
[],
"*":
[replace_by_single_child]
}
def CreateFixedEBNFTransformer() -> TransformationFunc:
"""Creates a transformation function that does not share state with other
threads or processes."""
return partial(traverse, processing_table=FixedEBNF_AST_transformation_table.copy())
return partial(traverse, processing_table=EBNF_AST_transformation_table.copy())
def get_transformer() -> TransformationFunc:
......
This diff is collapsed.
# FixedEBNF
PLACE A SHORT DESCRIPTION HERE
Author: AUTHOR'S NAME <EMAIL>, AFFILIATION
## License
FixedEBNF is open source software under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0)
Copyright YEAR AUTHOR'S NAME <EMAIL>, AFFILIATION
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment