-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_parser_tests.py
142 lines (119 loc) · 3.14 KB
/
js_parser_tests.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
import ply.lex as lex
import ply.yacc as yacc
import js_lexer
import js_parser
'''
test procedures (copied from tutorial): '''
def test_lexer(input_string):
lexer.input(input_string)
result = [ ]
while True:
tok = lexer.token()
if not tok: break
result = result + [tok.type, tok.value]
return result
def test_parser(input_string):
lexer.input(input_string)
parse_tree = parser.parse(input_string,lexer=lexer)
return parse_tree
lexer = lex.lex(module=js_lexer)
parser = yacc.yacc(module=js_parser)
'''
test_tokens = """ - ! && () * , / % ; { || } + < <= = == > >= else false function
if return true var """
print test_lexer(test_tokens)
comments = """
if // else mystery
=/*=*/=
true /* false
*/ return"""
print test_lexer(comments)
false_prophets = 'false_prophets "false_prophets true prophets"'
print test_lexer(false_prophets)
input1 = 'some_identifier -12.34 "a \\"escape\\" b"'
print test_lexer(input1)
input2 = '-12x34'
print test_lexer(input2)
js_simple_stmt = 'var president = trump;'
#print test_lexer(js_simple_stmt)
print test_parser(js_simple_stmt)
js_simple_func = "function myfun() { return nothing ; }"
#print test_lexer(js_simple_func)
print test_parser(js_simple_func)
js_func_args = "function nobletruths(dukkha,samudaya,nirodha,gamini) { return buddhism ; }"
print test_parser(js_func_args)
js_multi_top ="""var view = right;
var intention = right;
var speech = right;
action = right;
livelihood = right;
effort_right;
mindfulness_right;
concentration_right;"""
print test_parser(js_multi_top)
js_if_else = """
if cherry {
orchard;
if uncle_vanya {
anton ;
chekov ;
} else {
} ;
nineteen_oh_four ;
} ;
"""
print test_parser(js_if_else)
js_simple_exp = 'sum = 1 + 2;'
#print test_lexer(js_simple_exp)
print test_parser(js_simple_exp)
js_simple_exp = 'sum = 16 % 3;'
#print test_lexer(js_simple_exp)
print test_parser(js_simple_exp)
js_simple_declare = 'var string = "whatever";'
#print test_lexer(js_simple_declare)
print test_parser(js_simple_declare)
js_simple_associativity = '1 - 2 - 3;'
print test_parser(js_simple_associativity)
js_asso_precedence = '1 + 2 * 3 - 4 / 5 * (6 + 2);'
print test_parser(js_asso_precedence)
js_string_bool = '"hello" == "goodbye" || true && false;'
print test_parser(js_string_bool)
js_not = '! ! tricky || 3 < 5;'
print test_parser(js_not)
js_nested_calls = 'apply(1, 2 + eval(recursion), sqrt(2));'
print test_parser(js_nested_calls)
js_script = """function tricky(i) {
if (i <= 0) {
return i;
} ;
if ((i % 2) == 1) {
write("<b>");
write(i);
write("</b>");
} else {
write("<i>");
write(i);
write("</i>");
} ;
return tricky(i - 1);
}
tricky(10);"""
print test_lexer(js_script)
print test_parser(js_script)
'''
js_func_var = "var myfun = function() { return true ; }; write(myfun());"
print test_lexer(js_func_var)
print test_parser(js_func_var)
js_func_closure = """
function makeGreeting(salutation) {
var punc = "!";
var greeting = function(person) {
write(salutation);
write(person + punc);
};
return greeting;
}
var greet = makeGreeting("ciao");
greet("meg"); """
print test_lexer(js_func_closure)
print test_parser(js_func_closure)