-
Notifications
You must be signed in to change notification settings - Fork 0
/
TP_lex.py
83 lines (66 loc) · 1.45 KB
/
TP_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
import datetime
import ply.lex as lex
import re
tokens = (
'DATETIME',
'KEY',
'COMMENT',
'STRING',
'NUMBER',
'BOOLEAN',
'NEWLINE',
'EQUALS',
'LSQBRACKET',
'RSQBRACKET',
'COMMA',
'LCHAVETA',
'RCHAVETA',
'DOT',
'LPAREN',
'RPAREN'
)
t_NEWLINE = r'\n'
def t_DATETIME(t):
r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?([Zz]|[+-]\d{2}:\d{2})? | \d{4}\-\d{2}\-\d{2} | \d{2}\:\d{2}\:\d{2})'
return t
def t_COMMENT(t):
r'\#.*'
pass
def t_STRING(t):
r'("[^"\\]*(?:\\.[^"\\]*)*"|\'[^\'\\]*(?:\\.[^\'\\]*)*\')'
t.value = t.value[1:len(t.value)-1]
return t
def t_NUMBER(t):
r'-?\d+(\.\d+)?'
if '.' in t.value:
t.value = float(t.value)
else:
t.value = int(t.value)
return t
def t_BOOLEAN(t):
r'(true|false)'
if t.value == 'true':
t.value = True
else:
t.value = False
return t
def t_KEY(t):
r'[a-zA-Z]\w+'
return t
#t_INLINETABLE = r'\{[^{}]*\}'
#t_TABLENAME = r'(?<=\[)[^\[\]""]+(?=\])'
#t_SUBTABLENAME = r'(?<=\[)[^\[\]"]+\.[^\[\]"]+(?=\])'
t_LSQBRACKET = r'\['
t_RSQBRACKET = r'\]'
t_COMMA = r'\,'
t_EQUALS = r'\='
t_LCHAVETA = r'\{'
t_RCHAVETA = r'\}'
t_DOT = r'\.'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_ignore = ' \t'
def t_error(t):
print('Illegal character: ', t.value[0])
t.lexer.skip(1)
lexer = lex.lex()