Module: Minjs::Lex::Function

Includes:
Minjs
Included in:
Parser
Defined in:
lib/minjs/lex/function.rb

Overview

Function

Constant Summary

Constants included from Minjs

VERSION

Instance Method Summary collapse

Instance Method Details

#func_declaration(var_env) ⇒ Object

Note:

The function declaration in statement(block) is not permitted by ECMA262. However, almost all implementation permit it, so minjs cannot raise exception even if function declarataion in block.

Tests next literal is FunctionDeclaration or not.

If literal is FunctionDeclaration return ECMA262::StFunc object and forward lexical parser position. Otherwise return nil and position is not changed.

See Also:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/minjs/lex/function.rb', line 19

def func_declaration(var_env)
  # FunctionDeclaration :
  # function Identifier ( FormalParameterListopt ) { FunctionBody }
  return nil if eql_lit?(ECMA262::ID_FUNCTION).nil?

  new_var_env = ECMA262::LexEnv.new(outer: var_env)

  if id=identifier(var_env) and
    eql_lit?(ECMA262::PUNC_LPARENTHESIS) and
    args = formal_parameter_list(new_var_env) and
    eql_lit?(ECMA262::PUNC_RPARENTHESIS) and
    eql_lit?(ECMA262::PUNC_LCURLYBRAC) and
    b=func_body(new_var_env) and eql_lit?(ECMA262::PUNC_RCURLYBRAC)
    f = ECMA262::StFunc.new(new_var_env, id, args, b, {:decl => true})

    var_env.record.create_mutable_binding(id, nil)
    var_env.record.set_mutable_binding(id, f, nil)
    f
  else
    if b
      raise ParseError.new("No `}' at end of function", self)
    else
      raise ParseError.new("Bad function declaration", self)
    end
  end
end

#func_exp(var_env) ⇒ Object

Note:

The function expression and declaration uses same class for convenience.

Tests next literal is FunctionExpression or not.

If literal is FunctionExpression return ECMA262::StFunc object and forward lexical parser position. Otherwise return nil and position is not changed.

See Also:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/minjs/lex/function.rb', line 59

def func_exp(var_env)
  # FunctionExpression :
  # function Identifieropt ( FormalParameterListopt ) { FunctionBody }
  return nil if eql_lit?(ECMA262::ID_FUNCTION).nil?
  @logger.debug "*** func_exp"

  id_opt = identifier(var_env)
  new_var_env = ECMA262::LexEnv.new(outer: var_env)

  if eql_lit?(ECMA262::PUNC_LPARENTHESIS) and
    args = formal_parameter_list(new_var_env) and
    eql_lit?(ECMA262::PUNC_RPARENTHESIS) and
    eql_lit?(ECMA262::PUNC_LCURLYBRAC) and
    b = func_body(new_var_env) and eql_lit?(ECMA262::PUNC_RCURLYBRAC)
    f = ECMA262::StFunc.new(new_var_env, id_opt, args, b)
    #new_var_env.func = f
    if id_opt
      var_env.record.create_mutable_binding(id_opt, nil)
      var_env.record.set_mutable_binding(id_opt, f, nil)
    end
    f
  else
    if b
      raise ParseError.new("No `}' at end of function", self)
    else
      raise ParseError.new("Bad function expression", self)
    end
  end
end