class DSL

Simple DSL implementation for Ripper code generation

input: /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/ output:

VALUE v1, v2;
v1 = dispatch0(stmts_new);
v2 = dispatch0(void_stmt);
$$ = dispatch2(stmts_add, v1, v2);
  • The code must be a single line.

  • The code is basically Ruby code, even if it appears like in C and the result will be processed as C. e.g., comments need to be in Ruby style.

Constants

NAME_PATTERN
NOT_REF_PATTERN
TAG_PATTERN

Attributes

events[R]

Public Class Methods

line?(line, lineno = nil) click to toggle source
# File ripper/tools/dsl.rb, line 23
def self.line?(line, lineno = nil)
  if %r</\*% *ripper(?:\[(.*?)\])?: *(.*?) *%\*/> =~ line
    new($2, $1&.split(",") || [], lineno)
  end
end
new(code, options, lineno = nil) click to toggle source
# File ripper/tools/dsl.rb, line 29
def initialize(code, options, lineno = nil)
  @lineno = lineno
  @events = {}
  @error = options.include?("error")
  @brace = options.include?("brace")
  if options.include?("final")
    @final = "p->result"
  else
    @final = (options.grep(/\A\$#{NAME_PATTERN}\z/o)[0] || "p->s_lvalue")
  end
  @vars = 0

  # struct parser_params *p
  p = p = "p"

  @code = +""
  code = code.gsub(%r[\G#{NOT_REF_PATTERN}\K(\$|\$:|@)#{TAG_PATTERN}?#{NAME_PATTERN}]o, '"\&"')
  @last_value = eval(code)
rescue SyntaxError
  $stderr.puts "error on line #{@lineno}" if @lineno
  raise
end