Sha256: b83361291005adb1b750d9ef58936b39c3611e88c6c28c09006b6aed262c24e2

Contents?: true

Size: 955 Bytes

Versions: 5

Compression:

Stored size: 955 Bytes

Contents

module Furnace::AST
  # 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.
  module Sexp
    # Creates a {Node} with type `type` and children `children`.
    # Note that the resulting node is of the type AST::Node and not a
    # subclass.
    # This would not pose a problem with comparisons, as {Node#==}
    # ignores metadata.
    def s(type, *children)
      Node.new(type, children)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
furnace-0.4.0.beta.2 lib/furnace/ast/sexp.rb
furnace-0.4.0.beta.1 lib/furnace/ast/sexp.rb
furnace-0.3.1 lib/furnace/ast/sexp.rb
furnace-0.3.0 lib/furnace/ast/sexp.rb
furnace-0.3.0.beta3 lib/furnace/ast/sexp.rb