1- """Recursince decend parser"""
1+ """Recursive decent parser"""
22# pylint: disable=too-few-public-methods
33
44import json
55import re
66
7+
78class 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+
2023class 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
3337class 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+
7380def 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+
94102def 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+
116125def sequence (parsers ):
117126 def _ (state ):
118127 asts = []
@@ -135,6 +144,7 @@ def _(state):
135144 )
136145 return _
137146
147+
138148def 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+
163174def 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+
186198def 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+
212225def optional (parser ):
213226 def _ (state ):
214227 start_position = state .position
@@ -227,6 +240,7 @@ def _(state):
227240 )
228241 return _
229242
243+
230244def and_predicate (parser ):
231245 def _ (state ):
232246 current_text = state .text
@@ -246,6 +260,7 @@ def _(state):
246260 return False
247261 return _
248262
263+
249264def 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+
274290def 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+
293310def 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+
308326def 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