← Back to DAGDA | Support | Source: https://github.com/howl-editor/howl/commit/1e577da89efc1c1de780634b531f64346cf586d6
Skip to content

Commit 1e577da

Browse files
committed
Implement PEP 498 string interpolation
1 parent d99dd53 commit 1e577da

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎bundles/python/misc/examples.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070

7171
adjacent_strings_auto_concat = "hello " "world"
7272
same_as_above = "hello""world"
73+
raw_string = r'abc\d'
74+
75+
simple_f_string = f'abc{de}fgh{ij}klm'
76+
conv_f_string = f'ab{cd!s}ef'
77+
complicated_triple_f_string = f'''abc{a+'!':a=+#0{abc},.{prec}b}def'''
7378

7479
if False:
7580
# Dictionaries

‎bundles/python/python_lexer.moon‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,48 @@ howl.aux.lpeg_lexer ->
8383

8484
decorator = c 'preproc', P'@' * name * ('.' * name)^0
8585

86+
f_prefix = c 'special', any {
87+
S'rR' * S'fF'
88+
S'fF' * S'rR'
89+
S'fF'
90+
}
91+
92+
format_conv = c('operator', P'!') * c('special', S'sra')
93+
format_number = any {
94+
c 'number', integer
95+
c('operator', P'{') * (-P'}' * V'all') * c('operator', P'}')
96+
}
97+
format_spec = c('operator', ':') * sequence {
98+
(c('string', P 1)^-1 * c 'operator', S'<>=^')^-1 -- fill and align
99+
(P' ' + c 'operator', S'+-')^-1 -- sign
100+
c('operator', P'#')^-1 -- #
101+
c('number', P'0')^-1 -- 0
102+
format_number^-1 -- width
103+
c('operator', P',')^-1 -- ,
104+
(c('operator', P'.') * format_number)^-1 -- .precision
105+
c('special', S'bcdeEfFgGnosxX%')^-1 -- type
106+
}
107+
108+
gen_string_interpolation = (type) ->
109+
sequence {
110+
(-(P'}' + format_conv + format_spec) * (V'all' + 1))^1
111+
format_conv^-1
112+
format_spec^-1
113+
c 'operator', '}'
114+
V"#{type}q_string_chunk"
115+
}
116+
117+
gen_string_chunk = (quote, type) ->
118+
sequence {
119+
c 'string', scan_to(P(quote) + #P'{', P'\\')
120+
V"#{type}q_interpolation"^0
121+
}
122+
86123
P {
87124
'all'
88125

89126
all: any {
90-
string,
127+
V'string',
91128
comment,
92129
number,
93130
operator,
@@ -100,4 +137,24 @@ howl.aux.lpeg_lexer ->
100137
identifier,
101138
decorator
102139
}
140+
141+
tsq_interpolation: gen_string_interpolation 'ts'
142+
tdq_interpolation: gen_string_interpolation 'td'
143+
sq_interpolation: gen_string_interpolation 's'
144+
dq_interpolation: gen_string_interpolation 'd'
145+
tsq_string_chunk: gen_string_chunk "'''", 'ts'
146+
tdq_string_chunk: gen_string_chunk '"""', 'td'
147+
sq_string_chunk: gen_string_chunk "'", 's'
148+
dq_string_chunk: gen_string_chunk '"', 'd'
149+
f_string: any {
150+
capture('string', "'''") * V'tsq_string_chunk'
151+
capture('string', '"""') * V'tdq_string_chunk'
152+
capture('string', "'") * V'sq_string_chunk'
153+
capture('string', '"') * V'dq_string_chunk'
154+
}
155+
156+
string: any {
157+
f_prefix * V'f_string'
158+
string
159+
}
103160
}

0 commit comments

Comments
 (0)