Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
51de138c
Commit
51de138c
authored
Jul 11, 2021
by
Eckhart Arnold
Browse files
parser.py, ebnf.py: Allow Parsers for resume-skipping (refactorings pending)
parent
bb7e46ea
Changes
2
Hide whitespace changes
Inline
Side-by-side
DHParser/testing.py
View file @
51de138c
...
...
@@ -40,11 +40,10 @@ import time
from
typing
import
Dict
,
List
,
Union
,
Deque
,
cast
from
DHParser.configuration
import
get_config_value
from
DHParser.error
import
Error
,
is_error
,
add_source_locations
,
PARSER_LOOKAHEAD_MATCH_ONLY
,
\
from
DHParser.error
import
Error
,
is_error
,
PARSER_LOOKAHEAD_MATCH_ONLY
,
\
PARSER_LOOKAHEAD_FAILURE_ONLY
,
MANDATORY_CONTINUATION_AT_EOF
,
AUTORETRIEVED_SYMBOL_NOT_CLEARED
from
DHParser.log
import
is_logging
,
clear_logs
,
local_log_dir
,
log_parsing_history
from
DHParser.parse
import
Lookahead
from
DHParser.preprocess
import
gen_neutral_srcmap_func
from
DHParser.server
import
RX_CONTENT_LENGTH
,
RE_DATA_START
,
JSONRPC_HEADER_BYTES
from
DHParser.syntaxtree
import
Node
,
RootNode
,
deserialize
,
flatten_sxpr
,
ZOMBIE_TAG
from
DHParser.trace
import
set_tracer
,
all_descendants
,
trace_history
...
...
@@ -345,15 +344,7 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report='REPORT'
"""Returns True if the parser or any of its descendant parsers is a
Lookahead parser."""
return
parser
[
parser_name
].
apply
(
lambda
ctx
:
isinstance
(
ctx
[
-
1
],
Lookahead
))
# lookahead_found = False
#
# def find_lookahead(p: Parser):
# nonlocal lookahead_found
# if not lookahead_found:
# lookahead_found = isinstance(p, Lookahead)
#
# parser[parser_name].apply(find_lookahead)
# return lookahead_found
def
lookahead_artifact
(
syntax_tree
:
Node
):
"""
...
...
@@ -586,6 +577,7 @@ def unique_name(file_name: str) -> str:
time
.
sleep
(
1.0
/
resolution
)
return
name
def
grammar_suite
(
directory
,
parser_factory
,
transformer_factory
,
fn_patterns
=
(
'*test*'
,),
ignore_unknown_filetypes
=
False
,
...
...
examples/ts2dataclass/ts2dataclassParser.py
View file @
51de138c
...
...
@@ -13,6 +13,7 @@ import os
import
sys
from
typing
import
Tuple
,
List
,
Union
,
Any
,
Optional
,
Callable
,
Type
,
cast
try
:
scriptpath
=
os
.
path
.
dirname
(
__file__
)
except
NameError
:
...
...
@@ -52,7 +53,6 @@ from DHParser import start_logging, suspend_logging, resume_logging, is_filename
gen_find_include_func
,
preprocess_includes
,
make_preprocessor
,
chain_preprocessors
USE_PYTHON_3_10_TYPE_UNION
=
False
# https://www.python.org/dev/peps/pep-0604/
...
...
@@ -366,7 +366,7 @@ RESULT_FILE_EXTENSION = ".sxpr" # Change this according to your needs!
def
compile_src
(
source
:
str
)
->
Tuple
[
Any
,
List
[
Error
]]:
"""Compiles ``source`` and returns (result, errors
, ast
)."""
"""Compiles ``source`` and returns (result, errors)."""
result_tuple
=
compile_source
(
source
,
get_preprocessor
(),
get_grammar
(),
get_transformer
(),
get_compiler
())
return
result_tuple
[:
2
]
# drop the AST at the end of the result tuple
...
...
@@ -445,6 +445,51 @@ def batch_process(file_names: List[str], out_dir: str,
return
error_list
INSPECT_TEMPLATE
=
"""<h2>{testname}</h2>
<h3>AST</h3>
<code style="background-color: lightgrey;">
{ast_str}
</code>
<h3>Program code</h3>
<code style="background-color: yellow;">
{code}
</code>
"""
def
inspect
(
test_file_path
:
str
):
assert
test_file_path
[
-
4
:]
==
'.ini'
from
DHParser.testing
import
unit_from_file
test_unit
=
unit_from_file
(
test_file_path
)
grammar
=
get_grammar
()
transformer
=
get_transformer
()
compiler
=
get_compiler
()
results
=
[]
for
parser
in
test_unit
:
for
testname
,
test_code
in
test_unit
[
parser
].
get
(
'match'
,
dict
()).
items
():
ast
=
grammar
(
test_code
,
parser
)
ast_str
=
ast
.
as_tree
()
transformer
(
ast
)
code
=
compiler
(
ast
)
results
.
append
(
INSPECT_TEMPLATE
.
format
(
testname
=
testname
,
ast_str
=
ast_str
,
code
=
code
))
test_file_name
=
os
.
path
.
basename
(
test_file_path
)
results_str
=
'
\n
'
.
join
(
results
)
html
=
f
'''<html>
<head><meta charset="utf-8"><title>
{
test_file_name
}
</title></head>
<body>
<h1>
{
test_file_name
}
<h1>
{
results_str
}
</body></html>'''
destdir
=
os
.
path
.
join
(
os
.
path
.
dirname
(
test_file_path
),
"REPORT"
)
if
not
os
.
path
.
exists
(
destdir
):
os
.
mkdir
(
destdir
)
destpath
=
os
.
path
.
join
(
destdir
,
test_file_name
[:
-
4
]
+
'.html'
)
with
open
(
destpath
,
'w'
,
encoding
=
'utf-8'
)
as
f
:
f
.
write
(
html
)
import
webbrowser
webbrowser
.
open
(
'file://'
+
destpath
if
sys
.
platform
==
"darwin"
else
destpath
)
if
__name__
==
"__main__"
:
# recompile grammar if needed
if
__file__
.
endswith
(
'Parser.py'
):
...
...
@@ -534,6 +579,10 @@ if __name__ == "__main__":
print
(
'
\n
'
.
join
(
error_files
))
if
category
==
"ERRORS"
:
sys
.
exit
(
1
)
elif
file_names
[
0
][
-
4
:]
==
'.ini'
:
inspect
(
file_names
[
0
])
else
:
result
,
errors
=
compile_src
(
file_names
[
0
])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment