Belle II Software development
B2Lexer Class Reference
Inheritance diagram for B2Lexer:

Public Member Functions

 __init__ (self)
 
 LBRACK (self, t)
 
 RBRACK (self, t)
 
 LPAREN (self, t)
 
 RPAREN (self, t)
 
 DOUBLE (self, t)
 
 INTEGER (self, t)
 
 IDENTIFIER (self, t)
 

Public Attributes

 control_token_stack = list()
 control_token_stack (list): stack for keeping track of seen brackets and parenthesis.
 
index
 

Static Public Attributes

dict cut_tokens
 cut specific tokens
 
dict expression_tokens
 expression tokens, also needed for cut.
 
dict tokens = expression_tokens.union(cut_tokens)
 Set of all tokens.
 
str ignore = " \t\n"
 ignore spaces tabs and newlines
 
dict literals = {r","}
 comma token definition as literal
 
str EQUALEQUAL = r"=="
 token regular expression for '=='
 
str GREATEREQUAL = r">="
 token regular expression for '>='
 
str LESSEQUAL = r"<="
 token regular expression for '<='
 
str GREATER = r">"
 token regular expression for '>'
 
str LESS = r"<"
 token regular expression for '<'
 
str NOTEQUAL = r"!="
 token regular expression for '!='
 
str POWER = r"\*\*|\^"
 token regular expression for power, both '**' and '^' allowed
 
str TIMES = r"\*"
 token regular expression for '*'
 
str DIVIDE = r"/"
 token regular expression for '/'
 
str PLUS = r"\+"
 token regular expression for '+'
 
str MINUS = r"-"
 token regular expression for '-'
 

Detailed Description

Class responsible for scanning the cut and generating a stream of tokens. The token stream can be passed to `B2Parser` to generate a syntax tree.

Definition at line 73 of file b2parser.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self)
Initialize Lexer

Definition at line 79 of file b2parser.py.

79 def __init__(self):
80 """Initialize Lexer"""
81 ## control_token_stack (list): stack for keeping track of seen brackets
82 ## and parenthesis. Allows finding parenthesis and bracket syntax
83 ## errors on scanner level.
84 self.control_token_stack = list()
85

Member Function Documentation

◆ DOUBLE()

DOUBLE ( self,
t )
Scanning function for double values Parameters: t (sly.lex.Token): initial token generated by the scanner library. The value attribute is of type str initially, equals the matched sequence and is casted to float. Possible notations covered by this regular expression: Normal decimal notation e.g 0.1 Hanging decimal separator notation e.g 1. Preceding decimal separator notation e.g .1 Scientific notation with (signed) exponents e.g 1.0E4, 1.e-4, .1E+3 Exponents are case insensitive e.g 1.e4, 1.E4 Integer with exponent e.g 1E4 Returns: sly.lex.Token

Definition at line 250 of file b2parser.py.

250 def DOUBLE(self, t):
251 """
252 Scanning function for double values
253
254 Parameters:
255 t (sly.lex.Token): initial token generated by the scanner library.
256 The value attribute is of type str initially, equals
257 the matched sequence and is casted to float.
258
259 Possible notations covered by this regular expression:
260 Normal decimal notation e.g 0.1
261 Hanging decimal separator notation e.g 1.
262 Preceding decimal separator notation e.g .1
263 Scientific notation with (signed) exponents e.g 1.0E4, 1.e-4, .1E+3
264 Exponents are case insensitive e.g 1.e4, 1.E4
265 Integer with exponent e.g 1E4
266
267 Returns:
268 sly.lex.Token
269 """
270 t.value = float(t.value)
271 return t
272

◆ IDENTIFIER()

IDENTIFIER ( self,
t )
Scanning function for identifiers If a matched sequence equals reserved keywords of other tokens the token type and value is remapped via the reserved dictionary. Parameters: t (sly.lex.Token): initial token generated by the scanner library. value attribute equals the matched sequence. Returns: sly.lex.Token

Definition at line 302 of file b2parser.py.

302 def IDENTIFIER(self, t):
303 """
304 Scanning function for identifiers
305
306 If a matched sequence equals reserved keywords of other tokens
307 the token type and value is remapped via the reserved dictionary.
308
309 Parameters:
310 t (sly.lex.Token): initial token generated by the scanner library.
311 value attribute equals the matched sequence.
312
313 Returns:
314 sly.lex.Token
315 """
316 reserved = {
317 "and": "AND",
318 "or": "OR",
319 "not": "NOT",
320 "True": "BOOLEAN",
321 "true": "BOOLEAN",
322 "False": "BOOLEAN",
323 "false": "BOOLEAN",
324 "nan": "DOUBLE",
325 "infinity": "DOUBLE",
326 "inf": "DOUBLE",
327 }
328 # Check for reserved words
329 t.type = reserved.get(t.value, "IDENTIFIER")
330
331 # Set value to bool if BOOLEAN type was returned from reserved dict.
332 if t.type == "BOOLEAN":
333 t.value = t.value == "True" or t.value == "true"
334 # Take care of special infinity and nan values.
335 if t.type == "DOUBLE":
336 t.value = float(t.value)
337 if t.type == "IDENTIFIER":
338 try:
339 if self.text[self.index] == "(":
340 # Check that closing parenthesis exists
341 if ")" not in self.text[self.index:]:
342 raise SyntaxError("Unmatched '('")
343 else:
344 self.push_state(B2ParameterLexer)
345 except IndexError:
346 pass
347 return t
348
349

◆ INTEGER()

INTEGER ( self,
t )
Scanning function for integer values Allows normal and hex notation (case insensitive) Parameters: t (sly.lex.Token): initial token generated by the scanner library. The value attribute is of type str initially, equals the matched sequence and is casted to int. Warning: python int-objects are converted to the standard c++ int datatype (32bit). Overflows can happen because numerical limits of python int and c++ int datatypes differ. If you need to input large values write it as double. Returns: sly.lex.Token

Definition at line 274 of file b2parser.py.

274 def INTEGER(self, t):
275 """
276 Scanning function for integer values
277 Allows normal and hex notation (case insensitive)
278
279 Parameters:
280 t (sly.lex.Token): initial token generated by the scanner library.
281 The value attribute is of type str initially, equals
282 the matched sequence and is casted to int.
283
284 Warning:
285 python int-objects are converted
286 to the standard c++ int datatype (32bit).
287 Overflows can happen because numerical limits
288 of python int and c++ int datatypes differ.
289 If you need to input large values write it as double.
290
291 Returns:
292 sly.lex.Token
293 """
294 try:
295 t.value = int(t.value)
296 except ValueError:
297 # casting hex notation
298 t.value = int(t.value, base=16)
299 return t
300

◆ LBRACK()

LBRACK ( self,
t )
Scan opening bracket. Parameters: t (sly.lex.token): token of type LBRACK Raises: SyntaxError: if no following closing bracket is found in the input. Side Effect: Pushes 'BRACK' onto control_token_stack Returns: sly.lex.Token

Definition at line 146 of file b2parser.py.

146 def LBRACK(self, t):
147 """
148 Scan opening bracket.
149
150 Parameters:
151 t (sly.lex.token): token of type LBRACK
152
153 Raises:
154 SyntaxError: if no following closing bracket is found
155 in the input.
156
157 Side Effect:
158 Pushes 'BRACK' onto control_token_stack
159
160 Returns:
161 sly.lex.Token
162 """
163 if "]" not in self.text[self.index:]:
164 raise SyntaxError("Unmatched '[' in cut.")
165 self.control_token_stack.append("BRACK")
166 return t
167

◆ LPAREN()

LPAREN ( self,
t )
Scan opening parenthesis. Parameters: t (sly.lex.token): token of type LPAREN Raises: SyntaxError: if no following closing parenthesis is found in the input. Side Effect: Pushes 'PAREN' onto control_token_stack Returns: sly.lex.Token

Definition at line 198 of file b2parser.py.

198 def LPAREN(self, t):
199 """
200 Scan opening parenthesis.
201
202 Parameters:
203 t (sly.lex.token): token of type LPAREN
204
205 Raises:
206 SyntaxError: if no following closing parenthesis is found
207 in the input.
208
209 Side Effect:
210 Pushes 'PAREN' onto control_token_stack
211
212 Returns:
213 sly.lex.Token
214 """
215 if ")" not in self.text[self.index:]:
216 raise SyntaxError("Unmatched '('")
217 self.control_token_stack.append("PAREN")
218 return t
219

◆ RBRACK()

RBRACK ( self,
t )
Scan closing bracket. Parameters: t (sly.lex.token): token of type RBRACK Raises: SyntaxError: 1. If control_token_stack is empty, which means no bracket was opened previously. 2. If state of control_token_stack is 'PAREN', which means a closing parenthesis is expected. Side Effect: Pops object from control_token_stack Returns: sly.lex.Token

Definition at line 169 of file b2parser.py.

169 def RBRACK(self, t):
170 """
171 Scan closing bracket.
172
173 Parameters:
174 t (sly.lex.token): token of type RBRACK
175
176 Raises:
177 SyntaxError: 1. If control_token_stack is empty, which means
178 no bracket was opened previously.
179 2. If state of control_token_stack is 'PAREN', which
180 means a closing parenthesis is expected.
181
182 Side Effect:
183 Pops object from control_token_stack
184
185 Returns:
186 sly.lex.Token
187 """
188 try:
189 state = self.control_token_stack.pop()
190 except IndexError: # pop from empty list
191 raise SyntaxError("Unmatched ']' in cut.")
192 if state == "BRACK":
193 return t
194 elif state == "PAREN":
195 raise SyntaxError("Illegal ']', expected ')'.")
196

◆ RPAREN()

RPAREN ( self,
t )
Scan closing parenthesis. Parameters: t (sly.lex.token): token of type RPAREN Raises: SyntaxError: 1. If control_token_stack is empty, which means no parenthesis was opened previously. 2. If state of control_token_stack is 'BRACK', which means a closing bracket is expected. Side Effect: Pops state from control_token_stack Returns: sly.lex.Token

Definition at line 221 of file b2parser.py.

221 def RPAREN(self, t):
222 """
223 Scan closing parenthesis.
224
225 Parameters:
226 t (sly.lex.token): token of type RPAREN
227
228 Raises:
229 SyntaxError: 1. If control_token_stack is empty, which means
230 no parenthesis was opened previously.
231 2. If state of control_token_stack is 'BRACK', which
232 means a closing bracket is expected.
233
234 Side Effect:
235 Pops state from control_token_stack
236
237 Returns:
238 sly.lex.Token
239 """
240 try:
241 state = self.control_token_stack.pop()
242 except IndexError: # pop from empty list
243 raise SyntaxError("Unmatched ')' in cut.")
244 if state == "BRACK":
245 raise SyntaxError("Illegal ')', expected ']'.")
246 elif state == "PAREN":
247 return t
248

Member Data Documentation

◆ control_token_stack

control_token_stack = list()

control_token_stack (list): stack for keeping track of seen brackets and parenthesis.

Allows finding parenthesis and bracket syntax errors on scanner level.

Definition at line 84 of file b2parser.py.

◆ cut_tokens

dict cut_tokens
static
Initial value:
= {
# structure tokens
RBRACK, LBRACK, # noqa: F821
# boolean operators
AND, OR, NOT, # noqa: F821
# comparison operators
EQUALEQUAL, GREATEREQUAL, LESSEQUAL, GREATER, LESS, # noqa: F821
NOTEQUAL, # noqa: F821
}

cut specific tokens

Definition at line 87 of file b2parser.py.

◆ DIVIDE

str DIVIDE = r"/"
static

token regular expression for '/'

Definition at line 135 of file b2parser.py.

◆ EQUALEQUAL

str EQUALEQUAL = r"=="
static

token regular expression for '=='

Definition at line 117 of file b2parser.py.

◆ expression_tokens

dict expression_tokens
static
Initial value:
= {
LPAREN, RPAREN, # noqa: F821
# data types
DOUBLE, INTEGER, IDENTIFIER, BOOLEAN, # noqa: F821
# arithmetic operators
POWER, TIMES, DIVIDE, PLUS, MINUS # noqa: F821
}

expression tokens, also needed for cut.

Definition at line 97 of file b2parser.py.

◆ GREATER

str GREATER = r">"
static

token regular expression for '>'

Definition at line 123 of file b2parser.py.

◆ GREATEREQUAL

str GREATEREQUAL = r">="
static

token regular expression for '>='

Definition at line 119 of file b2parser.py.

◆ ignore

str ignore = " \t\n"
static

ignore spaces tabs and newlines

Definition at line 109 of file b2parser.py.

◆ index

] index

Definition at line 163 of file b2parser.py.

◆ LESS

str LESS = r"<"
static

token regular expression for '<'

Definition at line 125 of file b2parser.py.

◆ LESSEQUAL

str LESSEQUAL = r"<="
static

token regular expression for '<='

Definition at line 121 of file b2parser.py.

◆ literals

dict literals = {r","}
static

comma token definition as literal

Definition at line 113 of file b2parser.py.

◆ MINUS

str MINUS = r"-"
static

token regular expression for '-'

Definition at line 139 of file b2parser.py.

◆ NOTEQUAL

str NOTEQUAL = r"!="
static

token regular expression for '!='

Definition at line 127 of file b2parser.py.

◆ PLUS

str PLUS = r"\+"
static

token regular expression for '+'

Definition at line 137 of file b2parser.py.

◆ POWER

str POWER = r"\*\*|\^"
static

token regular expression for power, both '**' and '^' allowed

Definition at line 131 of file b2parser.py.

◆ TIMES

str TIMES = r"\*"
static

token regular expression for '*'

Definition at line 133 of file b2parser.py.

◆ tokens

dict tokens = expression_tokens.union(cut_tokens)
static

Set of all tokens.

Definition at line 105 of file b2parser.py.


The documentation for this class was generated from the following file: