-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvparser.py
895 lines (814 loc) · 27.6 KB
/
pvparser.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
"""
Recursive descent parser to check for the syntax and some semantic errors in
the EPICS pvload/pvsave files. The BNF was adapted (with some differences) from
the original implementation by W. Lupton.
/* A file is a list of items. Empty files are allowed. */
file
: file item
| item
| /* empty */
;
/* An item can be a group statement, an single statement or a sleep statement */
item
: group
| single
| sleep
;
/* A group statement is a grouped list of single items */
group: group_head TOKEN_LEFT_BRACE group_body TOKEN_RIGHT_BRACE group_tail
;
/* The group head starts with the token 'group'.
If no head is found, then no group is present.
*/
group_head
: TOKEN_GROUP
;
/* The group body is a list on single statements */
group_body
: group_body single
| single
| /* empty */
;
/* The group tail is an optional semicolon */
group_tail
: TOKEN_SEMICOLON
| /* empty */
/* A sleep statement starts with the token 'sleep'.
if no 'sleep' token is found, then no sleep statement is present.
The time to sleep is optional.
*/
sleep
: TOKEN_SLEEP TOKEN_SEMICOLON
| TOKEN_SLEEP TOKEN_INTEGER TOKEN_SEMICOLON
| TOKEN_SLEEP TOKEN_DOUBLE TOKEN_SEMICOLON
;
/* A single statement is a C like declaration and assignment */
single
: single_head single_equals single_body TOKEN_SEMICOLON
/* The single head contains all the elements before the equal sign */
single_head
: single_start single_type single_name single_count
;
/* The single start is the optional percent ('%') token */
single_start
: /* empty */
| TOKEN_PERCENT
;
/* The single type is any of the (optional) type declarations. */
single_type
: TOKEN_TYPE
| /* empty */
;
/* The single name must be a valid process variable name
single_name
: tokenPVNAME
;
/* The (optional) single count is used to define arrays */
single_count
: TOKEN_LEFT_BRACKET TOKEN_INTEGER TOKEN_RIGHT_BRACKET
| /* empty */
;
/* The single equals must be the token '=' */
single_equals
: TOKEN_EQUALS
/* The single body specifies the value to be assigned. This implementation differs from the
original pvload BNF in that it does not allow a list of values not enclosed in braces.
*/
single_body
: single_individual_value
| TOKEN_LEFT_BRACE single_body_value_list TOKEN_RIGHT_BRACE
;
/* The single value list is a list of single individual values separated by commas */
single_value_list
: single_individual_value
| single_value_list TOKEN_COMMA single_individual_value
;
/* A single individual value has an index, followed by a value and scale */
single_individual_value
: single_index single_value single_scale
;
/* The single index is an integer an (optional) number enclosed in square brackets */
single_index
: TOKEN_LEFT_BRACKET TOKEN_INTEGER TOKEN_RIGHT_BRACKET
| /* empty */
;
/* The single value must be either an integer, real or string.
single_value
: tokenINTEGER
| tokenREAL
| tokenSTRING
;
/* The single scale can be a scaling factor, or any of the accepted scaling units */
single_scale
: TOKEN_TIMES TOKEN_INTEGER
| TOKEN_TIMES TOKEN_REAL
| TOKEN_DIVIDED TOKEN_INTEGER
| TOKEN_DIVIDED TOKEN_REAL
| TOKEN_INTEGER
| TOKEN_REAL
| TOKEN_DIVIDED TOKEN_ARCSECS
| TOKEN_ARCSECS
| TOKEN_DIVIDED TOKEN_DEGREES
| TOKEN_DEGREES
| TOKEN_DIVIDED TOKEN_UM
| TOKEN_UM
| TOKEN_DIVIDED TOKEN_MM
| TOKEN_MM
| TOKEN_DIVIDED TOKEN_M
| TOKEN_M
| /* empty */
;
/* An index of array count is an integer number enclosed in square brackets */
single_index_or_count
: TOKEN_LEFT_BRACKET TOKEN_INTEGER TOKEN_RIGHT_BRACKET
| /* empty */
;
"""
from pvtoken import PvToken
from pvlexer import PvLexer
from pvlexer import TOKEN_NONE, TOKEN_EOF
from pvlexer import TOKEN_INTEGER, TOKEN_FLOAT, TOKEN_STRING, TOKEN_PVNAME
from pvlexer import TOKEN_TYPE, TOKEN_UNIT, TOKEN_GROUP, TOKEN_SLEEP
from pvlexer import TOKEN_SEMICOLON, TOKEN_COMMA
from pvlexer import TOKEN_EQUALS, TOKEN_TIMES, TOKEN_DIVIDED, TOKEN_PERCENT
from pvlexer import TOKEN_LEFT_BRACE, TOKEN_RIGHT_BRACE, TOKEN_LEFT_BRACKET, TOKEN_RIGHT_BRACKET
from pvlexer import TOKEN_ERROR
# Pvload defines a total of eight possible types for EPICS channels.
# They can be grouped into three different basic types.
TYPE_NONE = 0 # not defined yet (internal use)
TYPE_INTEGER = 1
TYPE_FLOAT = 2
TYPE_STRING = 3
class PvParser:
class PvSyntaxError(Exception):
def __init___(self, message):
Exception.__init__(self, message)
def __init__(self, debug=False, verbose=False):
self.f_in = None
self.file_name = ''
self.lex = PvLexer()
self.token = None
# output control
self.debug = debug
self.verbose = verbose
# the following variables are used for simple statement checks
self.single_data_type = TYPE_NONE
self.single_name = ''
self.single_count = 0
self.single_value_list = []
self.single_index_list = []
# initialize state
self.flush_token()
self.clear_single()
# dictionary to map types to a string representation
self.type_map = {TYPE_NONE: 'none', TYPE_INTEGER: 'int', TYPE_FLOAT: 'float', TYPE_STRING: 'string'}
def __str__(self):
return 'PvParser(' + \
'[' + self.file_name + '] ' + \
str(self.token) + ' - ' + \
self.type_map[self.single_data_type] + ' ' + \
'[' + str(self.single_name) + ']' + ' ' + \
str(self.single_count) + ' ' + \
str(self.single_index_list) + ' ' + \
str(self.single_value_list) + \
')'
def clear_single(self):
"""
Clear/reset the variables used to store the single statement elements.
:return: None
"""
self.single_data_type = TYPE_NONE # data type
self.single_name = '' # pv name
self.single_count = 1 # array size
self.single_value_list = [] # value list
self.single_index_list = [] # index list
def check_single(self):
"""
Check for single statement consistency.
"""
# Check for array consistency
if self.single_count != len(self.single_value_list):
self.pv_warning('list of values does not match array size')
if len(self.single_index_list) != len(set(self.single_index_list)):
self.pv_warning('repeated indices')
if len(self.single_index_list) and (len(self.single_value_list) != len(self.single_index_list)):
self.pv_warning('missing indices?')
# Check for type consistency
if self.single_data_type == TYPE_NONE:
self.pv_warning('type not defined')
elif self.single_data_type == TYPE_INTEGER:
for value in self.single_value_list:
try:
int(value)
except ValueError:
self.pv_warning('type mismatch')
break
elif self.single_data_type == TYPE_FLOAT:
for value in self.single_value_list:
try:
float(value)
except ValueError:
self.pv_warning('type mismatch')
break
elif self.single_data_type == TYPE_STRING:
for value in self.single_value_list:
try:
int(value)
self.pv_warning('type mismatch')
except ValueError:
pass
try:
float(value)
self.pv_warning('type mismatch')
except ValueError:
pass
@staticmethod
def map_type(token):
"""
Map the token pvload data type string (stored in it's value) into
a smaller set of (compatible) data types. This routine is used to check
for compatible types in single value statements.
:param token:
:type token: PvToken
:return: token data type
:rtype: int
"""
token_value = token.get_value()
if token_value in ['string', 'char', 'enum']:
return TYPE_STRING
elif token_value in ['short', 'int', 'long']:
return TYPE_INTEGER
elif token_value in ['float', 'double']:
return TYPE_FLOAT
else:
return TYPE_NONE
def pv_error(self, text=''):
"""
Print an error message. Used to report syntax errors in the pvload file.
It generates an exception. It is up to the caller to decide whether to
abort of try to recover from it.
:param text: error message
:type text: str
:raises: PvSyntaxError
"""
line_number, line_text = self.lex.get_last_line()
token_value = self.token.get_value()
if text:
format_string = 'Error: at \'{0}\', file {1}, line {2} -> {3}\n>> {4}'
message = format_string.format(token_value, self.file_name, line_number, text, line_text)
else:
format_string = 'Error: at \'{0}\', file {1}, line {2}\n>> {3}'
message = format_string.format(token_value, self.file_name, line_number, line_text)
raise self.PvSyntaxError(message)
def pv_warning(self, text=''):
"""
Print a warning message. Used to report minor inconsistencies in the pvload file.
:param text: error message
:type text: str
"""
line_number, line_text = self.lex.get_last_line()
if text:
format_string = 'Warning: file {0}, line {1} -> {2}\n>> {3}'
message = format_string.format(self.file_name, line_number, text, line_text)
else:
format_string = 'Warning: {0}, line {1}\n>> {2}'
message = format_string.format(self.file_name, line_number, line_text)
print(message)
return
def trace(self, text):
"""
Routine used to print details about the program execution.
Used extensively during the implementation and testing of the parser.
Trace is enabled at object creation time.
:param text: text to print
"""
if self.debug:
print('> ' + text, self.token)
def get_token(self):
"""
This routine is a front end to the lexer next_token() function.
The parser always keeps the latest token read and only gets rid of it
when it has been consumed by the parser. This is equivalent to having
a push back or look ahead functionality in the lexer.
Errors returned by the lexer are trapped here.
:return: next token
:rtype: PvToken
"""
if self.token.match(TOKEN_NONE):
self.token = self.lex.next_token(self.f_in)
# trap lexer errors here
if self.token.match(TOKEN_ERROR):
self.pv_error()
self.trace('+')
return self.token
def flush_token(self):
"""
Clear the latest token read (marked it as consumed). This will force get_token()
to read a new token from the lexer the next time is called.
"""
self.trace('flush_token')
self.token = PvToken(TOKEN_NONE, 'none')
def flush_and_get_token(self):
"""
Handy way of to calling flush_token() and get_token() in one call.
:return: next token
:rtype: PvToken
"""
self.trace('flush_and_get_token')
self.flush_token()
return self.get_token()
# --------------------------------------------------------
# The recursive parser routines start here
# --------------------------------------------------------
# --------------------------------------------------------
# - File
# --------------------------------------------------------
def pv_file(self, input_file_name):
"""
A file is a list of items. Files can be empty.
---
file
: file item
| item
| /* empty */
;
---
:param input_file_name: input file name
:type input_file_name: str
:return: True if file found, False otherwise
:rtype: bool
"""
self.trace('pv_file')
try:
self.f_in = open(input_file_name, 'r')
self.file_name = input_file_name
except IOError:
return False
if self.verbose:
print(self.file_name)
while True:
try:
if not self.pv_item():
break
except self.PvSyntaxError as e:
print(e)
self.flush_token()
self.clear_single()
self.lex.flush()
self.f_in.close()
self.f_in = None
self.file_name = ''
return True
# --------------------------------------------------------
# - Item
# --------------------------------------------------------
def pv_item(self):
"""
An item is a group statement, a single statement or a sleep statement.
---
item
: group
| single
| sleep
;
---
:return: True if group, single or sleep. Otherwise, raise exception.
:rtype: bool
:raises: PvSyntaxError
"""
self.trace('pv_item')
if self.pv_group():
return True
elif self.pv_sleep():
return True
elif self.pv_single():
return True
elif self.get_token().match(TOKEN_EOF):
return False # end of items
else:
self.pv_error()
# --------------------------------------------------------
# - Group
# --------------------------------------------------------
def pv_group(self):
"""
A group is a grouped list of single statements
---
group: group_head TOKEN_LEFT_BRACE group_body TOKEN_RIGHT_BRACE group_tail
;
---
:return: True if group found, False otherwise.
:rtype: bool
:raises: PvSyntaxError
"""
self.trace('pv_group')
if self.pv_group_head():
if self.get_token().match(TOKEN_LEFT_BRACE):
self.flush_token()
self.pv_group_body()
self.pv_group_tail()
if self.get_token().match(TOKEN_RIGHT_BRACE):
self.flush_token()
return True
else:
self.pv_error()
else:
return False
def pv_group_head(self):
"""
The group head starts with the token TOKEN_GROUP.
If no head is found, then no group is present.
---
group_head
: TOKEN_GROUP
;
---
:return: True if start of group found, False otherwise
:rtype: bool
"""
self.trace('pv_group_head')
if self.get_token().match(TOKEN_GROUP):
self.flush_token()
return True
else:
return False # no group found
def pv_group_body(self):
"""
The group body is a list on single statements. A group can be empty.
---
group_body
: group_body single
| single
| /* empty */
;
---
:return: Always true
:rtype: bool
"""
self.trace('pv_group_body')
while self.pv_single():
pass
return True
def pv_group_tail(self):
"""
The group tail is an optional TOKEN_SEMICOLON
---
group_tail
: TOKEN_SEMICOLON
| /* empty */
;
---
"""
self.trace('pv_group_tail')
if self.get_token().match(TOKEN_SEMICOLON):
self.flush_token()
# --------------------------------------------------------
# - Sleep
# --------------------------------------------------------
def pv_sleep(self):
"""
A sleep statement starts with the token TOKEN_SLEEP.
If it's not there, then no sleep statement is present.
---
sleep
: TOKEN_SLEEP TOKEN_SEMICOLON
| TOKEN_SLEEP TOKEN_INTEGER TOKEN_SEMICOLON
| TOKEN_SLEEP TOKEN_DOUBLE TOKEN_SEMICOLON
;
---
:return: True if a sleep statement was found, False otherwise
:rtype: bool
:raises: PvSyntaxError
"""
self.trace('pv_sleep')
if self.get_token().match(TOKEN_SLEEP):
token = self.flush_and_get_token()
if token.match(TOKEN_INTEGER) or token.match(TOKEN_FLOAT):
if self.flush_and_get_token().match(TOKEN_SEMICOLON):
self.flush_token()
else:
self.pv_error('expected \';\'')
elif token.match(TOKEN_SEMICOLON):
self.pv_warning('no time specified in sleep')
self.flush_token()
else:
self.pv_error('expected integer or float value')
return True
return False
# --------------------------------------------------------
# - Single
# --------------------------------------------------------
def pv_single(self):
"""
A single statement is a C like declaration and assignment
---
single
: single_head single_equals single_body TOKEN_SEMICOLON
---
:return: True if single statement, False otherwise
:rtype: bool
"""
self.trace('pv_single')
self.clear_single()
if self.pv_single_head():
if self.pv_single_equals():
if self.pv_single_body():
if self.get_token().match(TOKEN_SEMICOLON):
self.check_single()
self.flush_token()
return True
else:
self.pv_error('expected \';\'')
return False
# --------------------------------------------------------
# - Single head
# --------------------------------------------------------
def pv_single_head(self):
"""
The single statement head contains all the elements before the equal sign
---
single_head
: single_start single_type single_name single_count
;
---
:return: True if valid head
:rtype: bool
:raises: PvSyntaxError
"""
self.trace('pv_single_head')
return self.pv_single_start() and self.pv_single_type() and self.pv_single_name() and self.pv_single_count()
def pv_single_start(self):
"""
The single statement start is the optional TOKEN_PERCENT
---
single_start
: /* empty */
| TOKEN_PERCENT
;
---
:return: always true; percent is optional
:rtype: bool
"""
self.trace('pv_single_start')
if self.get_token().match(TOKEN_PERCENT):
self.flush_token()
return True
def pv_single_type(self):
"""
The single statement type is any of the optional type declarations.
---
single_type
: TOKEN_TYPE
| /* empty */
;
---
:return: always true; type optional
:rtype: bool
"""
self.trace('pv_single_type')
token = self.get_token()
if token.match(TOKEN_TYPE):
self.single_data_type = self.map_type(token)
self.flush_token()
return True
def pv_single_name(self):
"""
The single statement name must be a valid pv name
---
single_name
: tokenPVNAME
;
---
:return: True if name detected, False otherwise
:raises: PvSyntaxError
"""
self.trace('pv_single_name')
token = self.get_token()
if token.match(TOKEN_PVNAME):
self.single_name = token.get_value()
self.flush_token()
return True
else:
return False
def pv_single_count(self):
"""
The single statement count is used to define arrays.
---
single_count
: TOKEN_LEFT_BRACKET TOKEN_INTEGER TOKEN_RIGHT_BRACKET
| /* empty */
;
---
:return: Always True
:raises: PvSyntaxError
"""
self.trace('pv_single_count')
count = self.pv_single_index_or_count()
self.single_count = count if count is not None else 1
return True
# --------------------------------------------------------
# - Single equals
# --------------------------------------------------------
def pv_single_equals(self):
"""
The single statement equals must contain the token TOKEN_EQUALS
---
single_equals
: TOKEN_EQUALS
---
:return: True if equals detected, else raise exception
:raises: PvSyntaxError
"""
self.trace('pv_single_equals')
if self.get_token().match(TOKEN_EQUALS):
self.flush_token()
return True
else:
self.pv_error('\'=\' expected')
# --------------------------------------------------------
# - Single body
# --------------------------------------------------------
def pv_single_body(self):
"""
The single statement body specifies the value to be assigned
The implementation here deviates from the original BNF in that it does not
allow a list of values if it's not enclosed in braces.
---
single_body
: single_individual_value
| single_value_list <- NOT ALLOWED IN THIS IMPLEMENTATION!!
| TOKEN_LEFT_BRACE single_body_value_list TOKEN_RIGHT_BRACE
;
---
:return: True if single body
:rtype: bool
"""
self.trace('pv_single_body')
if self.get_token().match(TOKEN_LEFT_BRACE):
self.flush_token()
self.pv_single_value_list()
if self.get_token().match(TOKEN_RIGHT_BRACE):
self.flush_token()
return True
else:
self.pv_error('expected \'}\'')
else:
return self.pv_single_individual_value()
def pv_single_value_list(self):
"""
A single statement value list is a list of values delimited by commas.
---
single_value_list
: single_individual_value
| single_value_list TOKEN_COMMA single_individual_value
;
---
:return: Always true
:rtype: bool
"""
self.trace('pv_single_value_list')
while True:
if self.pv_single_individual_value():
if self.get_token().match(TOKEN_COMMA):
self.flush_token()
else:
break
else:
break
return True
# --------------------------------------------------------
# - Single individual value
# --------------------------------------------------------
def pv_single_individual_value(self):
"""
A single statement individual value consists of an index
a single value and a scale.
---
single_individual_value
: single_index single_value single_scale
;
---
:return: True if individual value is found, exception otherwise
:rtype: bool
:raises: PvSyntaxError
"""
self.trace('pv_single_individual_value')
return self.pv_single_index() and self.pv_single_value() and self.pv_single_scale()
def pv_single_index(self):
"""
A single statement index is an integer number in square brackets.
---
single_index
: TOKEN_LEFT_BRACKET TOKEN_INTEGER TOKEN_RIGHT_BRACKET
| /* empty */
;
---
:return: always true; index optional
:raises: PvSyntaxError
"""
self.trace('pv_single_index')
index = self.pv_single_index_or_count()
if index is not None:
self.single_index_list.append(index)
return True
def pv_single_value(self):
"""
The single statement value must be either an integer, real or string.
---
single_value
: tokenINTEGER
| tokenREAL
| tokenSTRING
;
---
:return: True if value found, exception otherwise
:rtype: bool
:raises: PvSyntaxError
"""
self.trace('pv_single_value')
token = self.get_token()
if token.is_in([TOKEN_INTEGER, TOKEN_FLOAT, TOKEN_STRING]):
self.single_value_list.append(token.get_value())
self.flush_token()
return True
else:
self.pv_error('expected string, float or integer value')
def pv_single_scale(self):
"""
A single statement scale can be a integer or floating point factor,
or any of the predefined scaling units.
---
single_scale
: TOKEN_TIMES TOKEN_INTEGER
| TOKEN_TIMES TOKEN_REAL
| TOKEN_DIVIDED TOKEN_INTEGER
| TOKEN_DIVIDED TOKEN_REAL
| TOKEN_INTEGER
| TOKEN_REAL
| TOKEN_DIVIDED TOKEN_ARCSECS
| TOKEN_ARCSECS
| TOKEN_DIVIDED TOKEN_DEGREES
| TOKEN_DEGREES
| TOKEN_DIVIDED TOKEN_UM
| TOKEN_UM
| TOKEN_DIVIDED TOKEN_MM
| TOKEN_MM
| TOKEN_DIVIDED TOKEN_M
| TOKEN_M
| /* empty */
;
---
:return: always true; scale optional
:rtype: bool
:raises: PvSyntaxError
"""
self.trace('pv_single_scale')
if self.get_token().match(TOKEN_TIMES):
token = self.flush_and_get_token()
if token in [TOKEN_INTEGER, TOKEN_FLOAT]:
self.flush_token()
else:
self.pv_error('expected integer or float value')
elif self.get_token().match(TOKEN_DIVIDED):
token = self.flush_and_get_token()
if token in [TOKEN_INTEGER, TOKEN_FLOAT, TOKEN_UNIT]:
self.flush_token()
else:
self.pv_error('expected integer/float value or unit qualifier')
else:
token = self.get_token()
if token in [TOKEN_INTEGER, TOKEN_FLOAT, TOKEN_UNIT]:
self.flush_token()
return True
def pv_single_index_or_count(self):
"""
This an common routine used to parse both an index or an array count.
Both are enclosed in square brackets so it made sense to have a single routine.
---
single_index_or_count
: TOKEN_LEFT_BRACKET TOKEN_INTEGER TOKEN_RIGHT_BRACKET
| /* empty */
;
---
:return: index or count optional, or None of not specified
:rtype: int
:raises: PvSyntaxError
"""
value = None
self.trace('pv_single_index_or_count')
if self.get_token().match(TOKEN_LEFT_BRACKET):
token = self.flush_and_get_token()
if token.match(TOKEN_INTEGER):
value = int(token.get_value())
if self.flush_and_get_token().match(TOKEN_RIGHT_BRACKET):
self.flush_token()
else:
self.pv_error('expected \']\'')
else:
self.pv_error('integer value expected')
return value
if __name__ == '__main__':
file_list = ['example1.pv']
parser = PvParser()
for file_name in file_list:
parser.pv_file(file_name)