-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixScanner.py
77 lines (64 loc) · 1.8 KB
/
MatrixScanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# mypy: disable-error-code="name-defined,index"
from sly import Lexer
from sly.lex import Token
from Utils import report_error
# noinspection PyUnresolvedReferences,PyUnboundLocalVariable
class MatrixScanner(Lexer):
tokens = {
DOTADD, DOTSUB, DOTMUL, DOTDIV,
ADDASSIGN, SUBASSIGN, MULASSIGN, DIVASSIGN,
LESS_EQUAL, GREATER_EQUAL, NOT_EQUAL, EQUAL,
IF, ELSE, FOR, WHILE, BREAK, CONTINUE, RETURN, EYE, ZEROS, ONES, PRINT,
ID, INTNUM, FLOAT, STRING
}
literals = {
# relation operators
'<', '>',
# assignment operators
'=',
# binary operators
'+', '-', '*', '/',
# brackets
'(', ')', '[', ']', '{', '}',
# other
':', "'", ',', ';'
}
# matrix binary operators
DOTADD = r'\.\+'
DOTSUB = r'\.-'
DOTMUL = r'\.\*'
DOTDIV = r'\./'
# relation operators
LESS_EQUAL = r'<='
GREATER_EQUAL = r'>='
NOT_EQUAL = r'!='
EQUAL = r'=='
# assignment operators
ADDASSIGN = r'\+='
SUBASSIGN = r'-='
MULASSIGN = r'\*='
DIVASSIGN = r'/='
ID = r'[a-zA-Z_][a-zA-Z0-9_]*'
# keywords
ID['if'] = IF
ID['else'] = ELSE
ID['for'] = FOR
ID['while'] = WHILE
ID['break'] = BREAK
ID['continue'] = CONTINUE
ID['return'] = RETURN
ID['eye'] = EYE
ID['zeros'] = ZEROS
ID['ones'] = ONES
ID['print'] = PRINT
FLOAT = r'(\d+(\.\d*)|\.\d+)([eE][+-]?\d+)?' # https://regex101.com/r/Obq7Y4/1
INTNUM = r'[0-9]+'
STRING = r'"[^"]*"'
ignore = ' \t'
ignore_comment = r'\#.*'
@_(r'\n+')
def ignore_newline(self, t: Token) -> None:
self.lineno += len(t.value)
def error(self, t: Token) -> None:
report_error(self, f"Illegal character '{t.value[0]}'", self.lineno)
self.index += 1