def parse_func_def_stat(lexer): lexer.get_next_token_of_kind(TokenKind.KW_FUNCTION) fn_exp, has_colon = StatParser.parse_func_name(lexer) fd_exp = ExpParser.parse_func_def_exp(lexer) if has_colon: fd_exp.insert(0, 'self') return lua_stat.AssignStat(fd_exp.line, [fn_exp], [fd_exp])
def parse_while_stat(lexer): lexer.get_next_token_of_kind(TokenKind.KW_WHILE) exp = ExpParser.parse_exp(lexer) lexer.get_next_token_of_kind(TokenKind.KW_DO) block = Parser.parse_block(lexer) lexer.get_next_token_of_kind(TokenKind.KW_END) return lua_stat.WhileStat(exp, block)
def finish_var_list(lexer, var0): var_list = [StatParser.check_var(lexer, var0)] while lexer.look_ahead() == TokenKind.SEP_COMMA: lexer.get_next_token() exp = ExpParser.parse_prefix_exp(lexer) var_list.append(StatParser.check_var(lexer, exp)) return var_list
def finish_for_in_stat(lexer, name): name_list = StatParser.finish_name_list(lexer, name) lexer.get_next_token_of_kind(TokenKind.KW_IN) exp_list = ExpParser.parse_exp_list(lexer) line_of_do, _ = lexer.get_next_token_of_kind(TokenKind.KW_DO) block = Parser.parse_block(lexer) lexer.get_next_token_of_kind(TokenKind.KW_END) return lua_stat.ForInStat(line_of_do, name_list, exp_list, block)
def finish_local_var_decl_stat(lexer): _, name0 = lexer.get_next_identifier() name_list = StatParser.finish_name_list(lexer, name0) exp_list = [] if lexer.look_ahead() == TokenKind.OP_ASSIGN: lexer.get_next_token() exp_list = ExpParser.parse_exp_list(lexer) last_line = lexer.get_line() return lua_stat.LocalVarDeclStat(last_line, name_list, exp_list)
def finish_for_num_stat(lexer, line_of_for, var_name): lexer.get_next_token_of_kind(TokenKind.OP_ASSIGN) init_exp = ExpParser.parse_exp(lexer) lexer.get_next_token_of_kind(TokenKind.SEP_COMMA) limit_exp = ExpParser.parse_exp(lexer) if lexer.look_ahead() == TokenKind.SEP_COMMA: lexer.get_next_token() step_exp = ExpParser.parse_exp(lexer) else: step_exp = lua_exp.IntegerExp(lexer.get_line(), 1) line_of_do, _ = lexer.get_next_token_of_kind(TokenKind.KW_DO) block = Parser.parse_block(lexer) lexer.get_next_token_of_kind(TokenKind.KW_END) return lua_stat.ForNumStat(line_of_for, line_of_do, var_name, init_exp, limit_exp, step_exp, block)
def parse_if_stat(lexer): exps = [] blocks = [] lexer.get_next_token_of_kind(TokenKind.KW_IF) exps.append(ExpParser.parse_exp(lexer)) lexer.get_next_token_of_kind(TokenKind.KW_THEN) blocks.append(Parser.parse_block(lexer)) while lexer.look_ahead() == TokenKind.KW_ELSEIF: lexer.get_next_token() exps.append(ExpParser.parse_exp(lexer)) lexer.get_next_token_of_kind(TokenKind.KW_THEN) blocks.append(Parser.parse_block(lexer)) if lexer.look_ahead() == TokenKind.KW_ELSE: lexer.get_next_token() exps.append(lua_exp.TrueExp(lexer.get_line())) blocks.append(Parser.parse_block(lexer)) lexer.get_next_token_of_kind(TokenKind.KW_END) return lua_stat.IfStat(exps, blocks)
def parse_ret_exps(lexer): if lexer.look_ahead() != TokenKind.KW_RETURN: return None lexer.get_next_token() kind = lexer.look_ahead() if kind in (TokenKind.EOF, TokenKind.KW_END, TokenKind.KW_ELSE, TokenKind.KW_ELSEIF, TokenKind.KW_UNTIL): return [] if kind == TokenKind.SEP_SEMI: lexer.get_next_token() return [] exps = ExpParser.parse_exp_list(lexer) if lexer.look_ahead() == TokenKind.SEP_SEMI: lexer.get_next_token() return exps
def parse_repeat_stat(lexer): lexer.get_next_token_of_kind(TokenKind.KW_REPEAT) block = Parser.parse_block(lexer) lexer.get_next_token_of_kind(TokenKind.KW_UNTIL) exp = ExpParser.parse_exp(lexer) return lua_stat.RepeatStat(block, exp)
def parse_assign_stat(lexer, var0): var_list = StatParser.finish_var_list(lexer, var0) lexer.get_next_token_of_kind(TokenKind.OP_ASSIGN) exp_list = ExpParser.parse_exp_list(lexer) last_line = lexer.get_line() return lua_stat.AssignStat(last_line, var_list, exp_list)
def parse_assign_or_func_call_stat(lexer): prefix_exp = ExpParser.parse_prefix_exp(lexer) if isinstance(prefix_exp, lua_exp.FuncCallExp): return lua_stat.FuncCallStat(prefix_exp) else: return StatParser.parse_assign_stat(lexer, prefix_exp)
def finish_local_func_def_stat(lexer): lexer.get_next_token_of_kind(TokenKind.KW_FUNCTION) _, name = lexer.get_next_identifier() fd_exp = ExpParser.parse_func_def_exp(lexer) return lua_stat.LocalFuncDefStat(name, fd_exp)