Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
DHParser
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
badw-it
DHParser
Commits
5d9dfd50
Commit
5d9dfd50
authored
Apr 21, 2017
by
Eckhart Arnold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- moved Mock Object Creation for syntax trees from
tests/test_syntaxtree.py to syntaxtree.py
parent
1e0bc6d8
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
91 additions
and
57 deletions
+91
-57
DHParser/syntaxtree.py
DHParser/syntaxtree.py
+58
-9
tests/test_dsl.py
tests/test_dsl.py
+27
-0
tests/test_syntaxtree.py
tests/test_syntaxtree.py
+6
-48
No files found.
DHParser/syntaxtree.py
View file @
5d9dfd50
...
@@ -22,6 +22,8 @@ permissions and limitations under the License.
...
@@ -22,6 +22,8 @@ permissions and limitations under the License.
import
itertools
import
itertools
import
os
import
os
from
functools
import
partial
from
functools
import
partial
try
:
try
:
import
regex
as
re
import
regex
as
re
except
ImportError
:
except
ImportError
:
...
@@ -56,7 +58,19 @@ __all__ = ['WHITESPACE_KEYWORD',
...
@@ -56,7 +58,19 @@ __all__ = ['WHITESPACE_KEYWORD',
'assert_content'
]
'assert_content'
]
class
ZombieParser
:
class
MockParser
:
def
__init__
(
self
,
name
=
''
):
self
.
name
=
name
def
__str__
(
self
):
return
self
.
name
or
self
.
__class__
.
__name__
def
__call__
(
self
,
text
):
"""Better call Saul ;-)"""
return
None
,
text
class
ZombieParser
(
MockParser
):
"""
"""
Serves as a substitute for a Parser instance.
Serves as a substitute for a Parser instance.
...
@@ -69,9 +83,9 @@ class ZombieParser:
...
@@ -69,9 +83,9 @@ class ZombieParser:
alive
=
False
alive
=
False
def
__init__
(
self
):
def
__init__
(
self
):
super
(
ZombieParser
,
self
).
__init__
(
"ZOMBIE"
)
assert
not
self
.
__class__
.
alive
,
"There can be only one!"
assert
not
self
.
__class__
.
alive
,
"There can be only one!"
assert
self
.
__class__
==
ZombieParser
,
"No derivatives, please!"
assert
self
.
__class__
==
ZombieParser
,
"No derivatives, please!"
self
.
name
=
"ZOMBIE"
self
.
__class__
.
alive
=
True
self
.
__class__
.
alive
=
True
def
__copy__
(
self
):
def
__copy__
(
self
):
...
@@ -80,13 +94,6 @@ class ZombieParser:
...
@@ -80,13 +94,6 @@ class ZombieParser:
def
__deepcopy__
(
self
,
memo
):
def
__deepcopy__
(
self
,
memo
):
return
self
return
self
def
__str__
(
self
):
return
self
.
name
def
__call__
(
self
,
text
):
"""Better call Saul ;-)"""
return
None
,
text
ZOMBIE_PARSER
=
ZombieParser
()
ZOMBIE_PARSER
=
ZombieParser
()
...
@@ -381,6 +388,48 @@ class Node:
...
@@ -381,6 +388,48 @@ class Node:
return
nav
(
path
.
split
(
'/'
))
return
nav
(
path
.
split
(
'/'
))
def
mock_syntax_tree
(
sexpr
):
"""Generates a tree of nodes from an S-expression.
Example:
>>> mock_syntax_tree("(a (b c))").as_sexpr()
(a
(b
c
)
)
"""
def
next_block
(
s
):
s
=
s
.
strip
()
while
s
[
0
]
!=
')'
:
assert
s
[
0
]
==
'('
,
s
level
=
1
;
i
=
1
while
level
>
0
:
if
s
[
i
]
==
'('
:
level
+=
1
elif
s
[
i
]
==
')'
:
level
-=
1
i
+=
1
yield
s
[:
i
]
s
=
s
[
i
:].
strip
()
sexpr
=
sexpr
.
strip
()
assert
sexpr
[
0
]
==
'('
,
sexpr
sexpr
=
sexpr
[
1
:].
strip
()
m
=
re
.
match
(
'\w+'
,
sexpr
)
name
=
sexpr
[:
m
.
end
()]
sexpr
=
sexpr
[
m
.
end
():].
strip
()
if
sexpr
[
0
]
==
'('
:
result
=
tuple
(
mock_syntax_tree
(
block
)
for
block
in
next_block
(
sexpr
))
else
:
m
=
re
.
match
(
'\w+'
,
sexpr
)
result
=
sexpr
[:
m
.
end
()]
sexpr
=
sexpr
[
m
.
end
():].
strip
()
assert
sexpr
[
0
]
==
')'
,
sexpr
return
Node
(
MockParser
(
name
),
result
)
########################################################################
########################################################################
#
#
# syntax tree transformation functions
# syntax tree transformation functions
...
...
tests/test_dsl.py
View file @
5d9dfd50
...
@@ -20,4 +20,31 @@ See the License for the specific language governing permissions and
...
@@ -20,4 +20,31 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
"""
"""
import
os
import
sys
from
DHParser.dsl
import
run_compiler
class
TestCompilerGeneration
:
trivial_lang
=
"""
text = { word | WSPC }
word = /\w+/
WSPC = /\s*/
"""
trivial_text
=
"""Es war ein König in Thule."""
def
setup
(
self
):
if
(
not
os
.
path
.
exists
(
'PopRetrieve_compiler.py'
)
or
suite_outdated
(
'PopRetrieve_compiler.py'
,
'PopRetrieve.ebnf'
)):
print
(
"recompiling PopRetrieve parser"
)
errors
=
run_compiler
(
"PopRetrieve.ebnf"
)
if
errors
:
print
(
'
\n\n
'
.
join
(
errors
))
sys
.
exit
(
1
)
if
__name__
==
"__main__"
:
from
run
import
runner
runner
(
""
,
globals
())
\ No newline at end of file
tests/test_syntaxtree.py
View file @
5d9dfd50
...
@@ -20,14 +20,13 @@ limitations under the License.
...
@@ -20,14 +20,13 @@ limitations under the License.
"""
"""
import
os
import
os
import
re
import
sys
import
sys
sys
.
path
.
append
(
os
.
path
.
abspath
(
'../../'
))
sys
.
path
.
append
(
os
.
path
.
abspath
(
'../../'
))
from
DHParser.toolkit
import
compact_sexpr
from
DHParser.toolkit
import
compact_sexpr
from
DHParser.syntaxtree
import
Node
,
travers
e
from
DHParser.syntaxtree
import
traverse
,
mock_syntax_tre
e
class
Dummy
Parser
:
class
Mock
Parser
:
def
__init__
(
self
,
name
=
''
):
def
__init__
(
self
,
name
=
''
):
self
.
name
=
name
self
.
name
=
name
...
@@ -38,47 +37,6 @@ class DummyParser:
...
@@ -38,47 +37,6 @@ class DummyParser:
return
None
,
text
return
None
,
text
def
from_sexpr
(
s
):
"""Generates a tree of nodes from an S-expression.
Example:
>>> from_sexpr("(a (b c))").as_sexpr()
(a
(b
c
)
)
"""
def
next_block
(
s
):
s
=
s
.
strip
()
while
s
[
0
]
!=
')'
:
assert
s
[
0
]
==
'('
,
s
level
=
1
;
i
=
1
while
level
>
0
:
if
s
[
i
]
==
'('
:
level
+=
1
elif
s
[
i
]
==
')'
:
level
-=
1
i
+=
1
yield
s
[:
i
]
s
=
s
[
i
:].
strip
()
s
=
s
.
strip
()
assert
s
[
0
]
==
'('
,
s
s
=
s
[
1
:].
strip
()
m
=
re
.
match
(
'\w+'
,
s
)
name
=
s
[:
m
.
end
()]
s
=
s
[
m
.
end
():].
strip
()
if
s
[
0
]
==
'('
:
result
=
tuple
(
from_sexpr
(
block
)
for
block
in
next_block
(
s
))
else
:
m
=
re
.
match
(
'\w+'
,
s
)
result
=
s
[:
m
.
end
()]
s
=
s
[
m
.
end
():].
strip
()
assert
s
[
0
]
==
')'
,
s
return
Node
(
DummyParser
(
name
),
result
)
class
TestSExpr
:
class
TestSExpr
:
"""
"""
Tests for S-expression handling.
Tests for S-expression handling.
...
@@ -88,7 +46,7 @@ class TestSExpr:
...
@@ -88,7 +46,7 @@ class TestSExpr:
def
test_selftest_from_sexpr
(
self
):
def
test_selftest_from_sexpr
(
self
):
sexpr
=
'(a (b c) (d e) (f (g h)))'
sexpr
=
'(a (b c) (d e) (f (g h)))'
tree
=
from_sexpr
(
sexpr
)
tree
=
mock_syntax_tree
(
sexpr
)
assert
compact_sexpr
(
tree
.
as_sexpr
(
prettyprint
=
False
))
==
sexpr
assert
compact_sexpr
(
tree
.
as_sexpr
(
prettyprint
=
False
))
==
sexpr
...
@@ -98,9 +56,9 @@ class TestNode:
...
@@ -98,9 +56,9 @@ class TestNode:
"""
"""
def
setup
(
self
):
def
setup
(
self
):
self
.
unique_nodes_sexpr
=
'(a (b c) (d e) (f (g h)))'
self
.
unique_nodes_sexpr
=
'(a (b c) (d e) (f (g h)))'
self
.
unique_tree
=
from_sexpr
(
self
.
unique_nodes_sexpr
)
self
.
unique_tree
=
mock_syntax_tree
(
self
.
unique_nodes_sexpr
)
self
.
recurring_nodes_sexpr
=
'(a (b x) (c (d e) (b y)))'
self
.
recurring_nodes_sexpr
=
'(a (b x) (c (d e) (b y)))'
self
.
recurr_tree
=
from_sexpr
(
self
.
recurring_nodes_sexpr
)
self
.
recurr_tree
=
mock_syntax_tree
(
self
.
recurring_nodes_sexpr
)
def
test_str
(
self
):
def
test_str
(
self
):
assert
str
(
self
.
unique_tree
)
==
"ceh"
assert
str
(
self
.
unique_tree
)
==
"ceh"
...
@@ -117,7 +75,7 @@ class TestNode:
...
@@ -117,7 +75,7 @@ class TestNode:
class
TestErrorHandling
:
class
TestErrorHandling
:
def
test_error_flag_propagation
(
self
):
def
test_error_flag_propagation
(
self
):
tree
=
from_sexpr
(
'(a (b c) (d (e (f (g h)))))'
)
tree
=
mock_syntax_tree
(
'(a (b c) (d (e (f (g h)))))'
)
def
find_h
(
node
):
def
find_h
(
node
):
if
node
.
result
==
"h"
:
if
node
.
result
==
"h"
:
...
...
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