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.
 

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 254 of file b2parser.py.

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

◆ 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 306 of file b2parser.py.

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

◆ 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 278 of file b2parser.py.

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

◆ 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 # \cond false positive doxygen warning
164 if "]" not in self.text[self.index:]:
165 raise SyntaxError("Unmatched '[' in cut.")
166 self.control_token_stack.append("BRACK")
167 return t
168 # \endcond
169

◆ 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 200 of file b2parser.py.

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

◆ 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 171 of file b2parser.py.

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

◆ 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 225 of file b2parser.py.

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

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.

◆ 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: