8000 Fix confusion in string context tracking · lezer-parser/python@92d171e · GitHub
[go: up one dir, main page]

Skip to content

Commit 92d171e

Browse files
committed
Fix confusion in string context tracking
FIX: Fix an issue where single-quoted strings inside format strings weren't tokenized properly. Closes #23
1 parent 798669d commit 92d171e

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/tokens.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const indentation = new ExternalTokenizer((input, stack) => {
7373
})
7474

7575
// Flags used in Context objects
76-
const cx_Bracketed = 1, cx_DoubleQuote = 2, cx_Long = 4, cx_Raw = 8, cx_Format = 16
76+
const cx_Bracketed = 1, cx_String = 2, cx_DoubleQuote = 4, cx_Long = 8, cx_Raw = 16, cx_Format = 32
7777

7878
function Context(parent, indent, flags) {
7979
this.parent = parent
@@ -108,13 +108,13 @@ const stringFlags = new Map([
108108
[stringStartFRD, cx_Format | cx_Raw | cx_DoubleQuote],
109109
[stringStartFRL, cx_Format | cx_Raw | cx_Long],
110110
[stringStartFRLD, cx_Format | cx_Raw | cx_Long | cx_DoubleQuote]
111-
])
111+
].map(([term, flags]) => [term, flags | cx_String]))
112112

113113
export const trackIndent = new ContextTracker({
114114
start: topIndent,
115-
reduce(context, term) {
115+
reduce(context, term, _, input) {
116116
if ((context.flags & cx_Bracketed) && bracketed.has(term) ||
117-
(term == StringTerm || term == FormatString) && context.flags && context.flags != cx_Bracketed)
117+
(term == StringTerm || term == FormatString) && (context.flags & cx_String))
118118
return context.parent
119119
return context
120120
},

test/expression.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ Script(ExpressionStatement(FormatString(FormatReplacement(Number),Escape,
6464
FormatReplacement(VariableName,FormatSelfDoc,FormatConversion),
6565
FormatReplacement(MemberExpression(VariableName,PropertyName),FormatSelfDoc,FormatSpec))))
6666

67+
# Nested quote types
68+
69+
f"a{'b'}c"
70+
71+
==>
72+
73+
Script(ExpressionStatement(FormatString(FormatReplacement(String))))
74+
6775
# Lambda
6876

6977
something.map(lambda x: x + 1)

0 commit comments

Comments
 (0)
0