forked from sampottinger/ljsimpleregisterlookup
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_ljsl.py
More file actions
317 lines (285 loc) · 10.1 KB
/
parse_ljsl.py
File metadata and controls
317 lines (285 loc) · 10.1 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""Logic to parse LabJack Scribe Language.
@author: Sam Pottinger (samnsparky, http://gleap.org)
@license: GNU GPL v2
"""
import collections
import re
STATE_LOOKING_FOR_AT = 1
STATE_LOOKING_FOR_R1 = 2
STATE_LOOKING_FOR_E1 = 3
STATE_LOOKING_FOR_G = 4
STATE_LOOKING_FOR_I = 5
STATE_LOOKING_FOR_S1 = 6
STATE_LOOKING_FOR_T = 7
STATE_LOOKING_FOR_E2 = 8
STATE_LOOKING_FOR_R2 = 9
STATE_LOOKING_FOR_S2 = 10
STATE_LOOKING_FOR_COLON = 11
STATE_LOOKING_FOR_HASH = 12
STATE_LOOKING_FOR_OPEN_PAREN = 13
STATE_READING_PREFIX = 14
STATE_READING_PARAM_1 = 15
STATE_READING_PARAM_2 = 16
STATE_READING_PARAM_3 = 17
STATE_READING_POSTFIX = 18
STATE_READING_TITLE = 19
STATE_RESET = 20
STATE_READING_DEVICE_TYPE = 21
DIGITS = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
VALID_NAME_CHAR_REGEX = re.compile("[a-zA-Z]|[0-9]|_")
VALID_DEVICE_TYPE_CHAR_REGEX = VALID_NAME_CHAR_REGEX
TagComponent = collections.namedtuple(
"TagComponent",
[
"title",
"prefix",
"start_num",
"num_regs",
"num_between_regs",
"postfix",
"includes_ljmmm",
"device_types",
]
)
class ParserAutomaton:
"""Automaton to parse LJSL.
An Automaton that scans for LJSL tags, converting each tag into a collection
of TagComponents.
"""
def __init__(self, start_state=STATE_LOOKING_FOR_AT):
"""Create a new ParserAutomaton in the state of looking for at.
Create a new ParserAutomaton without any tags that starts in the state
STATE_LOOKING_FOR_AT in which it is looking for the @ symbol.
"""
self.state = start_state
self.tags = []
self.errors = []
self.title = ""
self.device_types = []
self.device_type = ""
self.rules = {
STATE_LOOKING_FOR_AT: self.character_match(
"@",
STATE_LOOKING_FOR_R1,
STATE_RESET
),
STATE_LOOKING_FOR_R1: self.character_match(
"r",
STATE_LOOKING_FOR_E1,
STATE_RESET
),
STATE_LOOKING_FOR_E1: self.character_match(
"e",
STATE_LOOKING_FOR_G,
STATE_RESET
),
STATE_LOOKING_FOR_G: self.character_match(
"g",
STATE_LOOKING_FOR_I,
STATE_RESET
),
STATE_LOOKING_FOR_I: self.character_match(
"i",
STATE_LOOKING_FOR_S1,
STATE_RESET
),
STATE_LOOKING_FOR_S1: self.character_match(
"s",
STATE_LOOKING_FOR_T,
STATE_RESET
),
STATE_LOOKING_FOR_T: self.character_match(
"t",
STATE_LOOKING_FOR_E2,
STATE_RESET
),
STATE_LOOKING_FOR_E2: self.character_match(
"e",
STATE_LOOKING_FOR_R2,
STATE_RESET
),
STATE_LOOKING_FOR_R2: self.character_match(
"r",
STATE_LOOKING_FOR_S2,
STATE_RESET
),
STATE_LOOKING_FOR_S2: self.character_match(
"s",
STATE_LOOKING_FOR_COLON,
STATE_RESET
),
STATE_LOOKING_FOR_COLON: self.looking_for_colon,
STATE_READING_TITLE: self.reading_title,
STATE_READING_DEVICE_TYPE: self.reading_device_type,
STATE_READING_PREFIX: self.reading_prefix,
STATE_LOOKING_FOR_OPEN_PAREN: self.character_match(
"(",
STATE_READING_PARAM_1,
STATE_RESET
),
STATE_READING_PARAM_1: self.reading_param_1,
STATE_READING_PARAM_2: self.reading_param_2,
STATE_READING_PARAM_3: self.reading_param_3,
STATE_READING_POSTFIX: self.reading_postfix,
STATE_RESET: self.reset
}
def next_character(self, char):
"""Have the automaton parse the next character in the input language.
@param char: The character to parse. Should be a string containing a
single character.
@type char: str
"""
self.rules[self.state](char)
def character_match(self, target_char, next_state, fail_state):
"""Create a character matching enclosure that changes automaton state.
Function generator that returns a function that checks to see if the
provided character is as expected. If it is, it changes this automaton's
state to next_state and, if not, it changes the automaton's state to
fail_state. This is largely used to check for @,r,e,g,i,s,t,e,r,s in
succession.
@param target_char: The character to look for.
@type target_char: str
@param next_state: The ID of the state to put this automaton in if the
character is matched.
@type next_state: int
@param fail_state: The ID fo the state to put his automaton in if the
character is not matched.
@type fail_state: int
@return: Function that checks a character (single character string) and
changes state based on if that character was expected.
@rtype: function
"""
def inner(char):
"""Function that checks to see if the given char is expected.
@param char: The character to check. Should be a single character
string.
@type char: str
"""
if char == target_char: self.state = next_state
else: self.state = fail_state
return inner
def reading_title(self, char):
if char == ")": self.state = STATE_LOOKING_FOR_COLON
else: self.title += char
def reading_device_type(self, char):
if char == "]":
self.state = STATE_LOOKING_FOR_COLON
self.accept_current_device_type()
elif char == ",":
self.accept_current_device_type()
elif char != None and VALID_DEVICE_TYPE_CHAR_REGEX.match(char):
self.device_type += char
else:
self.state = STATE_RESET
self.accept_current_device_type()
def looking_for_colon(self, char):
if char == ":":
self.state = STATE_READING_PREFIX
self.clear_current_subtag()
self.tag_components = []
elif char == "(":
self.state = STATE_READING_TITLE
elif char == "[":
self.state = STATE_READING_DEVICE_TYPE
else: self.state = STATE_RESET
def reading_prefix(self, char):
if char == "#":
self.state = STATE_LOOKING_FOR_OPEN_PAREN
self.param_1 = ""
elif char != None and VALID_NAME_CHAR_REGEX.match(char):
self.prefix += char
else:
self.reading_postfix(char)
def reading_param_1(self, char):
if char == ":" and len(self.param_1) > 0:
self.state = STATE_READING_PARAM_2
self.param_2 = ""
elif char not in DIGITS:
self.state = STATE_RESET
else: self.param_1 += char
def reading_param_2(self, char):
if char == ":" and len(self.param_2) > 0:
self.state = STATE_READING_PARAM_3
self.param_3 = ""
elif char == ")" and len(self.param_2) > 0:
self.state = STATE_READING_POSTFIX
elif char not in DIGITS:
self.state = STATE_RESET
else: self.param_2 += char
def reading_param_3(self, char):
if char == ")" and len(self.param_3) > 0:
self.state = STATE_READING_POSTFIX
elif char not in DIGITS:
self.state = STATE_RESET
else: self.param_3 += char
def reading_postfix(self, char):
if char == ",":
self.state = STATE_READING_PREFIX
self.try_to_accept_current_component()
self.clear_current_subtag()
elif char != None and VALID_NAME_CHAR_REGEX.match(char):
self.postfix += char
else:
self.state = STATE_RESET
self.try_to_accept_current_component()
self.end_tag()
def accept_current_device_type(self):
if not self.device_type.upper() in self.device_types and self.device_type != "":
self.device_types.append(self.device_type.upper())
self.device_type = ""
def try_to_accept_current_component(self):
if self.param_1 == None:
self.tag_components.append(
TagComponent(
self.title,
self.prefix,
None,
None,
None,
None,
False,
self.device_types,
)
)
else:
try:
param_1 = int(self.param_1)
param_2 = int(self.param_2)
param_3 = self.param_3
if param_3 != None: param_3 = int(self.param_3)
except ValueError:
params_set = (self.param_1, self.param_2, self.param_3)
err = "%s:%s:%s must all be integers." % params_set
self.errors.append(err)
return
self.tag_components.append(
TagComponent(
self.title,
self.prefix,
param_1,
param_2,
param_3,
self.postfix,
True,
self.device_types,
)
)
def clear_current_subtag(self):
self.prefix = ""
self.postfix = ""
self.param_1 = None
self.param_2 = None
self.param_3 = None
def end_tag(self):
self.title = ""
self.tags.append(self.tag_components)
def reset(self, char):
self.title = ""
self.device_types = []
if char == "@": self.state=STATE_LOOKING_FOR_R1
def find_names(msg):
parser = ParserAutomaton()
for char in msg:
parser.next_character(char)
parser.next_character(None)
return parser.tags