def led(self, left): accessor = Node("accessor") # identifier accessor.children.append(left) # selector key = Node("key") accessor.children.append(key) key.children.append(expression()) advance("]") return accessor
def led(self, left): # first first = Node("first") first.children.append(left) self.children.append(first) # second second = Node("second") second.children.append(expression()) self.children.append(second) advance(":") # third third = Node("third") third.children.append(expression()) self.children.append(third) return self
def led(self, left): call = Node("call") # operand operand = Node("operand") call.children.append(operand) operand.children.append(left) # params params = Node("params") call.children.append(params) if token.id != ")": while True: params.children.append(expression()) if token.id != ",": break advance(",") advance(")") return call
def statements(): # plural! s = Node("statements") while True: if token.id == "}" or token.id == "eof": break st = statement() if st: s.children.append(st) return s
def nud(self): # optional name if token.id == "identifier": #self.children.append(token.value) #self.children.append(token) self.set("name", token.value) advance() # params assert isinstance(token, symbol("(")) params = Node("params") self.children.append(params) group = expression() params.children = group.children # body body = Node("body") self.children.append(body) if token.id == "{": body.children.append(block()) else: body.children.append(statement()) return self
def nud(self): arr = Node("array") if token.id != "]": while True: if token.id == "]": break arr.children.append(expression()) if token.id != ",": break advance(",") advance("]") return arr
def nud(self): mmap = Node("map") if token.id != "}": while True: if token.id == "}": break # key keyname = expression() key = Node("keyvalue") key.set("key", keyname.get("value")) mmap.children.append(key) advance(":") # value keyval = expression() val = Node("value") val.children.append(keyval) key.children.append(val) # <value> is a child of <keyvalue> if token.id != ",": break advance(",") advance("}") return mmap
def std(self): vardecl = Node("definitionList") while True: var = Node("definition") vardecl.children.append(var) n = token if n.id != "identifier": raise SyntaxError("Expected a new variable name (pos %d)" % self.spos) advance() if token.id == "=": # initialization t = token advance("=") t.children.append(n) t.children.append(expression()) var.children.append(t) else: var.children.append(n) if token.id != ",": break else: advance(",") #advance(";") return vardecl
def nud(self): comma = False group = Node("group") if token.id != ")": while True: if token.id == ")": break group.children.append(expression()) if token.id != ",": break comma = True advance(",") advance(")") return group
def led(self, left): if token.id != "identifier": SyntaxError("Expected an attribute name (pos %d)." % self.spos) #variable = Node("variable") #variable.children.append(left.getChild("identifier")) # unwrap from <variable/> #variable.children.append(left) #while True: # #variable.children.append(expression().getChildByPosition(0)) # unwrap from <variable/> # variable.children.append(expression()) # if token.id != ".": # break # advance(".") accessor = Node("dotaccessor") accessor.children.append(left) accessor.children.append(expression(symbol(".").lbp)) # i'm providing the rbp to expression() here explicitly, so "foo.bar(baz)" gets parsed # as (call (dotaccessor ...) (param baz)), and not (dotaccessor foo # (call bar (param baz))). return accessor
def __init__(self): # to override Node.__init__(self,type) #self.attributes = {} # compat with Node.attributes #self.children = [] # compat with Node.children Node.__init__(self, self.__class__.id)
def block(): t = token advance("{") s = Node("block") s.children.append(t.std()) # the "{".std takes care of closing "}" return s