Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
DHParser
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
badw-it
DHParser
Commits
2b52940c
Commit
2b52940c
authored
Feb 09, 2019
by
Eckhart Arnold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- Adjusted Arithmetic example
parent
eb54faa3
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
26 additions
and
18 deletions
+26
-18
dsl.py
DHParser/dsl.py
+1
-1
ebnf.py
DHParser/ebnf.py
+1
-1
syntaxtree.py
DHParser/syntaxtree.py
+4
-3
transform.py
DHParser/transform.py
+12
-5
dhparser.py
dhparser.py
+1
-1
Arithmetic.ebnf
examples/Arithmetic/Arithmetic.ebnf
+1
-1
ArithmeticCompiler.py
examples/Arithmetic/ArithmeticCompiler.py
+6
-5
example.dsl
examples/Arithmetic/example.dsl
+0
-1
No files found.
DHParser/dsl.py
View file @
2b52940c
...
...
@@ -102,7 +102,7 @@ from DHParser import logging, is_filename, load_if_file, \\
keep_children, is_one_of, not_one_of, has_content, apply_if, remove_first, remove_last,
\\
remove_anonymous_empty, keep_nodes, traverse_locally, strip, lstrip, rstrip,
\\
replace_content, replace_content_by, forbid, assert_content, remove_infix_operator,
\\
error_on, recompile_grammar, GLOBALS
reduce_anonymous_nodes,
error_on, recompile_grammar, GLOBALS
'''
.
format
(
dhparserdir
=
dhparserdir
)
...
...
DHParser/ebnf.py
View file @
2b52940c
...
...
@@ -562,7 +562,7 @@ class EBNFCompiler(Compiler):
tt_name
=
self
.
grammar_name
+
'_AST_transformation_table'
transtable
=
[
tt_name
+
' = {'
,
' # AST Transformations for the '
+
self
.
grammar_name
+
'-grammar'
]
transtable
.
append
(
' "<":
eliminat
e_anonymous_nodes,'
)
transtable
.
append
(
' "<":
reduc
e_anonymous_nodes,'
)
for
name
in
self
.
rules
:
transformations
=
'[]'
# rule = self.definitions[name]
...
...
DHParser/syntaxtree.py
View file @
2b52940c
...
...
@@ -313,9 +313,10 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
@
result
.
setter
def
result
(
self
,
result
:
ResultType
):
# # made obsolete by static type checking with mypy
# assert ((isinstance(result, tuple) and all(isinstance(child, Node) for child in result))
# or isinstance(result, Node)
# or isinstance(result, str)), str(result)
assert
((
isinstance
(
result
,
tuple
)
and
all
(
isinstance
(
child
,
Node
)
for
child
in
result
))
or
isinstance
(
result
,
Node
)
or
isinstance
(
result
,
str
)
or
isinstance
(
result
,
StringView
)),
"
%
s (
%
s)"
%
(
str
(
result
),
str
(
type
(
result
)))
# Possible optimization: Do not allow single nodes as argument:
# assert not isinstance(result, Node)
self
.
_len
=
-
1
# lazy evaluation
...
...
DHParser/transform.py
View file @
2b52940c
...
...
@@ -47,7 +47,7 @@ __all__ = ('TransformationDict',
'traverse'
,
'is_named'
,
'update_attr'
,
'
eliminat
e_anonymous_nodes'
,
'
reduc
e_anonymous_nodes'
,
'replace_by_single_child'
,
'reduce_single_child'
,
'replace_or_reduce'
,
...
...
@@ -559,8 +559,13 @@ def _reduce_child(node: Node, child: Node):
# _reduce_child(context[-1], child)
@
transformation_factory
def
eliminate_anonymous_nodes
(
context
:
List
[
Node
]):
def
reduce_anonymous_nodes
(
context
:
List
[
Node
]):
"""
Reduces (non-recursively) all anonymous non-leaf children by adding
their result to the result of the last node in the context. If the
last node is anonymous itself, it will be replaced by a single child.
Also drops any empty anonymous nodes.
"""
node
=
context
[
-
1
]
if
node
.
children
:
new_result
=
[]
...
...
@@ -577,11 +582,13 @@ def eliminate_anonymous_nodes(context: List[Node]):
child
=
new_result
[
0
]
if
node
.
is_anonymous
():
node
.
tag_name
=
child
.
tag_name
n
ew_result
=
[
child
.
result
]
n
ode
.
result
=
child
.
result
update_attr
(
node
,
child
)
return
elif
child
.
is_anonymous
():
n
ew_result
=
[
child
.
result
]
n
ode
.
result
=
child
.
result
update_attr
(
node
,
child
)
return
node
.
result
=
tuple
(
new_result
)
...
...
dhparser.py
View file @
2b52940c
...
...
@@ -16,7 +16,7 @@ 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.
"""
"""
3
# TODO: This is still a stub...
...
...
examples/Arithmetic/Arithmetic.ebnf
View file @
2b52940c
...
...
@@ -10,7 +10,7 @@
@ literalws = right # literals have implicit whitespace on the right hand side
@ comment = /#.*/ # comments range from a '#'-character to the end of the line
@ ignorecase = False # literals and regular expressions are case-sensitive
@ drop = whitespace # drop anonymous whitespace
and tokens
@ drop = whitespace # drop anonymous whitespace
#######################################################################
#
...
...
examples/Arithmetic/ArithmeticCompiler.py
View file @
2b52940c
...
...
@@ -33,7 +33,7 @@ from DHParser import logging, is_filename, load_if_file, \
keep_children
,
is_one_of
,
not_one_of
,
has_content
,
apply_if
,
remove_first
,
remove_last
,
\
remove_anonymous_empty
,
keep_nodes
,
traverse_locally
,
strip
,
lstrip
,
rstrip
,
\
replace_content
,
replace_content_by
,
forbid
,
assert_content
,
remove_infix_operator
,
\
error_on
,
recompile_grammar
,
GLOBALS
error_on
,
recompile_grammar
,
reduce_anonymous_nodes
,
GLOBALS
#######################################################################
...
...
@@ -59,7 +59,7 @@ class ArithmeticGrammar(Grammar):
r"""Parser for an Arithmetic source file.
"""
expression
=
Forward
()
source_hash__
=
"
d03e397fb4cabd6f20f3ae7c9add4ad5
"
source_hash__
=
"
a94242482d508901d1692b82c48ba903
"
parser_initialization__
=
[
"upon instantiation"
]
resume_rules__
=
{}
COMMENT__
=
r'#.*'
...
...
@@ -95,10 +95,11 @@ def get_grammar() -> ArithmeticGrammar:
Arithmetic_AST_transformation_table
=
{
# AST Transformations for the Arithmetic-grammar
"<"
:
re
move_empty
,
"<"
:
re
duce_anonymous_nodes
,
"expression"
:
[],
"term"
:
[],
"factor"
:
[
replace_or_reduce
],
"term"
:
[
reduce_single_child
],
"factor"
:
[
reduce_single_child
],
"group"
:
[
remove_tokens
(
'('
,
')'
),
replace_by_single_child
],
"NUMBER"
:
[],
"VARIABLE"
:
[],
":Token"
:
reduce_single_child
,
...
...
examples/Arithmetic/example.dsl
deleted
100644 → 0
View file @
eb54faa3
Life is but a walking shadow
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