Skip to content

Commit d162283

Browse files
WIP
1 parent b6bda05 commit d162283

9 files changed

Lines changed: 366 additions & 32 deletions

File tree

‎src/parser.ts‎

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,8 @@ export class Parser extends DiagnosticEmitter {
991991
} while (tn.skip(Token.Comma));
992992

993993
let ret = Node.createVariableStatement(decorators, declarations, tn.range(startPos, tn.pos));
994-
if (!tn.skip(Token.Semicolon) && !isFor) this.checkASI(tn);
994+
if (isFor) tn.skip(Token.Semicolon);
995+
else this.parseSemicolon(tn);
995996
return ret;
996997
}
997998

@@ -1019,7 +1020,8 @@ export class Parser extends DiagnosticEmitter {
10191020
);
10201021
}
10211022
let flags = parentFlags;
1022-
if (tn.skip(Token.Exclamation)) {
1023+
if (tn.peek() == Token.Exclamation && !tn.peekOnNewLine()) {
1024+
tn.next();
10231025
flags |= CommonFlags.DefinitelyAssigned;
10241026
}
10251027

@@ -1175,7 +1177,7 @@ export class Parser extends DiagnosticEmitter {
11751177
}
11761178

11771179
let ret = Node.createReturnStatement(expr, tn.range(startPos, tn.pos));
1178-
if (!tn.skip(Token.Semicolon)) this.checkASI(tn);
1180+
this.parseSemicolon(tn);
11791181
return ret;
11801182
}
11811183

@@ -1601,7 +1603,8 @@ export class Parser extends DiagnosticEmitter {
16011603
tn.range(startPos, tn.pos)
16021604
);
16031605
ret.overriddenModuleName = this.currentModuleName;
1604-
tn.skip(Token.Semicolon);
1606+
if (body) tn.skip(Token.Semicolon);
1607+
else this.parseSemicolon(tn);
16051608
return ret;
16061609
}
16071610

@@ -1669,6 +1672,13 @@ export class Parser extends DiagnosticEmitter {
16691672
}
16701673

16711674
if (arrowKind) {
1675+
if (tn.peek() == Token.Equals_GreaterThan && tn.peekOnNewLine()) {
1676+
this.error(
1677+
DiagnosticCode.Unexpected_token,
1678+
tn.range(tn.nextTokenPos)
1679+
);
1680+
return null;
1681+
}
16721682
if (!tn.skip(Token.Equals_GreaterThan)) {
16731683
this.error(
16741684
DiagnosticCode._0_expected,
@@ -2203,7 +2213,7 @@ export class Parser extends DiagnosticEmitter {
22032213
}
22042214
return null;
22052215
}
2206-
tn.skip(Token.Semicolon);
2216+
this.parseSemicolon(tn);
22072217
return retIndex;
22082218
}
22092219
if (!tn.skipIdentifier(IdentifierHandling.Always)) {
@@ -2367,8 +2377,10 @@ export class Parser extends DiagnosticEmitter {
23672377
body,
23682378
tn.range(startPos, tn.pos)
23692379
);
2370-
if (!(isInterface && tn.skip(Token.Comma))) {
2371-
tn.skip(Token.Semicolon);
2380+
let hasComma = isInterface && tn.skip(Token.Comma);
2381+
if (!hasComma) {
2382+
if (body) tn.skip(Token.Semicolon);
2383+
else this.parseSemicolon(tn);
23722384
}
23732385
return retMethod;
23742386

@@ -2421,7 +2433,8 @@ export class Parser extends DiagnosticEmitter {
24212433
tn.range(startPos, tn.pos)
24222434
);
24232435
}
2424-
if (tn.skip(Token.Exclamation)) {
2436+
if (tn.peek() == Token.Exclamation && !tn.peekOnNewLine()) {
2437+
tn.next();
24252438
flags |= CommonFlags.DefinitelyAssigned;
24262439
}
24272440
if (tn.skip(Token.Colon)) {
@@ -2465,9 +2478,7 @@ export class Parser extends DiagnosticEmitter {
24652478
initializer,
24662479
range
24672480
);
2468-
if (!(isInterface && tn.skip(Token.Comma))) {
2469-
tn.skip(Token.Semicolon);
2470-
}
2481+
if (!(isInterface && tn.skip(Token.Comma))) this.parseSemicolon(tn);
24712482
return retField;
24722483
}
24732484
return null;
@@ -2656,7 +2667,7 @@ export class Parser extends DiagnosticEmitter {
26562667
this.seenlog.add(internalPath);
26572668
}
26582669
}
2659-
tn.skip(Token.Semicolon);
2670+
this.parseSemicolon(tn);
26602671
return ret;
26612672
} else if (tn.skip(Token.Asterisk)) {
26622673
if (tn.skip(Token.From)) {
@@ -2672,7 +2683,7 @@ export class Parser extends DiagnosticEmitter {
26722683
this.dependees.set(internalPath, new Dependee(currentSource, path));
26732684
this.backlog.push(internalPath);
26742685
}
2675-
tn.skip(Token.Semicolon);
2686+
this.parseSemicolon(tn);
26762687
return ret;
26772688
} else {
26782689
this.error(
@@ -2754,7 +2765,7 @@ export class Parser extends DiagnosticEmitter {
27542765
range
27552766
)
27562767
], null, false, tn.range(startPos, tn.pos));
2757-
tn.skip(Token.Semicolon);
2768+
this.parseSemicolon(tn);
27582769
return ret;
27592770
}
27602771

@@ -2844,7 +2855,7 @@ export class Parser extends DiagnosticEmitter {
28442855
this.dependees.set(internalPath, new Dependee(assert(this.currentSource), path));
28452856
this.backlog.push(internalPath);
28462857
}
2847-
tn.skip(Token.Semicolon);
2858+
this.parseSemicolon(tn);
28482859
return ret;
28492860
} else {
28502861
this.error(
@@ -2915,7 +2926,7 @@ export class Parser extends DiagnosticEmitter {
29152926
if (tn.skipIdentifier()) {
29162927
let identifier = Node.createIdentifierExpression(tn.readIdentifier(), tn.range());
29172928
let ret = Node.createExportImportStatement(identifier, asIdentifier, tn.range(startPos, tn.pos));
2918-
tn.skip(Token.Semicolon);
2929+
this.parseSemicolon(tn);
29192930
return ret;
29202931
} else {
29212932
this.error(
@@ -3045,7 +3056,7 @@ export class Parser extends DiagnosticEmitter {
30453056
topLevel: bool
30463057
): BlockStatement | null {
30473058

3048-
// at '{': Statement* '}' ';'?
3059+
// at '{': Statement* '}'
30493060

30503061
let startPos = tn.tokenPos;
30513062
let statements = new Array<Statement>();
@@ -3062,7 +3073,6 @@ export class Parser extends DiagnosticEmitter {
30623073
}
30633074
}
30643075
let ret = Node.createBlockStatement(statements, tn.range(startPos, tn.pos));
3065-
if (topLevel) tn.skip(Token.Semicolon);
30663076
return ret;
30673077
}
30683078

@@ -3078,7 +3088,7 @@ export class Parser extends DiagnosticEmitter {
30783088
identifier = Node.createIdentifierExpression(tn.readIdentifier(), tn.range());
30793089
}
30803090
let ret = Node.createBreakStatement(identifier, tn.range());
3081-
tn.skip(Token.Semicolon);
3091+
this.parseSemicolon(tn);
30823092
return ret;
30833093
}
30843094

@@ -3094,7 +3104,7 @@ export class Parser extends DiagnosticEmitter {
30943104
identifier = Node.createIdentifierExpression(tn.readIdentifier(), tn.range());
30953105
}
30963106
let ret = Node.createContinueStatement(identifier, tn.range());
3097-
tn.skip(Token.Semicolon);
3107+
this.parseSemicolon(tn);
30983108
return ret;
30993109
}
31003110

@@ -3140,7 +3150,8 @@ export class Parser extends DiagnosticEmitter {
31403150
}
31413151

31423152
parseExpressionStatement(
3143-
tn: Tokenizer
3153+
tn: Tokenizer,
3154+
isFor: bool = false
31443155
): ExpressionStatement | null {
31453156

31463157
// at previous token
@@ -3149,7 +3160,8 @@ export class Parser extends DiagnosticEmitter {
31493160
if (!expr) return null;
31503161

31513162
let ret = Node.createExpressionStatement(expr);
3152-
tn.skip(Token.Semicolon);
3163+
if (isFor) tn.skip(Token.Semicolon);
3164+
else this.parseSemicolon(tn);
31533165
return ret;
31543166
}
31553167

@@ -3172,7 +3184,7 @@ export class Parser extends DiagnosticEmitter {
31723184
initializer = this.parseVariable(tn, CommonFlags.None, null, tn.tokenPos, true);
31733185

31743186
} else if (!tn.skip(Token.Semicolon)) {
3175-
initializer = this.parseExpressionStatement(tn);
3187+
initializer = this.parseExpressionStatement(tn, true);
31763188
if (!initializer) return null;
31773189
}
31783190

@@ -3234,7 +3246,7 @@ export class Parser extends DiagnosticEmitter {
32343246
if (tn.token == Token.Semicolon) {
32353247
let condition: ExpressionStatement | null = null;
32363248
if (!tn.skip(Token.Semicolon)) {
3237-
condition = this.parseExpressionStatement(tn);
3249+
condition = this.parseExpressionStatement(tn, true);
32383250
if (!condition) return null;
32393251
}
32403252

@@ -3375,7 +3387,6 @@ export class Parser extends DiagnosticEmitter {
33753387
switchCases.push(switchCase);
33763388
}
33773389
let ret = Node.createSwitchStatement(condition, switchCases, tn.range(startPos, tn.pos));
3378-
tn.skip(Token.Semicolon);
33793390
return ret;
33803391
} else {
33813392
this.error(
@@ -3467,10 +3478,17 @@ export class Parser extends DiagnosticEmitter {
34673478
// at 'throw': Expression ';'?
34683479

34693480
let startPos = tn.tokenPos;
3481+
if (tn.peekOnNewLine()) {
3482+
this.error(
3483+
DiagnosticCode.Unexpected_token,
3484+
tn.range(tn.nextTokenPos)
3485+
);
3486+
return null;
3487+
}
34703488
let expression = this.parseExpression(tn);
34713489
if (!expression) return null;
34723490
let ret = Node.createThrowStatement(expression, tn.range(startPos, tn.pos));
3473-
if (!tn.skip(Token.Semicolon)) this.checkASI(tn);
3491+
this.parseSemicolon(tn);
34743492
return ret;
34753493
}
34763494

@@ -3561,7 +3579,6 @@ export class Parser extends DiagnosticEmitter {
35613579
finallyStatements,
35623580
tn.range(startPos, tn.pos)
35633581
);
3564-
tn.skip(Token.Semicolon);
35653582
return ret;
35663583
} else {
35673584
this.error(
@@ -3608,7 +3625,7 @@ export class Parser extends DiagnosticEmitter {
36083625
type,
36093626
tn.range(startPos, tn.pos)
36103627
);
3611-
tn.skip(Token.Semicolon);
3628+
this.parseSemicolon(tn);
36123629
ret.overriddenModuleName = this.currentModuleName;
36133630
return ret;
36143631
} else {
@@ -3638,7 +3655,7 @@ export class Parser extends DiagnosticEmitter {
36383655
let moduleName = tn.readString();
36393656
let ret = Node.createModuleDeclaration(moduleName, flags, tn.range(startPos, tn.pos));
36403657
this.currentModuleName = moduleName;
3641-
tn.skip(Token.Semicolon);
3658+
this.parseSemicolon(tn);
36423659
return ret;
36433660
}
36443661

@@ -3652,7 +3669,7 @@ export class Parser extends DiagnosticEmitter {
36523669
let expression = this.parseExpression(tn, Precedence.Grouping);
36533670
if (!expression) return null;
36543671
let ret = Node.createVoidStatement(expression, tn.range(startPos, tn.pos));
3655-
tn.skip(Token.Semicolon);
3672+
this.parseSemicolon(tn);
36563673
return ret;
36573674
}
36583675

@@ -3670,7 +3687,6 @@ export class Parser extends DiagnosticEmitter {
36703687
let statement = this.parseStatement(tn);
36713688
if (!statement) return null;
36723689
let ret = Node.createWhileStatement(expression, statement, tn.range(startPos, tn.pos));
3673-
tn.skip(Token.Semicolon);
36743690
return ret;
36753691
} else {
36763692
this.error(
@@ -4131,6 +4147,15 @@ export class Parser extends DiagnosticEmitter {
41314147
while (
41324148
(nextPrecedence = determinePrecedence(tn.peek())) >= precedence
41334149
) {
4150+
let nextToken = tn.peek();
4151+
if (
4152+
(
4153+
nextToken == Token.Plus_Plus ||
4154+
nextToken == Token.Minus_Minus ||
4155+
nextToken == Token.As ||
4156+
nextToken == Token.Exclamation
4157+
) && tn.peekOnNewLine()
4158+
) break;
41344159
let token = tn.next();
41354160
switch (token) {
41364161

@@ -4430,6 +4455,12 @@ export class Parser extends DiagnosticEmitter {
44304455
);
44314456
}
44324457

4458+
private parseSemicolon(
4459+
tn: Tokenizer
4460+
): void {
4461+
if (!tn.skip(Token.Semicolon)) this.checkASI(tn);
4462+
}
4463+
44334464
/** Skips over a statement on errors in an attempt to reduce unnecessary diagnostic noise. */
44344465
skipStatement(tn: Tokenizer): void {
44354466
if (tn.peekOnNewLine()) tn.next(); // if reset() to the previous line

‎tests/compiler/asi.debug.wat‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(module
2+
(type $0 (func))
3+
(type $1 (func (param i32 i32 i32 i32)))
4+
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
5+
(global $asi/i (mut i32) (i32.const 0))
6+
(global $asi/j (mut i32) (i32.const 7))
7+
(global $asi/k (mut i32) (i32.const 1))
8+
(global $~lib/memory/__data_end i32 (i32.const 44))
9+
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 32812))
10+
(global $~lib/memory/__heap_base i32 (i32.const 32812))
11+
(memory $0 1)
12+
(data $0 (i32.const 12) "\1c\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\0c\00\00\00a\00s\00i\00.\00t\00s\00")
13+
(table $0 1 1 funcref)
14+
(elem $0 (i32.const 1))
15+
(export "memory" (memory $0))
16+
(start $~start)
17+
(func $start:asi
18+
global.get $asi/j
19+
global.set $asi/i
20+
global.get $asi/k
21+
i32.const 1
22+
i32.add
23+
global.set $asi/k
24+
global.get $asi/i
25+
i32.const 7
26+
i32.eq
27+
i32.eqz
28+
if
29+
i32.const 0
30+
i32.const 32
31+
i32.const 8
32+
i32.const 1
33+
call $~lib/builtins/abort
34+
unreachable
35+
end
36+
global.get $asi/k
37+
i32.const 2
38+
i32.eq
39+
i32.eqz
40+
if
41+
i32.const 0
42+
i32.const 32
43+
i32.const 9
44+
i32.const 1
45+
call $~lib/builtins/abort
46+
unreachable
47+
end
48+
)
49+
(func $~start
50+
call $start:asi
51+
)
52+
)

‎tests/compiler/asi.release.wat‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(module
2+
(type $0 (func))
3+
(type $1 (func (param i32 i32 i32 i32)))
4+
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
5+
(global $asi/k (mut i32) (i32.const 1))
6+
(memory $0 1)
7+
(data $0 (i32.const 1036) "\1c")
8+
(data $0.1 (i32.const 1048) "\02\00\00\00\0c\00\00\00a\00s\00i\00.\00t\00s")
9+
(export "memory" (memory $0))
10+
(start $~start)
11+
(func $~start
12+
global.get $asi/k
13+
i32.const 1
14+
i32.add
15+
global.set $asi/k
16+
global.get $asi/k
17+
i32.const 2
18+
i32.ne
19+
if
20+
i32.const 0
21+
i32.const 1056
22+
i32.const 9
23+
i32.const 1
24+
call $~lib/builtins/abort
25+
unreachable
26+
end
27+
)
28+
)

0 commit comments

Comments
 (0)