Sha256: 36bc5e2c1943d1721886f29f5f114d961e1e0b69e4d0b2436c6c5554bb8c1e2e

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module TireSwing

  module ParserExtension

    def ast(io)
      parser = new
      result = parser.parse(io)
      if result
        result.build
      else
        raise ParseError.new(
          [
            parser.failure_reason,
            parser.input.split("\n")[parser.failure_line-1],
            " " * parser.failure_index + "^"
          ].join("\n"),
          parser
        )
      end
    end

  end

  # Extends the treetop-provided grammar parser with a .ast class method for simple parsing and building of
  # an AST defined by TireSwing. Takes the grammar module as an argument.
  #
  # Additionally, this defines a #node method on the grammar to delegate to the class or the AST to create
  # new nodes, e.g. <node(:variable)> instead of <AST.create_node(:variable)>
  #
  # You can specify an alternate module which contains the AST if desired.
  def self.parses_grammar(grammar, ast=nil)
    # use either extlib or activesupport, if that's loaded instead
    parser = grammar.to_s + "Parser"
    parser = if defined?(Extlib::Inflection)
      Extlib::Inflection.constantize
    else
      parser.constantize
    end
    ast ||= grammar
    parser.module_eval do
      extend ParserExtension
      define_method(:node) { |*args| ast.create_node(*args) }
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aniero-tire_swing-0.0.6 lib/tire_swing/parser_extension.rb