Debian Codegraph @

src/drel/drel_lex.py

raw symbols SCIP
1 #Attempt to implement dREL using PLY (Python Lex Yacc)
2 from __future__ import print_function
3 import ply.lex as lex
4 import re #for multiline flag
5
6 states = (
7 ('paren','inclusive'),
8 )
9
10 tokens = (
11 'SHORTSTRING',
12 'LONGSTRING',
13 'INTEGER',
14 'BININT',
15 'HEXINT',
16 'OCTINT',
17 'REAL',
18 'POWER',
19 'ISEQUAL',
20 'NEQ',
21 'GTE',
22 'LTE',
23 'IMAGINARY',
24 'ID', #variable name
25 'ITEM_TAG', #cif item as variable
26 'COMMENT',
27 'STRPREFIX',
28 'ELLIPSIS',
29 'AND',
30 'BADAND',
31 'OR',
32 'BADOR',
33 'IN',
34 'NOT',
35 'DO',
36 'FOR',
37 'LOOP',
38 'REPEAT',
39 'AS',
40 'WITH',
41 'WHERE',
42 'ELSEIF',
43 'ELSE',
44 'BREAK',
45 'NEXT',
46 'IF',
47 'SWITCH',
48 'CASE',
49 'DEFAULT',
50 'AUGOP',
51 'FUNCTION',
52 'NEWLINE',
53 'ESCAPE_NEWLINE',
54 'OPEN_PAREN',
55 'CLOSE_PAREN'
56 )
57
58 literals = '+*-/;[],:^<>{}=.`&|' #'&' and '|' are pycifrw extensions
59 t_INITIAL_ignore = ' \t'
60 t_paren_ignore = ' \t\n'
61
62 def t_error(t):
63 print('Illegal character %s' % repr(t.value[0]))
64
65 t_POWER = r'\*\*'
66 t_ISEQUAL = r'=='
67 t_NEQ = r'!='
68 t_GTE = r'>='
69 t_LTE = r'<='
70 t_ELLIPSIS = r'\.\.\.'
71 t_BADOR = r'\|\|'
72 t_BADAND = r'&&'
73
74 def t_AUGOP(t):
75 r'(\+\+=)|(\+=)|(-=)|(--=)|(\*=)|(/=)'
76 return t
77
78 # We do not have this as a literal so that we can switch to ignoring newlines
79 def t_INITIAL_OPEN_PAREN(t):
80 r'\('
81 t.lexer.paren_level = 1
82 t.lexer.begin('paren')
83 return t
84
85 def t_paren_OPEN_PAREN(t):
86 r'\('
87 t.lexer.paren_level +=1
88 return t
89
90 def t_paren_CLOSE_PAREN(t):
91 r'\)'
92 t.lexer.paren_level -=1
93 if t.lexer.paren_level == 0:
94 t.lexer.begin('INITIAL')
95 return t
96
97 # Do the reals before the integers, otherwise the integer will
98 # match the first part of the real
99 #
100 def t_IMAGINARY(t):
101 r'(((([0-9]+[.][0-9]*)|([.][0-9]+))([Ee][+-]?[0-9]+)?)|([0-9]+))[jJ]'
102 return t
103
104 def t_REAL(t):
105 r'(([0-9]+[.][0-9]*)|([.][0-9]+))([Ee][+-]?[0-9]+)?'
106 try:
107 value = float(t.value)
108 except ValueError:
109 print('Error converting %s to real' % t.value)
110 return t
111
112 # Do the binary,octal etc before decimal integer otherwise the 0 at
113 # the front will match the decimal integer 0
114 #
115 def t_BININT(t):
116 r'0[bB][0-1]+'
117 try:
118 t.value = repr(int(t.value[2:],base=2))
119 except ValueError:
120 print('Unable to convert binary value %s' % t.value)
121 return t
122
123 def t_OCTINT(t):
124 r'0[oO][0-7]+'
125 try:
126 t.value = repr(int(t.value[2:],base=8))
127 except ValueError:
128 print('Unable to convert octal value %s' % t.value)
129 return t
130
131 def t_HEXINT(t):
132 r'0[xX][0-9a-fA-F]+'
133 try:
134 t.value = repr(int(t.value,base=16))
135 except ValueError:
136 print('Unable to convert hex value %s' % t.value)
137 return t
138
139 def t_INTEGER(t):
140 r'[0-9]+'
141 try:
142 value = int(t.value)
143 except ValueError:
144 print('Incorrect integer value %s' % t.value)
145 return t
146
147 def t_STRPREFIX(t):
148 r'r(?=["\'])|u(?=["\'])|R(?=["\'])|U(?=["\'])|ur(?=["\'])|UR(?=["\'])|Ur(?=["\'])|uR(?=["\'])'
149 return t
150
151 # try longstring first as otherwise the '' will match a shortstring
152 def t_LONGSTRING(t):
153 r"('''([^\\]|(\\.))*''')|(\"\"\"([^\\]|(\\.))*\"\"\")"
154 return t
155
156
157 def t_SHORTSTRING(t):
158 r"('([^'\n]|(\\.))*')|(\"([^\"\n]|(\\.))*\")"
159 return t
160
161 # special to avoid any ambiguity
162 def t_ELSEIF(t):
163 r"[Ee][Ll][Ss][Ee]\s+[Ii][Ff]"
164 return t
165
166 reserved = {
167 'and': 'AND',
168 'or': 'OR',
169 'in': 'IN',
170 'not': 'NOT',
171 'do': 'DO',
172 'Do': 'DO',
173 'for': 'FOR',
174 'For': 'FOR',
175 'loop': 'LOOP',
176 'Loop': 'LOOP',
177 'as': 'AS',
178 'with': 'WITH',
179 'With': 'WITH',
180 'where': 'WHERE',
181 'Where': 'WHERE',
182 'else': 'ELSE',
183 'Else': 'ELSE',
184 'Next': 'NEXT',
185 'next' : 'NEXT',
186 'break': 'BREAK',
187 'Break': 'BREAK',
188 'if': 'IF',
189 'If': 'IF',
190 'switch': 'SWITCH',
191 'case' : 'CASE',
192 'Function' : 'FUNCTION',
193 'function' : 'FUNCTION',
194 'Repeat': 'REPEAT',
195 'repeat': 'REPEAT',
196 }
197
198 def t_ID(t):
199 r'[a-zA-Z][a-zA-Z0-9_$]*'
200 t.type = reserved.get(t.value,'ID')
201 return t
202
203 # Item tags can have underscores and digits inside, and must have
204 # at least one underscore at the front
205 def t_ITEM_TAG(t):
206 r'_[a-zA-Z_0-9]+'
207 return t
208
209 def t_ESCAPE_NEWLINE(t):
210 r'\\\n'
211 t.lexer.lineno += 1
212
213 def t_INITIAL_NEWLINE(t):
214 r'\n[\n \t]*'
215 t.lexer.lineno+=len(t.value)
216 return t
217
218 def t_COMMENT(t):
219 r'\#.*'
220 pass
221
222 lexer = lex.lex(reflags=re.MULTILINE)
223 if __name__ == "__main__":
224 lex.runmain(lexer)