Skip to content

Commit 27d6efa

Browse files
committed
Fix more keywords that can sometimes be identifiers, see #666
1 parent bdd825b commit 27d6efa

6 files changed

Lines changed: 108 additions & 51 deletions

File tree

src/parser.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
Token,
1919
Range,
2020
CommentHandler,
21-
IdentifierHandling
21+
IdentifierHandling,
22+
isIllegalVariableIdentifier
2223
} from "./tokenizer";
2324

2425
import {
@@ -815,6 +816,12 @@ export class Parser extends DiagnosticEmitter {
815816
return null;
816817
}
817818
var identifier = Node.createIdentifierExpression(tn.readIdentifier(), tn.range());
819+
if (isIllegalVariableIdentifier(identifier.text)) {
820+
this.error(
821+
DiagnosticCode.Identifier_expected,
822+
identifier.range
823+
);
824+
}
818825
var flags = parentFlags;
819826
if (tn.skip(Token.EXCLAMATION)) {
820827
flags |= CommonFlags.DEFINITE_ASSIGNMENT;
@@ -1780,10 +1787,17 @@ export class Parser extends DiagnosticEmitter {
17801787

17811788
var readonlyStart: i32 = 0;
17821789
var readonlyEnd: i32 = 0;
1783-
if (tn.skip(Token.READONLY)) {
1784-
flags |= CommonFlags.READONLY;
1785-
readonlyStart = tn.tokenPos;
1786-
readonlyEnd = tn.pos;
1790+
if (tn.peek() == Token.READONLY) {
1791+
let state = tn.mark();
1792+
tn.next();
1793+
if (tn.peek() != Token.COLON) { // modifier
1794+
tn.discard(state);
1795+
flags |= CommonFlags.READONLY;
1796+
readonlyStart = tn.tokenPos;
1797+
readonlyEnd = tn.pos;
1798+
} else { // identifier
1799+
tn.reset(state);
1800+
}
17871801
}
17881802

17891803
// check if accessor: ('get' | 'set') ^\n Identifier
@@ -3426,7 +3440,9 @@ export class Parser extends DiagnosticEmitter {
34263440
);
34273441
}
34283442
case Token.IDENTIFIER: {
3429-
let identifier = Node.createIdentifierExpression(tn.readIdentifier(), tn.range(startPos, tn.pos));
3443+
let identifierText = tn.readIdentifier();
3444+
if (identifierText == "null") return Node.createNullExpression(tn.range()); // special
3445+
let identifier = Node.createIdentifierExpression(identifierText, tn.range(startPos, tn.pos));
34303446
if (tn.peek(true) == Token.EQUALS_GREATERTHAN && !tn.nextTokenOnNewLine) {
34313447
return this.parseFunctionExpressionCommon(
34323448
tn,

src/tokenizer.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ export function tokenIsAlsoIdentifier(token: Token): bool {
355355
case Token.KEYOF:
356356
case Token.MODULE:
357357
case Token.NAMESPACE:
358+
case Token.NULL:
358359
case Token.READONLY:
359360
case Token.SET:
360361
case Token.TYPE:
@@ -363,6 +364,18 @@ export function tokenIsAlsoIdentifier(token: Token): bool {
363364
}
364365
}
365366

367+
export function isIllegalVariableIdentifier(name: string): bool {
368+
assert(name.length);
369+
switch (name.charCodeAt(0)) {
370+
case CharCode.d: return name == "delete";
371+
case CharCode.f: return name == "for";
372+
case CharCode.i: return name == "instanceof";
373+
case CharCode.n: return name == "null";
374+
case CharCode.v: return name == "void";
375+
}
376+
return false;
377+
}
378+
366379
export function operatorTokenToString(token: Token): string {
367380
switch (token) {
368381
case Token.DELETE: return "delete";

tests/compiler/call-super.untouched.wat

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,6 @@
338338
local.get $0
339339
)
340340
(func $call-super/F#constructor (; 11 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
341-
(local $1 i32)
342-
(local $2 i32)
343341
local.get $0
344342
i32.eqz
345343
if
@@ -351,16 +349,11 @@
351349
end
352350
local.get $0
353351
call $call-super/E#constructor
354-
local.tee $1
355352
local.set $0
356353
local.get $0
357354
i32.const 2
358355
i32.store offset=4
359356
local.get $0
360-
local.set $2
361-
local.get $1
362-
call $~lib/rt/stub/__release
363-
local.get $2
364357
)
365358
(func $call-super/test3 (; 12 ;) (type $FUNCSIG$v)
366359
(local $0 i32)
@@ -412,8 +405,6 @@
412405
local.get $0
413406
)
414407
(func $call-super/H#constructor (; 14 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
415-
(local $1 i32)
416-
(local $2 i32)
417408
local.get $0
418409
i32.eqz
419410
if
@@ -425,16 +416,11 @@
425416
end
426417
local.get $0
427418
call $call-super/G#constructor
428-
local.tee $1
429419
local.set $0
430420
local.get $0
431421
i32.const 2
432422
i32.store offset=4
433423
local.get $0
434-
local.set $2
435-
local.get $1
436-
call $~lib/rt/stub/__release
437-
local.get $2
438424
)
439425
(func $call-super/test4 (; 15 ;) (type $FUNCSIG$v)
440426
(local $0 i32)
@@ -486,8 +472,6 @@
486472
local.get $0
487473
)
488474
(func $call-super/J#constructor (; 17 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
489-
(local $1 i32)
490-
(local $2 i32)
491475
local.get $0
492476
i32.eqz
493477
if
@@ -499,16 +483,11 @@
499483
end
500484
local.get $0
501485
call $call-super/I#constructor
502-
local.tee $1
503486
local.set $0
504487
local.get $0
505488
i32.const 2
506489
i32.store offset=4
507490
local.get $0
508-
local.set $2
509-
local.get $1
510-
call $~lib/rt/stub/__release
511-
local.get $2
512491
)
513492
(func $call-super/test5 (; 18 ;) (type $FUNCSIG$v)
514493
(local $0 i32)

tests/compiler/rt/instanceof.untouched.wat

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(module
22
(type $FUNCSIG$iii (func (param i32 i32) (result i32)))
33
(type $FUNCSIG$ii (func (param i32) (result i32)))
4-
(type $FUNCSIG$vi (func (param i32)))
54
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
65
(type $FUNCSIG$v (func))
76
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
@@ -133,12 +132,7 @@
133132
end
134133
local.get $0
135134
)
136-
(func $~lib/rt/stub/__release (; 4 ;) (type $FUNCSIG$vi) (param $0 i32)
137-
nop
138-
)
139-
(func $rt/instanceof/Cat#constructor (; 5 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
140-
(local $1 i32)
141-
(local $2 i32)
135+
(func $rt/instanceof/Cat#constructor (; 4 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
142136
local.get $0
143137
i32.eqz
144138
if
@@ -150,17 +144,10 @@
150144
end
151145
local.get $0
152146
call $rt/instanceof/Animal#constructor
153-
local.tee $1
154147
local.set $0
155148
local.get $0
156-
local.set $2
157-
local.get $1
158-
call $~lib/rt/stub/__release
159-
local.get $2
160149
)
161-
(func $rt/instanceof/BlackCat#constructor (; 6 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
162-
(local $1 i32)
163-
(local $2 i32)
150+
(func $rt/instanceof/BlackCat#constructor (; 5 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
164151
local.get $0
165152
i32.eqz
166153
if
@@ -172,15 +159,10 @@
172159
end
173160
local.get $0
174161
call $rt/instanceof/Cat#constructor
175-
local.tee $1
176162
local.set $0
177163
local.get $0
178-
local.set $2
179-
local.get $1
180-
call $~lib/rt/stub/__release
181-
local.get $2
182164
)
183-
(func $~lib/rt/__instanceof (; 7 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
165+
(func $~lib/rt/__instanceof (; 6 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
184166
(local $2 i32)
185167
(local $3 i32)
186168
local.get $0
@@ -217,7 +199,7 @@
217199
end
218200
i32.const 0
219201
)
220-
(func $start:rt/instanceof (; 8 ;) (type $FUNCSIG$v)
202+
(func $start:rt/instanceof (; 7 ;) (type $FUNCSIG$v)
221203
(local $0 i32)
222204
global.get $~lib/heap/__heap_base
223205
i32.const 15
@@ -713,7 +695,7 @@
713695
unreachable
714696
end
715697
)
716-
(func $start (; 9 ;) (type $FUNCSIG$v)
698+
(func $start (; 8 ;) (type $FUNCSIG$v)
717699
global.get $~lib/started
718700
if
719701
return
@@ -723,6 +705,6 @@
723705
end
724706
call $start:rt/instanceof
725707
)
726-
(func $null (; 10 ;) (type $FUNCSIG$v)
708+
(func $null (; 9 ;) (type $FUNCSIG$v)
727709
)
728710
)

tests/parser/also-identifier.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Foo {
2+
as: i32;
3+
declare: i32;
4+
delete: i32;
5+
from: i32;
6+
for: i32;
7+
get: i32;
8+
instanceof: i32;
9+
is: i32;
10+
keyof: i32;
11+
module: i32;
12+
namespace: i32;
13+
null: i32;
14+
readonly: i32;
15+
set: i32;
16+
type: i32;
17+
void: i32;
18+
}
19+
20+
var as: i32;
21+
var constructor: i32;
22+
var declare: i32;
23+
var from: i32;
24+
var get: i32;
25+
var is: i32;
26+
var keyof: i32;
27+
var module: i32;
28+
var namespace: i32;
29+
var readonly: i32;
30+
var set: i32;
31+
32+
// -- illegal --
33+
// var delete: i32;
34+
// var for: i32;
35+
// var instanceof: i32;
36+
// var null: i32;
37+
// var type: i32;
38+
// var void: i32;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Foo {
2+
as: i32;
3+
declare: i32;
4+
delete: i32;
5+
from: i32;
6+
for: i32;
7+
get: i32;
8+
instanceof: i32;
9+
is: i32;
10+
keyof: i32;
11+
module: i32;
12+
namespace: i32;
13+
null: i32;
14+
readonly: i32;
15+
set: i32;
16+
type: i32;
17+
void: i32;
18+
}
19+
var as: i32;
20+
var constructor: i32;
21+
var declare: i32;
22+
var from: i32;
23+
var get: i32;
24+
var is: i32;
25+
var keyof: i32;
26+
var module: i32;
27+
var namespace: i32;
28+
var readonly: i32;
29+
var set: i32;

0 commit comments

Comments
 (0)