Skip to content

Commit

Permalink
BailErrorStrategy added to raise Exceptions when parsing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
vargastat committed Mar 11, 2024
1 parent 24f3793 commit 4583f43
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/andromede/expression/parsing/parse_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from dataclasses import dataclass
from typing import Set

from antlr4 import CommonTokenStream, InputStream
from antlr4 import CommonTokenStream, InputStream, DiagnosticErrorListener
from antlr4.error.ErrorStrategy import BailErrorStrategy

from andromede.expression import ExpressionNode, literal, param, var
from andromede.expression.equality import expressions_equal
Expand Down Expand Up @@ -172,12 +173,24 @@ def visitShift(self, ctx: ExprParser.ShiftContext) -> ExpressionNode:
}


class AntaresParseException(Exception):
pass


def parse_expression(expression: str, identifiers: ModelIdentifiers) -> ExpressionNode:
"""
Parses a string expression to create the corresponding AST representation.
"""
input = InputStream(expression)
lexer = ExprLexer(input)
stream = CommonTokenStream(lexer)
parser = ExprParser(stream)
return ExpressionNodeBuilderVisitor(identifiers).visit(parser.fullexpr()) # type: ignore
try:
input = InputStream(expression)
lexer = ExprLexer(input)
stream = CommonTokenStream(lexer)
parser = ExprParser(stream)
parser._errHandler = BailErrorStrategy()

return ExpressionNodeBuilderVisitor(identifiers).visit(parser.fullexpr()) # type: ignore

except Exception as e:
raise AntaresParseException(
f"An error occurred during parsing: {type(e).__name__}"
) from e
31 changes: 31 additions & 0 deletions tests/andromede/expressions/parsing/test_expression_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from andromede.expression.parsing.parse_expression import (
ModelIdentifiers,
parse_expression,
AntaresParseException,
)


Expand Down Expand Up @@ -55,6 +56,7 @@
("x[t-1..t]", var("x").shift(ExpressionRange(-literal(1), literal(0)))),
("x[t..t+5]", var("x").shift(ExpressionRange(literal(0), literal(5)))),
("x[t]", var("x")),
("x[t+p]", var("x").shift(param("p"))),
(
"sum(x[-1..5])",
var("x").eval(ExpressionRange(-literal(1), literal(5))).sum(),
Expand Down Expand Up @@ -99,3 +101,32 @@ def test_parsing_visitor(expression_str: str, expected: ExpressionNode):
print()
print(print_expr(expr))
assert expressions_equal(expr, expected)


def test_parse_cancellation_err():
# Console log error is displayed !
identifiers = ModelIdentifiers(
variables={
"x",
"level",
"injection",
"withdrawal",
"nb_start",
"nb_on",
"generation",
},
parameters={
"p",
"inflows",
"efficiency",
"d_min_up",
"cost",
},
)
expression_str = "x[t+1-t]"

with pytest.raises(
AntaresParseException,
match=r"An error occurred during parsing: ParseCancellationException",
):
parse_expression(expression_str, identifiers)

0 comments on commit 4583f43

Please sign in to comment.