def factor(self): self.dbg_msg( "factor" ) tok=self.peek() if tok.kind == EzhilToken.LPAREN: lparen_tok = self.dequeue() val=self.expr() if self.dequeue().kind!= EzhilToken.RPAREN: raise SyntaxError("Missing Parens "+str(self.last_token())) elif tok.kind == EzhilToken.NUMBER: tok_num = self.dequeue() [l, c] = tok_num.get_line_col() val = Number( tok.val , l, c, self.debug ) elif tok.kind == EzhilToken.LOGICAL_NOT: tok_not = self.dequeue() [l, c] = tok_not.get_line_col() val = UnaryExpr( self.expr(), tok_not , l, c, self.debug ) self.dbg_msg("completed parsing unary expression"+str(val)) elif tok.kind == EzhilToken.ID: tok_id = self.dequeue() [l, c] = tok_id.get_line_col() val = Identifier( tok.val , l, c, self.debug ) ptok = self.peek() self.dbg_msg("factor: "+str(ptok) + " / "+str(tok) ) if ( ptok.kind == EzhilToken.LPAREN ): ## function call [l, c] = ptok.get_line_col() vallist = self.valuelist() val=ExprCall( val, vallist, l, c, self.debug ) elif ( ptok.kind == EzhilToken.LSQRBRACE ): ## indexing a array type variable or ID val=None raise ParseException("arrays not implemented"+str(ptok)); elif tok.kind == EzhilToken.STRING : str_tok = self.dequeue() [l,c] = str_tok.get_line_col() val = String( tok.val , l, c, self.debug ) elif tok.kind in EzhilToken.ADDSUB: unop = self.dequeue(); [l, c] = unop.get_line_col() val=Expr(Number(0),unop,self.term(),l,c,self.debug); elif tok.kind == EzhilToken.LSQRBRACE: # creating a list/array expression list_start = self.dequeue(); val = Array() while( True ): exprval = self.expr() val.append( exprval ) if self.debug : print(self.peek().__class__,self.peek()) if ( self.peek().kind == EzhilToken.RSQRBRACE ): break else: assert( self.peek().kind == EzhilToken.COMMA) self.dequeue() assert( self.peek().kind == EzhilToken.RSQRBRACE ) list_end = self.dequeue() else: raise ParseException("Expected Number, found something "+str(tok)) self.dbg_msg( "factor-returning: "+str(val) ) return val
def factor(self): self.dbg_msg( "factor" ) tok=self.peek() if tok.kind == EzhilToken.LPAREN: lparen_tok = self.dequeue() val=self.expr() if self.dequeue().kind!= EzhilToken.RPAREN: raise SyntaxError("Missing Parens "+unicode(self.last_token())) elif tok.kind == EzhilToken.NUMBER: tok_num = self.dequeue() [l, c] = tok_num.get_line_col() val = Number( tok.val , l, c, self.debug ) elif tok.kind == EzhilToken.LOGICAL_NOT: tok_not = self.dequeue() [l, c] = tok_not.get_line_col() val = UnaryExpr( self.expr(), tok_not , l, c, self.debug ) self.dbg_msg("completed parsing unary expression"+unicode(val)) elif tok.kind == EzhilToken.ID: tok_id = self.dequeue() [l, c] = tok_id.get_line_col() val = Identifier( tok.val , l, c, self.debug ) ptok = self.peek() self.dbg_msg(u"factor: "+unicode(ptok) + u" / "+ unicode(tok) ) if ( ptok.kind == EzhilToken.LPAREN ): ## function call [l, c] = ptok.get_line_col() vallist = self.valuelist() val=ExprCall( val, vallist, l, c, self.debug ) elif ( ptok.kind == EzhilToken.LSQRBRACE ): ## indexing a array type variable or ID [l,c] = ptok.get_line_col() ## replace with a call to __getitem__ exp = self.factor(); if ( hasattr(exp,'__getitem__') ): VL2 = ValueList([val,exp[0]],l,c,self.debug) else: # when exp is a expression VL2 = ValueList([val,exp],l,c,self.debug) val = ExprCall( Identifier("__getitem__",l,c), VL2,l,c,self.debug) for itr in range(1,len(exp)): VL2 = ValueList([val,exp[itr]],l,c,self.debug) val = ExprCall( Identifier("__getitem__",l,c), VL2,l,c,self.debug) #raise ParseException("array indexing implemented"+unicode(ptok)); elif ( ptok.kind == EzhilToken.LCURLBRACE ): val=None raise ParseException("dictionary indexing implemented"+unicode(ptok)); elif tok.kind == EzhilToken.STRING : str_tok = self.dequeue() [l,c] = str_tok.get_line_col() val = String( tok.val , l, c, self.debug ) elif tok.kind in EzhilToken.ADDSUB: unop = self.dequeue(); [l, c] = unop.get_line_col() val=Expr(Number(0),unop,self.term(),l,c,self.debug); elif tok.kind == EzhilToken.LCURLBRACE: # creating a list/dictionary expression dict_start = self.dequeue(); val = Dict() while( True ): if ( self.peek().kind == EzhilToken.RCURLBRACE ): break; exprkey = self.expr() tok_colon = self.match(EzhilToken.COLON) exprval = self.expr() val.update( {exprkey : exprval} ) if self.debug : print(self.peek().__class__,self.peek()) if ( self.peek().kind == EzhilToken.RCURLBRACE ): break else: assert( self.peek().kind == EzhilToken.COMMA) self.dequeue() assert( self.peek().kind == EzhilToken.RCURLBRACE ) list_end = self.dequeue() elif tok.kind == EzhilToken.LSQRBRACE: # creating a list/array expression list_start = self.dequeue(); val = Array() while( True ): if ( self.peek().kind == EzhilToken.RSQRBRACE ): break; exprval = self.expr() val.append( exprval ) if self.debug : print(self.peek().__class__,self.peek()) if ( self.peek().kind == EzhilToken.RSQRBRACE ): break else: assert( self.peek().kind == EzhilToken.COMMA) self.dequeue() assert( self.peek().kind == EzhilToken.RSQRBRACE ) list_end = self.dequeue() else: raise ParseException("Expected Number, found something "+unicode(tok)) self.dbg_msg( u"factor-returning: "+unicode(val) ) return val
def factor(self): self.dbg_msg("factor") tok = self.peek() if tok.kind == EzhilToken.LPAREN: lparen_tok = self.dequeue() val = self.expr() if self.dequeue().kind != EzhilToken.RPAREN: raise SyntaxError("Missing Parens " + str(self.last_token())) elif tok.kind == EzhilToken.NUMBER: tok_num = self.dequeue() [l, c] = tok_num.get_line_col() val = Number(tok.val, l, c, self.debug) elif tok.kind == EzhilToken.LOGICAL_NOT: tok_not = self.dequeue() [l, c] = tok_not.get_line_col() val = UnaryExpr(self.expr(), tok_not, l, c, self.debug) self.dbg_msg("completed parsing unary expression" + str(val)) elif tok.kind == EzhilToken.ID: tok_id = self.dequeue() [l, c] = tok_id.get_line_col() val = Identifier(tok.val, l, c, self.debug) ptok = self.peek() self.dbg_msg("factor: " + str(ptok) + " / " + str(tok)) if (ptok.kind == EzhilToken.LPAREN): ## function call [l, c] = ptok.get_line_col() vallist = self.valuelist() val = ExprCall(val, vallist, l, c, self.debug) elif (ptok.kind == EzhilToken.LSQRBRACE): ## indexing a array type variable or ID val = None raise ParseException("arrays not implemented" + str(ptok)) elif tok.kind == EzhilToken.STRING: str_tok = self.dequeue() [l, c] = str_tok.get_line_col() val = String(tok.val, l, c, self.debug) elif tok.kind in EzhilToken.ADDSUB: unop = self.dequeue() [l, c] = unop.get_line_col() val = Expr(Number(0), unop, self.term(), l, c, self.debug) elif tok.kind == EzhilToken.LSQRBRACE: # creating a list/array expression list_start = self.dequeue() val = Array() while (True): exprval = self.expr() val.append(exprval) if self.debug: print(self.peek().__class__, self.peek()) if (self.peek().kind == EzhilToken.RSQRBRACE): break else: assert (self.peek().kind == EzhilToken.COMMA) self.dequeue() assert (self.peek().kind == EzhilToken.RSQRBRACE) list_end = self.dequeue() else: raise ParseException("Expected Number, found something " + str(tok)) self.dbg_msg("factor-returning: " + str(val)) return val
def factor(self): self.dbg_msg("factor") tok = self.peek() if tok.kind == EzhilToken.LPAREN: lparen_tok = self.dequeue() val = self.expr() if self.dequeue().kind != EzhilToken.RPAREN: raise SyntaxError("Missing Parens " + str(self.last_token())) elif tok.kind == EzhilToken.NUMBER: tok_num = self.dequeue() [l, c] = tok_num.get_line_col() val = Number(tok.val, l, c, self.debug) elif tok.kind == EzhilToken.LOGICAL_NOT: tok_not = self.dequeue() [l, c] = tok_not.get_line_col() val = UnaryExpr(self.expr(), tok_not, l, c, self.debug) self.dbg_msg("completed parsing unary expression" + str(val)) elif tok.kind == EzhilToken.ID: tok_id = self.dequeue() [l, c] = tok_id.get_line_col() val = Identifier(tok.val, l, c, self.debug) ptok = self.peek() self.dbg_msg("factor: " + str(ptok) + " / " + str(tok)) if (ptok.kind == EzhilToken.LPAREN): ## function call [l, c] = ptok.get_line_col() vallist = self.valuelist() val = ExprCall(val, vallist, l, c, self.debug) elif (ptok.kind == EzhilToken.LSQRBRACE): ## indexing a array type variable or ID [l, c] = ptok.get_line_col() ## replace with a call to __getitem__ exp = self.factor() if (hasattr(exp, '__getitem__')): VL2 = ValueList([val, exp[0]], l, c, self.debug) else: # when exp is a expression VL2 = ValueList([val, exp], l, c, self.debug) val = ExprCall(Identifier("__getitem__", l, c), VL2, l, c, self.debug) for itr in range(1, len(exp)): VL2 = ValueList([val, exp[itr]], l, c, self.debug) val = ExprCall(Identifier("__getitem__", l, c), VL2, l, c, self.debug) #raise ParseException("array indexing implemented"+str(ptok)); elif (ptok.kind == EzhilToken.LCURLBRACE): val = None raise ParseException("dictionary indexing implemented" + str(ptok)) elif tok.kind == EzhilToken.STRING: str_tok = self.dequeue() [l, c] = str_tok.get_line_col() val = String(tok.val, l, c, self.debug) elif tok.kind in EzhilToken.ADDSUB: unop = self.dequeue() [l, c] = unop.get_line_col() val = Expr(Number(0), unop, self.term(), l, c, self.debug) elif tok.kind == EzhilToken.LCURLBRACE: # creating a list/dictionary expression dict_start = self.dequeue() val = Dict() while (True): if (self.peek().kind == EzhilToken.RCURLBRACE): break exprkey = self.expr() tok_colon = self.match(EzhilToken.COLON) exprval = self.expr() val.update({exprkey: exprval}) if self.debug: print(self.peek().__class__, self.peek()) if (self.peek().kind == EzhilToken.RCURLBRACE): break else: assert (self.peek().kind == EzhilToken.COMMA) self.dequeue() assert (self.peek().kind == EzhilToken.RCURLBRACE) list_end = self.dequeue() elif tok.kind == EzhilToken.LSQRBRACE: # creating a list/array expression list_start = self.dequeue() val = Array() while (True): if (self.peek().kind == EzhilToken.RSQRBRACE): break exprval = self.expr() val.append(exprval) if self.debug: print(self.peek().__class__, self.peek()) if (self.peek().kind == EzhilToken.RSQRBRACE): break else: assert (self.peek().kind == EzhilToken.COMMA) self.dequeue() assert (self.peek().kind == EzhilToken.RSQRBRACE) list_end = self.dequeue() else: raise ParseException("Expected Number, found something " + str(tok)) self.dbg_msg("factor-returning: " + str(val)) return val