aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild
diff options
context:
space:
mode:
authorPeter Lesslie <pclesslie@gmail.com>2022-04-15 15:02:14 -0500
committerEli Schwartz <eschwartz93@gmail.com>2022-05-01 12:47:37 -0400
commitd771fc7d0b45f8fa66f6570720fba73941de67cd (patch)
treec75bc9db23d033ed8caaf9a2a979ad3f0e77fbd1 /mesonbuild
parent78a6f3bd5c292afb68be7f4fbcd0e8c8ba0e3236 (diff)
downloadmeson-d771fc7d0b45f8fa66f6570720fba73941de67cd.zip
meson-d771fc7d0b45f8fa66f6570720fba73941de67cd.tar.gz
meson-d771fc7d0b45f8fa66f6570720fba73941de67cd.tar.bz2
Add support for multiline f-strings
+ Extend the parser to recognize the multiline f-strings, which the documentation already implies will work. The syntax is like: ``` x = 'hello' y = 'world' msg = f'''This is a multiline string. Sending a message: '@x@ @y@' ''' ``` which produces: ``` This is a multiline string. Sending a message: 'hello world' ``` + Added some f-string tests cases to "62 string arithmetic" to exercise the new behavior.
Diffstat (limited to 'mesonbuild')
-rw-r--r--mesonbuild/interpreterbase/interpreterbase.py9
-rw-r--r--mesonbuild/mparser.py23
2 files changed, 27 insertions, 5 deletions
diff --git a/mesonbuild/interpreterbase/interpreterbase.py b/mesonbuild/interpreterbase/interpreterbase.py
index 85aabd1..f9a3b07 100644
--- a/mesonbuild/interpreterbase/interpreterbase.py
+++ b/mesonbuild/interpreterbase/interpreterbase.py
@@ -217,7 +217,10 @@ class InterpreterBase:
elif isinstance(cur, mparser.TernaryNode):
return self.evaluate_ternary(cur)
elif isinstance(cur, mparser.FormatStringNode):
- return self.evaluate_fstring(cur)
+ if isinstance(cur, mparser.MultilineFormatStringNode):
+ return self.evaluate_multiline_fstring(cur)
+ else:
+ return self.evaluate_fstring(cur)
elif isinstance(cur, mparser.ContinueNode):
raise ContinueRequest()
elif isinstance(cur, mparser.BreakNode):
@@ -367,6 +370,10 @@ class InterpreterBase:
else:
return self.evaluate_statement(node.falseblock)
+ @FeatureNew('multiline format strings', '0.63.0')
+ def evaluate_multiline_fstring(self, node: mparser.MultilineFormatStringNode) -> InterpreterObject:
+ return self.evaluate_fstring(node)
+
@FeatureNew('format strings', '0.58.0')
def evaluate_fstring(self, node: mparser.FormatStringNode) -> InterpreterObject:
assert isinstance(node, mparser.FormatStringNode)
diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py
index 97a87d8..0995ee8 100644
--- a/mesonbuild/mparser.py
+++ b/mesonbuild/mparser.py
@@ -114,6 +114,7 @@ class Lexer:
self.token_specification = [
# Need to be sorted longest to shortest.
('ignore', re.compile(r'[ \t]')),
+ ('multiline_fstring', re.compile(r"f'''(.|\n)*?'''", re.M)),
('fstring', re.compile(r"f'([^'\\]|(\\.))*'")),
('id', re.compile('[_a-zA-Z][_0-9a-zA-Z]*')),
('number', re.compile(r'0[bB][01]+|0[oO][0-7]+|0[xX][0-9a-fA-F]+|0|[1-9]\d*')),
@@ -203,9 +204,17 @@ class Lexer:
value = ESCAPE_SEQUENCE_SINGLE_RE.sub(decode_match, value)
except MesonUnicodeDecodeError as err:
raise MesonException(f"Failed to parse escape sequence: '{err.match}' in string:\n {match_text}")
- elif tid == 'multiline_string':
- tid = 'string'
- value = match_text[3:-3]
+ elif tid in {'multiline_string', 'multiline_fstring'}:
+ # For multiline strings, parse out the value and pass
+ # through the normal string logic.
+ # For multiline format strings, we have to emit a
+ # different AST node so we can add a feature check,
+ # but otherwise, it follows the normal fstring logic.
+ if tid == 'multiline_string':
+ value = match_text[3:-3]
+ tid = 'string'
+ else:
+ value = match_text[4:-3]
lines = match_text.split('\n')
if len(lines) > 1:
lineno += len(lines) - 1
@@ -298,7 +307,11 @@ class FormatStringNode(ElementaryNode[str]):
assert isinstance(self.value, str)
def __str__(self) -> str:
- return "Format string node: '{self.value}' ({self.lineno}, {self.colno})."
+ return f"Format string node: '{self.value}' ({self.lineno}, {self.colno})."
+
+class MultilineFormatStringNode(FormatStringNode):
+ def __str__(self) -> str:
+ return f"Multiline Format string node: '{self.value}' ({self.lineno}, {self.colno})."
class ContinueNode(ElementaryNode):
pass
@@ -685,6 +698,8 @@ class Parser:
return StringNode(t)
if self.accept('fstring'):
return FormatStringNode(t)
+ if self.accept('multiline_fstring'):
+ return MultilineFormatStringNode(t)
return EmptyNode(self.current.lineno, self.current.colno, self.current.filename)
def key_values(self) -> ArgumentNode: