def parse_exp0(lexer): kind = lexer.look_ahead() if kind == TokenKind.VARARG: line, _, _ = lexer.get_next_token() return lua_exp.VarArgExp(line) if kind == TokenKind.KW_NIL: line, _, _ = lexer.get_next_token() return lua_exp.NilExp(line) if kind == TokenKind.KW_TRUE: line, _, _ = lexer.get_next_token() return lua_exp.TrueExp(line) if kind == TokenKind.KW_FALSE: line, _, _ = lexer.get_next_token() return lua_exp.FalseExp(line) if kind == TokenKind.STRING: line, _, token = lexer.get_next_token() return lua_exp.StringExp(line, token) if kind == TokenKind.NUMBER: return ExpParser.parse_number_exp(lexer) if kind == TokenKind.SEP_LCURLY: return ExpParser.parse_table_constructor_exp(lexer) if kind == TokenKind.KW_FUNCTION: lexer.get_next_token() return ExpParser.parse_func_def_exp(lexer) return ExpParser.parse_prefix_exp(lexer)
def parse_func_name(lexer): line, name = lexer.get_next_identifier() exp = lua_exp.NameExp(line, name) has_colon = False while lexer.look_ahead() == TokenKind.SEP_DOT: lexer.get_next_token() line, name = lexer.get_next_identifier() idx = lua_exp.StringExp(line, name) exp = lua_exp.TableAccessExp(line, exp, idx) if lexer.look_ahead() == TokenKind.SEP_COLON: lexer.get_next_token() line, name = lexer.get_next_identifier() idx = lua_exp.StringExp(line, name) exp = lua_exp.TableAccessExp(line, exp, idx) has_colon = True return exp, has_colon
def parse_args(lexer): args = [] kind = lexer.look_ahead() if kind == TokenKind.SEP_LPAREN: # '(' [explist] ')' lexer.get_next_token() if lexer.look_ahead() != TokenKind.SEP_RPAREN: args = ExpParser.parse_exp_list(lexer) lexer.get_next_token_of_kind(TokenKind.SEP_RPAREN) elif kind == TokenKind.SEP_LCURLY: # '{' [fieldlist] '}' args = [ExpParser.parse_table_constructor_exp(lexer)] else: # LiteralString line, s = lexer.get_next_token_of_kind(TokenKind.STRING) args = [lua_exp.StringExp(line, s)] return args
def finish_prefix_exp(lexer, exp): while True: kind = lexer.look_ahead() if kind == TokenKind.SEP_LBRACK: lexer.get_next_token() key_exp = ExpParser.parse_exp(lexer) lexer.get_next_token_of_kind(TokenKind.SEP_RBRACK) exp = lua_exp.TableAccessExp(lexer.get_line(), exp, key_exp) elif kind == TokenKind.SEP_DOT: lexer.get_next_token() line, name = lexer.get_next_identifier() key_exp = lua_exp.StringExp(line, name) exp = lua_exp.TableAccessExp(line, exp, key_exp) elif kind in (TokenKind.SEP_COLON, TokenKind.SEP_LPAREN, TokenKind.SEP_LCURLY, TokenKind.STRING): exp = ExpParser.finish_func_call_exp(lexer, exp) return exp
def parse_field(lexer): if lexer.look_ahead() == TokenKind.SEP_LBRACK: lexer.get_next_token() k = ExpParser.parse_exp(lexer) lexer.get_next_token_of_kind(TokenKind.SEP_RBRACK) lexer.get_next_token_of_kind(TokenKind.OP_ASSIGN) v = ExpParser.parse_exp(lexer) return k, v exp = ExpParser.parse_exp(lexer) if isinstance(exp, lua_exp.NameExp): if lexer.look_ahead() == TokenKind.OP_ASSIGN: lexer.get_next_token() k = lua_exp.StringExp(exp.line, exp.name) v = ExpParser.parse_exp(lexer) return k, v return None, exp
def parse_name_exp(lexer): if lexer.look_ahead() == TokenKind.SEP_COLON: lexer.get_next_token() line, name = lexer.get_next_identifier() return lua_exp.StringExp(line, name) return None