module AST::Sexp

Override to return {BELParser::Parsers::AST::Node} from Sexp shortcut.

This simple module is very useful in the cases where one needs to define deeply nested ASTs from Ruby code, for example, in tests. It should be used like this:

describe YourLanguage::AST do
  include Sexp

  it "should correctly parse expressions" do
    YourLanguage.parse("1 + 2 * 3").should ==
        s(:add,
          s(:integer, 1),
          s(:multiply,
            s(:integer, 2),
            s(:integer, 3)))
  end
end

This way the amount of boilerplate code is greatly reduced.

Public Instance Methods

s(type, *children) click to toggle source
# File lib/bel_parser/parsers/ast/sexp.rb, line 4
def s(type, *children)
  BELParser::Parsers::AST::Node.new(type, children)
end