-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserSyntaxTests.cs
More file actions
327 lines (294 loc) · 12.5 KB
/
Copy pathParserSyntaxTests.cs
File metadata and controls
327 lines (294 loc) · 12.5 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
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
using AuroraScript.Compiler.Analyzer;
using AuroraScript.Compiler.Ast;
using AuroraScript.Compiler.Ast.Expressions;
using AuroraScript.Source;
using AuroraScript.Tests.Infrastructure;
using System;
using System.IO;
using Xunit;
namespace AuroraScript.Tests;
public sealed class ParserSyntaxTests
{
[Theory]
[InlineData("var value = 1; const fixedValue = 2;")]
[InlineData("func add(a, b = 2) { return a + b; } function empty() { return; }")]
[InlineData("enum Color { Red, Green = 4, Blue }")]
[InlineData("if (true) { var x = 1; } else if (false) { var x = 2; } else { var x = 3; }")]
[InlineData("for (var i = 0; i < 3; i++) { if (i == 1) continue; }")]
[InlineData("for (var item in [1, 2, 3]) { if (item == 2) break; }")]
[InlineData("while (false) { break; }")]
[InlineData("try { throw new Error('failure'); } catch (error) { var message = error.message; } finally { var done = true; }")]
[InlineData("try { var value = 1; }")]
[InlineData("var array = [1, , 2, ...[3, 4]]; var map = { a: 1, b: 2, key, ...{ c: 3 } };")]
[InlineData("var { a, b } = { a: 1, b: 2 }; var [first, ...rest, last] = [1, 2, 3];")]
[InlineData("var expression = (a, b) => a + b; var block = () => { return 1; };")]
[InlineData("func mutate(obj) { delete obj.value; debugger; }")]
[InlineData("func values() { return `value=${1 + 2}`; }")]
[InlineData("func values() { return `outer=${`inner=${1 + 2}`}`; }")]
[InlineData("func values() { return `literal=\\${value}`; }")]
[InlineData("func values() { return `object=${{ value: 1 }.value}`; }")]
[InlineData("func regex() { return /a[\\/]b+/gi; }")]
public void ParsesSupportedGrammarBranches(string body)
{
var module = Parse("@module(TEST);\n" + body);
Assert.Equal("TEST", module.ModuleName);
}
[Fact]
public void ParsesImportIncludeAndExportDeclarations()
{
using var workspace = new TestWorkspace();
workspace.WriteSource("dependency.as", "@module(DEPENDENCY); export var value = 1;");
workspace.WriteSource("included.as", "export const INCLUDED = 2;");
var source = "@module(TEST);\n" +
"import dependency from 'dependency';\n" +
"include 'included';\n" +
"export var value = dependency.value;\n" +
"export func run() { return value + INCLUDED; }\n" +
"export enum Mode { One, Two }\n";
var module = Parse(source, workspace.Root);
Assert.Equal(2, module.Imports.Count);
Assert.Contains(module.Imports, import => import.Include);
Assert.Contains(module.Imports, import => !import.Include);
}
[Fact]
public void AllowsMultipleImportsAndIncludesAtModuleTop()
{
using var workspace = new TestWorkspace();
workspace.WriteSource("first.as", "@module(FIRST);");
workspace.WriteSource("second.as", "@module(SECOND);");
workspace.WriteSource("included.as", "export const VALUE = 1;");
var module = Parse(
"""
@module(TEST);
import first from 'first';
include 'included';
import second from 'second';
export func run() { return VALUE; }
""",
workspace.Root);
Assert.Equal(3, module.Imports.Count);
Assert.Empty(module.Statements);
Assert.Single(module.Functions);
}
[Theory]
[InlineData("var value = ;")]
[InlineData("if (true) {")]
[InlineData("enum Broken { Value = 'text' }")]
[InlineData("declare var value = 1;")]
[InlineData("declare const value = 1;")]
[InlineData("declare var { value };")]
[InlineData("export if (true) { }")]
[InlineData("func broken( { }")]
[InlineData("var { key } =;")]
[InlineData("var [first] =;")]
[InlineData("var value = 1 +;")]
[InlineData("var value = array[];")]
[InlineData("func call() { target(,); }")]
[InlineData("func value() { return `missing=${1 + 2`; }")]
[InlineData("if () { var value = 1; }")]
[InlineData("while () { break; }")]
[InlineData("for (var i = 0; ; ) ;")]
[InlineData("throw;")]
[InlineData("delete;")]
[InlineData("var value = { key: };")]
[InlineData("var value = [...];")]
[InlineData("func duplicate(value, value) { }")]
[InlineData("func value() { return `empty=${}`; }")]
[InlineData("func value() { return `extra=${1 2}`; }")]
[InlineData("1 = value;")]
[InlineData("(a + b) = 1;")]
[InlineData("func f() { } f() = 1;")]
[InlineData("a + b += 1;")]
[InlineData("++1;")]
[InlineData("target(1,,2);")]
[InlineData("func f(a = ) { }")]
[InlineData("func f(...rest, next) { }")]
[InlineData("func f(...rest = 1) { }")]
[InlineData("var value = {,};")]
[InlineData("var value = { a: 1,, b: 2 };")]
[InlineData("var value = { ... };")]
[InlineData("if (true)")]
[InlineData("else { var value = 1; }")]
[InlineData("while (true)")]
[InlineData("try { } catch")]
[InlineData("try { } finally")]
[InlineData("try { } catch () { }")]
[InlineData("try { } catch (123) { }")]
[InlineData("for (var i = 0 i < 3; i++) { }")]
[InlineData("for (var item in) { }")]
[InlineData("for (var item in [1, 2])")]
[InlineData("enum E { A,,B }")]
[InlineData("enum E { A = 1.5 }")]
[InlineData("enum E { A = 2147483648 }")]
public void RejectsInvalidSyntax(string body)
{
var exception = Record.Exception(() => Parse("@module(TEST);\n" + body));
Assert.NotNull(exception);
Assert.IsAssignableFrom<AuroraException>(exception);
}
[Fact]
public void RejectsImportAfterExecutableStatement()
{
using var workspace = new TestWorkspace();
workspace.WriteSource("dependency.as", "@module(DEPENDENCY);");
var exception = Record.Exception(() => Parse(
"@module(TEST); var value = 1; import dependency from 'dependency';",
workspace.Root));
var parse = Assert.IsType<AuroraCompilationException>(exception);
Assert.Contains("top of the module", parse.Message, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void ParsesGlobalDeclarationFiles()
{
var module = Parse(
"""
// comments may precede the header
@global();
declare var HOST_VALUE;
declare const HOST_CONST;
declare func HOST_ADD(left, right);
""");
Assert.True(module.IsGlobalDeclarationFile);
Assert.Null(module.ModuleName);
Assert.Equal(2, module.Statements.Count);
Assert.Single(module.Functions);
var variable = Assert.IsType<VariableDeclaration>(module.Statements[0]);
Assert.Equal("HOST_VALUE", variable.Name.Value);
Assert.True(variable.IsDeclare);
Assert.False(variable.IsConst);
Assert.Equal(MemberAccess.Internal, variable.Access);
Assert.Null(variable.Initializer);
Assert.Null(variable.Pattern);
var constant = Assert.IsType<VariableDeclaration>(module.Statements[1]);
Assert.Equal("HOST_CONST", constant.Name.Value);
Assert.True(constant.IsDeclare);
Assert.True(constant.IsConst);
var function = Assert.Single(module.Functions);
Assert.Equal("HOST_ADD", function.Name.Value);
Assert.Equal(FunctionFlags.Declare, function.Flags);
}
[Theory]
[InlineData("@module(TEST);\ndeclare var HOST_VALUE;", "declare is only allowed")]
[InlineData("@module(TEST);\nexport declare var HOST_VALUE;", "export declare is not supported")]
[InlineData("@global();\nexport declare var HOST_VALUE;", "export declare is not supported")]
[InlineData("@global();\nvar value = 1;", "only allow declare")]
[InlineData("@global();\n@module(TEST);", "cannot also declare @module")]
[InlineData("@module(TEST);\n@global();", "must be the first")]
public void RejectsInvalidGlobalDeclarationUsage(string source, string message)
{
var parse = Assert.Throws<AuroraCompilationException>(() => Parse(source));
Assert.Contains(message, parse.Message, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void RejectsIncludeAfterExecutableStatement()
{
using var workspace = new TestWorkspace();
workspace.WriteSource("dependency.as", "@module(DEPENDENCY);");
var exception = Record.Exception(() => Parse(
"@module(TEST); var value = 1; include 'dependency';",
workspace.Root));
var parse = Assert.IsType<AuroraCompilationException>(exception);
Assert.Contains("top of the module", parse.Message, StringComparison.OrdinalIgnoreCase);
}
[Theory]
[InlineData("@module(TEST); import from 'dependency';")]
[InlineData("@module(TEST); import dependency 'dependency';")]
[InlineData("@module(TEST); include dependency;")]
public void RejectsMalformedImportOrInclude(string source)
{
using var workspace = new TestWorkspace();
workspace.WriteSource("dependency.as", "@module(DEPENDENCY);");
var exception = Record.Exception(() => Parse(source, workspace.Root));
Assert.NotNull(exception);
Assert.IsAssignableFrom<AuroraException>(exception);
}
[Fact]
public void ParserKeepsMissingImportAsRawPath()
{
using var workspace = new TestWorkspace();
var module = Parse(
"@module(TEST); import missing from 'does-not-exist';",
workspace.Root);
var import = Assert.Single(module.Imports);
Assert.Equal("does-not-exist", import.File.Value);
Assert.Null(import.FullPath);
}
[Theory]
[InlineData("@module();")]
[InlineData("@module(TEST)")]
[InlineData("@module(TEST, EXTRA);")]
[InlineData("@;")]
[InlineData("@module('TEST');")]
public void RejectsMalformedModuleMetadata(string source)
{
var error = Record.Exception(() => Parse(source));
Assert.NotNull(error);
Assert.IsAssignableFrom<AuroraException>(error);
}
[Fact]
public void ParsesNonModuleMetadata()
{
var module = Parse("@module(TEST);\n@author(TEST);");
Assert.Equal("TEST", module.MetaInfos["author"]);
Assert.Equal("TEST", module.ModuleName);
}
[Theory]
[InlineData("@author(TEST);\n@module(TEST);")]
[InlineData("var value = 1;\n@module(TEST);")]
[InlineData(";\n@module(TEST);")]
[InlineData("@directCall()\nfunc run() { return 1; }\n@module(TEST);")]
public void RejectsModuleMetadataAfterOtherSyntax(string source)
{
var error = Record.Exception(() => Parse(source));
Assert.NotNull(error);
Assert.IsAssignableFrom<AuroraException>(error);
}
[Fact]
public void ParsesFunctionAnnotations()
{
var module = Parse(
"""
@module(TEST);
@directCall
export func run() { return 42; }
""");
var function = Assert.Single(module.Functions);
Assert.Equal("run", function.Name.Value);
var annotation = Assert.Single(function.Annotations);
Assert.Equal("directCall", annotation.Name.Value);
Assert.Empty(annotation.Arguments);
}
[Fact]
public void ParsesDirectCallAnnotationArgument()
{
var module = Parse(
"""
@module(TEST);
@directCall(false)
func run() { return 42; }
""");
var function = Assert.Single(module.Functions);
var annotation = Assert.Single(function.Annotations);
Assert.Equal("directCall", annotation.Name.Value);
var argument = Assert.IsType<AuroraScript.Tokens.BooleanToken>(annotation.Arguments[0]);
Assert.False(argument.BoolValue);
}
[Fact]
public void DiagnosticReportsVirtualSourceCoordinates()
{
var root = Path.GetTempPath();
var error = Assert.Throws<AuroraCompilationException>(() => Parse(
"@module(TEST);\nfunc run() {\n return 1 +;\n}",
root));
Assert.Contains("parser-test.as", error.FileName, StringComparison.OrdinalIgnoreCase);
Assert.Equal(3, error.LineNumber);
Assert.True(error.ColumnNumber > 0);
}
private static ModuleDeclaration Parse(string source, string? root = null)
{
root ??= Path.GetTempPath();
using var lexer = new AuroraLexer(root, new MemorySource(root, Path.Combine(root, "parser-test.as"), source));
var parser = new AuroraParser(lexer, EngineOptions.Default.WithCompiler(compiler => compiler.SourceResolver = AuroraScript.Core.ScriptSources.FileSystem(root)));
return parser.Parse();
}
}