File tree Expand file tree Collapse file tree 3 files changed +35
-10
lines changed Expand file tree Collapse file tree 3 files changed +35
-10
lines changed Original file line number Diff line number Diff line change 1212
1313# pylint: disable=anomalous-backslash-in-string
1414from ahbicht .expressions import parsing_logger
15+ from ahbicht .expressions .sanitizer import sanitize_expression
1516from ahbicht .utility_functions import tree_copy
1617
1718GRAMMAR = """
3132# and CTRL+F for "Mus[2]" in the unittest that fails if you remove the lookahead.
3233_parser = Lark (GRAMMAR , start = "ahb_expression" )
3334
34- _replacements : dict [str , str ] = {
35- "\u00a0 " : " " , # no-break space,
36- "V" : "∨" , # Vogel-V != logical OR
37- "v" : "∨" ,
38- }
39-
4035
4136@tree_copy
4237@lru_cache (maxsize = 1024 )
@@ -51,9 +46,7 @@ def parse_ahb_expression_to_single_requirement_indicator_expressions(ahb_express
5146 :return parsed_tree:
5247 """
5348 try :
54- if ahb_expression is not None :
55- for key , value in _replacements .items ():
56- ahb_expression = ahb_expression .replace (key , value )
49+ ahb_expression = sanitize_expression (ahb_expression )
5750 parsed_tree = _parser .parse (ahb_expression )
5851 parsing_logger .debug ("Successfully parsed '%s' as AHB expression" , ahb_expression )
5952 except (UnexpectedEOF , UnexpectedCharacters , TypeError ) as eof :
Original file line number Diff line number Diff line change 1414
1515from ahbicht .condition_node_distinction import derive_condition_node_type
1616from ahbicht .expressions import parsing_logger
17+ from ahbicht .expressions .sanitizer import sanitize_expression
1718from ahbicht .models .categorized_key_extract import CategorizedKeyExtract
1819from ahbicht .models .condition_node_type import ConditionNodeType
1920from ahbicht .utility_functions import tree_copy
2021
2122GRAMMAR = r"""
2223?expression: expression "O"i expression -> or_composition
23- | expression "∨" expression -> or_composition
24+ | expression "∨" expression -> or_composition // the logical or
25+ | expression "V"i expression -> or_composition // a 'v' for those who first chose to introduce logical symbols but now can't find them on their keyboard
2426 | expression "X"i expression -> xor_composition
2527 | expression "⊻" expression -> xor_composition
2628 | expression "U"i expression -> and_composition
@@ -59,6 +61,7 @@ def parse_condition_expression_to_tree(condition_expression: str) -> Tree[Token]
5961 :return parsed_tree: Tree
6062 """
6163 try :
64+ condition_expression = sanitize_expression (condition_expression )
6265 parsed_tree = _parser .parse (condition_expression )
6366 parsing_logger .debug ("Successfully parsed '%s' as condition expression" , condition_expression )
6467 except (UnexpectedEOF , UnexpectedCharacters , TypeError ) as eof :
Original file line number Diff line number Diff line change 1+ """contains a function to sanitize user input/expressions from the AHBs"""
2+
3+ from typing import Literal , Optional , overload
4+
5+ _replacements : dict [str , str ] = {
6+ "\u00a0 " : " " , # no-break space,
7+ "V" : "∨" , # Vogel-V != logical OR
8+ "v" : "∨" ,
9+ }
10+
11+
12+ @overload
13+ def sanitize_expression (expression : Literal [None ]) -> Literal [None ]: ...
14+ @overload
15+ def sanitize_expression (expression : str ) -> str : ...
16+
17+
18+ def sanitize_expression (expression : Optional [str ]) -> Optional [str ]:
19+ """
20+ fixes some common issues with expressions from the AHBs
21+ """
22+ if expression is None :
23+ return None
24+ for key , value in _replacements .items ():
25+ expression = expression .replace (key , value )
26+ return expression .strip ()
27+
28+
29+ __all__ = ["sanitize_expression" ]
You can’t perform that action at this time.
0 commit comments