Go to the documentation of this file.00001 __all__ = ["ParseError", "parse_nested_list"]
00002
00003 class ParseError(Exception):
00004 pass
00005
00006
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:
00014 raise ParseError("Unexpected token: %s." % tok)
00015 return result
00016
00017 def tokenize(input):
00018
00019
00020 inModOpts = False
00021 modOptsOpenParen = 0
00022 for line in input:
00023 line = line.split(";", 1)[0]
00024 line = line.replace("(", " ( ").replace(")", " ) ").replace("?", " ?")
00025
00026 line = line.replace("[", " [ ").replace("]", " ] ")
00027 for token in line.split():
00028 if token == ":moduleoptions":
00029 inModOpts = True
00030 modOptsOpenParen = 1
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:
00039 yield token
00040 else:
00041 yield token.lower()
00042
00043 def parse_list_aux(tokenstream):
00044
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