-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathparsingTest.js
More file actions
195 lines (180 loc) · 7.74 KB
/
parsingTest.js
File metadata and controls
195 lines (180 loc) · 7.74 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
let assert = require('assert');
let parserFacade = require('../../main-generated/javascript/ParserFacade.js');
function checkToken(tokens, index, typeName, column, text) {
it('should have ' + typeName + ' in position ' + index, function () {
assert.equal(tokens[index].type, CalcLexer[typeName]);
assert.equal(tokens[index].column, column);
assert.equal(tokens[index].text, text);
});
}
function checkError(actualError, expectedError) {
it('should have startLine ' + expectedError.startLine, function () {
assert.equal(actualError.startLine, expectedError.startLine);
});
it('should have endLine ' + expectedError.endLine, function () {
assert.equal(actualError.endLine, expectedError.endLine);
});
it('should have startCol ' + expectedError.startCol, function () {
assert.equal(actualError.startCol, expectedError.startCol);
});
it('should have endCol ' + expectedError.endCol, function () {
assert.equal(actualError.endCol, expectedError.endCol);
});
it('should have message ' + expectedError.message, function () {
assert.equal(actualError.message, expectedError.message);
});
}
function checkErrors(actualErrors, expectedErrors) {
it('should have ' + expectedErrors.length + ' error(s)', function (){
assert.equal(actualErrors.length, expectedErrors.length);
});
var i;
for (i = 0; i < expectedErrors.length; i++) {
checkError(actualErrors[i], expectedErrors[i]);
}
}
function parseAndCheckErrors(input, expectedErrors) {
let errors = parserFacade.validate(input);
checkErrors(errors, expectedErrors);
}
describe('Basic parsing of empty file', function () {
assert.equal(parserFacade.parseTreeStr(""), "(compilationUnit <EOF>)")
});
describe('Basic parsing of single input definition', function () {
assert.equal(parserFacade.parseTreeStr("input a\n"), "(compilationUnit (input input a (eol \\n)) <EOF>)")
});
describe('Basic parsing of single output definition', function () {
assert.equal(parserFacade.parseTreeStr("output a\n"), "(compilationUnit (output output a (eol \\n)) <EOF>)")
});
describe('Basic parsing of single calculation', function () {
assert.equal(parserFacade.parseTreeStr("a = b + 1\n"), "(compilationUnit (calc a = (expression (expression b) + (expression 1)) (eol \\n)) <EOF>)")
});
describe('Basic parsing of simple script', function () {
assert.equal(parserFacade.parseTreeStr("input i\no = i + 1\noutput o\n"), "(compilationUnit (input input i (eol \\n)) (calc o = (expression (expression i) + (expression 1)) (eol \\n)) (output output o (eol \\n)) <EOF>)")
});
describe('Validation of simple errors on single lines', function () {
describe('should have recognize missing operand', function () {
parseAndCheckErrors("o = 1 + \n", [
new parserFacade.Error(1, 1, 8, 9, "mismatched input '\\n' expecting {NUMBER_LIT, ID, '(', '-'}")
]);
});
describe('should have recognize extra operator', function () {
parseAndCheckErrors("o = 1 +* 2 \n", [
new parserFacade.Error(1, 1, 7, 8, "extraneous input '*' expecting {NUMBER_LIT, ID, '(', '-'}")
]);
});
});
describe('Validation of simple errors in small scripts', function () {
describe('should have recognize missing operand', function () {
let input = "input i\no = i + \noutput o\n";
parseAndCheckErrors(input, [
new parserFacade.Error(2, 2, 8, 9, "mismatched input '\\n' expecting {NUMBER_LIT, ID, '(', '-'}")
]);
});
describe('should have recognize extra operator', function () {
let input = "input i\no = i +* 2 \noutput o\n";
parseAndCheckErrors(input, [
new parserFacade.Error(2, 2, 7, 8, "extraneous input '*' expecting {NUMBER_LIT, ID, '(', '-'}")
]);
});
});
describe('Validation of examples being edited', function () {
describe('deleting number from division', function () {
let input = "input a\n" +
"b = a * 2\n" +
"c = (a - b) / \n" +
"output c\n";
parseAndCheckErrors(input, [
new parserFacade.Error(3, 3, 14, 15, "mismatched input '\\n' expecting {NUMBER_LIT, ID, '(', '-'}")
]);
});
describe('deleting number from multiplication', function () {
let input = "input a\n" +
"b = a * \n" +
"c = (a - b) / 3\n" +
"output c\n";
parseAndCheckErrors(input, [
new parserFacade.Error(2, 2, 8, 9, "mismatched input '\\n' expecting {NUMBER_LIT, ID, '(', '-'}")
]);
});
describe('adding plus to expression', function () {
let input = "input a\n" +
"b = a * 2 +\n" +
"c = (a - b) / 3\n" +
"output c\n";
parseAndCheckErrors(input, [
new parserFacade.Error(2, 2, 11, 12, "mismatched input '\\n' expecting {NUMBER_LIT, ID, '(', '-'}")
]);
});
});
describe('Unrecognized tokens cause errors', function () {
describe('extra dollor before identifier', function () {
let input = "input a\n" +
"$b = a * 2\n" +
"c = (a - b) / 3\n" +
"output c\n";
parseAndCheckErrors(input, [
new parserFacade.Error(2, 2, 0, 1, "extraneous input '$' expecting {<EOF>, 'input', 'output', ID}")
]);
});
});
describe('Semantic validation', function () {
describe('should report input already declared', function () {
let input = "input i\ninput i\n";
parseAndCheckErrors(input, [
new parserFacade.Error(2, 2, 7, 8, "input already declared")
]);
});
describe('should report input already declared twice', function () {
let input = "input i\ninput i\ninput i\n";
parseAndCheckErrors(input, [
new parserFacade.Error(2, 2, 7, 8, "input already declared"),
new parserFacade.Error(3, 3, 7, 8, "input already declared")
]);
});
describe('should report undeclared symbol', function () {
let input = "o = i\n";
parseAndCheckErrors(input, [
new parserFacade.Error(1, 1, 5, 6, "undeclared symbol")
]);
});
describe('should report undeclared symbol in line 4', function () {
let input = "input i\no = i\no = o\no = u\n";
parseAndCheckErrors(input, [
new parserFacade.Error(4, 4, 5, 6, "undeclared symbol")
]);
});
describe('should report undeclared symbol for output', function () {
let input = "output o\n";
parseAndCheckErrors(input, [
new parserFacade.Error(1, 1, 8, 9, "undeclared symbol")
]);
});
describe('should report undeclared symbol for second output', function () {
let input = "input i\no = i\noutput o\noutput x\n";
parseAndCheckErrors(input, [
new parserFacade.Error(4, 4, 8, 9, "undeclared symbol")
]);
});
});
describe('Semantic validation of examples being edited', function () {
describe('deleting input identifier', function () {
let input = "input a\ninput\ninput a\n";
parseAndCheckErrors(input, [
new parserFacade.Error(2, 2, 5, 6, "missing ID at '\\n'"),
new parserFacade.Error(3, 3, 7, 8, "input already declared")
]);
});
describe('deleting calculation value still declares the target', function () {
let input = "a = 1 + 1\nb = \nc = a + b\n";
parseAndCheckErrors(input, [
new parserFacade.Error(2, 2, 4, 5, "mismatched input '\\n' expecting {NUMBER_LIT, ID, '(', '-'}"),
]);
});
describe('deleting output identifier', function () {
let input = "input a\noutput\noutput a\n";
parseAndCheckErrors(input, [
new parserFacade.Error(2, 2, 6, 7, "missing ID at '\\n'"),
]);
});
});