Skip to content

Instantly share code, notes, and snippets.

@teliosdev
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save teliosdev/643a270bf291f1b41b57 to your computer and use it in GitHub Desktop.

Select an option

Save teliosdev/643a270bf291f1b41b57 to your computer and use it in GitHub Desktop.

Revisions

  1. teliosdev revised this gist Jul 20, 2014. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions predict_match.rb
    Original file line number Diff line number Diff line change
    @@ -9,13 +9,17 @@ module PredictMatch
    #
    # @param actions [Hash<Symbol => Symbol>]
    # @return [Object] the result of the compiliation.
    def predict(actions)
    def predict(*params, actions)

    action = actions.fetch(peek) do
    actions.fetch(:_)
    end

    send(:"compile_#{action}")
    if action.respond_to?(:call)
    action.call(*params)
    else
    send(:"compile_#{action}", *params)
    end
    end

    # Matches the next token of the input. If it is not of the
  2. teliosdev created this gist Jul 20, 2014.
    45 changes: 45 additions & 0 deletions predict_match.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    module Liquidscript
    class Parser
    module PredictMatch

    # Makes a prediction based on a set of actions. If the peek token
    # doesn't have an action, it will try the special action `:_`, and
    # then give up. If it finds a corresponding action either time, it
    # will run the action.
    #
    # @param actions [Hash<Symbol => Symbol>]
    # @return [Object] the result of the compiliation.
    def predict(actions)

    action = actions.fetch(peek) do
    actions.fetch(:_)
    end

    send(:"compile_#{action}")
    end

    # Matches the next token of the input. If it is not of the
    # expected type, it raises a SyntaxError. Otherwise, it returns
    # the token.
    #
    # @param name [Symbol] the token to match.
    # @return [Array] the token.
    # @raise SyntaxError if the peeked token is invalid.
    def match(name)
    if name == peek
    input.next
    else
    raise SyntaxError, "Unexpected token #{peek}"
    end
    end

    # Returns the type of the next token.
    #
    # @return [Symbol] the type of the next token.
    def peek
    input.peek[0]
    end

    end
    end
    end