Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
feefd262
Commit
feefd262
authored
Jun 22, 2017
by
Eckhart Arnold
Browse files
- bugfixes
parent
70ee21b7
Changes
4
Hide whitespace changes
Inline
Side-by-side
DHParser/parsers.py
View file @
feefd262
...
...
@@ -328,7 +328,12 @@ class Grammar:
self
.
dirty_flag
=
False
self
.
history_tracking
=
False
self
.
_reset
()
self
.
_assign_parser_names
()
# prepare class first
# prepare parsers in the class, first
self
.
_assign_parser_names
()
# then deep-copy the parser tree from class to instance;
# parsers not connected to the root object will be copied later
# on demand (see Grammar.__getitem__()). Usually, the need to
# do so only arises during testing.
self
.
root__
=
root
if
root
else
copy
.
deepcopy
(
self
.
__class__
.
root__
)
if
self
.
wspL__
:
self
.
wsp_left_parser__
=
Whitespace
(
self
.
wspL__
)
# type: ParserBase
...
...
@@ -350,8 +355,14 @@ class Grammar:
except
KeyError
:
parser
=
getattr
(
self
,
key
,
None
)
if
parser
:
raise
KeyError
((
'Parser "%s" inaccesible, because it is not connected '
'to the root parser "%s" !'
)
%
(
key
,
self
.
root__
.
name
))
# if toolkit.warnings():
# raise KeyError(('Parser "%s" inaccesible, because it is not connected '
# 'to the root parser "%s" !') % (key, self.root__.name))
print
(
'Parser "%s" not connected to root parser.'
%
key
)
# add parser to grammar object on the fly...
setattr
(
self
,
key
,
copy
.
deepcopy
(
parser
))
self
[
key
].
apply
(
self
.
_add_parser
)
# might become a problem with all_parsers
return
self
[
key
]
raise
KeyError
(
'Unknown parser "%s" !'
%
key
)
def
_reset
(
self
):
...
...
@@ -369,7 +380,7 @@ class Grammar:
# TODO: Either make sure not to miss out unconnected parsers or raise an error! Actually, the EBNF-Compiler should keep track of this!
def
_add_parser
(
self
,
parser
:
Parser
)
->
None
:
"""Adds the copy of the
classes
parser object to this
"""Adds the
particular
copy of the parser object to this
particular instance of Grammar.
"""
if
parser
.
name
:
...
...
examples/LaTeX/LaTeX.ebnf
View file @
feefd262
...
...
@@ -6,7 +6,7 @@
latexdoc = preamble document
preamble = { command }+
document = [PARSEP]
sequence
[PARSEP] §EOF
document = [PARSEP]
{
[PARSEP]
paragraph }
§EOF
genericenv = beginenv sequence §endenv
beginenv = "\begin" §( "{" NAME "}" )
...
...
examples/LaTeX/tst_grammar.py
View file @
feefd262
...
...
@@ -27,8 +27,9 @@ from DHParser import toolkit
from
LaTeXCompiler
import
get_grammar
,
get_transformer
with
toolkit
.
logging
(
True
):
error_report
=
testing
.
grammar_suite
(
'grammar_tests'
,
get_grammar
,
get_transformer
,
verbose
=
True
)
with
toolkit
.
supress_warnings
(
True
):
error_report
=
testing
.
grammar_suite
(
'grammar_tests'
,
get_grammar
,
get_transformer
,
verbose
=
True
)
assert
not
error_report
,
error_report
test/test_ebnf.py
View file @
feefd262
...
...
@@ -20,12 +20,13 @@ See the License for the specific language governing permissions and
limitations under the License.
"""
import
sys
from
functools
import
partial
from
multiprocessing
import
Pool
import
sys
sys
.
path
.
extend
([
'../'
,
'./'
])
from
DHParser.toolkit
import
is_logging
,
compile_python_object
from
DHParser.toolkit
import
is_logging
,
compile_python_object
,
supress_warnings
from
DHParser.parsers
import
compile_source
,
Retrieve
,
WHITESPACE_PTYPE
,
nil_scanner
from
DHParser.ebnf
import
get_ebnf_grammar
,
get_ebnf_transformer
,
EBNFTransformer
,
get_ebnf_compiler
from
DHParser.dsl
import
CompilationError
,
compileDSL
,
DHPARSER_IMPORTS
,
parser_factory
...
...
@@ -348,7 +349,8 @@ class TestBoundaryCases:
unconnected = /.*/
"""
try
:
grammar
=
parser_factory
(
ebnf
)()
with
supress_warnings
(
False
):
grammar
=
parser_factory
(
ebnf
)()
assert
False
,
"EBNF compiler should complain about unconnected rules."
except
CompilationError
as
err
:
grammar_src
=
err
.
result
...
...
@@ -357,10 +359,8 @@ class TestBoundaryCases:
assert
grammar
[
'root'
],
"Grammar objects should be subscriptable by parser names!"
try
:
unconnected
=
grammar
[
'unconnected'
]
assert
False
,
"Grammar objects should raise a KeyError if subscripted by "
\
"names of parsers that are unconnected to the root parser!"
except
KeyError
:
p
ass
ass
ert
False
,
"Grammar objects should be able to cope with unconnected parsers!"
try
:
nonexistant
=
grammar
[
'nonexistant'
]
assert
False
,
"Grammar object shoul raise a KeyError if subscripted by "
\
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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