Sha256: 5ba5908eff809abaa9f8e69b8b95b41c03522b98d1f084baf75180b2045b3304

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

Antelope.define :example, version: "0.0.1", require: "~> 0.3",
                          output: :ruby do
  define "output.verbose"    => true,
         "output.panic-mode" => true,
         "ruby.error-class"  => "SyntaxError",
         "ruby.indent"       => 2,
         "salt.variant"      => "normal"

  terminals do
    # We set the value to true, because the default value in a hash
    # is nil.
    terminal NUMBER: true,
             MULTIPLY: "*",
             EXPONENTIATE: "^",
             DIVIDE: "/",
             ADD: "+",
             SUBTRACT: "-",
             LPAREN: "(",
             RPAREN: ")"
  end

  precedences do
    precedence :right, [:EXPONENTIATE]
    precedence :left,  [:MULTIPLY, :DIVIDE]
    precedence :left,  [:ADD, :SUBTRACT]
  end

  productions do
    production :expression do
      match body:   [:NUMBER],
            action: %{ match[0][1] }
      match body:   [:expression, :EXPONENTIATE, :expression],
            action: %{ match[0] ** match[2] }
      match body:   [:expression, :ADD, :expression],
            action: %{ match[0] + match[2] }
      match body:   [:expression, :SUBTRACT, :expression],
            action: %{ match[0] - match[2] }
      match body:   [:expression, :MULTIPLY, :expression],
            action: %{ match[0] * match[2] }
      match body:   [:expression, :DIVIDE, :expression],
            action: %{ match[0] / match[2] }
      match body:   [:LPAREN, :expression, :RPAREN],
            action: %{ match[1] }
      match body:   [:LPAREN, :$error, :RPAREN],
            action: %{ match[1] }
    end
  end

  template <<-RUBY
    class ExampleParser
      %{write}

      def type(token)
        token[0]
      end
    end

    input = [
      [:LPAREN],
      [:NUMBER, 2],
      [:ADD],
      [:ADD, 2],
      [:RPAREN],
      [:MULTIPLY],
      [:NUMBER, 3]
    ]

    p ExampleParser.new.parse(input)
  RUBY
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
antelope-0.4.1 examples/example.ate
antelope-0.4.0 examples/example.ate