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
96570573
Commit
96570573
authored
Sep 07, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixing PEP-8 violations
parent
923c4768
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
43 deletions
+47
-43
__init__.py
arpeggio/__init__.py
+47
-43
No files found.
arpeggio/__init__.py
View file @
96570573
...
@@ -55,7 +55,8 @@ class NoMatch(Exception):
...
@@ -55,7 +55,8 @@ class NoMatch(Exception):
position (int): A position in the input stream where exception
position (int): A position in the input stream where exception
occurred.
occurred.
parser (Parser): An instance of a parser.
parser (Parser): An instance of a parser.
exp_str(str): What is expected? If not given it is deduced from the rule.
exp_str(str): What is expected? If not given it is deduced from
the rule.
soft(bool): Used to indicate soft no match exception.
soft(bool): Used to indicate soft no match exception.
"""
"""
def
__init__
(
self
,
rule
,
position
,
parser
,
exp_str
=
None
,
soft
=
False
):
def
__init__
(
self
,
rule
,
position
,
parser
,
exp_str
=
None
,
soft
=
False
):
...
@@ -186,7 +187,7 @@ class ParsingExpression(object):
...
@@ -186,7 +187,7 @@ class ParsingExpression(object):
try
:
try
:
while
True
:
while
True
:
parser
.
comments
.
append
(
parser
.
comments
.
append
(
parser
.
comments_model
.
parse
(
parser
))
parser
.
comments_model
.
parse
(
parser
))
parser
.
_skip_ws
()
parser
.
_skip_ws
()
except
NoMatch
:
except
NoMatch
:
# NoMatch in comment matching is perfectly
# NoMatch in comment matching is perfectly
...
@@ -229,7 +230,6 @@ class ParsingExpression(object):
...
@@ -229,7 +230,6 @@ class ParsingExpression(object):
if
parser
.
nm
:
if
parser
.
nm
:
parser
.
nm
.
_up
=
False
parser
.
nm
.
_up
=
False
# Remember last parsing expression and set this as
# Remember last parsing expression and set this as
# the new last.
# the new last.
_last_pexpression
=
parser
.
_last_pexpression
_last_pexpression
=
parser
.
_last_pexpression
...
@@ -282,7 +282,7 @@ class ParsingExpression(object):
...
@@ -282,7 +282,7 @@ class ParsingExpression(object):
return
result
return
result
#TODO: _nm_change_rule should be called from every parser expression parse
#
TODO: _nm_change_rule should be called from every parser expression parse
# method that can potentially be the root parser rule.
# method that can potentially be the root parser rule.
def
_nm_change_rule
(
self
,
nm
,
parser
):
def
_nm_change_rule
(
self
,
nm
,
parser
):
"""
"""
...
@@ -324,7 +324,7 @@ class Sequence(ParsingExpression):
...
@@ -324,7 +324,7 @@ class Sequence(ParsingExpression):
raise
raise
except
NoMatch
as
m
:
except
NoMatch
as
m
:
parser
.
position
=
c_pos
# Backtracking
parser
.
position
=
c_pos
# Backtracking
self
.
_nm_change_rule
(
m
,
parser
)
self
.
_nm_change_rule
(
m
,
parser
)
raise
raise
...
@@ -384,7 +384,7 @@ class Optional(Repetition):
...
@@ -384,7 +384,7 @@ class Optional(Repetition):
except
NoMatch
as
e
:
except
NoMatch
as
e
:
parser
.
position
=
c_pos
# Backtracking
parser
.
position
=
c_pos
# Backtracking
raise
NoMatch
(
e
.
rule
,
e
.
position
,
e
.
parser
,
raise
NoMatch
(
e
.
rule
,
e
.
position
,
e
.
parser
,
exp_str
=
e
.
exp_str
,
soft
=
True
)
exp_str
=
e
.
exp_str
,
soft
=
True
)
return
result
return
result
...
@@ -528,8 +528,8 @@ class Combine(Decorator):
...
@@ -528,8 +528,8 @@ class Combine(Decorator):
results
=
flatten
(
results
)
results
=
flatten
(
results
)
# Create terminal from result
# Create terminal from result
return
Terminal
(
self
,
c_pos
,
\
return
Terminal
(
self
,
c_pos
,
""
.
join
([
str
(
result
)
for
result
in
results
]))
""
.
join
([
str
(
result
)
for
result
in
results
]))
except
NoMatch
:
except
NoMatch
:
parser
.
position
=
c_pos
# Backtracking
parser
.
position
=
c_pos
# Backtracking
raise
raise
...
@@ -547,15 +547,14 @@ class Match(ParsingExpression):
...
@@ -547,15 +547,14 @@ class Match(ParsingExpression):
@property
@property
def
name
(
self
):
def
name
(
self
):
if
self
.
root
:
if
self
.
root
:
return
"
%
s=
%
s(
%
s)"
%
(
self
.
rule_name
,
self
.
__class__
.
__name__
,
self
.
to_match
)
return
"
%
s=
%
s(
%
s)"
%
(
self
.
rule_name
,
self
.
__class__
.
__name__
,
self
.
to_match
)
else
:
else
:
return
"
%
s(
%
s)"
%
(
self
.
__class__
.
__name__
,
self
.
to_match
)
return
"
%
s(
%
s)"
%
(
self
.
__class__
.
__name__
,
self
.
to_match
)
def
parse
(
self
,
parser
):
def
parse
(
self
,
parser
):
self
.
_parse_intro
(
parser
)
self
.
_parse_intro
(
parser
)
c_pos
=
parser
.
position
try
:
try
:
match
=
self
.
_parse
(
parser
)
match
=
self
.
_parse
(
parser
)
except
NoMatch
as
nm
:
except
NoMatch
as
nm
:
...
@@ -594,8 +593,8 @@ class RegExMatch(Match):
...
@@ -594,8 +593,8 @@ class RegExMatch(Match):
m
=
self
.
regex
.
match
(
parser
.
input
[
c_pos
:])
m
=
self
.
regex
.
match
(
parser
.
input
[
c_pos
:])
if
m
:
if
m
:
if
parser
.
debug
:
if
parser
.
debug
:
print
(
"++ Match '
%
s' at
%
d => '
%
s'"
%
(
m
.
group
(),
\
print
(
"++ Match '
%
s' at
%
d => '
%
s'"
%
(
m
.
group
(),
c_pos
,
parser
.
context
(
len
(
m
.
group
()))))
c_pos
,
parser
.
context
(
len
(
m
.
group
()))))
parser
.
position
+=
len
(
m
.
group
())
parser
.
position
+=
len
(
m
.
group
())
return
Terminal
(
self
,
c_pos
,
m
.
group
())
return
Terminal
(
self
,
c_pos
,
m
.
group
())
else
:
else
:
...
@@ -622,15 +621,15 @@ class StrMatch(Match):
...
@@ -622,15 +621,15 @@ class StrMatch(Match):
c_pos
=
parser
.
position
c_pos
=
parser
.
position
input_frag
=
parser
.
input
[
c_pos
:
c_pos
+
len
(
self
.
to_match
)]
input_frag
=
parser
.
input
[
c_pos
:
c_pos
+
len
(
self
.
to_match
)]
if
parser
.
debug
:
if
parser
.
debug
:
print
(
"Input = "
,
input_frag
)
print
(
"Input = "
,
input_frag
)
if
self
.
ignore_case
:
if
self
.
ignore_case
:
match
=
input_frag
.
lower
()
==
self
.
to_match
.
lower
()
match
=
input_frag
.
lower
()
==
self
.
to_match
.
lower
()
else
:
else
:
match
=
input_frag
==
self
.
to_match
match
=
input_frag
==
self
.
to_match
if
match
:
if
match
:
if
parser
.
debug
:
if
parser
.
debug
:
print
(
"++ Match '{}' at {} => '{}'"
.
format
(
self
.
to_match
,
\
print
(
"++ Match '{}' at {} => '{}'"
.
format
(
self
.
to_match
,
c_pos
,
parser
.
context
(
len
(
self
.
to_match
))))
c_pos
,
parser
.
context
(
len
(
self
.
to_match
))))
parser
.
position
+=
len
(
self
.
to_match
)
parser
.
position
+=
len
(
self
.
to_match
)
# If this match is inside sequence than mark for suppression
# If this match is inside sequence than mark for suppression
...
@@ -653,7 +652,7 @@ class StrMatch(Match):
...
@@ -653,7 +652,7 @@ class StrMatch(Match):
# HACK: Kwd class is a bit hackish. Need to find a better way to
# HACK: Kwd class is a bit hackish. Need to find a better way to
#
introduce different classes of string tokens.
#
introduce different classes of string tokens.
class
Kwd
(
StrMatch
):
class
Kwd
(
StrMatch
):
"""
"""
A specialization of StrMatch to specify keywords of the language.
A specialization of StrMatch to specify keywords of the language.
...
@@ -692,7 +691,7 @@ def EOF():
...
@@ -692,7 +691,7 @@ def EOF():
# ---------------------------------------------------------
# ---------------------------------------------------------
#---------------------------------------------------
#
---------------------------------------------------
# Parse Tree node classes
# Parse Tree node classes
class
ParseTreeNode
(
object
):
class
ParseTreeNode
(
object
):
...
@@ -702,8 +701,8 @@ class ParseTreeNode(object):
...
@@ -702,8 +701,8 @@ class ParseTreeNode(object):
Attributes:
Attributes:
rule (ParsingExpression): The rule that created this node.
rule (ParsingExpression): The rule that created this node.
rule_name (str): The name of the rule that created this node if
root rule
rule_name (str): The name of the rule that created this node if
or empty string otherwise.
root rule
or empty string otherwise.
position (int): A position in the input stream where the match
position (int): A position in the input stream where the match
occurred.
occurred.
error (bool): Is this a false parse tree node created during error
error (bool): Is this a false parse tree node created during error
...
@@ -762,7 +761,8 @@ class NonTerminal(ParseTreeNode, list):
...
@@ -762,7 +761,8 @@ class NonTerminal(ParseTreeNode, list):
"""
"""
Non-leaf node of the Parse Tree. Represents language syntax construction.
Non-leaf node of the Parse Tree. Represents language syntax construction.
At the same time used in ParseTreeNode navigation expressions.
At the same time used in ParseTreeNode navigation expressions.
See test_ptnode_navigation_expressions.py for examples of navigation expressions.
See test_ptnode_navigation_expressions.py for examples of navigation
expressions.
Attributes:
Attributes:
nodes (list of ParseTreeNode): Children parse tree nodes.
nodes (list of ParseTreeNode): Children parse tree nodes.
...
@@ -802,8 +802,8 @@ class NonTerminal(ParseTreeNode, list):
...
@@ -802,8 +802,8 @@ class NonTerminal(ParseTreeNode, list):
this node rule.
this node rule.
"""
"""
# Prevent infinite recursion
# Prevent infinite recursion
if
rule_name
in
[
'_expr_cache'
,
'_filtered'
,
'rule'
,
'rule_name'
,
if
rule_name
in
[
'_expr_cache'
,
'_filtered'
,
'rule'
,
'rule_name'
,
'position'
,
'append'
,
'extend'
]:
'position'
,
'append'
,
'extend'
]:
raise
AttributeError
raise
AttributeError
# First check the cache
# First check the cache
...
@@ -828,8 +828,10 @@ class NonTerminal(ParseTreeNode, list):
...
@@ -828,8 +828,10 @@ class NonTerminal(ParseTreeNode, list):
nodes
.
append
(
n
)
nodes
.
append
(
n
)
rule
=
n
.
rule
rule
=
n
.
rule
# For expression NonTerminals instances position does not have any sense.
# For expression NonTerminals instances position does not have
result
=
NonTerminal
(
rule
=
rule
,
position
=
None
,
nodes
=
nodes
,
_filtered
=
True
)
# any sense.
result
=
NonTerminal
(
rule
=
rule
,
position
=
None
,
nodes
=
nodes
,
_filtered
=
True
)
self
.
_expr_cache
[
rule_name
]
=
result
self
.
_expr_cache
[
rule_name
]
=
result
return
result
return
result
...
@@ -901,7 +903,7 @@ class SemanticActionResults(list):
...
@@ -901,7 +903,7 @@ class SemanticActionResults(list):
def
append_result
(
self
,
name
,
result
):
def
append_result
(
self
,
name
,
result
):
if
name
:
if
name
:
if
n
ot
name
in
self
.
results
:
if
n
ame
not
in
self
.
results
:
self
.
results
[
name
]
=
[]
self
.
results
[
name
]
=
[]
self
.
results
[
name
]
.
append
(
result
)
self
.
results
[
name
]
.
append
(
result
)
...
@@ -1021,8 +1023,8 @@ class Parser(object):
...
@@ -1021,8 +1023,8 @@ class Parser(object):
if
self
.
debug
:
if
self
.
debug
:
from
arpeggio.export
import
PTDOTExporter
from
arpeggio.export
import
PTDOTExporter
root_rule_name
=
self
.
parse_tree
.
rule_name
root_rule_name
=
self
.
parse_tree
.
rule_name
PTDOTExporter
()
.
exportFile
(
self
.
parse_tree
,
PTDOTExporter
()
.
exportFile
(
"{}_parse_tree.dot"
.
format
(
root_rule_name
))
self
.
parse_tree
,
"{}_parse_tree.dot"
.
format
(
root_rule_name
))
return
self
.
parse_tree
return
self
.
parse_tree
def
parse_file
(
self
,
file_name
):
def
parse_file
(
self
,
file_name
):
...
@@ -1044,11 +1046,12 @@ class Parser(object):
...
@@ -1044,11 +1046,12 @@ class Parser(object):
sem_actions (dict): The semantic actions dictionary to use for
sem_actions (dict): The semantic actions dictionary to use for
semantic analysis. Rule names are the keys and semantic action
semantic analysis. Rule names are the keys and semantic action
objects are values.
objects are values.
defaults (bool): If True a default semantic action will be
applied in
defaults (bool): If True a default semantic action will be
case no action is defined for the node.
applied in
case no action is defined for the node.
"""
"""
if
not
self
.
parse_tree
:
if
not
self
.
parse_tree
:
raise
Exception
(
"Parse tree is empty. You did call parse(), didn't you?"
)
raise
Exception
(
"Parse tree is empty. You did call parse(), didn't you?"
)
if
sem_actions
is
None
:
if
sem_actions
is
None
:
if
not
self
.
sem_actions
:
if
not
self
.
sem_actions
:
...
@@ -1081,14 +1084,14 @@ class Parser(object):
...
@@ -1081,14 +1084,14 @@ class Parser(object):
if
self
.
debug
:
if
self
.
debug
:
print
(
"Processing "
,
node
.
name
,
"= '"
,
str
(
node
),
print
(
"Processing "
,
node
.
name
,
"= '"
,
str
(
node
),
"' type:"
,
type
(
node
)
.
__name__
,
\
"' type:"
,
type
(
node
)
.
__name__
,
"len:"
,
len
(
node
)
if
isinstance
(
node
,
list
)
else
""
)
"len:"
,
len
(
node
)
if
isinstance
(
node
,
list
)
else
""
)
for
i
,
a
in
enumerate
(
children
):
for
i
,
a
in
enumerate
(
children
):
print
(
"
\t
%
d:"
%
(
i
+
1
),
unicode
(
a
),
"type:"
,
type
(
a
)
.
__name__
)
print
(
"
\t
%
d:"
%
(
i
+
1
),
str
(
a
),
"type:"
,
type
(
a
)
.
__name__
)
if
node
.
rule_name
in
sem_actions
:
if
node
.
rule_name
in
sem_actions
:
sem_action
=
sem_actions
[
node
.
rule_name
]
sem_action
=
sem_actions
[
node
.
rule_name
]
if
type
(
sem_action
)
is
types
.
FunctionType
:
if
isinstance
(
sem_action
,
types
.
FunctionType
)
:
retval
=
sem_action
(
self
,
node
,
children
)
retval
=
sem_action
(
self
,
node
,
children
)
else
:
else
:
retval
=
sem_action
.
first_pass
(
self
,
node
,
children
)
retval
=
sem_action
.
first_pass
(
self
,
node
,
children
)
...
@@ -1098,8 +1101,8 @@ class Parser(object):
...
@@ -1098,8 +1101,8 @@ class Parser(object):
if
self
.
debug
:
if
self
.
debug
:
action_name
=
sem_action
.
__name__
\
action_name
=
sem_action
.
__name__
\
if
hasattr
(
sem_action
,
'__name__'
)
\
if
hasattr
(
sem_action
,
'__name__'
)
\
else
sem_action
.
__class__
.
__name__
else
sem_action
.
__class__
.
__name__
print
(
"
\t
Applying semantic action "
,
action_name
)
print
(
"
\t
Applying semantic action "
,
action_name
)
else
:
else
:
...
@@ -1117,7 +1120,7 @@ class Parser(object):
...
@@ -1117,7 +1120,7 @@ class Parser(object):
if
retval
is
None
:
if
retval
is
None
:
print
(
"
\t
Suppressed."
)
print
(
"
\t
Suppressed."
)
else
:
else
:
print
(
"
\t
Resolved to = "
,
unicode
(
retval
),
print
(
"
\t
Resolved to = "
,
str
(
retval
),
" type:"
,
type
(
retval
)
.
__name__
)
" type:"
,
type
(
retval
)
.
__name__
)
return
retval
return
retval
...
@@ -1139,7 +1142,7 @@ class Parser(object):
...
@@ -1139,7 +1142,7 @@ class Parser(object):
"""
"""
if
not
self
.
line_ends
:
if
not
self
.
line_ends
:
try
:
try
:
#TODO: Check this implementation on Windows.
#
TODO: Check this implementation on Windows.
self
.
line_ends
.
append
(
self
.
input
.
index
(
"
\n
"
))
self
.
line_ends
.
append
(
self
.
input
.
index
(
"
\n
"
))
while
True
:
while
True
:
try
:
try
:
...
@@ -1245,10 +1248,10 @@ class ParserPython(Parser):
...
@@ -1245,10 +1248,10 @@ class ParserPython(Parser):
from
arpeggio.export
import
PMDOTExporter
from
arpeggio.export
import
PMDOTExporter
root_rule
=
language_def
.
__name__
root_rule
=
language_def
.
__name__
PMDOTExporter
()
.
exportFile
(
self
.
parser_model
,
PMDOTExporter
()
.
exportFile
(
self
.
parser_model
,
"{}_parser_model.dot"
.
format
(
root_rule
))
"{}_parser_model.dot"
.
format
(
root_rule
))
# 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
:
self
.
comments_model
.
root
=
True
self
.
comments_model
.
root
=
True
self
.
comments_model
.
rule_name
=
comment_def
.
__name__
self
.
comments_model
.
rule_name
=
comment_def
.
__name__
...
@@ -1270,7 +1273,8 @@ class ParserPython(Parser):
...
@@ -1270,7 +1273,8 @@ class ParserPython(Parser):
def
inner_from_python
(
expression
):
def
inner_from_python
(
expression
):
retval
=
None
retval
=
None
if
type
(
expression
)
==
types
.
FunctionType
:
# Is this expression a parser rule?
if
isinstance
(
expression
,
types
.
FunctionType
):
# If this expression is a parser rule
rule_name
=
expression
.
__name__
rule_name
=
expression
.
__name__
if
rule_name
in
__rule_cache
:
if
rule_name
in
__rule_cache
:
c_rule
=
__rule_cache
.
get
(
rule_name
)
c_rule
=
__rule_cache
.
get
(
rule_name
)
...
@@ -1291,7 +1295,7 @@ class ParserPython(Parser):
...
@@ -1291,7 +1295,7 @@ class ParserPython(Parser):
__rule_cache
[
rule_name
]
=
CrossRef
(
rule_name
)
__rule_cache
[
rule_name
]
=
CrossRef
(
rule_name
)
curr_expr
=
expression
curr_expr
=
expression
while
type
(
curr_expr
)
is
types
.
FunctionType
:
while
isinstance
(
curr_expr
,
types
.
FunctionType
)
:
# If function directly returns another function
# If function directly returns another function
# go into until non-function is returned.
# go into until non-function is returned.
curr_expr
=
curr_expr
()
curr_expr
=
curr_expr
()
...
...
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