Skip to content

Commit

Permalink
fixed test, fixed #188 but not allowing <foo>[0][0]
Browse files Browse the repository at this point in the history
  • Loading branch information
TimScheckenbach committed Jan 7, 2025
1 parent 8f04f7f commit 52e1f3f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/fandango/language/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ def check_constraints_existence(grammar, constraints):
error.add_note(f"Possible symbols: {defined_symbols_str}")
raise error

parent = constraint_matches_children[0]
#parent = constraint_matches_children[0]
for i in range(len(constraint_matches_children) - 1):
parent = constraint_matches_children[i]
LOGGER.debug(f"Parent: {parent}")
parent_sym = parent[0]
if parent[1] == "":
symbol = constraint_matches[i + 1][0]
symbol = constraint_matches[i + 1]
indirect = f"<{parent_sym}>..<{symbol}>" in str(value)
if not check_constraints_existence_children(
grammar, parent_sym, symbol, indirect, indirect_child
Expand All @@ -132,15 +134,16 @@ def check_constraints_existence(grammar, constraints):
else:
indirect = f"<{parent_sym}>{parent[1]}..<{constraint_matches[i+1][0]}>" in str(value)
if not check_constraint_existence_access(
grammar, parent_sym, parent[1][1:-1], indirect, constraint_matches[i+1][0]
grammar, parent_sym, int(parent[1][1:-1]), indirect, constraint_matches[i+1], indirect_child
):
msg = f"Constraint {constraint}: <{parent_sym}> has no child <{symbol}>"
raise ValueError(msg)




def check_constraint_existence_access(grammar, parent, id, recurse, next):
def check_constraint_existence_access(grammar, parent, id, recurse, next, indirect_child):
LOGGER.debug(f"In Access with {parent}, {id}, {next}")
rules = grammar.rules[NonTerminal(f"<{parent}>")]
if isinstance(rules, Alternative):
child = None
Expand All @@ -154,11 +157,11 @@ def check_constraint_existence_access(grammar, parent, id, recurse, next):
if child is None:
raise IndexError(f"Nonterminal <{parent}> has no {id}-th children")
else:
nonterminals = re.findall(r"<([^>]*)>", str(rule))
nonterminals = re.findall(r"<([^>]*)>", str(rules))
if len(nonterminals) < id:
raise IndexError(f"Nonterminal <{parent}> has no {id}-th children")
child = nonterminals[id]
return check_constraints_existence_children(grammar, child, next, recurse)
return check_constraints_existence_children(grammar, child, next, recurse, indirect_child)
return False


Expand Down
2 changes: 1 addition & 1 deletion tests/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def test_accessing_children(self):
<number> ::= <digit> | <digit><number>;
<digit> ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "0";
"""
constraint = "int(<start>[0].<digit>) == 0"
constraint = "int(<start>[0].<digit>) == 0;"
grammar, constraint = parse(grammar + constraint)
constraint = constraint[0]

Expand Down

0 comments on commit 52e1f3f

Please sign in to comment.