-
Notifications
You must be signed in to change notification settings - Fork 7
/
flscriptparse.py
895 lines (715 loc) · 26.8 KB
/
flscriptparse.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
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from builtins import input
from builtins import str
from builtins import range
# -----------------------------------------------------------------------------
# flscriptparse.py
#
# Simple parser for FacturaLUX SCripting Language (QSA).
# -----------------------------------------------------------------------------
from optparse import OptionParser
import pprint
import sys, math
import hashlib
import ply.yacc as yacc
import ply.lex as lex
try:
from pineboolib.flparser import flex
from pineboolib.flparser.flclasses import *
except ImportError:
import flex
from flclasses import *
# Get the token map
tokens = flex.tokens
start = "source"
reserv=['nonassoc']
reserv+=list(flex.reserved)
endoffile = None
def cnvrt(val):
val = str(val)
val = val.replace('&','&')
val = val.replace('"','"')
val = val.replace("'",''')
val = val.replace("<",'<')
val = val.replace(">",'>')
return val
precedence = (
('nonassoc', 'EQUALS', 'TIMESEQUAL', 'DIVEQUAL', 'MODEQUAL', 'PLUSEQUAL', 'MINUSEQUAL'),
('left','LOR', 'LAND'),
('left', 'LT', 'LE', 'GT', 'GE', 'EQ', 'NE', 'EQQ', 'NEQ'),
('right', 'LNOT'),
('left', 'PLUS', 'MINUS'),
('left', 'TIMES', 'DIVIDE', 'MOD'),
('left', 'OR', 'AND', 'XOR', 'LSHIFT', 'RSHIFT'),
)
seen_tokens = []
tokelines = {}
last_lexspan = None
def p_parse(token):
global input_data
lexspan = list(token.lexspan(0))
data = str(token.lexer.lexdata[lexspan[0]:lexspan[1]])
if len(lexspan) == 2:
fromline = token.lineno(0)
global endoffile
endoffile = fromline, lexspan, token.slice[0]
#print fromline, lexspan, token.slice[0]
token[0] = { "00-toktype": str(token.slice[0]), "02-size" : lexspan, "50-contents" : [ { "01-type": s.type, "99-value" : s.value} for s in token.slice[1:] ] }
numelems = len([ s for s in token.slice[1:] if s.type != 'empty' and s.value is not None ])
rspan = lexspan[0]
if str(token.slice[0]) == 'empty' or numelems == 0: token[0] = None
else:
rvalues = []
for n,s in enumerate(token.slice[1:]):
if s.type != 'empty' and s.value is not None:
val = None
if isinstance(s.value,str):
val = token.lexspan(n+1)[0] + len(s.value) - 1
else:
val = token.lexspan(n+1)[1]
rvalues.append(val)
rspan = max(rvalues)
lexspan[1] = rspan
#if str(token.slice[0]) == 'regexbody':
# token[0] = { "00-toktype": str(token.slice[0]) , "02-size" : lexspan, "50-contents" : input_data[lexspan[0]:lexspan[1]+1] }
#if str(token.slice[0]) == 'regex':
# print "\r\n",str(token.slice[0]) ,":" , input_data[lexspan[0]:lexspan[1]+1]
# print " " + "\n ".join([ "%s(%r): %r" % (s.type, token.lexspan(n+1), s.value) for n,s in enumerate(token.slice[1:]) ])
global seen_tokens, last_ok_token
last_ok_token = token
seen_tokens.append((str(token.slice[0]), token.lineno(0),input_data[lexspan[0]:lexspan[1]+1] ))
global ok_count
ok_count += 1
if lexspan[0] not in tokelines:
tokelines[lexspan[0]] = token.lexer.lineno
global last_lexspan
last_lexspan = lexspan
last_ok_token = None
error_count = 0
last_error_token = None
last_error_line = -1
ok_count = 0
def p_error(t):
global error_count
global ok_count
global last_error_token
global last_error_line, seen_tokens , last_ok_token
debug = False # Poner a True para toneladas de debug.
# if error_count == 0: print
if t is not None:
if last_error_token is None or t.lexpos != getattr(last_error_token,"lexpos",None):
if abs(last_error_line - t.lineno) > 4 and ok_count > 1 and error_count < 4:
error_count += 1
try: print_context(t)
except Exception: pass
if debug == True:
error_count += 20 # no imprimir mas de un error en debug.
print
for tokname, tokln, tokdata in seen_tokens[-32:]:
if tokln == t.lineno:
print(tokname, tokdata)
print(repr(last_ok_token[0]))
for s in last_ok_token.slice[:]:
print(">>>" , s.lineno, repr(s), pprint.pformat(s.value,depth=3))
last_error_line = t.lineno
elif abs(last_error_line - t.lineno) > 1 and ok_count > 1:
last_error_line = t.lineno
parser.errok()
ok_count = 0
return
ok_count = 0
if t is None:
if last_error_token != "EOF":
print("ERROR: End of the file reached.")
global endoffile
print("Last data:", endoffile)
if last_lexspan:
try:
print("HINT: Last lexspan:", last_lexspan)
print("HINT: Last line:", tokelines[last_lexspan[0]])
except Exception as e:
print("ERROR:", e)
last_error_token = "EOF"
return t
t = parser.token()
parser.restart()
last_error_token = t
return t
p_parse.__doc__ = '''
exprval : constant
| variable
| funccall
| error
identifier : ID
dictobject_value : LBRACE RBRACE
| LBRACE dictobject_value_elemlist RBRACE
dictobject_value_elemlist : dictobject_value_elem
| dictobject_value_elemlist COMMA dictobject_value_elem
dictobject_value_elem : exprval COLON expression
base_expression : exprval
| inlinestoreinstruction
| base_expression mathoperator base_expression
| base_expression cmp_symbol base_expression
| base_expression boolcmp_symbol base_expression
| parentheses
| unary_operator
| new_operator
| ternary_operator
| dictobject_value
| typeof_operator
parentheses : LPAREN base_expression RPAREN
| LPAREN variable_1 RPAREN
unary_operator : LNOT base_expression
| MINUS base_expression
| PLUS base_expression
new_operator : NEW funccall_1
| NEW identifier
typeof_operator : TYPEOF variable
| TYPEOF base_expression
ternary_operator : base_expression CONDITIONAL1 base_expression COLON base_expression
expression : base_expression
| funcdeclaration_anon
| funcdeclaration_anon_exec
| LPAREN expression RPAREN
| error
case_cblock_list : case_block
case_cblock_list : case_cblock_list case_block
case_block : CASE expression COLON statement_list
case_default : DEFAULT COLON statement_list
case_block_list : empty
case_block_list : case_default
case_block_list : case_cblock_list
case_block_list : case_cblock_list case_default
source_element : docstring
| vardeclaration
| classdeclaration
| funcdeclaration
| funcdeclaration_anon
| funcdeclaration_anon_exec
source : source_element
source : source source_element
| statement_list
basicsource : statement_list
| empty
statement : instruction
| vardeclaration
| ifstatement
| whilestatement
| dowhilestatement
| withstatement
| forstatement
| forinstatement
| switch
| trycatch
statement_list : statement_list statement
statement_list : statement
statement_list : LBRACE statement_list RBRACE
statement_list : LBRACE RBRACE
statement_list : empty
optvartype : COLON ID
| empty
vardeclaration : VAR vardecl_list SEMI
| CONST vardecl_list SEMI
vardeclaration : VAR vardecl_list
| CONST vardecl_list
| STATIC VAR vardecl_list
vardecl : ID optvartype EQUALS expression
vardecl : ID optvartype
vardecl_list : vardecl
| vardecl_list COMMA vardecl
arglist : vardecl_list
|
funcdeclaration : FUNCTION ID LPAREN arglist RPAREN optvartype LBRACE basicsource RBRACE
funcdeclaration : STATIC FUNCTION ID LPAREN arglist RPAREN optvartype LBRACE basicsource RBRACE
funcdeclaration_anon : FUNCTION LPAREN arglist RPAREN LBRACE basicsource RBRACE
| FUNCTION LPAREN RPAREN LBRACE basicsource RBRACE
funcdeclaration_anon_exec : funcdeclaration_anon LPAREN RPAREN
| funcdeclaration_anon LPAREN arglist RPAREN
callarg : expression
callargs : callarg
| callargs COMMA callarg
| empty
varmemcall : variable_1
| funccall_1
| member_call
| member_var
| base_expression
member_var : varmemcall PERIOD variable_1
member_call : LPAREN member_var RPAREN PERIOD funccall_1
member_call : varmemcall PERIOD funccall_1
member_call : LPAREN member_call RPAREN PERIOD funccall_1
funccall : funccall_1
| member_call
| LPAREN member_call RPAREN
| LPAREN funccall_1 RPAREN
| LPAREN error RPAREN
funccall_1 : ID LPAREN callargs RPAREN
| ID LPAREN RPAREN
| TYPEOF LPAREN callargs RPAREN
mathoperator : PLUS
| MINUS
| TIMES
| DIVIDE
| MOD
| XOR
| OR
| LSHIFT
| RSHIFT
| AND
variable : variable_1
| member_var
| LPAREN variable_1 RPAREN
| LPAREN member_var RPAREN
variable_1 : identifier
| array_member
array_member : variable_1 LBRACKET expression RBRACKET
| funccall_1 LBRACKET expression RBRACKET
inlinestoreinstruction : PLUSPLUS variable
| MINUSMINUS variable
| variable PLUSPLUS
| variable MINUSMINUS
updateoperator : EQUALS
| PLUSEQUAL
| MINUSEQUAL
| MODEQUAL
| DIVEQUAL
| TIMESEQUAL
updateinstruction : variable updateoperator expression
| variable updateoperator updateinstruction
deleteinstruction : DELETE variable
storeinstruction : inlinestoreinstruction
| updateinstruction
| deleteinstruction
flowinstruction : RETURN expression
| THROW expression
| RETURN
| BREAK
| CONTINUE
instruction : base_instruction SEMI
| SEMI
| base_instruction
| funcdeclaration
| error SEMI
callinstruction : funccall
| variable
base_instruction : storeinstruction
| callinstruction
| flowinstruction
varorcall : variable
| funccall
| base_expression
optextends : EXTENDS ID
| empty
classdeclaration : CLASS ID optextends LBRACE classdeclarationsource RBRACE
classdeclarationsource : vardeclaration
| funcdeclaration
| classdeclarationsource vardeclaration
| classdeclarationsource funcdeclaration
| SEMI
| classdeclarationsource SEMI
docstring : DOCSTRINGOPEN AT ID COMMENTCLOSE
| DOCSTRINGOPEN AT ID ID COMMENTCLOSE
list_constant : LBRACKET callargs RBRACKET
list_constant : LBRACKET callargs COMMA RBRACKET
constant : ICONST
| FCONST
| CCONST
| SCONST
| regex
| list_constant
regex : DIVIDE regexbody DIVIDE regexflags
| DIVIDE regexbody COMMENTCLOSE regexflags
regexbody : regexchar
| regexbody regexchar
regexchar : LPAREN
| RPAREN
| ID
| COMMA
| XOR
| LBRACKET
| RBRACKET
| ICONST
| PLUS
| MINUS
| LBRACE
| RBRACE
| DOLLAR
| SQOUTE
| DQOUTE
| PERIOD
| BACKSLASH
| CONDITIONAL1
| EQUALS
| OR
| SCONST
| SEMI
| error
regexflags : ID
| empty
statement_block : statement
| LBRACE statement_list RBRACE
| LBRACE RBRACE
optelse : ELSE statement_block
| empty
cmp_symbol : LT
| LE
| GT
| GE
| EQ
| NE
| EQQ
| NEQ
| IN
boolcmp_symbol : LOR
| LAND
condition : expression
| error
ifstatement : IF LPAREN condition RPAREN statement_block optelse
whilestatement : WHILE LPAREN condition RPAREN statement_block
dowhilestatement : DO statement_block WHILE LPAREN condition RPAREN SEMI
| DO statement_block WHILE LPAREN condition RPAREN
withstatement : WITH LPAREN variable RPAREN statement_block
| error
storeormember : storeinstruction
| member_var
for_initialize : storeinstruction
| VAR vardecl
| for_initialize COMMA for_initialize
| empty
for_compare : expression
| empty
for_increment : storeormember
| for_increment COMMA for_increment
| empty
forstatement : FOR LPAREN for_initialize SEMI for_compare SEMI for_increment RPAREN statement_block
| error
forinstatement : FOR LPAREN for_initialize IN varorcall RPAREN statement_block
| FOR LPAREN variable IN varorcall RPAREN statement_block
| error
switch : SWITCH LPAREN condition RPAREN LBRACE case_block_list RBRACE
optid : ID
| empty
trycatch : TRY statement_block CATCH LPAREN optid RPAREN statement_block
empty :
'''
# Build the grammar
parser = yacc.yacc(method='LALR',debug=0,
optimize = 1, write_tables = 1, debugfile = '/tmp/yaccdebug.txt',outputdir='/tmp/')
#profile.run("yacc.yacc(method='LALR')")
global input_data
def print_context(token):
global input_data
if token is None: return
last_cr = input_data.rfind('\n',0,token.lexpos)
next_cr = input_data.find('\n',token.lexpos)
column = (token.lexpos - last_cr)
column1 = (token.lexpos - last_cr)
while column1 < 16:
column1 = (token.lexpos - last_cr)
last_cr = input_data.rfind('\n',0,last_cr-1)
print(input_data[last_cr:next_cr].replace("\t"," "))
print((" " * (column-1)) + "^", column, "#ERROR#" , token)
def my_tokenfunc(*args, **kwargs):
#print("Call token:" ,args, kwargs)
ret = lex.lexer.token(*args, **kwargs)
#print "Return (",args, kwargs,") = " , ret
return ret
def print_tokentree(token, depth = 0):
print(" " * depth, token.__class__ , "=" , token)
if str(token.__class__) == "ply.yacc.YaccProduction":
print(token.lexer)
for tk in token.slice:
if tk.value == token: continue
print(" " * (depth+1), tk.type, end=' ')
try:
print(tk.lexpos, end=' ')
print(tk.endlexpos, end=' ')
except:
pass
print()
print_tokentree(tk.value, depth +1)
def calctree(obj, depth = 0, num = [], otype = "source", alias_mode = 1):
#if depth > 5: return
source_data = [
'source',
'source_element',
'statement_list',
'statement',
'classdeclarationsource',
'vardecl_list',
]
final_obj = {}
final_obj['range'] = obj['02-size']
has_data = 0
has_objects = 0
contentlist = []
if alias_mode == 0:
ctype_alias = {}
elif alias_mode == 1:
ctype_alias = {
"member_var" : "member",
"member_call" : "member",
"variable_1" : "variable",
"funccall_1" : "funccall",
"flowinstruction" : "instruction",
"storeequalinstruction" : "instruction",
"vardecl" : "vardeclaration",
#"vardecl_list" : "vardeclaration",
}
else:
raise ValueError("alias_mode unknown")
if otype in ctype_alias:
otype = ctype_alias[otype]
#print " " * depth , obj['02-size']
for n,content in enumerate(obj['50-contents']):
if not isinstance(content,dict):
print("ERROR: content is not a dict!:", repr(content))
print(".. obj:", repr(obj))
raise TypeError("content is not a dict")
continue
ctype = content['01-type']
value = content['99-value']
if ctype in ctype_alias:
ctype = ctype_alias[ctype]
#if ctype in source_data:
# if depth == 0: print "--"
# print_tree(value,depth,num)
# continue
#print " " * depth , "%s:" % ".".join(num+[str(n)]), ctype,
if type(value) is dict:
#print "*"
if depth < 600:
try:
tree_obj = calctree(value,depth+1,num+[str(n)], ctype, alias_mode=alias_mode)
except Exception:
print("ERROR: trying to calculate member %d on:" % n, repr(obj))
else:
tree_obj = None
if type(tree_obj) is dict:
if (tree_obj['has_data'] or alias_mode == 0) and ctype != otype :
contentlist.append([ctype,tree_obj])
has_objects += 1
else:
contentlist+=tree_obj["content"]
has_data += tree_obj["has_data"]
has_objects += tree_obj["has_objects"]
else:
#print "=", repr(value)
contentlist.append([ctype,value])
has_data += 1
final_obj['content'] = contentlist
final_obj['has_data'] = has_data
final_obj['has_objects'] = has_objects
return final_obj
hashes = []
ranges = []
def printtree(tree, depth = 0, otype = "source", mode = None, output = sys.stdout):
global hashes, ranges
if depth == 0:
hashes = []
ranges = []
sep = " "
marginblocks = {
"classdeclaration" : 1,
"funcdeclaration" : 1,
"statement_block" : 1,
#"instruction" : 1,
}
closingtokens = [
"RBRACE",
"RPAREN",
"RBRACKET",
"SEMI",
]
nuevalinea = False
name = ""
lines = []
l = 0
for ctype, value in tree['content']:
if nuevalinea and ctype in closingtokens:
nuevalinea = False
if nuevalinea:
for i in range(int(math.ceil(l/2.0))):
lines.append(sep * depth)
nuevalinea = False
if type(value) is dict and ctype == otype:
tname,tlines,trange = printtree(value, depth, ctype)
if name == "" and tname:
name = tname
lines += tlines
elif type(value) is dict:
l = 0
if ctype in marginblocks: l = marginblocks[ctype]
for i in range(int(math.floor(l/2.0))):
lines.append(sep * depth)
tname,tlines,trange = printtree(value, depth+1, ctype)
# lines.append(sep * depth + "<!-- %d -->" % (len("".join(tlines))))
if value['has_data'] > 0 and value['has_objects'] == 0 and False:
# Do it inline!
if value['has_data']==1 and tname:
lines.append(sep * depth + "<%s id=\"%s\" />" % (ctype,tname))
else:
txt = "".join([ x.strip() for x in tlines])
lines.append(sep * depth + "<%s>%s</%s>" % (ctype,txt,ctype))
else:
attrs = []
if tname:
attrs.append(("id",tname))
txtinline = "".join([ line.strip() for line in tlines ])
#if len(tlines)>1:
txthash = hashlib.sha1(txtinline).hexdigest()[:16]
#hashes.append(("depth:",depth,"hash:",txthash,"element:",ctype+":"+tname))
hashes.append((txthash,ctype+":"+tname+"(%d)"% len(txtinline)))
ranges.append([depth,txthash]+trange+[ctype+":"+tname,len(txtinline)])
#,"start:",trange[0],"end:",trange[1]))
#attrs.append(("start",trange[0]))
#attrs.append(("end",trange[1]))
#attrs.append(("hash",txthash))
txtattrs=""
for name1, val1 in attrs:
txtattrs+=" %s=\"%s\"" % (name1,cnvrt(val1))
lines.append(sep * depth + "<%s%s>" % (ctype,txtattrs))
if depth > 50:
lines.append(sep * (depth+1) + "...")
else:
if len(txtinline)<80:
lines.append(sep * (depth+1) + txtinline)
else:
lines+=tlines
if txtattrs:
txtattrs = "<!--%s -->" % txtattrs
lines.append(sep * depth + "</%s>" % (ctype))
nuevalinea = True
else:
if ctype == "ID" and name == "":
name = value
if ctype in flex.token_literals:
lines.append(sep * depth + "<%s value=\"%s\" />" % (ctype,cnvrt(value)))
else:
lines.append(sep * depth + "<%s />" % (ctype))
if mode == "hash":
#print "\n".join(lines)
for row in sorted(ranges):
output.write("\t".join([ str(x) for x in row]))
output.write("\n")
output.flush()
if mode == "xml":
for row in lines:
output.write(row)
output.write("\n")
output.flush()
return name, lines, tree['range']
def parse(data):
global input_data
global error_count
global seen_tokens
seen_tokens[:] = []
parser.error = 0
input_data = data
flex.lexer.lineno = 1
error_count = 0
p = parser.parse(data, debug = 0, tracking = 1, tokenfunc = my_tokenfunc)
if error_count > 0:
print("ERRORS (%d)" % error_count)
if p is None: return p
try:
p["error_count"] = error_count
except Exception as e:
print(e)
return None
if parser.error:
return None
return p
def main():
global start
parser = OptionParser()
#parser.add_option("-f", "--file", dest="filename",
# help="write report to FILE", metavar="FILE")
parser.add_option("-O", "--output", dest="output", default = "none",
help="Set output TYPE: xml|hash", metavar="TYPE")
parser.add_option("--start", dest="start", default = None,
help="Set start block", metavar="STMT")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
parser.add_option("--optdebug",
action="store_true", dest="optdebug", default=False,
help="debug optparse module")
parser.add_option("--debug",
action="store_true", dest="debug", default=False,
help="prints lots of useless messages")
(options, args) = parser.parse_args()
if options.optdebug:
print(options, args)
if options.start:
start = options.start
print("Start setted to:" , start)
def do_it():
if options.output == "none": return
tree_data = calctree(prog)
if options.output == "hash":
printtree(tree_data, mode = "hash")
elif options.output == "xml":
printtree(tree_data, mode = "xml")
elif options.output == "file":
f1_hash = open(filename+".hash","w")
printtree(tree_data, mode = "hash", output = f1_hash)
f1_hash.close()
f1_xml = open(filename+".xml","w")
printtree(tree_data, mode = "xml", output = f1_xml)
f1_xml.close()
elif options.output == "yaml":
import yaml
print(yaml.dump(tree_data['content']))
else:
print("Unknown outputmode", options.output)
prog = "$$$"
if len(args) > 0 :
for filename in args:
fs = filename.split("/")
sys.stderr.write("Loading %s ..." % fs[-1])
sys.stderr.flush()
data = open(filename).read()
sys.stderr.write(" parsing ...")
sys.stderr.flush()
prog = parse(data)
sys.stderr.write(" formatting ...")
sys.stderr.flush()
if prog: do_it()
sys.stderr.write(" Done.\n")
sys.stderr.flush()
else:
line = ""
while 1:
try:
line1 = input("flscript> ")
if line1.startswith("#"):
comm = line1[1:].split(" ")
if comm[0] == "setstart":
start = comm[1]
print("Start setted to:" , start)
if comm[0] == "parse":
print()
prog = parse(line)
line = ""
else:
line += line1
except EOFError:
break;
line += "\n"
print()
prog = parse(line)
do_it()
"""
import yaml
print yaml.dump(tree_data)
"""
#print_tokentree(prog)
#for varName in prog.byDefName:
# var = prog.byDefName[varName]
# print "%-15s / %-15s > " % var.type , varName
#import tests.ifaceclass
#tests.ifaceclass.do_test(prog)
if __name__ == "__main__": main()