|
1 | 1 | import {expect} from "chai";
|
2 | 2 |
|
3 |
| -import {Issue, Node} from "../src"; |
| 3 | +import {Issue, IssueSeverity, IssueType, Node, Point, Position} from "../src"; |
4 | 4 | import {SimpleLangLexer} from "./parser/SimpleLangLexer";
|
5 | 5 | import {CharStream, Lexer, TokenStream} from "antlr4ng";
|
6 | 6 | import {CompilationUnitContext, SimpleLangParser} from "./parser/SimpleLangParser";
|
@@ -36,4 +36,193 @@ describe('Parsing', function() {
|
36 | 36 | expect(result.root!.parseTree).to.equal(result.firstStage!.root);
|
37 | 37 | expect(result.code).to.equal(code);
|
38 | 38 | });
|
| 39 | + it("produce correct issues for: display 1 +", |
| 40 | + function () { |
| 41 | + const code = "display 1 +"; |
| 42 | + const parser = new SLParser(new ANTLRTokenFactory()); |
| 43 | + const result = parser.parse(code); |
| 44 | + expect(result.issues).to.eql([new Issue( |
| 45 | + IssueType.SYNTACTIC, |
| 46 | + "mismatched input '<EOF>' expecting {INT_LIT, DEC_LIT, STRING_LIT, BOOLEAN_LIT}", |
| 47 | + IssueSeverity.ERROR, |
| 48 | + new Position(new Point(1, 11), new Point(1, 11)), |
| 49 | + undefined, |
| 50 | + "parser.mismatchedinput", |
| 51 | + [ |
| 52 | + { |
| 53 | + name: "mismatched", |
| 54 | + value: "<EOF>" |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "expected", |
| 58 | + value: "INT_LIT" |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "expected", |
| 62 | + value: "DEC_LIT" |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "expected", |
| 66 | + value: "STRING_LIT" |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "expected", |
| 70 | + value: "BOOLEAN_LIT" |
| 71 | + } |
| 72 | + ] |
| 73 | + )]) |
| 74 | + }); |
| 75 | + it("produce correct issues for: display 1 ++", |
| 76 | + function () { |
| 77 | + const code = "display 1 ++"; |
| 78 | + const parser = new SLParser(new ANTLRTokenFactory()); |
| 79 | + const result = parser.parse(code); |
| 80 | + expect(result.issues).to.eql([ |
| 81 | + new Issue( |
| 82 | + IssueType.SYNTACTIC, |
| 83 | + "mismatched input '+' expecting {INT_LIT, DEC_LIT, STRING_LIT, BOOLEAN_LIT}", |
| 84 | + IssueSeverity.ERROR, |
| 85 | + new Position(new Point(1, 11), new Point(1, 11)), |
| 86 | + undefined, |
| 87 | + "parser.mismatchedinput", |
| 88 | + [ |
| 89 | + { |
| 90 | + name: "mismatched", |
| 91 | + value: "+" |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "expected", |
| 95 | + value: "INT_LIT" |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "expected", |
| 99 | + value: "DEC_LIT" |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "expected", |
| 103 | + value: "STRING_LIT" |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "expected", |
| 107 | + value: "BOOLEAN_LIT" |
| 108 | + } |
| 109 | + ] |
| 110 | + ), |
| 111 | + new Issue( |
| 112 | + IssueType.SYNTACTIC, |
| 113 | + "mismatched input '<EOF>' expecting {INT_LIT, DEC_LIT, STRING_LIT, BOOLEAN_LIT}", |
| 114 | + IssueSeverity.ERROR, |
| 115 | + new Position(new Point(1, 12), new Point(1, 12)), |
| 116 | + undefined, |
| 117 | + "parser.mismatchedinput", |
| 118 | + [ |
| 119 | + { |
| 120 | + name: "mismatched", |
| 121 | + value: "<EOF>" |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "expected", |
| 125 | + value: "INT_LIT" |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "expected", |
| 129 | + value: "DEC_LIT" |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "expected", |
| 133 | + value: "STRING_LIT" |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "expected", |
| 137 | + value: "BOOLEAN_LIT" |
| 138 | + } |
| 139 | + ] |
| 140 | + )]) |
| 141 | + }); |
| 142 | + it("produce correct issues for: display", |
| 143 | + function () { |
| 144 | + const code = "display"; |
| 145 | + const parser = new SLParser(new ANTLRTokenFactory()); |
| 146 | + const result = parser.parse(code); |
| 147 | + expect(result.issues).to.eql([ |
| 148 | + new Issue( |
| 149 | + IssueType.SYNTACTIC, |
| 150 | + "mismatched input '<EOF>' expecting {INT_LIT, DEC_LIT, STRING_LIT, BOOLEAN_LIT}", |
| 151 | + IssueSeverity.ERROR, |
| 152 | + new Position(new Point(1, 7), new Point(1, 7)), |
| 153 | + undefined, |
| 154 | + "parser.mismatchedinput", |
| 155 | + [ |
| 156 | + { |
| 157 | + name: "mismatched", |
| 158 | + value: "<EOF>" |
| 159 | + }, |
| 160 | + { |
| 161 | + name: "expected", |
| 162 | + value: "INT_LIT" |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "expected", |
| 166 | + value: "DEC_LIT" |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "expected", |
| 170 | + value: "STRING_LIT" |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "expected", |
| 174 | + value: "BOOLEAN_LIT" |
| 175 | + } |
| 176 | + ] |
| 177 | + )]) |
| 178 | + }); |
| 179 | + it("produce correct issues for: ###", |
| 180 | + function () { |
| 181 | + const code = "###"; |
| 182 | + const parser = new SLParser(new ANTLRTokenFactory()); |
| 183 | + const result = parser.parse(code); |
| 184 | + expect(result.issues).to.eql([ |
| 185 | + new Issue( |
| 186 | + IssueType.LEXICAL, |
| 187 | + "token recognition error at: '#'", |
| 188 | + IssueSeverity.ERROR, |
| 189 | + new Position(new Point(1, 0), new Point(1, 0)), |
| 190 | + undefined, |
| 191 | + "lexer.tokenrecognitionerror", |
| 192 | + [ |
| 193 | + { |
| 194 | + name: "token", |
| 195 | + value: "#" |
| 196 | + } |
| 197 | + ] |
| 198 | + ), |
| 199 | + new Issue( |
| 200 | + IssueType.LEXICAL, |
| 201 | + "token recognition error at: '#'", |
| 202 | + IssueSeverity.ERROR, |
| 203 | + new Position(new Point(1, 1), new Point(1, 1)), |
| 204 | + undefined, |
| 205 | + "lexer.tokenrecognitionerror", |
| 206 | + [ |
| 207 | + { |
| 208 | + name: "token", |
| 209 | + value: "#" |
| 210 | + } |
| 211 | + ] |
| 212 | + ), |
| 213 | + new Issue( |
| 214 | + IssueType.LEXICAL, |
| 215 | + "token recognition error at: '#'", |
| 216 | + IssueSeverity.ERROR, |
| 217 | + new Position(new Point(1, 2), new Point(1, 2)), |
| 218 | + undefined, |
| 219 | + "lexer.tokenrecognitionerror", |
| 220 | + [ |
| 221 | + { |
| 222 | + name: "token", |
| 223 | + value: "#" |
| 224 | + } |
| 225 | + ] |
| 226 | + )]) |
| 227 | + }) |
39 | 228 | });
|
0 commit comments