-
Notifications
You must be signed in to change notification settings - Fork 11
/
vbs4.jison
192 lines (156 loc) · 4.09 KB
/
vbs4.jison
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
/* VBScript */
%lex
%%
\n+ { return 'NEWLINE'; }
[ \t]+ { /* skip whitespace */ }
"Call" { return 'CALL'; }
"call" { return 'CALL'; } /* how do we do case insensitive? */
"Function" { return 'FUNCTION'; }
"Sub" { return 'SUB'; }
"If" { return 'IF'; }
"Then" { return 'THEN'; }
"Else" { return 'ELSE'; }
"Is" { return 'IS'; }
"End" { return 'END'; }
"Set" { return 'SET'; }
"MOD" { return yytext; }
"Nothing" { yytext = 'null'; return 'IDENTIFIER'; }
L?\"(\\.|[^\\"])*\" { return 'STRING'; } /* taken from ansi C yacc file */
"()" { return 'EMPTYBRACKETS'; }
[<>\(\)=,\.] { return yytext; }
\w+ { return 'IDENTIFIER'; }
<<EOF>> { return 'EOF'; }
/lex
%left '.'
%left '^'
%left '*' '/'
%left '\\\\'
%left MOD
%left '+' '-'
%left '&'
%left COMPARISON '<>' '<' '>' '<=' '>=' IS
%left NOT
%left AND
%left OR
%left XOR
%left EQV
%left IMP
%left '(' ')' ','
%left '='
%left TO IN STEP
%left ELSEIF ELSE
%left ':'
%left NEWLINE fake_newline
%start program
%%
/* does this mean a program must end with a newline? */
program
: statement_list EOF { return { type:'Program', body: $1 }; }
;
statement_list
: statement_line
{ $$ = [$statement_line]; }
| statement_list statement_line
{
if ($statement_list['push']) {
$statement_list.push($statement_line);
$$ = $statement_list;
} else {
console.log("So... statement_list isn't cool... let's see what it is");
console.log($statement_list);
//console.log($statement_line);
var tmp = [$statement_list];
tmp.push($statement_line);
$$ = tmp;
}
//$$ = $1.concat($3);
}
;
statement_line
: statement NEWLINE
;
statement
: STRING
| function
| if_statement
| if_else_statement
| assignment
| call_statement
;
function
: function_or_sub member_access arguments NEWLINE statement_list END function_or_sub
{ $$ = { type: 'Function', sub_type: $function_or_sub, name: $member_access, arguments: $arguments, body: $statement_list }; }
;
function_or_sub
: FUNCTION
| SUB
;
if_statement
: IF conditional THEN NEWLINE statement_list END IF
{ $$ = { type: 'If', condition: $conditional, body: $statement_list}; }
;
if_else_statement
: IF conditional THEN NEWLINE statement_list ELSE NEWLINE statement_list END IF
{ $$ = { type: 'If', condition: $conditional, body: $statement_list1, else_body: $statement_list2}; }
;
conditional
: comparable compare comparable
{ $$ = { type: 'Conditional', first: $comparable1, second: $comparable2, compare: $compare}; }
;
comparable
: member_access
{ $$ = { type: 'Call', blah: "fooo", name: $member_access, arguments: null}; }
| call_statement
| STRING
{ $$ = { type: 'String', value: $STRING }; }
;
compare
: '>'
| '<'
| '>='
| '<='
| '='
| 'IS'
{ $$ = '=='; }
;
call_statement
: member_access arguments
{ $$ = { type: 'Call', name: $member_access, arguments: $arguments }; }
| CALL member_access arguments
{ $$ = { type: 'Call', bare_call: true, name: $member_access, arguments: $arguments }; }
;
member_access
: member_access '.' IDENTIFIER
{ $$ = $member_access + "." + $IDENTIFIER; }
| IDENTIFIER
;
member_access_or_string
: member_access
| STRING
;
assignment
: SET member_access '=' member_access_or_string
{ $$ = { type: 'Assignment', name: $member_access, value: $member_access_or_string }; }
| member_access '=' member_access_or_string
{ $$ = { type: 'Assignment', name: $member_access, value: $member_access_or_string }; }
;
arguments
: EMPTYBRACKETS
| '(' argument_list ')'
{ $$ = $2; }
;
argument_list
: /* can be empty */
{ $$ = []; }
| argument
{ $$ = [$1]; }
| argument_list ',' argument
{ $$ = $1.concat($3); }
;
/* optional args? */
argument
: IDENTIFIER
{ $$ = {type: 'Argument', value: $1 }; }
| STRING
{ $$ = {type: 'Argument', value: $1 }; }
;