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
dfbfdc56
Commit
dfbfdc56
authored
Feb 05, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Factoring out some common code. Fixing docstrings.
parent
0fe30eed
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
42 deletions
+18
-42
__init__.py
arpeggio/__init__.py
+18
-42
No files found.
arpeggio/__init__.py
View file @
dfbfdc56
...
...
@@ -54,10 +54,7 @@ class NoMatch(Exception):
"""
def
__init__
(
self
,
rule
,
position
,
parser
):
self
.
rule
=
rule
# Position in the input stream where error occurred
self
.
position
=
position
self
.
parser
=
parser
# By default when NoMatch is thrown we will go up the Parser Model.
...
...
@@ -89,21 +86,32 @@ class ParsingExpression(object):
Represents the node of the Parser Model.
Attributes:
elements: A list (or other python object) used as a staging structure
for python based grammar definition. Used in _from_python for
building nodes list of child parser expressions.
rule (str): The name of the parser rule if this is the root rule.
root (bool): Does this parser expression represents the
root of the parser rule? The root parser rule will create
non-terminal node of the parse tree during parsing.
nodes (list of ParsingExpression): A list of child parser expressions.
"""
def
__init__
(
self
,
rule
=
None
,
root
=
False
,
nodes
=
None
):
def
__init__
(
self
,
*
elements
,
**
kwargs
):
if
len
(
elements
)
==
1
:
elements
=
elements
[
0
]
self
.
elements
=
elements
self
.
rule
=
kwargs
.
get
(
'rule'
)
self
.
root
=
kwargs
.
get
(
'root'
,
False
)
nodes
=
kwargs
.
get
(
'nodes'
,
[])
if
not
hasattr
(
nodes
,
'__iter__'
):
nodes
=
[
nodes
]
self
.
nodes
=
nodes
# Memoization. Every node cache the parsing results for the given input
# positions.
self
.
result_cache
=
{}
# position -> parse tree
self
.
nodes
=
nodes
if
nodes
is
None
:
self
.
nodes
=
[]
# child expressions
self
.
rule
=
rule
self
.
root
=
root
@property
def
desc
(
self
):
...
...
@@ -112,7 +120,7 @@ class ParsingExpression(object):
@property
def
name
(
self
):
if
self
.
root
:
return
"
%
s(
%
s)"
%
(
self
.
__class__
.
__name__
,
self
.
rule
)
return
"
%
s(
%
s)"
%
(
self
.
rule
,
self
.
__class__
.
__name__
)
else
:
return
self
.
__class__
.
__name__
...
...
@@ -200,16 +208,7 @@ class ParsingExpression(object):
class
Sequence
(
ParsingExpression
):
"""
Will match sequence of parser expressions in exact order they are defined.
Attributes:
elements (list): A list used as a staging structure for python based
grammar definition. Used in _from_python for building nodes list of
child parser expressions.
"""
def
__init__
(
self
,
elements
=
None
,
rule
=
None
,
root
=
False
,
nodes
=
None
):
super
(
Sequence
,
self
)
.
__init__
(
rule
,
root
,
nodes
)
self
.
elements
=
elements
def
_parse
(
self
,
parser
):
results
=
[]
try
:
...
...
@@ -252,17 +251,6 @@ class Repetition(ParsingExpression):
"""
Base class for all repetition-like parser expressions (?,*,+)
"""
def
__init__
(
self
,
*
elements
,
**
kwargs
):
super
(
Repetition
,
self
)
.
__init__
(
None
)
if
len
(
elements
)
==
1
:
elements
=
elements
[
0
]
self
.
elements
=
elements
nodes
=
kwargs
.
get
(
'nodes'
,
[])
if
not
hasattr
(
nodes
,
'__iter__'
):
nodes
=
[
nodes
]
self
.
nodes
=
nodes
class
Optional
(
Repetition
):
"""
...
...
@@ -325,18 +313,6 @@ class SyntaxPredicate(ParsingExpression):
Predicates are parser expressions that will do the match but will not
consume any input.
"""
def
__init__
(
self
,
*
elements
,
**
kwargs
):
if
len
(
elements
)
==
1
:
elements
=
elements
[
0
]
self
.
elements
=
elements
nodes
=
kwargs
.
get
(
'nodes'
,
[])
if
not
hasattr
(
nodes
,
'__iter__'
):
nodes
=
[
nodes
]
self
.
nodes
=
nodes
super
(
SyntaxPredicate
,
self
)
.
__init__
(
None
)
class
And
(
SyntaxPredicate
):
"""
...
...
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