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
297eea71
Commit
297eea71
authored
Dec 17, 2013
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixing PEP8 violations in peg_peg.
parent
40c1b0a9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
32 deletions
+33
-32
peg_peg.py
examples/peg_peg.py
+33
-32
No files found.
examples/peg_peg.py
View file @
297eea71
...
...
@@ -5,35 +5,36 @@
# Author: Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2009 Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#
#
# PEG can be used to describe PEG.
# This example demonstrates building PEG parser using PEG based grammar of PEG
# This example demonstrates building PEG parser using PEG based grammar of PEG
# grammar definition language.
##############################################################################
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio
import
RegExMatch
as
_
from
arpeggio.peg
import
ParserPEG
import
logging
import
logging
# Semantic actions
from
arpeggio.peg
import
SemGrammar
,
SemRule
,
SemOrderedChoice
,
SemSequence
,
SemPrefix
,
\
SemSufix
,
SemExpression
,
SemRegEx
,
SemIdentifier
,
SemLiteral
,
SemTerminal
from
arpeggio.peg
import
SemGrammar
,
SemRule
,
SemOrderedChoice
,
SemSequence
,
\
SemPrefix
,
SemSufix
,
SemExpression
,
SemRegEx
,
SemIdentifier
,
SemLiteral
,
\
SemTerminal
sem_actions
=
{
"grammar"
:
SemGrammar
(),
"rule"
:
SemRule
(),
"ordered_choice"
:
SemOrderedChoice
(),
"sequence"
:
SemSequence
(),
"prefix"
:
SemPrefix
(),
"sufix"
:
SemSufix
(),
"expression"
:
SemExpression
(),
"regex"
:
SemRegEx
(),
"identifier"
:
SemIdentifier
(),
"literal"
:
SemLiteral
()
"grammar"
:
SemGrammar
(),
"rule"
:
SemRule
(),
"ordered_choice"
:
SemOrderedChoice
(),
"sequence"
:
SemSequence
(),
"prefix"
:
SemPrefix
(),
"sufix"
:
SemSufix
(),
"expression"
:
SemExpression
(),
"regex"
:
SemRegEx
(),
"identifier"
:
SemIdentifier
(),
"literal"
:
SemLiteral
()
}
for
sem
in
[
"LEFT_ARROW"
,
"SLASH"
,
"STAR"
,
"QUESTION"
,
"PLUS"
,
"AND"
,
"NOT"
,
"OPEN"
,
"CLOSE"
]:
for
sem
in
[
"LEFT_ARROW"
,
"SLASH"
,
"STAR"
,
"QUESTION"
,
"PLUS"
,
"AND"
,
"NOT"
,
"OPEN"
,
"CLOSE"
]:
sem_actions
[
sem
]
=
SemTerminal
()
...
...
@@ -45,7 +46,7 @@ peg_grammar = r"""
sequence <- prefix+;
prefix <- (AND/NOT)? sufix;
sufix <- expression (QUESTION/STAR/PLUS)?;
expression <- regex / (identifier !LEFT_ARROW)
expression <- regex / (identifier !LEFT_ARROW)
/ ("(" ordered_choice ")") / literal;
identifier <- r'[a-zA-Z_]([a-zA-Z_]|[0-9])*';
...
...
@@ -63,27 +64,27 @@ peg_grammar = r"""
DOT <- '.';
comment <- '//' r'.*\n';
"""
try
:
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
# ParserPEG will use ParserPython to parse peg_grammar definition and
# ParserPEG will use ParserPython to parse peg_grammar definition and
# create parser_model for parsing PEG based grammars
parser
=
ParserPEG
(
peg_grammar
,
'grammar'
)
# Exporting parser model to dot file in order to visualise.
PMDOTExport
()
.
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.
"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
)
PTDOTExport
()
.
exportFile
(
parser
.
parse_tree
,
"peg_peg_parse_tree.dot"
)
"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
)
...
...
@@ -91,13 +92,13 @@ try:
# This graph should be the same as peg_peg_parser_model.dot because
# they define the same parser.
PMDOTExport
()
.
exportFile
(
asg
,
"peg_peg_asg.dot"
)
"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
)
except
NoMatch
,
e
:
print
"Expected
%
s at position
%
s."
%
(
e
.
value
,
str
(
e
.
parser
.
pos_to_linecol
(
e
.
position
)))
print
"Expected
%
s at position
%
s."
%
\
(
e
.
value
,
str
(
e
.
parser
.
pos_to_linecol
(
e
.
position
)))
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