Sha256: e66f7f0f17dd4ab6e02efcf21b4a765c5bbf0b5fd7e829e7e6ea8b0bf17873fa

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

# This file assumes that the output of the generator will be placed
# within a module or a class.  However, the module/class requires a
# `type` method, which takes a terminal and gives its type, as a
# symbol.  These types should line up with the terminals that were
# defined in the original grammar.

# The actions to take during parsing.  In every state, there are a
# set of acceptable peek tokens; this table tells the parser what
# to do on each acceptable peek token.  The possible actions include
# `:accept`, `:reduce`, and `:state`; `:accept` means to accept the
# input and return the value of the pasing.  `:reduce` means to
# reduce the top of the stack into a given nonterminal.  `:state`
# means to transition to another state.
#
# @return [Array<Hash<(Symbol, Array<(Symbol, Numeric)>)>>]
ACTION_TABLE = <%= generate_action_table %>.freeze # >

# A list of all of the productions.  Only includes the left-hand side,
# the number of tokens on the right-hand side, and the block to call
# on reduction.
#
# @return [Array<Array<(Symbol, Numeric, Proc)>>]
PRODUCTIONS  = <%= generate_productions_list %>.freeze # >

# Runs the parser.
#
# @param input [Array<Object>] the input to run the parser over.
# @return [Object] the result of the accept.
def parse(input)
  stack = []
  stack.push([nil, 0])
  input = input.dup
  last  = nil

  until stack.empty? do
    peek_token = if input.empty?
      :"$"
    else
      type(input.first)
    end

    action = ACTION_TABLE[stack.last.last].fetch(peek_token)
    case action.first
    when :accept
      production = PRODUCTIONS[action.last]
      last       = stack.pop(production[1]).first.first
      stack.pop
    when :reduce
      production = PRODUCTIONS[action.last]
      removing   = stack.pop(production[1])
      value = production[2].call(*removing.map(&:first))
      goto  = ACTION_TABLE[stack.last.last][production[0]]
      stack.push([value, goto.last])
    when :state
      stack.push([input.shift, action.last])
    else
      raise
    end
  end

  last
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
antelope-0.1.1 lib/antelope/generator/templates/ruby.erb
antelope-0.1.0 lib/antelope/generator/templates/ruby.erb
antelope-0.0.1 lib/antelope/generator/templates/ruby.erb