lib/minjs/statement.rb in minjs-0.1.3 vs lib/minjs/statement.rb in minjs-0.1.5

- old
+ new

@@ -14,10 +14,12 @@ elsif a == ECMA262::PUNC_RCURLYBRAC lex.rewind_pos a elsif a == ECMA262::LIT_LINE_FEED a + elsif a.nil? + ECMA262::LIT_LINE_FEED elsif a.lt? a else nil end @@ -26,11 +28,10 @@ #12 def statement(lex, context) [:block, :var_statement, - :exp_statement, :if_statement, :iteration_statement, :continue_statement, :break_statement, :return_statement, @@ -38,18 +39,18 @@ :labelled_statement, :switch_statement, :throw_statement, :try_statement, :debugger_statement, + :exp_statement, # # function declaration in statement(block) is not permitted by ECMA262. # however, almost all implementation permit it. # :func_declaration, :empty_statement, ].each do |f| - puts "* checking #{f.to_s}" if @debug t = lex.eval_lit { __send__(f, lex, context) } return t if t end @@ -60,11 +61,11 @@ # block def block(lex, context) pos0 = lex.pos return nil unless lex.match_lit(ECMA262::PUNC_LCURLYBRAC) if lex.match_lit(ECMA262::PUNC_RCURLYBRAC) - return ECMA262::StBlock.new(ECMA262::StList.new([])) + return ECMA262::StBlock.new(ECMA262::StatementList.new([])) end lex.eval_lit { if s = statement_list(lex, context) and lex.match_lit(ECMA262::PUNC_RCURLYBRAC) ECMA262::StBlock.new(s) else @@ -81,11 +82,11 @@ t.push(s) else break end end - ECMA262::StList.new(t) + ECMA262::StatementList.new(t) } end # #12.2 # variable_statement @@ -101,11 +102,10 @@ context.var_env.record.create_mutable_binding(dn, nil) context.var_env.record.set_mutable_binding(dn, :undefined, nil) end ECMA262::StVar.new(context, vl) else - lex.debug_lit raise Minjs::ParseError.new("var_statement", lex) end } end @@ -168,11 +168,15 @@ return false if lex.next_lit == ECMA262::ID_FUNCTION lex.eval_lit{ if a=exp(lex, context, {}) and semicolon(lex, context) ECMA262::StExp.new(a) else - nil + if a + raise ParseError.new("no semicolon at end of expression statement", lex) + else + nil + end end } end # #12.5 @@ -200,21 +204,19 @@ def while_statement(lex, context) return nil unless lex.match_lit(ECMA262::ID_WHILE) if lex.match_lit(ECMA262::PUNC_LPARENTHESIS) and e=exp(lex, context, {}) and lex.match_lit(ECMA262::PUNC_RPARENTHESIS) and s=statement(lex, context) ECMA262::StWhile.new(e, s) else - lex.debug_lit raise ParseError.new("while_statement", lex) end end def do_while_statement(lex, context) return nil unless lex.match_lit(ECMA262::ID_DO) if s=statement(lex, context) and lex.match_lit(ECMA262::ID_WHILE) and lex.match_lit(ECMA262::PUNC_LPARENTHESIS) and e=exp(lex, context, {}) and lex.match_lit(ECMA262::PUNC_RPARENTHESIS) and semicolon(lex, context) ECMA262::StDoWhile.new(e, s) else - lex.debug_lit raise ParseError.new("do_while_statement", lex) end end def for_statement(lex, context) @@ -273,12 +275,18 @@ def continue_statement(lex, context) return nil unless lex.match_lit(ECMA262::ID_CONTINUE) lex.eval_lit { if semicolon(lex, context) ECMA262::StContinue.new - elsif e=exp(lex, context, {}) and semicolon(lex, context) + elsif e=identifier(lex, context) and semicolon(lex, context) ECMA262::StContinue.new(e) + else + if e + raise ParseError.new("no semicolon at end of continue statement", lex) + else + raise ParseError.new("bad continue statement", lex) + end end } end # # 12.8 @@ -286,12 +294,18 @@ def break_statement(lex, context) return nil unless lex.match_lit(ECMA262::ID_BREAK) lex.eval_lit { if semicolon(lex, context) ECMA262::StBreak.new - elsif e=exp(lex, context, {}) and semicolon(lex, context) + elsif e=identifier(lex, context) and semicolon(lex, context) ECMA262::StBreak.new(e) + else + if e + raise ParseError.new("no semicolon at end of break statement", lex) + else + raise ParseError.new("bad break statement", lex) + end end } end # # 12.9 @@ -315,11 +329,10 @@ return nil unless lex.match_lit(ECMA262::ID_WITH) lex.eval_lit { if lex.match_lit(ECMA262::PUNC_LPARENTHESIS) and e=exp(lex, context, {}) and lex.match_lit(ECMA262::PUNC_RPARENTHESIS) and s=statement(lex, context) ECMA262::StWith.new(e, s) else - lex.debug_lit raise ParseError.new("switch_statement", lex) end } end # @@ -329,11 +342,10 @@ return nil unless lex.match_lit(ECMA262::ID_SWITCH) lex.eval_lit { if lex.match_lit(ECMA262::PUNC_LPARENTHESIS) and e=exp(lex, context, {}) and lex.match_lit(ECMA262::PUNC_RPARENTHESIS) and c = case_block(lex, context) ECMA262::StSwitch.new(e, c) else - lex.debug_lit raise ParseError.new("switch_statement", lex) end } end @@ -372,14 +384,19 @@ # 12.13 # def throw_statement(lex, context) return nil unless lex.match_lit(ECMA262::ID_THROW) lex.eval_lit{ - if e=exp(lex, context, {}) and semicolon(lex, context) + if semicolon(lex, context) + raise ParseError.new("no line terminator here", lex) + elsif e=exp(lex, context, {}) and semi = semicolon(lex, context) ECMA262::StThrow.new(e) else - lex.debug_lit - raise ParseError.new("throw_statement", lex) + if e + raise ParseError.new("no semicolon at end of throw statement", lex) + else + raise ParseError.new("bad throw statement", lex) + end end } end # # 12.14