parser.py
Go to the documentation of this file.
00001 __all__ = ["ParseError", "parse_nested_list"]
00002 
00003 class ParseError(Exception):
00004   pass
00005 
00006 # Basic functions for parsing PDDL (Lisp) files.
00007 def parse_nested_list(input_file):
00008   tokens = tokenize(input_file)
00009   next_token = tokens.next()
00010   if next_token != "(":
00011     raise ParseError("Expected '(', got %s." % next_token)
00012   result = list(parse_list_aux(tokens))
00013   for tok in tokens:  # Check that generator is exhausted.
00014     raise ParseError("Unexpected token: %s." % tok)
00015   return result
00016   
00017 def tokenize(input):
00018   # Here comes the ugly hack: :moduleoptions section must not be decapitalized!
00019   # therefore need to make sure, when the section ends.
00020   inModOpts = False
00021   modOptsOpenParen = 0
00022   for line in input:
00023     line = line.split(";", 1)[0]  # Strip comments.
00024     line = line.replace("(", " ( ").replace(")", " ) ").replace("?", " ?")
00025     # added to tokenize module calls
00026     line = line.replace("[", " [ ").replace("]", " ] ")
00027     for token in line.split():
00028       if token == ":moduleoptions":
00029         inModOpts = True
00030         modOptsOpenParen = 1  # the one right before :moduleoptions
00031       if token == "(" and inModOpts:
00032         modOptsOpenParen += 1
00033       if token == ")" and inModOpts:
00034         modOptsOpenParen -= 1
00035       if inModOpts and modOptsOpenParen <= 0:
00036         inModOpts = False
00037         
00038       if token.find("@") != -1 or inModOpts:   # keep library calls correct
00039         yield token
00040       else:
00041         yield token.lower()
00042 
00043 def parse_list_aux(tokenstream):
00044   # Leading "(" has already been swallowed.
00045   while True:
00046     try:
00047       token = tokenstream.next()
00048     except StopIteration:
00049       raise ParseError()
00050     if token == ")":
00051       return
00052     elif token == "(":
00053       yield list(parse_list_aux(tokenstream))
00054     else:
00055       yield token
00056 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Tue Jan 22 2013 12:25:03