Skip to content

Commit 5354887

Browse files
committed
Store identifier type in node
1 parent d6664d1 commit 5354887

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/parser/ast.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export interface PropertyAccessNode extends BaseNode {
125125

126126
export interface IdentifierNode extends BaseNode {
127127
type: NodeType.identifier;
128+
tokenType: TokenType;
128129
text: string;
129130
}
130131

src/parser/grammar.ne

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@preprocessor typescript
22
@{%
33
import LexerAdapter from './LexerAdapter.js';
4-
import { NodeType, AstNode, CommentNode, KeywordNode } from './ast.js';
4+
import { NodeType, AstNode, CommentNode, KeywordNode, IdentifierNode } from './ast.js';
55
import { Token, TokenType } from '../lexer/token.js';
66

77
// The lexer here is only to provide the has() method,
@@ -16,6 +16,12 @@ const lexer = new LexerAdapter(chunk => []);
1616
// which otherwise produce single element nested inside two arrays
1717
const unwrap = <T>([[el]]: T[][]): T => el;
1818

19+
const toIdentifierNode = (token: Token): IdentifierNode => ({
20+
type: NodeType.identifier,
21+
tokenType: token.type,
22+
text: token.text,
23+
});
24+
1925
const toKeywordNode = (token: Token): KeywordNode => ({
2026
type: NodeType.keyword,
2127
tokenType: token.type,
@@ -202,7 +208,7 @@ atomic_expression ->
202208
array_subscript -> %ARRAY_IDENTIFIER _ square_brackets {%
203209
([arrayToken, _, brackets]) => ({
204210
type: NodeType.array_subscript,
205-
array: addComments({ type: NodeType.identifier, text: arrayToken.text}, { trailing: _ }),
211+
array: addComments({ type: NodeType.identifier, tokenType: TokenType.ARRAY_IDENTIFIER, text: arrayToken.text}, { trailing: _ }),
206212
parenthesis: brackets,
207213
})
208214
%}
@@ -309,7 +315,7 @@ operator -> ( %OPERATOR ) {% ([[token]]) => ({ type: NodeType.operator, text: to
309315
identifier ->
310316
( %IDENTIFIER
311317
| %QUOTED_IDENTIFIER
312-
| %VARIABLE ) {% ([[token]]) => ({ type: NodeType.identifier, text: token.text }) %}
318+
| %VARIABLE ) {% ([[token]]) => toIdentifierNode(token) %}
313319

314320
parameter ->
315321
( %NAMED_PARAMETER

test/unit/__snapshots__/Parser.test.ts.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Array [
88
"children": Array [
99
Object {
1010
"text": "age",
11+
"tokenType": "IDENTIFIER",
1112
"type": "identifier",
1213
},
1314
Object {
@@ -127,6 +128,7 @@ Array [
127128
"expr": Array [
128129
Object {
129130
"text": "foo",
131+
"tokenType": "IDENTIFIER",
130132
"type": "identifier",
131133
},
132134
],
@@ -288,6 +290,7 @@ Array [
288290
Object {
289291
"object": Object {
290292
"text": "ident",
293+
"tokenType": "IDENTIFIER",
291294
"type": "identifier",
292295
},
293296
"property": Object {
@@ -320,6 +323,7 @@ Array [
320323
Object {
321324
"array": Object {
322325
"text": "my_array",
326+
"tokenType": "ARRAY_IDENTIFIER",
323327
"type": "identifier",
324328
},
325329
"parenthesis": Object {
@@ -360,6 +364,7 @@ Array [
360364
Object {
361365
"array": Object {
362366
"text": "my_array",
367+
"tokenType": "ARRAY_IDENTIFIER",
363368
"trailingComments": Array [
364369
Object {
365370
"precedingWhitespace": " ",
@@ -408,6 +413,7 @@ Array [
408413
"children": Array [
409414
Object {
410415
"text": "foo",
416+
"tokenType": "IDENTIFIER",
411417
"type": "identifier",
412418
},
413419
Object {
@@ -416,6 +422,7 @@ Array [
416422
},
417423
Object {
418424
"text": "bar",
425+
"tokenType": "IDENTIFIER",
419426
"type": "identifier",
420427
},
421428
],
@@ -495,6 +502,7 @@ Array [
495502
},
496503
Object {
497504
"text": "a",
505+
"tokenType": "IDENTIFIER",
498506
"type": "identifier",
499507
},
500508
Object {
@@ -517,6 +525,7 @@ Array [
517525
},
518526
Object {
519527
"text": "b",
528+
"tokenType": "IDENTIFIER",
520529
"type": "identifier",
521530
},
522531
],
@@ -541,6 +550,7 @@ Array [
541550
"children": Array [
542551
Object {
543552
"text": "foo",
553+
"tokenType": "IDENTIFIER",
544554
"type": "identifier",
545555
},
546556
],
@@ -551,6 +561,7 @@ Array [
551561
"children": Array [
552562
Object {
553563
"text": "bar",
564+
"tokenType": "IDENTIFIER",
554565
"type": "identifier",
555566
},
556567
],
@@ -570,6 +581,7 @@ Array [
570581
"children": Array [
571582
Object {
572583
"text": "birth_year",
584+
"tokenType": "IDENTIFIER",
573585
"type": "identifier",
574586
},
575587
Object {
@@ -580,6 +592,7 @@ Array [
580592
"children": Array [
581593
Object {
582594
"text": "CURRENT_DATE",
595+
"tokenType": "IDENTIFIER",
583596
"type": "identifier",
584597
},
585598
Object {
@@ -626,16 +639,19 @@ Array [
626639
"object": Object {
627640
"object": Object {
628641
"text": "foo",
642+
"tokenType": "IDENTIFIER",
629643
"type": "identifier",
630644
},
631645
"property": Object {
632646
"text": "bar",
647+
"tokenType": "IDENTIFIER",
633648
"type": "identifier",
634649
},
635650
"type": "property_access",
636651
},
637652
"property": Object {
638653
"text": "baz",
654+
"tokenType": "IDENTIFIER",
639655
"type": "identifier",
640656
},
641657
"type": "property_access",
@@ -664,6 +680,7 @@ Array [
664680
"children": Array [
665681
Object {
666682
"text": "foo",
683+
"tokenType": "IDENTIFIER",
667684
"type": "identifier",
668685
},
669686
],
@@ -679,6 +696,7 @@ Array [
679696
"children": Array [
680697
Object {
681698
"text": "bar",
699+
"tokenType": "IDENTIFIER",
682700
"type": "identifier",
683701
},
684702
],
@@ -704,6 +722,7 @@ Array [
704722
"children": Array [
705723
Object {
706724
"text": "foo",
725+
"tokenType": "IDENTIFIER",
707726
"type": "identifier",
708727
},
709728
],
@@ -719,6 +738,7 @@ Array [
719738
"children": Array [
720739
Object {
721740
"text": "baz",
741+
"tokenType": "IDENTIFIER",
722742
"type": "identifier",
723743
},
724744
],

0 commit comments

Comments
 (0)