Skip to content

Commit 75fbd86

Browse files
committed
PEP8 compliance and simple test of PegJsVisitor
1 parent 13a46a6 commit 75fbd86

11 files changed

+218
-105
lines changed

.editorconfig

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# top-most EditorConfig file
2+
root = true
3+
4+
# Unix-style newlines with a newline ending every file
5+
[*]
6+
end_of_line = lf
7+
insert_final_newline = true
8+
9+
# Matches multiple files with brace expansion notation
10+
# Set default charset
11+
[*.{js,py}]
12+
charset = utf-8
13+
14+
# 4 space indentation
15+
[*.py]
16+
indent_style = space
17+
indent_size = 4

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,6 @@ ENV/
8787

8888
# Rope project settings
8989
.ropeproject
90+
91+
# IDE
92+
.idea/

README.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Python version of SimplePEG
2-
--------
2+
---------------------------
33
.. image:: https://travis-ci.org/SimplePEG/Python.svg?branch=master
4-
:target: https://travis-ci.org/SimplePEG/Python
4+
:target: https://travis-ci.org/SimplePEG/Python
55
.. image:: https://coveralls.io/repos/github/SimplePEG/Python/badge.svg?branch=master
6-
:target: https://coveralls.io/github/SimplePEG/Python?branch=master
6+
:target: https://coveralls.io/github/SimplePEG/Python?branch=master
77

88
To use, simply do::
99

@@ -18,4 +18,4 @@ or::
1818
>>> from simplepeg import SPEG
1919
>>> parser = SPEG()
2020
>>> ast = parser.parse('GRAMMAR test b -> "a";', 'a')
21-
>>> print ast.to_json()
21+
>>> print ast.to_json()

setup.py

+25-21
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
from setuptools import setup
22

3+
34
def readme():
45
with open('README.rst') as f:
56
return f.read()
67

7-
with open('LICENSE') as f:
8-
license = f.read()
8+
9+
def license_text():
10+
with open('LICENSE') as f:
11+
return f.read()
12+
913

1014
setup(name='simplepeg',
11-
version='1.0.2',
12-
description='Python version of SimplePEG',
13-
long_description=readme(),
14-
classifiers=[
15-
'Development Status :: 5 - Production/Stable',
16-
'License :: OSI Approved :: MIT License',
17-
'Programming Language :: Python :: 2.7',
18-
'Topic :: Text Processing :: Linguistic',
19-
],
20-
url='https://github.com/SimplePEG/Python',
21-
author='Oleksii Okhrymenko',
22-
author_email='[email protected]',
23-
keywords='peg parser grammar',
24-
license=license,
25-
test_suite='nose.collector',
26-
tests_require=['nose'],
27-
packages=['simplepeg'],
28-
include_package_data=True,
29-
zip_safe=False)
15+
version='1.0.3',
16+
description='Python version of SimplePEG',
17+
long_description=readme(),
18+
classifiers=[
19+
'Development Status :: 5 - Production/Stable',
20+
'License :: OSI Approved :: MIT License',
21+
'Programming Language :: Python :: 2.7',
22+
'Topic :: Text Processing :: Linguistic',
23+
],
24+
url='https://github.com/SimplePEG/Python',
25+
author='Oleksii Okhrymenko',
26+
author_email='[email protected]',
27+
keywords='peg parser grammar',
28+
license=license_text(),
29+
test_suite='nose.collector',
30+
tests_require=['nose'],
31+
packages=['simplepeg'],
32+
include_package_data=True,
33+
zip_safe=False)

simplepeg/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .speg import SPEG
1+
from .speg import SPEG

simplepeg/rd_parser.py

+37-19
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
"""Recursince decend parser"""
1+
"""Recursive decent parser"""
22
# pylint: disable=too-few-public-methods
33

44
import json
55
import re
66

7+
78
class State(object):
89
"""Current parser state"""
910
text = ""
1011
position = 0
1112
rules = []
12-
lastExpectations = []
13+
last_expectations = []
14+
1315
def __init__(self, **kwargs):
1416
self.__dict__.update(kwargs)
1517

1618
def to_json(self):
1719
"""returns json string"""
1820
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=False, indent=2)
1921

22+
2023
class Node(object):
2124
"""Node of AST"""
2225
match = ""
2326
children = None
2427
action = None
28+
2529
def __init__(self, **kwargs):
2630
self.__dict__.update(kwargs)
2731

@@ -32,26 +36,28 @@ def to_json(self):
3236

3337
class Expectation(object):
3438
"""Expectation object"""
39+
3540
def __init__(self, **kwargs):
3641
self.__dict__.update(kwargs)
3742

3843
def to_json(self):
3944
"""returns json string"""
4045
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=False, indent=2)
4146

42-
def getLastError(state):
43-
if len(state.lastExpectations) < 1:
47+
48+
def get_last_error(state):
49+
if len(state.last_expectations) < 1:
4450
return False
4551
lines = state.text.split('\n')
46-
last_exp_position = max([exp.position for exp in state.lastExpectations])
52+
last_exp_position = max([exp.position for exp in state.last_expectations])
4753
last_position = 0
4854
line_of_error = ''
4955
error_line_number = None
5056
position_of_error = 0
5157
i = 0
5258
while i < len(lines):
5359
line_lenght = len(lines[i]) + 1
54-
if last_exp_position >= last_position and last_exp_position < last_position + line_lenght:
60+
if last_position <= last_exp_position < last_position + line_lenght:
5561
line_of_error = lines[i]
5662
position_of_error = last_exp_position - last_position
5763
error_line_number = i + 1
@@ -64,15 +70,16 @@ def getLastError(state):
6470
if last_exp_position < len(state.text):
6571
unexpected_char = state.text[last_exp_position]
6672
unexpected = 'Unexpected "' + unexpected_char + '"'
67-
expected_rules = [exp.rule for exp in state.lastExpectations]
73+
expected_rules = [exp.rule for exp in state.last_expectations]
6874
expected = ' expected (' + ' or '.join(expected_rules) + ')'
6975
pointer = ('-'*(position_of_error + 2 + error_ln_length)) + '^'
7076
extra = line_of_error + '\n' + pointer
7177
return unexpected + expected + '\n' + str_error_ln + ': ' + extra
7278

79+
7380
def string(rule):
7481
def _(state):
75-
state.lastExpectations = []
82+
state.last_expectations = []
7683
if state.text[state.position:state.position+len(rule)] == rule:
7784
start_position = state.position
7885
state.position += len(rule)
@@ -83,17 +90,18 @@ def _(state):
8390
end_position=state.position
8491
)
8592
else:
86-
state.lastExpectations = [Expectation(
93+
state.last_expectations = [Expectation(
8794
type='string',
8895
rule=rule,
8996
position=state.position
9097
)]
9198
return False
9299
return _
93100

101+
94102
def regex_char(rule):
95103
def _(state):
96-
state.lastExpectations = []
104+
state.last_expectations = []
97105
match = re.match(rule, state.text[state.position:])
98106
if match and match.start() == 0:
99107
start_position = state.position
@@ -105,14 +113,15 @@ def _(state):
105113
end_position=state.position
106114
)
107115
else:
108-
state.lastExpectations = [Expectation(
116+
state.last_expectations = [Expectation(
109117
type='regex_char',
110118
rule=rule,
111119
position=state.position
112120
)]
113121
return False
114122
return _
115123

124+
116125
def sequence(parsers):
117126
def _(state):
118127
asts = []
@@ -135,6 +144,7 @@ def _(state):
135144
)
136145
return _
137146

147+
138148
def ordered_choice(parsers):
139149
def _(state):
140150
expectations = []
@@ -154,12 +164,13 @@ def _(state):
154164
else:
155165
state.text = initial_text
156166
state.position = initial_position
157-
expectations = expectations + state.lastExpectations
167+
expectations = expectations + state.last_expectations
158168
i += 1
159-
state.lastExpectations = expectations
169+
state.last_expectations = expectations
160170
return False
161171
return _
162172

173+
163174
def zero_or_more(parser):
164175
def _(state):
165176
asts = []
@@ -172,7 +183,7 @@ def _(state):
172183
asts.append(ast)
173184
else:
174185
state.position = state_position
175-
state.lastExpectations = []
186+
state.last_expectations = []
176187
match = ''.join([(ast.match if ast.match is not None else '') for ast in asts])
177188
return Node(
178189
type='zero_or_more',
@@ -183,6 +194,7 @@ def _(state):
183194
)
184195
return _
185196

197+
186198
def one_or_more(parser):
187199
def _(state):
188200
asts = []
@@ -196,7 +208,7 @@ def _(state):
196208
else:
197209
state.position = state_position
198210
if len(asts) > 0:
199-
state.lastExpectations = []
211+
state.last_expectations = []
200212
match = ''.join([(ast.match if ast.match is not None else '') for ast in asts])
201213
return Node(
202214
type='one_or_more',
@@ -209,6 +221,7 @@ def _(state):
209221
return False
210222
return _
211223

224+
212225
def optional(parser):
213226
def _(state):
214227
start_position = state.position
@@ -227,6 +240,7 @@ def _(state):
227240
)
228241
return _
229242

243+
230244
def and_predicate(parser):
231245
def _(state):
232246
current_text = state.text
@@ -246,6 +260,7 @@ def _(state):
246260
return False
247261
return _
248262

263+
249264
def not_predicate(parser):
250265
def _(state):
251266
current_text = state.text
@@ -254,14 +269,14 @@ def _(state):
254269
if ast:
255270
state.text = current_text
256271
state.position = current_position
257-
state.lastExpectations = [Expectation(
272+
state.last_expectations = [Expectation(
258273
type='not_predicate',
259274
children=[ast],
260275
position=state.position
261276
)]
262277
return False
263278
else:
264-
state.lastExpectations = []
279+
state.last_expectations = []
265280
return Node(
266281
type='not_predicate',
267282
match=None,
@@ -271,6 +286,7 @@ def _(state):
271286
)
272287
return _
273288

289+
274290
def end_of_file():
275291
def _(state):
276292
if len(state.text) == state.position:
@@ -282,16 +298,17 @@ def _(state):
282298
end_position=state.position
283299
)
284300
else:
285-
state.lastExpectations = [Expectation(
301+
state.last_expectations = [Expectation(
286302
type='end_of_file',
287303
rule='EOF',
288304
position=state.position
289305
)]
290306
return False
291307
return _
292308

309+
293310
def rec(func):
294-
"""Allows you to do recurrcive currying"""
311+
"""Allows you to do recursive currying"""
295312
def _(*args, **kwargs):
296313
return func()(*args, **kwargs)
297314
return _
@@ -305,6 +322,7 @@ def _(*args, **kwargs):
305322
return ast
306323
return _
307324

325+
308326
def call_rule_by_name(name):
309327
def _(state):
310328
rule = next((x for x in state.rules if x.name == name), None)

0 commit comments

Comments
 (0)