Commit e559b396 authored by Marien Fressinaud's avatar Marien Fressinaud

tec: Refactor BoopsySyntaxError calls

parent faca8199
......@@ -81,7 +81,7 @@ class Template:
if token.startswith("{{"):
expression = token[2:-2].strip()
if len(expression) == 0:
raise BoopsySyntaxError("an expression is expected between {{ }}")
self._oopsy("an expression is expected between {{ }}")
# Expression token is just valid Python code so we add it to
# the rendering array. It will be evaluated later.
......@@ -93,9 +93,7 @@ class Template:
args = words[1:]
if statement == "if":
if len(args) < 1:
raise BoopsySyntaxError(
"a condition is expected in if statement"
)
self._oopsy("a condition is expected in if statement")
ops_stack.append("if")
condition = " ".join(args)
code.write(f"if {condition}:")
......@@ -104,13 +102,11 @@ class Template:
elif statement == "elif":
last_op = ops_stack[-1]
if last_op != "if":
raise BoopsySyntaxError(
self._oopsy(
"elif statement can only be used with a if statement"
)
if len(args) < 1:
raise BoopsySyntaxError(
"a condition is expected in elif statement"
)
self._oopsy("a condition is expected in elif statement")
condition = " ".join(args)
code.dedent()
code.write(f"elif {condition}:")
......@@ -119,43 +115,40 @@ class Template:
elif statement == "else":
last_op = ops_stack[-1]
if last_op != "if":
raise BoopsySyntaxError(
self._oopsy(
"else statement can only be used with a if statement"
)
if len(args) != 0:
raise BoopsySyntaxError("else statement expects no arguments")
self._oopsy("else statement expects no arguments")
code.dedent()
code.write(f"else:")
code.indent()
elif statement == "for":
if len(args) != 3:
raise BoopsySyntaxError("for statement expects 3 arguments")
self._oopsy("for statement expects 3 arguments")
if args[1] != "in":
raise BoopsySyntaxError(
"for statement expects second argument to be 'in'"
)
self._oopsy("for statement expects second argument to be 'in'")
ops_stack.append("for")
code.write(f"for {args[0]} in {args[2]}:")
code.indent()
elif statement == "endif" or statement == "endfor":
if len(args) != 0:
raise BoopsySyntaxError(f"{statement} expects no argument")
self._oopsy(f"{statement} expects no argument")
if not ops_stack:
raise BoopsySyntaxError(
f"unexpected {statement} (too many ends)"
)
self._oopsy(f"unexpected {statement} (too many ends)")
current_op = statement[3:]
expected_op = ops_stack.pop()
if expected_op != current_op:
raise BoopsySyntaxError(
self._oopsy(
f"unexpected {statement} (should be end{expected_op})"
)
code.dedent()
else:
raise BoopsySyntaxError(f"unknown statement (got '{statement}')")
self._oopsy(f"unknown statement (got '{statement}')")
elif token:
# default tokens must be added to the rendering array
......@@ -163,9 +156,7 @@ class Template:
code.write(f"rendering.append({literal})")
if ops_stack:
raise BoopsySyntaxError(
f"missing end statement (expected 'end{ops_stack[-1]}')"
)
self._oopsy(f"missing end statement (expected 'end{ops_stack[-1]}')")
# `result` will store the `rendering` variable so we can get the finale
# string to return to the caller.
......@@ -177,3 +168,13 @@ class Template:
# interprated template. `item` might be None and so `join` can fail,
# `str` makes sure we are manipulating only strings.
return "".join(str(item) for item in result["rendering"])
def _oopsy(self, message):
"""Raise a BoopsySyntaxError.
This method has no real interest, it's mainly for the pun / similarity
between Boopsy and Oopsy (https://en.wiktionary.org/wiki/oopsy).
It is called when parsing the template.
"""
raise BoopsySyntaxError(message)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment