Object
If set to true, `__FILE__` and `__LINE__` are transformed to literal nodes. For example, `s(:str, "lib/foo.rb")` and `s(:int, 10)`.
If set to false, `__FILE__` and `__LINE__` are emitted as-is, i.e. as `s(:__FILE__)` and `s(:__LINE__)` nodes.
Source maps are identical in both cases.
@return [Boolean]
# File lib/parser/builders/default.rb, line 370 def __ENCODING__(__ENCODING__t) n0(:__ENCODING__, token_map(__ENCODING__t)) end
# File lib/parser/builders/default.rb, line 125 def __FILE__(__FILE__t) n0(:__FILE__, token_map(__FILE__t)) end
# File lib/parser/builders/default.rb, line 89 def __LINE__(__LINE__t) n0(:__LINE__, token_map(__LINE__t)) end
# File lib/parser/builders/default.rb, line 315 def accessible(node) case node.type when :__FILE__ if @emit_file_line_as_literals n(:str, [ node.loc.expression.source_buffer.name ], node.loc) else node end when :__LINE__ if @emit_file_line_as_literals n(:int, [ node.loc.expression.line ], node.loc) else node end when :__ENCODING__ n(:const, [ n(:const, [ nil, :Encoding], nil), :UTF_8 ], node.loc) when :ident name, = *node if @parser.static_env.declared?(name) node.updated(:lvar) else name, = *node n(:send, [ nil, name ], var_send_map(node)) end else node end end
# File lib/parser/builders/default.rb, line 508 def alias(alias_t, to, from) n(:alias, [ to, from ], keyword_map(alias_t, nil, [to, from], nil)) end
# File lib/parser/builders/default.rb, line 523 def arg(name_t) n(:arg, [ value(name_t).to_sym ], variable_map(name_t)) end
Ruby 1.8 block arguments
# File lib/parser/builders/default.rb, line 577 def arg_expr(expr) if expr.type == :lvasgn expr.updated(:arg) else n(:arg_expr, [ expr ], expr.loc) end end
Formal arguments
# File lib/parser/builders/default.rb, line 517 def args(begin_t, args, end_t, check_args=true) args = check_duplicate_args(args) if check_args n(:args, args, collection_map(begin_t, args, end_t)) end
Arrays
# File lib/parser/builders/default.rb, line 181 def array(begin_t, elements, end_t) n(:array, elements, collection_map(begin_t, elements, end_t)) end
# File lib/parser/builders/default.rb, line 416 def assign(lhs, eql_t, rhs) (lhs << rhs).updated(nil, nil, :location => lhs.loc. with_operator(loc(eql_t)). with_expression(join_exprs(lhs, rhs))) end
Assignment
# File lib/parser/builders/default.rb, line 379 def assignable(node) case node.type when :cvar node.updated(:cvasgn) when :ivar node.updated(:ivasgn) when :gvar node.updated(:gvasgn) when :const if @parser.in_def? diagnostic :error, :dynamic_const, nil, node.loc.expression end node.updated(:casgn) when :ident name, = *node @parser.static_env.declare(name) node.updated(:lvasgn) when :nil, :self, :true, :false, :__FILE__, :__LINE__, :__ENCODING__ diagnostic :error, :invalid_assignment, nil, node.loc.expression when :back_ref, :nth_ref diagnostic :error, :backref_assignment, nil, node.loc.expression end end
# File lib/parser/builders/default.rb, line 259 def associate(begin_t, pairs, end_t) n(:hash, [ *pairs ], collection_map(begin_t, pairs, end_t)) end
# File lib/parser/builders/default.rb, line 659 def attr_asgn(receiver, dot_t, selector_t) method_name = (value(selector_t) + '=').to_sym # Incomplete method call. n(:send, [ receiver, method_name ], send_map(receiver, dot_t, selector_t)) end
# File lib/parser/builders/default.rb, line 305 def back_ref(token) n(:back_ref, [ value(token).to_sym ], token_map(token)) end
# File lib/parser/builders/default.rb, line 911 def begin(begin_t, body, end_t) if body.nil? # A nil expression: `()'. n0(:begin, collection_map(begin_t, nil, end_t)) elsif body.type == :mlhs || (body.type == :begin && body.loc.begin.nil? && body.loc.end.nil?) # Synthesized (begin) from compstmt "a; b" or (mlhs) # from multi_lhs "(a, b) = *foo". n(body.type, body.children, collection_map(begin_t, body.children, end_t)) else n(:begin, [ body ], collection_map(begin_t, [ body ], end_t)) end end
# File lib/parser/builders/default.rb, line 868 def begin_body(compound_stmt, rescue_bodies=[], else_t=nil, else_=nil, ensure_t=nil, ensure_=nil) if rescue_bodies.any? if else_t compound_stmt = n(:rescue, [ compound_stmt, *(rescue_bodies + [ else_ ]) ], eh_keyword_map(compound_stmt, nil, rescue_bodies, else_t, else_)) else compound_stmt = n(:rescue, [ compound_stmt, *(rescue_bodies + [ nil ]) ], eh_keyword_map(compound_stmt, nil, rescue_bodies, nil, nil)) end end if ensure_t compound_stmt = n(:ensure, [ compound_stmt, ensure_ ], eh_keyword_map(compound_stmt, ensure_t, [ ensure_ ], nil, nil)) end compound_stmt end
# File lib/parser/builders/default.rb, line 929 def begin_keyword(begin_t, body, end_t) if body.nil? # A nil expression: `begin end'. n0(:kwbegin, collection_map(begin_t, nil, end_t)) elsif (body.type == :begin && body.loc.begin.nil? && body.loc.end.nil?) # Synthesized (begin) from compstmt "a; b". n(:kwbegin, body.children, collection_map(begin_t, body.children, end_t)) else n(:kwbegin, [ body ], collection_map(begin_t, [ body ], end_t)) end end
# File lib/parser/builders/default.rb, line 678 def binary_op(receiver, operator_t, arg) source_map = send_binary_op_map(receiver, operator_t, arg) if @parser.version == 18 operator = value(operator_t) if operator == '!=' method_call = n(:send, [ receiver, :==, arg ], source_map) elsif operator == '!~' method_call = n(:send, [ receiver, :=~, arg ], source_map) end if %(!= !~).include?(operator) return n(:not, [ method_call ], expr_map(source_map.expression)) end end n(:send, [ receiver, value(operator_t).to_sym, arg ], source_map) end
# File lib/parser/builders/default.rb, line 626 def block(method_call, begin_t, args, body, end_t) _receiver, _selector, *call_args = *method_call if method_call.type == :yield diagnostic :error, :block_given_to_yield, nil, method_call.loc.keyword, [loc(begin_t)] end last_arg = call_args.last if last_arg && last_arg.type == :block_pass diagnostic :error, :block_and_blockarg, nil, last_arg.loc.expression, [loc(begin_t)] end if [:send, :super, :zsuper].include?(method_call.type) n(:block, [ method_call, args, body ], block_map(method_call.loc.expression, begin_t, end_t)) else # Code like "return foo 1 do end" is reduced in a weird sequence. # Here, method_call is actually (return). actual_send, = *method_call block = n(:block, [ actual_send, args, body ], block_map(actual_send.loc.expression, begin_t, end_t)) n(method_call.type, [ block ], method_call.loc.with_expression(join_exprs(method_call, block))) end end
# File lib/parser/builders/default.rb, line 654 def block_pass(amper_t, arg) n(:block_pass, [ arg ], unary_op_map(amper_t, arg)) end
# File lib/parser/builders/default.rb, line 570 def blockarg(amper_t, name_t) n(:blockarg, [ value(name_t).to_sym ], arg_prefix_map(amper_t, name_t)) end
# File lib/parser/builders/default.rb, line 597 def blockarg_expr(amper_t, expr) if expr.type == :lvasgn expr.updated(:blockarg) else n(:blockarg_expr, [ expr ], expr.loc) end end
# File lib/parser/builders/default.rb, line 621 def call_lambda(lambda_t) n(:send, [ nil, :lambda ], send_map(nil, nil, lambda_t)) end
Method calls
# File lib/parser/builders/default.rb, line 610 def call_method(receiver, dot_t, selector_t, lparen_t=nil, args=[], rparen_t=nil) if selector_t.nil? n(:send, [ receiver, :call, *args ], send_map(receiver, dot_t, nil, lparen_t, args, rparen_t)) else n(:send, [ receiver, value(selector_t).to_sym, *args ], send_map(receiver, dot_t, selector_t, lparen_t, args, rparen_t)) end end
# File lib/parser/builders/default.rb, line 805 def case(case_t, expr, when_bodies, else_t, else_body, end_t) n(:case, [ expr, *(when_bodies << else_body)], condition_map(case_t, expr, nil, nil, else_t, else_body, end_t)) end
# File lib/parser/builders/default.rb, line 120 def character(char_t) n(:str, [ value(char_t) ], prefix_string_map(char_t)) end
# File lib/parser/builders/default.rb, line 68 def complex(complex_t) numeric(:complex, complex_t) end
Expression grouping
# File lib/parser/builders/default.rb, line 899 def compstmt(statements) case when statements.none? nil when statements.one? statements.first else n(:begin, statements, collection_map(nil, statements, nil)) end end
Conditionals
# File lib/parser/builders/default.rb, line 781 def condition(cond_t, cond, then_t, if_true, else_t, if_false, end_t) n(:if, [ check_condition(cond), if_true, if_false ], condition_map(cond_t, cond, then_t, if_true, else_t, if_false, end_t)) end
# File lib/parser/builders/default.rb, line 787 def condition_mod(if_true, if_false, cond_t, cond) n(:if, [ check_condition(cond), if_true, if_false ], keyword_mod_map(if_true || if_false, cond_t, cond)) end
# File lib/parser/builders/default.rb, line 353 def const(name_t) n(:const, [ nil, value(name_t).to_sym ], constant_map(nil, nil, name_t)) end
# File lib/parser/builders/default.rb, line 365 def const_fetch(scope, t_colon2, name_t) n(:const, [ scope, value(name_t).to_sym ], constant_map(scope, t_colon2, name_t)) end
# File lib/parser/builders/default.rb, line 358 def const_global(t_colon3, name_t) cbase = n0(:cbase, token_map(t_colon3)) n(:const, [ cbase, value(name_t).to_sym ], constant_map(cbase, t_colon3, name_t)) end
# File lib/parser/builders/default.rb, line 412 def const_op_assignable(node) node.updated(:casgn) end
# File lib/parser/builders/default.rb, line 300 def cvar(token) n(:cvar, [ value(token).to_sym ], variable_map(token)) end
Class and module definition
# File lib/parser/builders/default.rb, line 459 def def_class(class_t, name, lt_t, superclass, body, end_t) n(:class, [ name, superclass, body ], module_definition_map(class_t, name, lt_t, end_t)) end
Method (un)definition
# File lib/parser/builders/default.rb, line 482 def def_method(def_t, name_t, args, body, end_t) n(:def, [ value(name_t).to_sym, args, body ], definition_map(def_t, nil, name_t, end_t)) end
# File lib/parser/builders/default.rb, line 472 def def_module(module_t, name, body, end_t) n(:module, [ name, body ], module_definition_map(module_t, name, nil, end_t)) end
# File lib/parser/builders/default.rb, line 466 def def_sclass(class_t, lshft_t, expr, body, end_t) n(:sclass, [ expr, body ], module_definition_map(class_t, nil, lshft_t, end_t)) end
# File lib/parser/builders/default.rb, line 488 def def_singleton(def_t, definee, dot_t, name_t, args, body, end_t) case definee.type when :int, :str, :dstr, :sym, :dsym, :regexp, :array, :hash diagnostic :error, :singleton_literal, nil, definee.loc.expression else n(:defs, [ definee, value(name_t).to_sym, args, body ], definition_map(def_t, dot_t, name_t, end_t)) end end
# File lib/parser/builders/default.rb, line 49 def false(false_t) n0(:false, token_map(false_t)) end
# File lib/parser/builders/default.rb, line 60 def float(float_t) numeric(:float, float_t) end
# File lib/parser/builders/default.rb, line 826 def for(for_t, iterator, in_t, iteratee, do_t, body, end_t) n(:for, [ iterator, iteratee, body ], for_map(for_t, in_t, do_t, end_t)) end
# File lib/parser/builders/default.rb, line 295 def gvar(token) n(:gvar, [ value(token).to_sym ], variable_map(token)) end
# File lib/parser/builders/default.rb, line 285 def ident(token) n(:ident, [ value(token).to_sym ], variable_map(token)) end
# File lib/parser/builders/default.rb, line 667 def index(receiver, lbrack_t, indexes, rbrack_t) n(:send, [ receiver, :[], *indexes ], send_index_map(receiver, lbrack_t, rbrack_t)) end
# File lib/parser/builders/default.rb, line 672 def index_asgn(receiver, lbrack_t, indexes, rbrack_t) # Incomplete method call. n(:send, [ receiver, :[]=, *indexes ], send_index_map(receiver, lbrack_t, rbrack_t)) end
Numerics
# File lib/parser/builders/default.rb, line 56 def integer(integer_t) numeric(:int, integer_t) end
# File lib/parser/builders/default.rb, line 290 def ivar(token) n(:ivar, [ value(token).to_sym ], variable_map(token)) end
Keywords
# File lib/parser/builders/default.rb, line 834 def keyword_cmd(type, keyword_t, lparen_t=nil, args=[], rparen_t=nil) if type == :yield && args.count > 0 last_arg = args.last if last_arg.type == :block_pass diagnostic :error, :block_given_to_yield, nil, loc(keyword_t), [last_arg.loc.expression] end end n(type, args, keyword_map(keyword_t, lparen_t, args, rparen_t)) end
# File lib/parser/builders/default.rb, line 545 def kwarg(name_t) n(:kwarg, [ value(name_t).to_sym ], kwarg_map(name_t)) end
# File lib/parser/builders/default.rb, line 550 def kwoptarg(name_t, value) n(:kwoptarg, [ value(name_t).to_sym, value ], kwarg_map(name_t, value)) end
# File lib/parser/builders/default.rb, line 555 def kwrestarg(dstar_t, name_t=nil) if name_t n(:kwrestarg, [ value(name_t).to_sym ], arg_prefix_map(dstar_t, name_t)) else n0(:kwrestarg, arg_prefix_map(dstar_t)) end end
# File lib/parser/builders/default.rb, line 254 def kwsplat(dstar_t, arg) n(:kwsplat, [ arg ], unary_op_map(dstar_t, arg)) end
Logical operations: and, or
# File lib/parser/builders/default.rb, line 774 def logical_op(type, lhs, op_t, rhs) n(type, [ lhs, rhs ], binary_op_map(lhs, op_t, rhs)) end
Loops
# File lib/parser/builders/default.rb, line 812 def loop(type, keyword_t, cond, do_t, body, end_t) n(type, [ check_condition(cond), body ], keyword_map(keyword_t, do_t, nil, end_t)) end
# File lib/parser/builders/default.rb, line 817 def loop_mod(type, body, keyword_t, cond) if body.type == :kwbegin type = :"#{type}_post" end n(type, [ check_condition(cond), body ], keyword_mod_map(body, keyword_t, cond)) end
# File lib/parser/builders/default.rb, line 700 def match_op(receiver, match_t, arg) source_map = send_binary_op_map(receiver, match_t, arg) if receiver.type == :regexp && receiver.children.count == 2 && receiver.children.first.type == :str str_node, opt_node = *receiver regexp_body, = *str_node *regexp_opt = *opt_node if defined?(Encoding) regexp_body = case when regexp_opt.include?(:u) regexp_body.encode(Encoding::UTF_8) when regexp_opt.include?(:e) regexp_body.encode(Encoding::EUC_JP) when regexp_opt.include?(:s) regexp_body.encode(Encoding::WINDOWS_31J) when regexp_opt.include?(:n) regexp_body.encode(Encoding::BINARY) else regexp_body end end Regexp.new(regexp_body).names.each do |name| @parser.static_env.declare(name) end n(:match_with_lvasgn, [ receiver, arg ], source_map) else n(:send, [ receiver, :=~, arg ], source_map) end end
# File lib/parser/builders/default.rb, line 450 def multi_assign(lhs, eql_t, rhs) n(:masgn, [ lhs, rhs ], binary_op_map(lhs, eql_t, rhs)) end
# File lib/parser/builders/default.rb, line 445 def multi_lhs(begin_t, items, end_t) n(:mlhs, [ *items ], collection_map(begin_t, items, end_t)) end
# File lib/parser/builders/default.rb, line 78 def negate(uminus_t, numeric) value, = *numeric operator_loc = loc(uminus_t) numeric.updated(nil, [ -value ], :location => Source::Map::Operator.new( operator_loc, operator_loc.join(numeric.loc.expression))) end
Singletons
# File lib/parser/builders/default.rb, line 39 def nil(nil_t) n0(:nil, token_map(nil_t)) end
# File lib/parser/builders/default.rb, line 750 def not_op(not_t, begin_t=nil, receiver=nil, end_t=nil) if @parser.version == 18 n(:not, [ receiver ], unary_op_map(not_t, receiver)) else if receiver.nil? nil_node = n0(:begin, collection_map(begin_t, nil, end_t)) n(:send, [ nil_node, :'!' ], send_unary_op_map(not_t, nil_node)) else n(:send, [ receiver, :'!' ], send_unary_op_map(not_t, receiver)) end end end
# File lib/parser/builders/default.rb, line 310 def nth_ref(token) n(:nth_ref, [ value(token) ], token_map(token)) end
# File lib/parser/builders/default.rb, line 423 def op_assign(lhs, op_t, rhs) case lhs.type when :gvasgn, :ivasgn, :lvasgn, :cvasgn, :casgn, :send operator = value(op_t)[0..-1].to_sym source_map = lhs.loc. with_operator(loc(op_t)). with_expression(join_exprs(lhs, rhs)) case operator when :'&&' n(:and_asgn, [ lhs, rhs ], source_map) when :'||' n(:or_asgn, [ lhs, rhs ], source_map) else n(:op_asgn, [ lhs, operator, rhs ], source_map) end when :back_ref, :nth_ref diagnostic :error, :backref_assignment, nil, lhs.loc.expression end end
# File lib/parser/builders/default.rb, line 528 def optarg(name_t, eql_t, value) n(:optarg, [ value(name_t).to_sym, value ], variable_map(name_t). with_operator(loc(eql_t)). with_expression(loc(name_t).join(value.loc.expression))) end
Hashes
# File lib/parser/builders/default.rb, line 229 def pair(key, assoc_t, value) n(:pair, [ key, value ], binary_op_map(key, assoc_t, value)) end
# File lib/parser/builders/default.rb, line 246 def pair_keyword(key_t, value) key_map, pair_map = pair_keyword_map(key_t, value) key = n(:sym, [ value(key_t).to_sym ], key_map) n(:pair, [ key, value ], pair_map) end
# File lib/parser/builders/default.rb, line 234 def pair_list_18(list) if list.size % 2 != 0 diagnostic :error, :odd_hash, nil, list.last.loc.expression else list. each_slice(2).map do |key, value| n(:pair, [ key, value ], binary_op_map(key, nil, value)) end end end
# File lib/parser/builders/default.rb, line 853 def postexe(postexe_t, lbrace_t, compstmt, rbrace_t) n(:postexe, [ compstmt ], keyword_map(postexe_t, lbrace_t, [], rbrace_t)) end
BEGIN, END
# File lib/parser/builders/default.rb, line 848 def preexe(preexe_t, lbrace_t, compstmt, rbrace_t) n(:preexe, [ compstmt ], keyword_map(preexe_t, lbrace_t, [], rbrace_t)) end
# File lib/parser/builders/default.rb, line 271 def range_exclusive(lhs, dot3_t, rhs) n(:erange, [ lhs, rhs ], binary_op_map(lhs, dot3_t, rhs)) end
Ranges
# File lib/parser/builders/default.rb, line 266 def range_inclusive(lhs, dot2_t, rhs) n(:irange, [ lhs, rhs ], binary_op_map(lhs, dot2_t, rhs)) end
# File lib/parser/builders/default.rb, line 64 def rational(rational_t) numeric(:rational, rational_t) end
# File lib/parser/builders/default.rb, line 174 def regexp_compose(begin_t, parts, end_t, options) n(:regexp, (parts << options), regexp_map(begin_t, end_t, options)) end
Regular expressions
# File lib/parser/builders/default.rb, line 165 def regexp_options(regopt_t) options = value(regopt_t). each_char.sort.uniq. map(&:to_sym) n(:regopt, options, token_map(regopt_t)) end
Exception handling
# File lib/parser/builders/default.rb, line 860 def rescue_body(rescue_t, exc_list, assoc_t, exc_var, then_t, compound_stmt) n(:resbody, [ exc_list, exc_var, compound_stmt ], rescue_body_map(rescue_t, exc_list, assoc_t, exc_var, then_t, compound_stmt)) end
# File lib/parser/builders/default.rb, line 535 def restarg(star_t, name_t=nil) if name_t n(:restarg, [ value(name_t).to_sym ], arg_prefix_map(star_t, name_t)) else n0(:restarg, arg_prefix_map(star_t)) end end
# File lib/parser/builders/default.rb, line 586 def restarg_expr(star_t, expr=nil) if expr.nil? n0(:restarg, token_map(star_t)) elsif expr.type == :lvasgn expr.updated(:restarg) else n(:restarg_expr, [ expr ], expr.loc) end end
Access
# File lib/parser/builders/default.rb, line 280 def self(token) n0(:self, token_map(token)) end
# File lib/parser/builders/default.rb, line 565 def shadowarg(name_t) n(:shadowarg, [ value(name_t).to_sym ], variable_map(name_t)) end
# File lib/parser/builders/default.rb, line 186 def splat(star_t, arg=nil) if arg.nil? n0(:splat, unary_op_map(star_t)) else n(:splat, [ arg ], unary_op_map(star_t, arg)) end end
Strings
# File lib/parser/builders/default.rb, line 96 def string(string_t) n(:str, [ value(string_t) ], delimited_string_map(string_t)) end
# File lib/parser/builders/default.rb, line 106 def string_compose(begin_t, parts, end_t) if collapse_string_parts?(parts) if begin_t.nil? && end_t.nil? parts.first else n(:str, parts.first.children, string_map(begin_t, parts, end_t)) end else n(:dstr, [ *parts ], string_map(begin_t, parts, end_t)) end end
# File lib/parser/builders/default.rb, line 101 def string_internal(string_t) n(:str, [ value(string_t) ], unquoted_map(string_t)) end
Symbols
# File lib/parser/builders/default.rb, line 132 def symbol(symbol_t) n(:sym, [ value(symbol_t).to_sym ], prefix_string_map(symbol_t)) end
# File lib/parser/builders/default.rb, line 142 def symbol_compose(begin_t, parts, end_t) if collapse_string_parts?(parts) str = parts.first n(:sym, [ str.children.first.to_sym ], collection_map(begin_t, str.loc.expression, end_t)) elsif @parser.version == 18 && parts.empty? diagnostic :error, :empty_symbol, nil, loc(begin_t).join(loc(end_t)) else n(:dsym, [ *parts ], collection_map(begin_t, parts, end_t)) end end
# File lib/parser/builders/default.rb, line 137 def symbol_internal(symbol_t) n(:sym, [ value(symbol_t).to_sym ], unquoted_map(symbol_t)) end
# File lib/parser/builders/default.rb, line 210 def symbols_compose(begin_t, parts, end_t) parts = parts.map do |part| case part.type when :str value, = *part part.updated(:sym, [ value.to_sym ]) when :dstr part.updated(:dsym) else part end end n(:array, [ *parts ], collection_map(begin_t, parts, end_t)) end
# File lib/parser/builders/default.rb, line 792 def ternary(cond, question_t, if_true, colon_t, if_false) n(:if, [ check_condition(cond), if_true, if_false ], ternary_map(cond, question_t, if_true, colon_t, if_false)) end
# File lib/parser/builders/default.rb, line 44 def true(true_t) n0(:true, token_map(true_t)) end
# File lib/parser/builders/default.rb, line 738 def unary_op(op_t, receiver) case value(op_t) when '+', '-' method = value(op_t) + '@' else method = value(op_t) end n(:send, [ receiver, method.to_sym ], send_unary_op_map(op_t, receiver)) end
# File lib/parser/builders/default.rb, line 503 def undef_method(undef_t, names) n(:undef, [ *names ], keyword_map(undef_t, nil, names, nil)) end
Case matching
# File lib/parser/builders/default.rb, line 799 def when(when_t, patterns, then_t, body) children = patterns << body n(:when, children, keyword_map(when_t, then_t, children, nil)) end
# File lib/parser/builders/default.rb, line 196 def word(parts) if collapse_string_parts?(parts) parts.first else n(:dstr, [ *parts ], collection_map(nil, parts, nil)) end end
Generated with the Darkfish Rdoc Generator 2.