Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
A
arpeggio-gm
Project
Project
Details
Activity
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
backend
arpeggio-gm
Commits
6a197bfd
Commit
6a197bfd
authored
Jul 28, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Making examples testable + test for examples.
parent
2a5b5d24
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
267 additions
and
87 deletions
+267
-87
bibtex.py
examples/bibtex.py
+16
-8
bibtex_example.bib
examples/bibtex_example.bib
+74
-0
calc.py
examples/calc.py
+19
-4
calc_peg.py
examples/calc_peg.py
+25
-17
csv.py
examples/csv.py
+9
-6
json.py
examples/json.py
+14
-7
peg_peg.py
examples/peg_peg.py
+30
-21
robot.py
examples/robot.py
+18
-4
robot_peg.py
examples/robot_peg.py
+8
-4
simple.py
examples/simple.py
+24
-16
test_examples.py
tests/unit/test_examples.py
+30
-0
No files found.
examples/bibtex.py
View file @
6a197bfd
...
...
@@ -3,7 +3,7 @@
# Name: bibtex.py
# Purpose: Parser for bibtex files
# Author: Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2013 Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2013
-2014
Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#
# This example demonstrates grammar and parser for bibtex files.
...
...
@@ -11,7 +11,7 @@
from
__future__
import
print_function
import
pprint
import
sys
import
sys
,
os
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
...
...
@@ -105,27 +105,30 @@ field.sem = FieldSem()
fieldvalue_braces
.
sem
=
FieldValueSem
()
fieldvalue_quotes
.
sem
=
FieldValueSem
()
if
__name__
==
"__main__"
:
def
main
(
debug
=
False
,
file_name
=
None
)
:
# First we will make a parser - an instance of the bib parser model.
# Parser model is given in the form of python constructs therefore we
# are using ParserPython class.
parser
=
ParserPython
(
bibfile
,
reduce_tree
=
True
)
if
debug
:
# Then we export it to a dot file in order to visualise it. This is
# particulary handy for debugging purposes.
# We can make a jpg out of it using dot (part of graphviz) like this
# dot -O -Tjpg calc_parse_tree_model.dot
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"bib_parse_tree_model.dot"
)
# First parameter is bibtex file
if
len
(
sys
.
argv
)
>
1
:
with
open
(
sys
.
argv
[
1
],
"r"
)
as
bibtexfile
:
if
not
file_name
:
file_name
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'bibtex_example.bib'
)
with
open
(
file_name
,
"r"
)
as
bibtexfile
:
bibtexfile_content
=
bibtexfile
.
read
()
# We create a parse tree or abstract syntax tree out of
# textual input
parse_tree
=
parser
.
parse
(
bibtexfile_content
)
if
debug
:
# Then we export it to a dot file in order to visualize it.
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"bib_parse_tree.dot"
)
...
...
@@ -133,8 +136,13 @@ if __name__ == "__main__":
# In this case semantic analysis will list of bibentry maps.
ast
=
parser
.
getASG
()
pp
=
pprint
.
PrettyPrinter
(
indent
=
4
)
pp
.
pprint
(
ast
)
return
ast
if
__name__
==
"__main__"
:
# First parameter is bibtex file
if
len
(
sys
.
argv
)
>
1
:
entries
=
main
(
debug
=
True
,
file_name
=
sys
.
argv
[
1
])
pp
=
pprint
.
PrettyPrinter
(
indent
=
4
)
pp
.
pprint
(
entries
)
else
:
print
(
"Usage: python bibtex.py file_to_parse"
)
examples/bibtex_example.bib
0 → 100644
View file @
6a197bfd
***
*** M6x
***
@inproceedings{DejanovicArpeggioPakratparserinterpreter2010,
author = "Igor Dejanovi\'{c} and Branko Peri\v{s}i\'{c} and Gordana Milosavljevi\'{c}",
title = "Arpeggio: Pakrat parser interpreter",
booktitle = "Zbornik radova na CD-ROM-u, YUInfo 2010",
year = "2010",
address = "Kopaonik",
type = "M63"
}
***
*** M2x
***
@article{DejanovicADomain-SpecificLanguageforDefiningStaticStructureofDatabaseApplications2010,
author = "Igor Dejanovi\'{c} and Gordana Milosavljevi\'{c} and Branko Peri\v{s}i\'{c} and Maja Tumbas",
title = "A {D}omain-Specific Language for Defining Static Structure of Database Applications",
journal = "Computer Science and Information Systems",
year = "2010",
volume = "7",
pages = "409--440",
number = "3",
month = "June",
issn = "1820-0214",
doi = "10.2298/CSIS090203002D",
url = "http://www.comsis.org/ComSIS/Vol7No3/RegularPapers/paper2.htm",
type = "M23"
}
***
*** M3x
***
@inproceedings{MilosavljevicUMLProfileForSpecifyingUI2010,
author = "Gordana Milosavljevi\'{c} and Igor Dejanovi\'{c} and Branko Peri\v{s}i\'{c} and Branko Milosavljevi\'{c}",
title = "UML Profile for Specifying User Interfaces of Business Applications",
booktitle = "Advances in Databases and Information Systems",
year = "2010",
pages = "77-94",
address = "Novi Sad",
type = "M33"
}
@inproceedings{DejanovicComparisonofTextualandVisualNotationsofDOMMLiteDomainSpecificLanguage2010,
author = "Igor Dejanovi\'{c} and Maja Tumbas \v{Z}ivanov and Gordana Milosavljevi\'{c} and Branko Peri\v{s}i\'{c}",
title = "Comparison of Textual and Visual Notations of DOMMLite Domain-Specific Language",
booktitle = "Proceedings of the Advances in Databases and Information Systems",
year = "2010",
pages = "20-24",
address = "Novi Sad",
type = "M33"
}
@article{DejanovicReact2010,
author = "Mirjana Dejanovi\'{c} and Igor Dejanovi\'{c}",
title = "React! An Extensible Software Application for Creating, Performing and Analyzing Results of Psychophysiological Experiments",
journal = "International Journal of Psychophysiology",
year = "2010",
volume = "77",
pages = "301 - 301",
number = "3",
note = "PROCEEDINGS OF THE 15TH WORLD CONGRESS OF PSYCHOPHYSIOLOGY of the
International Organization of Psychophysiology (I.O.P.) Budapest,
Hungary September 1-4, 2010",
doi = "10.1016/j.ijpsycho.2010.06.193",
issn = "0167-8760",
url = "http://www.sciencedirect.com/science/article/B6T3M-50R1KFN-93/2/ca6c51240ae3c5db819938c3fbfd759a",
type = "M34"
}
examples/calc.py
View file @
6a197bfd
...
...
@@ -29,6 +29,7 @@ class ToFloat(SemanticAction):
Converts node value to float.
"""
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Converting {}."
.
format
(
node
.
value
))
return
float
(
node
.
value
)
...
...
@@ -38,6 +39,7 @@ class Factor(SemanticAction):
Removes parenthesis if exists and returns what was contained inside.
"""
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Factor {}"
.
format
(
children
))
if
len
(
children
)
==
1
:
return
children
[
0
]
...
...
@@ -54,6 +56,7 @@ class Term(SemanticAction):
Factor nodes will be already evaluated.
"""
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Term {}"
.
format
(
children
))
term
=
children
[
0
]
for
i
in
range
(
2
,
len
(
children
),
2
):
...
...
@@ -61,6 +64,7 @@ class Term(SemanticAction):
term
*=
children
[
i
]
else
:
term
/=
children
[
i
]
if
parser
.
debug
:
print
(
"Term = {}"
.
format
(
term
))
return
term
...
...
@@ -71,6 +75,7 @@ class Expr(SemanticAction):
Term nodes will be already evaluated.
"""
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Expression {}"
.
format
(
children
))
expr
=
0
start
=
0
...
...
@@ -84,7 +89,9 @@ class Expr(SemanticAction):
else
:
expr
+=
children
[
i
]
if
parser
.
debug
:
print
(
"Expression = {}"
.
format
(
expr
))
return
expr
...
...
@@ -94,13 +101,13 @@ factor.sem = Factor()
term
.
sem
=
Term
()
expression
.
sem
=
Expr
()
if
__name__
==
"__main__"
:
def
main
(
debug
=
False
):
# First we will make a parser - an instance of the calc parser model.
# Parser model is given in the form of python constructs therefore we
# are using ParserPython class.
parser
=
ParserPython
(
calc
)
parser
=
ParserPython
(
calc
,
debug
=
debug
)
if
debug
:
# Then we export it to a dot file in order to visualise it.
# This step is optional but it is handy for debugging purposes.
# We can make a png out of it using dot (part of graphviz) like this
...
...
@@ -114,11 +121,19 @@ if __name__ == "__main__":
# We create a parse tree out of textual input_expr
parse_tree
=
parser
.
parse
(
input_expr
)
if
debug
:
# Then we export it to a dot file in order to visualise it.
# This is also optional.
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"calc_parse_tree.dot"
)
result
=
parser
.
getASG
()
if
debug
:
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# returned value will be the result of the input_expr expression.
print
(
"{} = {}"
.
format
(
input_expr
,
parser
.
getASG
()))
print
(
"{} = {}"
.
format
(
input_expr
,
result
))
if
__name__
==
"__main__"
:
main
(
debug
=
True
)
examples/calc_peg.py
View file @
6a197bfd
...
...
@@ -38,28 +38,36 @@ sem_actions = {
"expression"
:
Expr
(),
}
def
main
(
debug
=
False
):
# First we will make a parser - an instance of the calc parser model.
# Parser model is given in the form of PEG notation therefore we
# are using ParserPEG class. Root rule name (parsing expression) is "calc".
parser
=
ParserPEG
(
calc_grammar
,
"calc"
,
debug
=
True
)
# First we will make a parser - an instance of the calc parser model.
# Parser model is given in the form of PEG notation therefore we
# are using ParserPEG class. Root rule name (parsing expression) is "calc".
parser
=
ParserPEG
(
calc_grammar
,
"calc"
,
debug
=
debug
)
if
debug
:
# Then we export it to a dot file.
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"calc_peg_parser_model.dot"
)
# Then we export it to a dot file.
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"calc_peg_parser_model.dot"
)
# An expression we want to evaluate
input_expr
=
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# An expression we want to evaluate
input_expr
=
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# Then parse tree is created out of the input_expr expression.
parse_tree
=
parser
.
parse
(
input_expr
)
# Then parse tree is created out of the input_expr expression.
parse_tree
=
parser
.
parse
(
input_expr
)
if
debug
:
# We save it to dot file in order to visualise it.
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"calc_peg_parse_tree.dot"
)
# We save it to dot file in order to visualise it.
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"calc_peg_parse_tree.dot"
)
result
=
parser
.
getASG
(
sem_actions
)
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# returned value will be evaluated result of the input_expr expression.
# Semantic actions are supplied to the getASG function.
print
(
"{} = {}"
.
format
(
input_expr
,
parser
.
getASG
(
sem_actions
)))
if
debug
:
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# returned value will be evaluated result of the input_expr expression.
# Semantic actions are supplied to the getASG function.
print
(
"{} = {}"
.
format
(
input_expr
,
result
))
if
__name__
==
"__main__"
:
main
(
debug
=
True
)
examples/csv.py
View file @
6a197bfd
...
...
@@ -16,23 +16,22 @@ def field_content(): return _(r'([^,\n])+')
def
field_content_quoted
():
return
_
(
r'(("")|([^"]))+'
)
def
csvfile
():
return
OneOrMore
([
record
,
'
\n
'
]),
EOF
if
__name__
==
"__main__"
:
test_data
=
'''
test_data
=
'''
Unquoted test, "Quoted test", 23234, One Two Three, "343456.45"
Unquoted test 2, "Quoted test with ""inner"" quotes", 23234, One Two Three, "343456.45"
Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45"
'''
'''
def
main
(
debug
=
False
):
# First we will make a parser - an instance of the CVS parser model.
# Parser model is given in the form of python constructs therefore we
# are using ParserPython class.
# Skipping of whitespace will be done only for tabs and spaces. Newlines
# have semantics in csv files. They are used to separate records.
parser
=
ParserPython
(
csvfile
,
ws
=
'
\t
'
,
reduce_tree
=
True
,
debug
=
True
)
parser
=
ParserPython
(
csvfile
,
ws
=
'
\t
'
,
reduce_tree
=
True
,
debug
=
debug
)
if
debug
:
# Then we export it to a dot file in order to visualise it.
# This step is optional but it is handy for debugging purposes.
# We can make a png out of it using dot (part of graphviz) like this:
...
...
@@ -42,8 +41,12 @@ Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45"
# Creating parse tree out of textual input
parse_tree
=
parser
.
parse
(
test_data
)
if
debug
:
# Then we export it to a dot file in order to visualise it.
# This is also optional.
# dot -O -Tpng calc_parse_tree.dot
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"csv_parse_tree.dot"
)
if
__name__
==
"__main__"
:
main
(
debug
=
True
)
examples/json.py
View file @
6a197bfd
...
...
@@ -2,7 +2,7 @@
# Name: json.py
# Purpose: Implementation of a simple JSON parser in arpeggio.
# Author: Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2009 Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2009
-2014
Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#
# This example is based on jsonParser.py from the pyparsing project
...
...
@@ -51,9 +51,8 @@ def jsonObject(): return "{", Optional(jsonMembers), "}"
def
jsonFile
():
return
jsonObject
,
EOF
if
__name__
==
"__main__"
:
testdata
=
"""
{
testdata
=
"""
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
...
...
@@ -79,16 +78,24 @@ if __name__ == "__main__":
}
}
}
}
"""
}
"""
def
main
(
debug
=
False
):
# Creating parser from parser model.
parser
=
ParserPython
(
jsonFile
,
debug
=
True
)
parser
=
ParserPython
(
jsonFile
,
debug
=
debug
)
if
debug
:
# Exporting parser model to dot file in order to visualise it.
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"json_parser_model.dot"
)
# Parse json string
parse_tree
=
parser
.
parse
(
testdata
)
if
debug
:
# Export parse tree for visualization
PTDOTExporter
()
.
exportFile
(
parser
.
parse_tree
,
"json_parse_tree.dot"
)
if
__name__
==
"__main__"
:
main
(
debug
=
True
)
examples/peg_peg.py
View file @
6a197bfd
...
...
@@ -60,34 +60,43 @@ peg_grammar = r"""
comment <- '//' r'.*\n';
"""
def
main
(
debug
=
False
):
# ParserPEG will use ParserPython to parse peg_grammar definition and
# create parser_model for parsing PEG based grammars
parser
=
ParserPEG
(
peg_grammar
,
'grammar'
,
debug
=
True
)
# ParserPEG will use ParserPython to parse peg_grammar definition and
# create parser_model for parsing PEG based grammars
parser
=
ParserPEG
(
peg_grammar
,
'grammar'
,
debug
=
debug
)
# Exporting parser model to dot file for visualization.
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
if
debug
:
# Exporting parser model to dot file for visualization.
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"peg_peg_parser_model.dot"
)
# Now we will use created parser to parse the same peg_grammar used for
# parser initialization. We can parse peg_grammar because it is specified
# using PEG itself.
parser
.
parse
(
peg_grammar
)
# Now we will use created parser to parse the same peg_grammar used for
# parser initialization. We can parse peg_grammar because it is specified
# using PEG itself.
parser
.
parse
(
peg_grammar
)
# Again we export parse tree in dot file for visualization.
PTDOTExporter
()
.
exportFile
(
parser
.
parse_tree
,
if
debug
:
# Again we export parse tree in dot file for visualization.
PTDOTExporter
()
.
exportFile
(
parser
.
parse_tree
,
"peg_peg_parse_tree.dot"
)
# ASG should be the same as parser.parser_model because semantic
# actions will create PEG parser (tree of ParsingExpressions).
asg
=
parser
.
getASG
(
sem_actions
)
# ASG should be the same as parser.parser_model because semantic
# actions will create PEG parser (tree of ParsingExpressions).
asg
=
parser
.
getASG
(
sem_actions
)
# This graph should be the same as peg_peg_parser_model.dot because
# they define the same parser.
PMDOTExporter
()
.
exportFile
(
asg
,
if
debug
:
# This graph should be the same as peg_peg_parser_model.dot because
# they define the same parser.
PMDOTExporter
()
.
exportFile
(
asg
,
"peg_peg_asg.dot"
)
# If we replace parser_mode with ASG constructed parser it will still
# parse PEG grammars
parser
.
parser_model
=
asg
parser
.
parse
(
peg_grammar
)
# If we replace parser_mode with ASG constructed parser it will still
# parse PEG grammars
parser
.
parser_model
=
asg
parser
.
parse
(
peg_grammar
)
if
__name__
==
'__main__'
:
main
(
debug
=
True
)
examples/robot.py
View file @
6a197bfd
...
...
@@ -35,36 +35,42 @@ def right(): return 'right'
# Semantic actions
class
Up
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Going up"
)
return
(
0
,
1
)
class
Down
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Going down"
)
return
(
0
,
-
1
)
class
Left
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Going left"
)
return
(
-
1
,
0
)
class
Right
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Going right"
)
return
(
1
,
0
)
class
Command
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Command"
)
return
children
[
0
]
class
Program
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Evaluating position"
)
position
=
[
0
,
0
]
for
move
in
children
[
1
:
-
2
]:
...
...
@@ -80,8 +86,7 @@ down.sem = Down()
left
.
sem
=
Left
()
right
.
sem
=
Right
()
if
__name__
==
"__main__"
:
def
main
(
debug
=
False
):
# Program code
input_program
=
'''
begin
...
...
@@ -96,8 +101,9 @@ if __name__ == "__main__":
# First we will make a parser - an instance of the robot parser model.
# Parser model is given in the form of python constructs therefore we
# are using ParserPython class.
parser
=
ParserPython
(
program
,
debug
=
True
)
parser
=
ParserPython
(
program
,
debug
=
debug
)
if
debug
:
# Then we export it to a dot file in order to visualize it.
# This step is optional but it is handy for debugging purposes.
# We can make a png out of it using dot (part of graphviz) like this
...
...
@@ -108,6 +114,7 @@ if __name__ == "__main__":
# We create a parse tree out of textual input
parse_tree
=
parser
.
parse
(
input_program
)
if
debug
:
# Then we export it to a dot file in order to visualize it.
# dot -O -Tpng robot_parse_tree.dot
PTDOTExporter
()
.
exportFile
(
parse_tree
,
...
...
@@ -116,4 +123,11 @@ if __name__ == "__main__":
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# returned value will be the final position of the robot.
print
(
"position = "
,
parser
.
getASG
())
result
=
parser
.
getASG
()
if
debug
:
print
(
"position = "
,
result
)
if
__name__
==
"__main__"
:
main
(
debug
=
True
)
examples/robot_peg.py
View file @
6a197bfd
...
...
@@ -46,8 +46,7 @@ semantic_actions = {
}
if
__name__
==
"__main__"
:
def
main
(
debug
=
False
):
# Program code
input
=
'''
begin
...
...
@@ -63,8 +62,9 @@ if __name__ == "__main__":
# First we will make a parser - an instance of the robot parser model.
# Parser model is given in the form of PEG specification therefore we
# are using ParserPEG class.
parser
=
ParserPEG
(
robot_grammar
,
'program'
,
debug
=
True
)
parser
=
ParserPEG
(
robot_grammar
,
'program'
,
debug
=
debug
)
if
debug
:
# Then we export it to a dot file in order to visualize it.
# This step is optional but it is handy for debugging purposes.
# We can make a png out of it using dot (part of graphviz) like this
...
...
@@ -75,6 +75,7 @@ if __name__ == "__main__":
# We create a parse tree out of textual input
parse_tree
=
parser
.
parse
(
input
)
if
debug
:
# Then we export it to a dot file in order to visualize it.
# dot -O -Tpng robot_peg_parse_tree.dot
PTDOTExporter
()
.
exportFile
(
parse_tree
,
...
...
@@ -83,5 +84,8 @@ if __name__ == "__main__":
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# returned value will be the final position of the robot.
print
(
"position = "
,
parser
.
getASG
(
sem_actions
=
semantic_actions
))
return
parser
.
getASG
(
sem_actions
=
semantic_actions
)
if
__name__
==
"__main__"
:
print
(
"position = "
,
main
(
debug
=
True
))
examples/simple.py
View file @
6a197bfd
...
...
@@ -30,19 +30,6 @@ def functioncall(): return symbol, "(", expressionlist, ")"
def
function
():
return
Kwd
(
"function"
),
symbol
,
parameterlist
,
block
def
simpleLanguage
():
return
function
# Parser instantiation. simpleLanguage is the definition of the root rule
# and comment is a grammar rule for comments.
parser
=
ParserPython
(
simpleLanguage
,
comment
,
debug
=
True
)
# We save parser model to dot file in order to visualise it.
# We can make a png out of it using dot (part of graphviz) like this
# dot -Tpng -O simple_parser.dot
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"simple_parser_model.dot"
)
# Parser model for comments is handled as separate model
PMDOTExporter
()
.
exportFile
(
parser
.
comments_model
,
"simple_parser_comments.dot"
)
input
=
"""
function fak(n) {
if (n==0) {
...
...
@@ -53,8 +40,29 @@ input = """
};
}
"""
parse_tree
=
parser
.
parse
(
input
)
# Export parse tree for visualization
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"simple_parse_tree.dot"
)
def
main
(
debug
=
False
):
# Parser instantiation. simpleLanguage is the definition of the root rule
# and comment is a grammar rule for comments.
parser
=
ParserPython
(
simpleLanguage
,
comment
,
debug
=
debug
)
if
debug
:
# We save parser model to dot file in order to visualise it.
# We can make a png out of it using dot (part of graphviz) like this
# dot -Tpng -O simple_parser.dot
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"simple_parser_model.dot"
)
# Parser model for comments is handled as separate model
PMDOTExporter
()
.
exportFile
(
parser
.
comments_model
,
"simple_parser_comments.dot"
)
parse_tree
=
parser
.
parse
(
input
)
if
debug
:
# Export parse tree for visualization
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"simple_parse_tree.dot"
)
if
__name__
==
"__main__"
:
main
(
debug
=
True
)
tests/unit/test_examples.py
0 → 100644
View file @
6a197bfd
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_examples
# Purpose: Test that examples run without errors.
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
import
pytest
import
os
,
sys
import
imp
def
test_examples
():
examples_dir
=
os
.
path
.
join
(
os
.
path
.
abspath
(
os
.
path
.
dirname
(
__file__
)),
'../../examples/'
)
if
not
examples_dir
in
sys
.
path
:
sys
.
path
.
insert
(
0
,
examples_dir
)
examples
=
[
f
for
f
in
os
.
listdir
(
examples_dir
)
if
f
!=
'__init__.py'
and
f
.
endswith
(
'.py'
)]
for
e
in
examples
:
(
module_name
,
_
)
=
os
.
path
.
splitext
(
e
)
(
module_file
,
module_path
,
desc
)
=
imp
.
find_module
(
module_name
,
[
examples_dir
])
m
=
imp
.
load_module
(
module_name
,
module_file
,
module_path
,
desc
)
if
hasattr
(
m
,
'main'
):
m
.
main
(
debug
=
False
)
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