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
0722066c
Commit
0722066c
authored
Jan 24, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support for debug option in ParserPEG
parent
7eb2e080
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
21 deletions
+20
-21
peg.py
arpeggio/peg.py
+20
-21
No files found.
arpeggio/peg.py
View file @
0722066c
...
@@ -22,7 +22,7 @@ def ordered_choice(): return sequence, ZeroOrMore(SLASH, sequence)
...
@@ -22,7 +22,7 @@ def ordered_choice(): return sequence, ZeroOrMore(SLASH, sequence)
def
sequence
():
return
OneOrMore
(
prefix
)
def
sequence
():
return
OneOrMore
(
prefix
)
def
prefix
():
return
Optional
([
AND
,
NOT
]),
sufix
def
prefix
():
return
Optional
([
AND
,
NOT
]),
sufix
def
sufix
():
return
expression
,
Optional
([
QUESTION
,
STAR
,
PLUS
])
def
sufix
():
return
expression
,
Optional
([
QUESTION
,
STAR
,
PLUS
])
def
expression
():
return
[
regex
,(
identifier
,
Not
(
LEFT_ARROW
)),
def
expression
():
return
[
regex
,(
identifier
,
Not
(
LEFT_ARROW
)),
(
OPEN
,
ordered_choice
,
CLOSE
),
(
OPEN
,
ordered_choice
,
CLOSE
),
literal
]
literal
]
...
@@ -60,7 +60,7 @@ class PEGSemanticAction(SemanticAction):
...
@@ -60,7 +60,7 @@ class PEGSemanticAction(SemanticAction):
class
SemGrammar
(
SemanticAction
):
class
SemGrammar
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
return
parser
.
peg_rules
[
parser
.
root_rule_name
]
return
parser
.
peg_rules
[
parser
.
root_rule_name
]
class
SemRule
(
PEGSemanticAction
):
class
SemRule
(
PEGSemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
rule_name
=
nodes
[
0
]
.
value
rule_name
=
nodes
[
0
]
.
value
...
@@ -70,11 +70,11 @@ class SemRule(PEGSemanticAction):
...
@@ -70,11 +70,11 @@ class SemRule(PEGSemanticAction):
retval
=
nodes
[
2
]
retval
=
nodes
[
2
]
retval
.
rule
=
rule_name
retval
.
rule
=
rule_name
retval
.
root
=
True
retval
.
root
=
True
if
not
hasattr
(
parser
,
"peg_rules"
):
if
not
hasattr
(
parser
,
"peg_rules"
):
parser
.
peg_rules
=
{}
# Used for linking phase
parser
.
peg_rules
=
{}
# Used for linking phase
parser
.
peg_rules
[
"EndOfFile"
]
=
EndOfFile
()
parser
.
peg_rules
[
"EndOfFile"
]
=
EndOfFile
()
parser
.
peg_rules
[
rule_name
]
=
retval
parser
.
peg_rules
[
rule_name
]
=
retval
return
retval
return
retval
...
@@ -107,7 +107,7 @@ class SemPrefix(PEGSemanticAction):
...
@@ -107,7 +107,7 @@ class SemPrefix(PEGSemanticAction):
retval
.
nodes
=
[
nodes
[
1
]]
retval
.
nodes
=
[
nodes
[
1
]]
else
:
else
:
retval
=
nodes
[
0
]
retval
=
nodes
[
0
]
return
retval
return
retval
class
SemSufix
(
PEGSemanticAction
):
class
SemSufix
(
PEGSemanticAction
):
...
@@ -127,7 +127,7 @@ class SemSufix(PEGSemanticAction):
...
@@ -127,7 +127,7 @@ class SemSufix(PEGSemanticAction):
retval
.
nodes
=
[
nodes
[
0
]]
retval
.
nodes
=
[
nodes
[
0
]]
else
:
else
:
retval
=
nodes
[
0
]
retval
=
nodes
[
0
]
return
retval
return
retval
class
SemExpression
(
PEGSemanticAction
):
class
SemExpression
(
PEGSemanticAction
):
...
@@ -142,12 +142,12 @@ class SemIdentifier(SemanticAction):
...
@@ -142,12 +142,12 @@ class SemIdentifier(SemanticAction):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
logger
.
debug
(
"Identifier
%
s."
%
node
.
value
)
logger
.
debug
(
"Identifier
%
s."
%
node
.
value
)
return
node
return
node
class
SemRegEx
(
SemanticAction
):
class
SemRegEx
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
logger
.
debug
(
"RegEx
%
s."
%
nodes
[
2
]
.
value
)
logger
.
debug
(
"RegEx
%
s."
%
nodes
[
2
]
.
value
)
return
RegExMatch
(
nodes
[
2
]
.
value
)
return
RegExMatch
(
nodes
[
2
]
.
value
)
class
SemLiteral
(
SemanticAction
):
class
SemLiteral
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
logger
.
debug
(
"Literal:
%
s"
%
node
.
value
)
logger
.
debug
(
"Literal:
%
s"
%
node
.
value
)
...
@@ -173,21 +173,21 @@ identifier.sem = SemIdentifier()
...
@@ -173,21 +173,21 @@ identifier.sem = SemIdentifier()
literal
.
sem
=
SemLiteral
()
literal
.
sem
=
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
.
sem
=
SemTerminal
()
sem
.
sem
=
SemTerminal
()
class
ParserPEG
(
Parser
):
class
ParserPEG
(
Parser
):
def
__init__
(
self
,
language_def
,
root_rule_name
,
comment_rule_name
=
None
,
skipws
=
True
,
ws
=
DEFAULT_WS
):
def
__init__
(
self
,
language_def
,
root_rule_name
,
comment_rule_name
=
None
,
*
args
,
**
kwargs
):
super
(
ParserPEG
,
self
)
.
__init__
(
skipws
,
w
s
)
super
(
ParserPEG
,
self
)
.
__init__
(
*
args
,
**
kwarg
s
)
self
.
root_rule_name
=
root_rule_name
self
.
root_rule_name
=
root_rule_name
# PEG Abstract Syntax Graph
# PEG Abstract Syntax Graph
self
.
parser_model
=
self
.
_from_peg
(
language_def
)
self
.
parser_model
=
self
.
_from_peg
(
language_def
)
# Comments should be optional and there can be more of them
# Comments should be optional and there can be more of them
if
self
.
comments_model
:
# and not isinstance(self.comments_model, ZeroOrMore):
if
self
.
comments_model
:
# and not isinstance(self.comments_model, ZeroOrMore):
self
.
comments_model
.
root
=
True
self
.
comments_model
.
root
=
True
self
.
comments_model
.
rule
=
comment_rule_name
self
.
comments_model
.
rule
=
comment_rule_name
def
_parse
(
self
):
def
_parse
(
self
):
return
self
.
parser_model
.
parse
(
self
)
return
self
.
parser_model
.
parse
(
self
)
...
@@ -196,17 +196,17 @@ class ParserPEG(Parser):
...
@@ -196,17 +196,17 @@ class ParserPEG(Parser):
parser
.
root_rule_name
=
self
.
root_rule_name
parser
.
root_rule_name
=
self
.
root_rule_name
parse_tree
=
parser
.
parse
(
language_def
)
parse_tree
=
parser
.
parse
(
language_def
)
return
parser
.
getASG
()
return
parser
.
getASG
()
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
try
:
try
:
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
parser
=
ParserPython
(
grammar
,
None
)
parser
=
ParserPython
(
grammar
,
None
)
f
=
open
(
"peg_parser_model.dot"
,
"w"
)
f
=
open
(
"peg_parser_model.dot"
,
"w"
)
f
.
write
(
str
(
DOTSerializator
(
parser
.
parser_model
)))
f
.
write
(
str
(
DOTSerializator
(
parser
.
parser_model
)))
f
.
close
()
f
.
close
()
except
NoMatch
,
e
:
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
)))
\ No newline at end of file
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