-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathexpr_lexer_rules.py
More file actions
131 lines (113 loc) · 3.72 KB
/
expr_lexer_rules.py
File metadata and controls
131 lines (113 loc) · 3.72 KB
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
128
129
130
131
# Copyright 2022 The FeatHub Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Dict, Optional, Any, Tuple
from ply import lex
from ply.lex import TOKEN
from feathub.common.exceptions import FeathubExpressionException
VARIABLE_NAME_REGEX = r"[a-zA-Z_][a-zA-Z0-9_]*"
ID_REGEX = rf"{VARIABLE_NAME_REGEX}|`{VARIABLE_NAME_REGEX}`"
class ExprLexerRules:
literals = ["+", "-", "*", "/"]
data_types = [
"bytes",
"string",
"integer",
"bigint",
"float",
"double",
"boolean",
"timestamp",
]
# Map from reserved keywords to its token type and token value. If token value is
# None, the value is set to the text that matches the reserved keywords.
reserved: Dict[str, Tuple[str, Optional[Any]]] = {
"true": ("TRUE", True),
"false": ("FALSE", False),
"cast": ("CAST", "CAST"),
"try_cast": ("TRY_CAST", "TRY_CAST"),
"as": ("AS", "AS"),
"is": ("IS", "IS"),
"not": ("NOT", "NOT"),
"null": ("NULL", "NULL"),
"case": ("CASE", "CASE"),
"when": ("WHEN", "WHEN"),
"then": ("THEN", "THEN"),
"else": ("ELSE", "ELSE"),
"end": ("END", "END"),
"and": ("AND", "AND"),
"or": ("OR", "OR"),
**{dtype: ("DTYPE", dtype.upper()) for dtype in data_types},
}
# List of token names. This is always required
tokens = [
"LPAREN",
"RPAREN",
"LBRACKET",
"RBRACKET",
"LT",
"LE",
"GT",
"GE",
"EQ",
"NE",
"COMMA",
"FLOAT",
"INTEGER",
"STRING",
"ID",
] + list(set([v[0] for v in reserved.values()]))
# Regular expression rules for simple tokens
t_LPAREN = r"\("
t_RPAREN = r"\)"
t_LBRACKET = r"\["
t_RBRACKET = r"\]"
t_LT = r"<"
t_LE = r"<="
t_GT = r">"
t_GE = r">="
t_EQ = r"="
t_NE = r"<>"
t_COMMA = r"\,"
t_STRING = r"(\".*?\"|\'.*?\')"
# A string containing ignored characters (spaces and tabs)
t_ignore = " \t"
@TOKEN(r"((\d*\.\d+)(E[\+-]?\d+)?|([1-9]\d*E[\+-]?\d+))")
def t_FLOAT(self, t: lex.LexToken) -> lex.LexToken:
t.value = float(t.value)
return t
@TOKEN(r"\d+")
def t_INTEGER(self, t: lex.LexToken) -> lex.LexToken:
t.value = int(t.value)
return t
@TOKEN(ID_REGEX)
def t_ID(self, t: lex.LexToken) -> lex.LexToken:
# Do not check the reserved keywords map if the id is surrounded by backticks
if t.value[0] == "`" and t.value[-1] == "`":
t.type = "ID"
t.value = t.value[1:-1]
return t
token_type, token_value = ExprLexerRules.reserved.get(
t.value.lower(), ("ID", None)
)
t.type = token_type # Check for reserved words case-insensitive
if token_value is not None:
t.value = token_value
return t
# Define a rule so we can track line numbers
@TOKEN(r"\n+")
def t_newline(self, t: lex.LexToken) -> None:
t.lexer.lineno += len(t.value)
# Error handling rule
def t_error(self, t: lex.LexToken) -> None:
raise FeathubExpressionException(f"Illegal character '{t.value[0]}'")