Skip to content

Commit

Permalink
[issue1030] translator: Improve PDDL error reporting.
Browse files Browse the repository at this point in the history
We have substantially extended the PDDL syntax checks in the translator. Many PDDL errors are now diagnosed much better and lead to a meaningful error message and exit code where previously they would trigger failed assertions or be silently accepted with unclear semantics.
  • Loading branch information
PatrickFerber authored Jan 18, 2024
1 parent 2349940 commit 768091d
Show file tree
Hide file tree
Showing 7 changed files with 683 additions and 346 deletions.
19 changes: 13 additions & 6 deletions src/translate/pddl/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,22 @@ def dump(self):
for axiom in self.axioms:
axiom.dump()


REQUIREMENT_LABELS = [
":strips", ":adl", ":typing", ":negation", ":equality",
":negative-preconditions", ":disjunctive-preconditions",
":existential-preconditions", ":universal-preconditions",
":quantified-preconditions", ":conditional-effects",
":derived-predicates", ":action-costs"
]


class Requirements:
def __init__(self, requirements: List[str]):
self.requirements = requirements
for req in requirements:
assert req in (
":strips", ":adl", ":typing", ":negation", ":equality",
":negative-preconditions", ":disjunctive-preconditions",
":existential-preconditions", ":universal-preconditions",
":quantified-preconditions", ":conditional-effects",
":derived-predicates", ":action-costs"), req
if req not in REQUIREMENT_LABELS:
raise ValueError(f"Invalid requirement. Got: {req}\n"
f"Expected: {', '.join(REQUIREMENT_LABELS)}")
def __str__(self):
return ", ".join(self.requirements)
1 change: 1 addition & 0 deletions src/translate/pddl_parser/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .parse_error import ParseError
from .pddl_file import open
19 changes: 8 additions & 11 deletions src/translate/pddl_parser/lisp_parser.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
__all__ = ["ParseError", "parse_nested_list"]
__all__ = ["parse_nested_list"]

class ParseError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
from .parse_error import ParseError

# Basic functions for parsing PDDL (Lisp) files.
def parse_nested_list(input_file):
tokens = tokenize(input_file)
next_token = next(tokens)
if next_token != "(":
raise ParseError("Expected '(', got %s." % next_token)
raise ParseError(f"Expected '(', got '{next_token}'.")
result = list(parse_list_aux(tokens))
for tok in tokens: # Check that generator is exhausted.
raise ParseError("Unexpected token: %s." % tok)
remaining_tokens = list(tokens)
if remaining_tokens:
raise ParseError(f"Tokens remaining after parsing: "
f"{' '.join(remaining_tokens)}")
return result

def tokenize(input):
Expand All @@ -23,8 +21,7 @@ def tokenize(input):
try:
line.encode("ascii")
except UnicodeEncodeError:
raise ParseError("Non-ASCII character outside comment: %s" %
line[0:-1])
raise ParseError(f"Non-ASCII character outside comment: {line[0:-1]}")
line = line.replace("(", " ( ").replace(")", " ) ").replace("?", " ?")
for token in line.split():
yield token.lower()
Expand Down
2 changes: 2 additions & 0 deletions src/translate/pddl_parser/parse_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ParseError(Exception):
pass
Loading

0 comments on commit 768091d

Please sign in to comment.