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
58e82c62
Commit
58e82c62
authored
Feb 10, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring exporter and tidying up examples.
parent
ed5cc11a
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
83 additions
and
82 deletions
+83
-82
export.py
arpeggio/export.py
+29
-29
bibtex.py
examples/bibtex.py
+3
-3
calc.py
examples/calc.py
+9
-8
calc_peg.py
examples/calc_peg.py
+3
-3
csv.py
examples/csv.py
+6
-5
json.py
examples/json.py
+3
-3
peg_peg.py
examples/peg_peg.py
+6
-5
robot.py
examples/robot.py
+9
-10
robot_peg.py
examples/robot_peg.py
+9
-10
simple.py
examples/simple.py
+6
-6
No files found.
arpeggio/export.py
View file @
58e82c62
...
...
@@ -11,13 +11,13 @@ import StringIO
from
arpeggio
import
Terminal
class
Export
(
object
):
class
Export
er
(
object
):
"""
Base class for all Exporters.
"""
def
__init__
(
self
):
super
(
Export
,
self
)
.
__init__
()
super
(
Export
er
,
self
)
.
__init__
()
# Export initialization
self
.
_render_set
=
set
()
# Used in rendering to prevent
...
...
@@ -25,7 +25,6 @@ class Export(object):
# of the same node multiple times
self
.
_adapter_map
=
{}
# Used as a registry of adapters to
# ensure
# ensure that the same adapter is
# returned for the same adaptee object
...
...
@@ -64,14 +63,14 @@ class Export(object):
class
ExportAdapter
(
object
):
'''
"""
Base adapter class for the export support.
Adapter should be defined for every graph type.
Adapter should be defined for every
export and
graph type.
Attributes:
adaptee: A node to adapt.
export: An export object used as a context of the export.
'''
"""
def
__init__
(
self
,
node
,
export
):
self
.
adaptee
=
node
# adaptee is adapted graph node
self
.
export
=
export
...
...
@@ -100,9 +99,9 @@ class DOTExportAdapter(ExportAdapter):
raise
NotImplementedError
()
@property
def
children
(
self
):
def
neighbours
(
self
):
"""
Children of the graph node
.
A set of adjacent graph nodes
.
"""
raise
NotImplementedError
()
...
...
@@ -120,26 +119,27 @@ class PMDOTExportAdapter(DOTExportAdapter):
return
self
.
adaptee
.
desc
@property
def
children
(
self
):
if
not
hasattr
(
self
,
"_
children
"
):
self
.
_
children
=
[]
def
neighbours
(
self
):
if
not
hasattr
(
self
,
"_
neighbours
"
):
self
.
_
neighbours
=
[]
# Registry of adapters used in this export
adapter_map
=
self
.
export
.
_adapter_map
for
c
,
n
in
enumerate
(
self
.
adaptee
.
nodes
):
if
isinstance
(
n
,
PMDOTExportAdapter
):
# if
child
node is already adapted use that adapter
self
.
_
children
.
append
((
str
(
c
+
1
),
n
))
# if
the neighbour
node is already adapted use that adapter
self
.
_
neighbours
.
append
((
str
(
c
+
1
),
n
))
elif
id
(
n
)
in
adapter_map
:
# current node is adaptee -> there is registered adapter
self
.
_
children
.
append
((
str
(
c
+
1
),
adapter_map
[
id
(
n
)]))
self
.
_
neighbours
.
append
((
str
(
c
+
1
),
adapter_map
[
id
(
n
)]))
else
:
# Create new adapter
adapter
=
PMDOTExportAdapter
(
n
,
self
.
export
)
self
.
_
children
.
append
((
str
(
c
+
1
),
adapter
))
self
.
_
neighbours
.
append
((
str
(
c
+
1
),
adapter
))
adapter_map
[
adapter
.
id
]
=
adapter
return
self
.
_
children
return
self
.
_
neighbours
class
PTDOTExportAdapter
(
PMDOTExportAdapter
):
...
...
@@ -147,19 +147,19 @@ class PTDOTExportAdapter(PMDOTExportAdapter):
Adapter for ParseTreeNode graph types.
"""
@property
def
children
(
self
):
def
neighbours
(
self
):
if
isinstance
(
self
.
adaptee
,
Terminal
):
return
[]
else
:
if
not
hasattr
(
self
,
"_
children
"
):
self
.
_
children
=
[]
if
not
hasattr
(
self
,
"_
neighbours
"
):
self
.
_
neighbours
=
[]
for
c
,
n
in
enumerate
(
self
.
adaptee
.
nodes
):
adapter
=
PTDOTExportAdapter
(
n
,
self
.
export
)
self
.
_
children
.
append
((
str
(
c
+
1
),
adapter
))
return
self
.
_
children
self
.
_
neighbours
.
append
((
str
(
c
+
1
),
adapter
))
return
self
.
_
neighbours
class
DOTExport
(
Export
):
class
DOTExport
er
(
Exporter
):
"""
Export to DOT language (part of GraphViz, see http://www.graphviz.org/)
"""
...
...
@@ -173,7 +173,7 @@ class DOTExport(Export):
# retval += self.node(root.comments)
# retval += '\n%s->%s [label="comment"]' % \
#(id(root), id(root.comments))
for
name
,
n
in
node
.
children
:
for
name
,
n
in
node
.
neighbours
:
self
.
_outf
.
write
(
'
\n
%
s->
%
s [label="
%
s"]'
%
(
node
.
id
,
n
.
id
,
name
))
self
.
_outf
.
write
(
'
\n
'
)
...
...
@@ -192,27 +192,27 @@ class DOTExport(Export):
return
to_esc
class
PMDOTExport
(
DOTExport
):
class
PMDOTExport
er
(
DOTExporter
):
"""
A convenience DOTExport extension that uses ParserExpressionDOTExportAdapter
"""
def
export
(
self
,
obj
):
return
super
(
PMDOTExport
,
self
)
.
\
return
super
(
PMDOTExport
er
,
self
)
.
\
export
(
PMDOTExportAdapter
(
obj
,
self
))
def
exportFile
(
self
,
obj
,
file_name
):
return
super
(
PMDOTExport
,
self
)
.
\
return
super
(
PMDOTExport
er
,
self
)
.
\
exportFile
(
PMDOTExportAdapter
(
obj
,
self
),
file_name
)
class
PTDOTExport
(
DOTExport
):
class
PTDOTExport
er
(
DOTExporter
):
"""
A convenience DOTExport extension that uses PTDOTExportAdapter
"""
def
export
(
self
,
obj
):
return
super
(
PTDOTExport
,
self
)
.
\
return
super
(
PTDOTExport
er
,
self
)
.
\
export
(
PTDOTExportAdapter
(
obj
,
self
))
def
exportFile
(
self
,
obj
,
file_name
):
return
super
(
PTDOTExport
,
self
)
.
\
return
super
(
PTDOTExport
er
,
self
)
.
\
exportFile
(
PTDOTExportAdapter
(
obj
,
self
),
file_name
)
examples/bibtex.py
View file @
58e82c62
...
...
@@ -11,7 +11,7 @@
import
sys
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio.export
import
PMDOTExport
er
,
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
...
...
@@ -94,7 +94,7 @@ if __name__ == "__main__":
# 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
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
PMDOTExport
er
()
.
exportFile
(
parser
.
parser_model
,
"bib_parse_tree_model.dot"
)
# First parameter is bibtex file
...
...
@@ -107,7 +107,7 @@ if __name__ == "__main__":
parse_tree
=
parser
.
parse
(
bibtexfile_content
)
# Then we export it to a dot file in order to visualise it.
PTDOTExport
()
.
exportFile
(
parse_tree
,
PTDOTExport
er
()
.
exportFile
(
parse_tree
,
"bib_parse_tree.dot"
)
# getASG will start semantic analysis.
...
...
examples/calc.py
View file @
58e82c62
...
...
@@ -11,7 +11,7 @@
#######################################################################
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio.export
import
PMDOTExport
er
,
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
def
number
():
return
_
(
r'\d*\.\d*|\d+'
)
...
...
@@ -104,21 +104,22 @@ if __name__ == "__main__":
# are using ParserPython class.
parser
=
ParserPython
(
calc
)
# Then we export it to a dot file in order to visualise it.
This is
#
particularly
handy for debugging purposes.
# We can make a
jp
g out of it using dot (part of graphviz) like this
# dot -O -T
jp
g calc_parse_tree_model.dot
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
# 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
pn
g out of it using dot (part of graphviz) like this
# dot -O -T
pn
g calc_parse_tree_model.dot
PMDOTExport
er
()
.
exportFile
(
parser
.
parser_model
,
"calc_parse_tree_model.dot"
)
# An expression we want to evaluate
input
=
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# We create a parse tree o
r abstract syntax tree o
ut of textual input
# We create a parse tree out of textual input
parse_tree
=
parser
.
parse
(
input
)
# Then we export it to a dot file in order to visualise it.
PTDOTExport
()
.
exportFile
(
parse_tree
,
# This is also optional.
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"calc_parse_tree.dot"
)
# getASG will start semantic analysis.
...
...
examples/calc_peg.py
View file @
58e82c62
...
...
@@ -15,7 +15,7 @@
from
arpeggio
import
*
from
arpeggio.peg
import
ParserPEG
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio.export
import
PMDOTExport
er
,
PTDOTExporter
# Semantic actions
from
calc
import
ToFloat
,
Factor
,
Term
,
Expr
,
Calc
...
...
@@ -48,7 +48,7 @@ try:
# Then we export it to a dot file.
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
PMDOTExport
er
()
.
exportFile
(
parser
.
parser_model
,
"calc_peg_parser_model.dot"
)
# An expression we want to evaluate
...
...
@@ -58,7 +58,7 @@ try:
parse_tree
=
parser
.
parse
(
input
)
# We save it to dot file in order to visualise it.
PTDOTExport
()
.
exportFile
(
parse_tree
,
PTDOTExport
er
()
.
exportFile
(
parse_tree
,
"calc_peg_parse_tree.dot"
)
# getASG will start semantic analysis.
...
...
examples/csv.py
View file @
58e82c62
...
...
@@ -6,7 +6,7 @@
# License: MIT License
##############################################################################
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio.export
import
PMDOTExport
er
,
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
def
record
():
return
field
,
ZeroOrMore
(
","
,
field
)
...
...
@@ -34,19 +34,20 @@ Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45"
# have semantics in csv files. They are used to separate records.
parser
=
ParserPython
(
csvfile
,
ws
=
'
\t
'
,
reduce_tree
=
True
,
debug
=
True
)
# Then we export it to a dot file in order to visualise it.
This is
#
particularly
handy for debugging purposes.
# 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:
# dot -O -Tpng calc_parse_tree_model.dot
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
PMDOTExport
er
()
.
exportFile
(
parser
.
parser_model
,
"csv_parse_tree_model.dot"
)
# Creating parse tree out of textual input
parse_tree
=
parser
.
parse
(
test_data
)
# Then we export it to a dot file in order to visualise it.
# This is also optional.
# dot -O -Tpng calc_parse_tree.dot
PTDOTExport
()
.
exportFile
(
parse_tree
,
PTDOTExport
er
()
.
exportFile
(
parse_tree
,
"csv_parse_tree.dot"
)
except
NoMatch
,
e
:
...
...
examples/json.py
View file @
58e82c62
...
...
@@ -34,7 +34,7 @@ value
"""
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio.export
import
PMDOTExport
er
,
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
def
TRUE
():
return
"true"
...
...
@@ -86,12 +86,12 @@ if __name__ == "__main__":
parser
=
ParserPython
(
jsonFile
,
debug
=
True
)
# Exporting parser model to dot file in order to visualise it.
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
PMDOTExport
er
()
.
exportFile
(
parser
.
parser_model
,
"json_parser_model.dot"
)
parse_tree
=
parser
.
parse
(
testdata
)
PTDOTExport
()
.
exportFile
(
parser
.
parse_tree
,
PTDOTExport
er
()
.
exportFile
(
parser
.
parse_tree
,
"json_parse_tree.dot"
)
except
NoMatch
,
e
:
...
...
examples/peg_peg.py
View file @
58e82c62
...
...
@@ -11,7 +11,7 @@
# grammar definition language.
##############################################################################
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio.export
import
PMDOTExport
er
,
PTDOTExporter
from
arpeggio.peg
import
ParserPEG
# Semantic actions
...
...
@@ -71,8 +71,8 @@ try:
# create parser_model for parsing PEG based grammars
parser
=
ParserPEG
(
peg_grammar
,
'grammar'
,
debug
=
True
)
# Exporting parser model to dot file
in order to visualise
.
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
# Exporting parser model to dot file
for visualization
.
PMDOTExport
er
()
.
exportFile
(
parser
.
parser_model
,
"peg_peg_parser_model.dot"
)
# Now we will use created parser to parse the same peg_grammar used for
...
...
@@ -80,7 +80,8 @@ try:
# using PEG itself.
parser
.
parse
(
peg_grammar
)
PTDOTExport
()
.
exportFile
(
parser
.
parse_tree
,
# Again we export parse tree in dot file for vizualization.
PTDOTExporter
()
.
exportFile
(
parser
.
parse_tree
,
"peg_peg_parse_tree.dot"
)
# ASG should be the same as parser.parser_model because semantic
...
...
@@ -89,7 +90,7 @@ try:
# This graph should be the same as peg_peg_parser_model.dot because
# they define the same parser.
PMDOTExport
()
.
exportFile
(
asg
,
PMDOTExport
er
()
.
exportFile
(
asg
,
"peg_peg_asg.dot"
)
# If we replace parser_mode with ASG constructed parser it will still
...
...
examples/robot.py
View file @
58e82c62
...
...
@@ -20,8 +20,7 @@
#######################################################################
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio
import
RegExMatch
as
_
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
# Grammar rules
def
program
():
return
Kwd
(
'begin'
),
ZeroOrMore
(
command
),
Kwd
(
'end'
),
EndOfFile
...
...
@@ -92,19 +91,19 @@ if __name__ == "__main__":
# are using ParserPython class.
parser
=
ParserPython
(
program
,
debug
=
True
)
# Then we export it to a dot file in order to visualize it.
This is
#
particularly
handy for debugging purposes.
# We can make a
jp
g out of it using dot (part of graphviz) like this
# dot -O -T
jpg robot_parse_tree
_model.dot
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
"robot_parse
_tree
_model.dot"
)
# 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
pn
g out of it using dot (part of graphviz) like this
# dot -O -T
png robot_parser
_model.dot
PMDOTExport
er
()
.
exportFile
(
parser
.
parser_model
,
"robot_parse
r
_model.dot"
)
# We create a parse tree out of textual input
parse_tree
=
parser
.
parse
(
input
)
# Then we export it to a dot file in order to visualize it.
# dot -O -T
jp
g robot_parse_tree.dot
PTDOTExport
()
.
exportFile
(
parse_tree
,
# dot -O -T
pn
g robot_parse_tree.dot
PTDOTExport
er
()
.
exportFile
(
parse_tree
,
"robot_parse_tree.dot"
)
# getASG will start semantic analysis.
...
...
examples/robot_peg.py
View file @
58e82c62
...
...
@@ -20,8 +20,7 @@
#######################################################################
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio
import
RegExMatch
as
_
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
from
arpeggio.peg
import
ParserPEG
# Grammar rules
...
...
@@ -66,19 +65,19 @@ if __name__ == "__main__":
# are using ParserPEG class.
parser
=
ParserPEG
(
robot_grammar
,
'program'
,
debug
=
True
)
# Then we export it to a dot file in order to visualize it.
This is
#
particularly
handy for debugging purposes.
# We can make a
jp
g out of it using dot (part of graphviz) like this
# dot -O -T
jpg robot_peg_parse_tree
_model.dot
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
"robot_peg_parse
_tree
_model.dot"
)
# 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
pn
g out of it using dot (part of graphviz) like this
# dot -O -T
png robot_peg_parser
_model.dot
PMDOTExport
er
()
.
exportFile
(
parser
.
parser_model
,
"robot_peg_parse
r
_model.dot"
)
# We create a parse tree out of textual input
parse_tree
=
parser
.
parse
(
input
)
# Then we export it to a dot file in order to visualize it.
# dot -O -T
jp
g robot_peg_parse_tree.dot
PTDOTExport
()
.
exportFile
(
parse_tree
,
# dot -O -T
pn
g robot_peg_parse_tree.dot
PTDOTExport
er
()
.
exportFile
(
parse_tree
,
"robot_peg_parse_tree.dot"
)
# getASG will start semantic analysis.
...
...
examples/simple.py
View file @
58e82c62
...
...
@@ -10,7 +10,7 @@
#######################################################################
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio.export
import
PMDOTExport
er
,
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
def
comment
():
return
[
_
(
"//.*"
),
_
(
"/
\
*.*
\
*/"
)]
...
...
@@ -36,13 +36,13 @@ try:
parser
=
ParserPython
(
simpleLanguage
,
comment
,
debug
=
True
)
# We save parser model to dot file in order to visualise it.
# We can make a
jp
g out of it using dot (part of graphviz) like this
# dot -T
jp
g -O simple_parser.dot
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
# We can make a
pn
g out of it using dot (part of graphviz) like this
# dot -T
pn
g -O simple_parser.dot
PMDOTExport
er
()
.
exportFile
(
parser
.
parser_model
,
"simple_parser_model.dot"
)
# Parser model for comments is handled as separate model
PMDOTExport
()
.
exportFile
(
parser
.
comments_model
,
PMDOTExport
er
()
.
exportFile
(
parser
.
comments_model
,
"simple_parser_comments.dot"
)
input
=
"""
...
...
@@ -57,7 +57,7 @@ try:
"""
parse_tree
=
parser
.
parse
(
input
)
PTDOTExport
()
.
exportFile
(
parse_tree
,
PTDOTExport
er
()
.
exportFile
(
parse_tree
,
"simple_parse_tree.dot"
)
except
NoMatch
,
e
:
...
...
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