Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a1bb996

Browse files
committedMay 17, 2023
Treat special identifiers as variables
Fixes #9
1 parent d5aa294 commit a1bb996

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed
 

‎src/parser.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ import {ParserErrors} from "./errors";
4747
type Expr = ExprNS.Expr;
4848
type Stmt = StmtNS.Stmt;
4949

50+
const PSEUD_NAMES = [
51+
TokenType.TRUE,
52+
TokenType.FALSE,
53+
TokenType.NONE,
54+
]
5055

5156
export class Parser {
5257
private readonly source: string;
@@ -148,7 +153,8 @@ export class Parser {
148153
private stmt(): Stmt {
149154
if (this.check(TokenType.DEF, TokenType.FOR, TokenType.IF, TokenType.WHILE)) {
150155
return this.compound_stmt();
151-
} else if (this.check(TokenType.NAME, TokenType.NUMBER, TokenType.PASS, TokenType.BREAK, TokenType.CONTINUE,
156+
} else if (this.check(TokenType.NAME, ...PSEUD_NAMES, TokenType.NUMBER,
157+
TokenType.PASS, TokenType.BREAK, TokenType.CONTINUE,
152158
TokenType.RETURN, TokenType.FROM, TokenType.GLOBAL, TokenType.NONLOCAL,
153159
TokenType.ASSERT, TokenType.LPAR, ...SPECIAL_IDENTIFIER_TOKENS)) {
154160
return this.simple_stmt();
@@ -496,7 +502,7 @@ export class Parser {
496502
return new ExprNS.Literal(startToken, this.previous(), Number(this.previous().lexeme));
497503
}
498504

499-
if (this.match(TokenType.NAME)) {
505+
if (this.match(TokenType.NAME, ...PSEUD_NAMES)) {
500506
return new ExprNS.Variable(startToken, this.previous(), this.previous());
501507
}
502508

‎src/tests/regression.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ def foo(
2727
pass
2828
2929
pass
30+
`;
31+
toEstreeAST(text);
32+
})
33+
test('Issue #9', () => {
34+
const text = `
35+
add_one = lambda : None
36+
add_one = lambda : True
37+
add_one = lambda : False
3038
`;
3139
toEstreeAST(text);
3240
})

‎test.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
def foo(
2-
a, b
3-
):
4-
pass
5-
6-
pass
1+
add_one = lambda : None

0 commit comments

Comments
 (0)
Please sign in to comment.