Commit 9a6df97d authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

further changes to LaTeX ebnf-transformation

parent 3978f82c
...@@ -317,9 +317,9 @@ class EBNFCompiler(Compiler): ...@@ -317,9 +317,9 @@ class EBNFCompiler(Compiler):
self.current_symbols = [] # type: List[Node] self.current_symbols = [] # type: List[Node]
self.symbols = {} # type: Dict[str, Node] self.symbols = {} # type: Dict[str, Node]
self.variables = set() # type: Set[str] self.variables = set() # type: Set[str]
self.definitions = [] # type: List[Tuple[str, str]] # self.definitions = [] # type: List[Tuple[str, str]]
self.recursive = set() # type: Set[str] self.recursive = set() # type: Set[str]
self.deferred_tasks = [] # type: List[Callable] self.deferred_tasks = [] # type: List[Callable]
self.root = "" # type: str self.root = "" # type: str
self.directives = {'whitespace': self.WHITESPACE['horizontal'], self.directives = {'whitespace': self.WHITESPACE['horizontal'],
'comment': '', 'comment': '',
......
...@@ -98,6 +98,10 @@ def get_report(test_unit): ...@@ -98,6 +98,10 @@ def get_report(test_unit):
""" """
Returns a text-report of the results of a grammar unit test. Returns a text-report of the results of a grammar unit test.
""" """
def indent(txt):
lines = txt.split('\n')
lines[0] = ' ' + lines[0]
return "\n ".join(lines)
report = [] report = []
for parser_name, tests in test_unit.items(): for parser_name, tests in test_unit.items():
heading = 'Test of parser: "%s"' % parser_name heading = 'Test of parser: "%s"' % parser_name
...@@ -106,7 +110,7 @@ def get_report(test_unit): ...@@ -106,7 +110,7 @@ def get_report(test_unit):
heading = 'Match-test "%s"' % test_name heading = 'Match-test "%s"' % test_name
report.append('\n%s\n%s\n' % (heading, '-' * len(heading))) report.append('\n%s\n%s\n' % (heading, '-' * len(heading)))
report.append('### Test-code:') report.append('### Test-code:')
report.append(test_code) report.append(indent(test_code))
error = tests.get('__err__', {}).get(test_name, "") error = tests.get('__err__', {}).get(test_name, "")
if error: if error:
report.append('\n### Error:') report.append('\n### Error:')
...@@ -115,10 +119,10 @@ def get_report(test_unit): ...@@ -115,10 +119,10 @@ def get_report(test_unit):
cst = tests.get('__cst__', {}).get(test_name, None) cst = tests.get('__cst__', {}).get(test_name, None)
if cst and (not ast or cst == ast): if cst and (not ast or cst == ast):
report.append('\n### CST') report.append('\n### CST')
report.append(cst.as_sxpr()) report.append(indent(cst.as_sxpr()))
elif ast: elif ast:
report.append('\n### AST') report.append('\n### AST')
report.append(ast.as_sxpr()) report.append(indent(ast.as_sxpr()))
return '\n'.join(report) return '\n'.join(report)
...@@ -199,7 +203,7 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report=True, ve ...@@ -199,7 +203,7 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report=True, ve
report_dir = "REPORT" report_dir = "REPORT"
if not os.path.exists(report_dir): if not os.path.exists(report_dir):
os.mkdir(report_dir) os.mkdir(report_dir)
with open(os.path.join(report_dir, unit_name + '.report'), 'w') as f: with open(os.path.join(report_dir, unit_name + '.md'), 'w') as f:
f.write(get_report(test_unit)) f.write(get_report(test_unit))
return errata return errata
......
...@@ -314,7 +314,7 @@ def expand_table(compact_table): ...@@ -314,7 +314,7 @@ def expand_table(compact_table):
value = compact_table[key] value = compact_table[key]
for k in smart_list(key): for k in smart_list(key):
if k in expanded_table: if k in expanded_table:
raise KeyError("Key %s used more than once in compact table!" % key) raise KeyError('Key "%s" used more than once in compact table!' % key)
expanded_table[k] = value expanded_table[k] = value
return expanded_table return expanded_table
......
...@@ -102,7 +102,7 @@ known_command = footnote | includegraphics | caption ...@@ -102,7 +102,7 @@ known_command = footnote | includegraphics | caption
generic_command = !no_command CMDNAME [[ //~ config ] //~ block ] generic_command = !no_command CMDNAME [[ //~ config ] //~ block ]
footnote = "\footnote" block_of_paragraphs footnote = "\footnote" block_of_paragraphs
includegraphics = "\includegraphics" config block includegraphics = "\includegraphics" [ config ] block
caption = "\caption" block caption = "\caption" block
......
...@@ -346,7 +346,7 @@ LaTeX_AST_transformation_table = { ...@@ -346,7 +346,7 @@ LaTeX_AST_transformation_table = {
"block_environment": replace_by_single_child, "block_environment": replace_by_single_child,
"known_environment": replace_by_single_child, "known_environment": replace_by_single_child,
"generic_block": [], "generic_block": [],
"begin_generic_block, end_generic_block": reduce_single_child, "begin_generic_block, end_generic_block": replace_by_single_child,
"itemize, enumerate": [remove_brackets, flatten], "itemize, enumerate": [remove_brackets, flatten],
"item": [remove_first], "item": [remove_first],
"figure": [], "figure": [],
...@@ -361,7 +361,7 @@ LaTeX_AST_transformation_table = { ...@@ -361,7 +361,7 @@ LaTeX_AST_transformation_table = {
"inline_environment": replace_by_single_child, "inline_environment": replace_by_single_child,
"known_inline_env": replace_by_single_child, "known_inline_env": replace_by_single_child,
"generic_inline_env": [], "generic_inline_env": [],
"begin_inline_env, end_inline_env": [reduce_single_child], "begin_inline_env, end_inline_env": [replace_by_single_child],
"begin_environment, end_environment": [remove_brackets, reduce_single_child], "begin_environment, end_environment": [remove_brackets, reduce_single_child],
"inline_math": [remove_brackets, reduce_single_child], "inline_math": [remove_brackets, reduce_single_child],
"command": [], "command": [],
...@@ -372,11 +372,9 @@ LaTeX_AST_transformation_table = { ...@@ -372,11 +372,9 @@ LaTeX_AST_transformation_table = {
"caption": [], "caption": [],
"config": [remove_brackets], "config": [remove_brackets],
"block": [remove_brackets, reduce_single_child(is_anonymous)], "block": [remove_brackets, reduce_single_child(is_anonymous)],
"text": [reduce_single_child, merge_children('word_sequence', ':Whitespace', 'TEXTCHUNK')], "text": collapse,
"cfgtext": [flatten, reduce_single_child, replace_parser('text')], "cfgtext, word_sequence": [],
"word_sequence": collapse, # [flatten, merge_children('TEXTCHUNK', ':Whitespace'), reduce_single_child], "no_command, blockcmd": [],
"no_command": [],
"blockcmd": [],
"structural": [], "structural": [],
"CMDNAME": [remove_whitespace, reduce_single_child(is_anonymous)], "CMDNAME": [remove_whitespace, reduce_single_child(is_anonymous)],
"NAME": [reduce_single_child, remove_whitespace, reduce_single_child], "NAME": [reduce_single_child, remove_whitespace, reduce_single_child],
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
8 : Unknwon \xy commands within paragraphs may be simple 8 : Unknwon \xy commands within paragraphs may be simple
or \xy{complex}. or \xy{complex}.
[fail:paragraph] [fail:paragraph]
1 : \begin{enumerate} 1 : \begin{enumerate}
2 : \item 2 : \item
......
[match:text]
1: Some plain text
2: Text containing [ brackets ] is distinguished from cfgtext
3: Text can also "escaped" characters like \&, \% or \_ .
4: Text can contain both brackets [] and "esacped" characters \& .
...@@ -35,7 +35,7 @@ if not DHParser.dsl.recompile_grammar('LaTeX.ebnf', force=True): # recompiles G ...@@ -35,7 +35,7 @@ if not DHParser.dsl.recompile_grammar('LaTeX.ebnf', force=True): # recompiles G
from DHParser import toolkit from DHParser import toolkit
from LaTeXCompiler import get_grammar, get_transformer from LaTeXCompiler import get_grammar, get_transformer
with toolkit.logging(True): with toolkit.logging(False):
error_report = testing.grammar_suite('grammar_tests', get_grammar, error_report = testing.grammar_suite('grammar_tests', get_grammar,
get_transformer, report=True, verbose=True) get_transformer, report=True, verbose=True)
if error_report: if error_report:
......
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