-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.py
executable file
·127 lines (114 loc) · 2.02 KB
/
lex.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import sly
class Lexer(sly.Lexer):
"""MochaScript lexer class."""
tokens = {
NUMBER,
STRING,
IDENT,
LPAREN,
RPAREN,
LBRACK,
RBRACK,
LBRACE,
RBRACE,
COMMA,
ARROW,
LINE_TERM,
COLON,
DOT,
PLUSEQ,
MINUSEQ,
MULEQ,
DIVEQ,
MODEQ,
EXPEQ,
OREQ,
ANDEQ,
EQEQ,
NTEQ,
LTEQ,
GTEQ,
LT,
GT,
AND,
OR,
PLUS,
MINUS,
MUL,
DIV,
MOD,
EXP,
EQ,
TRUE,
FALSE,
IF,
ELSE,
SAY,
ASK,
EXIT,
FOR,
WHILE,
TO,
IN,
FN,
}
ignore_comment = r"#.*"
ignore = " \t\n"
# Literals and identifiers
NUMBER = r"([0-9]+\.?[0-9]*|\.[0-9]+)"
STRING = r"\"[^\"]*\""
IDENT = r"[a-zA-Z_][a-zA-Z0-9_]*"
# Parentheses types
LPAREN = r"\("
RPAREN = r"\)"
LBRACK = r"\["
RBRACK = r"\]"
LBRACE = r"\{"
RBRACE = r"\}"
# Special
COMMA = r","
ARROW = r"->"
LINE_TERM = ";"
COLON = r"\:"
DOT = r"\."
# Operator-assignments
MODEQ = r"%="
EXPEQ = r"\*\*="
PLUSEQ = r"\+="
MINUSEQ = r"\-="
MULEQ = r"\*="
DIVEQ = r"\/="
OREQ = r"\|\|="
ANDEQ = r"&&="
# Comparison operators
EQEQ = r"=="
NTEQ = r"!="
LTEQ = r"<="
GTEQ = r">="
LT = r"<"
GT = r">"
# Logical operators
AND = r"\&\&"
OR = r"\|\|"
# Mathematical operators
MOD = r"\%"
EXP = r"\*\*"
PLUS = r"\+"
MINUS = r"\-"
MUL = r"\*"
DIV = r"\/"
# Assignment operator
EQ = r"="
# Keywords
IDENT["true"] = TRUE
IDENT["false"] = FALSE
IDENT["if"] = IF
IDENT["else"] = ELSE
IDENT["say"] = SAY
IDENT["ask"] = ASK
IDENT["exit"] = EXIT
IDENT["for"] = FOR
IDENT["while"] = WHILE
IDENT["to"] = TO
IDENT["in"] = IN
IDENT["fn"] = FN