Skip to content

Commit

Permalink
Replace tokenType with quoted:boolean field in IdentifierNode
Browse files Browse the repository at this point in the history
Better to limit the amount of lexer information we expose in
parser result.

Also the code in parser is more likely to be updated when
lexer tokens change, unlike the code in formatter, which is
further separated.
  • Loading branch information
nene committed Nov 13, 2023
1 parent 282b84d commit a0811e9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/formatter/ExpressionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,9 @@ export default class ExpressionFormatter {
}

private showIdentifier(node: IdentifierNode): string {
if (node.tokenType === TokenType.IDENTIFIER || node.tokenType === TokenType.ARRAY_IDENTIFIER) {
if (node.quoted) {
return node.text;
} else {
switch (this.cfg.identifierCase) {
case 'preserve':
return node.text;
Expand All @@ -519,8 +521,6 @@ export default class ExpressionFormatter {
case 'lower':
return node.text.toLowerCase();
}
} else {
return node.text;
}
}
}
2 changes: 1 addition & 1 deletion src/parser/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export interface PropertyAccessNode extends BaseNode {

export interface IdentifierNode extends BaseNode {
type: NodeType.identifier;
tokenType: TokenType;
quoted: boolean;
text: string;
}

Expand Down
10 changes: 2 additions & 8 deletions src/parser/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ const lexer = new LexerAdapter(chunk => []);
// which otherwise produce single element nested inside two arrays
const unwrap = <T>([[el]]: T[][]): T => el;

const toIdentifierNode = (token: Token): IdentifierNode => ({
type: NodeType.identifier,
tokenType: token.type,
text: token.text,
});

const toKeywordNode = (token: Token): KeywordNode => ({
type: NodeType.keyword,
tokenType: token.type,
Expand Down Expand Up @@ -208,7 +202,7 @@ atomic_expression ->
array_subscript -> %ARRAY_IDENTIFIER _ square_brackets {%
([arrayToken, _, brackets]) => ({
type: NodeType.array_subscript,
array: addComments({ type: NodeType.identifier, tokenType: TokenType.ARRAY_IDENTIFIER, text: arrayToken.text}, { trailing: _ }),
array: addComments({ type: NodeType.identifier, quoted: false, text: arrayToken.text}, { trailing: _ }),
parenthesis: brackets,
})
%}
Expand Down Expand Up @@ -315,7 +309,7 @@ operator -> ( %OPERATOR ) {% ([[token]]) => ({ type: NodeType.operator, text: to
identifier ->
( %IDENTIFIER
| %QUOTED_IDENTIFIER
| %VARIABLE ) {% ([[token]]) => toIdentifierNode(token) %}
| %VARIABLE ) {% ([[token]]) => ({ type: NodeType.identifier, quoted: token.type !== "IDENTIFIER", text: token.text }) %}

parameter ->
( %NAMED_PARAMETER
Expand Down
40 changes: 20 additions & 20 deletions test/unit/__snapshots__/Parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Array [
Object {
"children": Array [
Object {
"quoted": false,
"text": "age",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
Object {
Expand Down Expand Up @@ -127,8 +127,8 @@ Array [
},
"expr": Array [
Object {
"quoted": false,
"text": "foo",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
],
Expand Down Expand Up @@ -289,8 +289,8 @@ Array [
"children": Array [
Object {
"object": Object {
"quoted": false,
"text": "ident",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
"property": Object {
Expand Down Expand Up @@ -322,8 +322,8 @@ Array [
"children": Array [
Object {
"array": Object {
"quoted": false,
"text": "my_array",
"tokenType": "ARRAY_IDENTIFIER",
"type": "identifier",
},
"parenthesis": Object {
Expand Down Expand Up @@ -363,8 +363,8 @@ Array [
"children": Array [
Object {
"array": Object {
"quoted": false,
"text": "my_array",
"tokenType": "ARRAY_IDENTIFIER",
"trailingComments": Array [
Object {
"precedingWhitespace": " ",
Expand Down Expand Up @@ -412,17 +412,17 @@ Array [
Object {
"children": Array [
Object {
"quoted": false,
"text": "foo",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
Object {
"text": ":",
"type": "operator",
},
Object {
"quoted": false,
"text": "bar",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
],
Expand Down Expand Up @@ -501,8 +501,8 @@ Array [
"type": "keyword",
},
Object {
"quoted": false,
"text": "a",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
Object {
Expand All @@ -524,8 +524,8 @@ Array [
"type": "function_call",
},
Object {
"quoted": false,
"text": "b",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
],
Expand All @@ -549,8 +549,8 @@ Array [
Object {
"children": Array [
Object {
"quoted": false,
"text": "foo",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
],
Expand All @@ -560,8 +560,8 @@ Array [
Object {
"children": Array [
Object {
"quoted": false,
"text": "bar",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
],
Expand All @@ -580,8 +580,8 @@ Array [
Object {
"children": Array [
Object {
"quoted": false,
"text": "birth_year",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
Object {
Expand All @@ -591,8 +591,8 @@ Array [
Object {
"children": Array [
Object {
"quoted": false,
"text": "CURRENT_DATE",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
Object {
Expand Down Expand Up @@ -638,20 +638,20 @@ Array [
Object {
"object": Object {
"object": Object {
"quoted": false,
"text": "foo",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
"property": Object {
"quoted": false,
"text": "bar",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
"type": "property_access",
},
"property": Object {
"quoted": false,
"text": "baz",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
"type": "property_access",
Expand Down Expand Up @@ -679,8 +679,8 @@ Array [
Object {
"children": Array [
Object {
"quoted": false,
"text": "foo",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
],
Expand All @@ -695,8 +695,8 @@ Array [
Object {
"children": Array [
Object {
"quoted": false,
"text": "bar",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
],
Expand All @@ -721,8 +721,8 @@ Array [
Object {
"children": Array [
Object {
"quoted": false,
"text": "foo",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
],
Expand All @@ -737,8 +737,8 @@ Array [
Object {
"children": Array [
Object {
"quoted": false,
"text": "baz",
"tokenType": "IDENTIFIER",
"type": "identifier",
},
],
Expand Down

0 comments on commit a0811e9

Please sign in to comment.