[voltLib] Context can take multiple except/in_context, multiple left or right contexts

This commit is contained in:
moyogo 2015-10-07 00:27:22 +01:00
parent 6f4f7849cf
commit ffa929b404
2 changed files with 33 additions and 12 deletions

View File

@ -61,3 +61,10 @@ class SubstitutionDefinition(ast.Statement):
def __init__(self, location, src, dest): def __init__(self, location, src, dest):
ast.Statement.__init__(self, location) ast.Statement.__init__(self, location)
self.mapping = zip(src, dest) self.mapping = zip(src, dest)
class ContextDefinition(ast.Statement):
def __init__(self, location, ex_or_in, left=[], right=[]):
ast.Statement.__init__(self, location)
self.ex_or_in = ex_or_in
self.left = left
self.right = right

View File

@ -162,8 +162,8 @@ class Parser(object):
if self.next_token_ == "COMMENTS": if self.next_token_ == "COMMENTS":
self.expect_keyword_("COMMENTS") self.expect_keyword_("COMMENTS")
comments = self.expect_string_() comments = self.expect_string_()
context = None context = []
if self.next_token_ in ("EXCEPT_CONTEXT", "IN_CONTEXT"): while self.next_token_ in ("EXCEPT_CONTEXT", "IN_CONTEXT"):
context = self.parse_context_() context = self.parse_context_()
as_pos_or_sub = self.expect_name_() as_pos_or_sub = self.expect_name_()
sub = None sub = None
@ -178,16 +178,30 @@ class Parser(object):
return def_lookup return def_lookup
def parse_context_(self): def parse_context_(self):
except_or_in = self.expect_name_() location = self.cur_token_location_
assert except_or_in in ("EXCEPT_CONTEXT", "IN_CONTEXT") contexts = []
while self.next_token_ in ("EXCEPT_CONTEXT", "IN_CONTEXT"):
side = None side = None
coverage = None coverage = None
if self.next_token_ != "END_CONTEXT" : ex_or_in = self.expect_name_()
side_contexts = []
if self.next_token_ != "END_CONTEXT":
left = []
right = []
while self.next_token_ in ("LEFT", "RIGHT"):
side = self.expect_name_() side = self.expect_name_()
assert side in ("LEFT", "RIGHT")
coverage = self.parse_coverage_() coverage = self.parse_coverage_()
if side == "LEFT":
left.append(coverage)
else:
right.append(coverage)
self.expect_keyword_("END_CONTEXT") self.expect_keyword_("END_CONTEXT")
return (except_or_in, side) context = ast.ContextDefinition(location, ex_or_in, left,
right)
contexts.append(context)
else:
self.expect_keyword_("END_CONTEXT")
return contexts
def parse_substitution_(self): def parse_substitution_(self):
assert self.is_cur_keyword_("AS_SUBSTITUTION") assert self.is_cur_keyword_("AS_SUBSTITUTION")