Class: Rattler::Parsers::DirectAction

Inherits:
Parser show all
Includes:
Combining
Defined in:
lib/rattler/parsers/direct_action.rb

Overview

DirectAction decorates a parser to peform a symantic action on success by evaluating ruby code.

Author:

  • Jason Arhart

Direct Known Subclasses

SemanticAssert, SemanticDisallow, SideEffect

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Combining

#capturing?, #with_ws

Methods inherited from Parser

#&, #capturing?, #labeled?, #one_or_more, #optional, #skip, #variable_capture_count?, #with_ws, #zero_or_more, #|

Methods inherited from Util::Node

#==, #[], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #initialize, #inspect, #method_missing, #name, #respond_to?, #same_contents?, #to_graphviz, #with_attrs, #with_attrs!, #with_children

Constructor Details

This class inherits a constructor from Rattler::Util::Node

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rattler::Util::Node

Class Method Details

.[](child, code) ⇒ Object



20
21
22
# File 'lib/rattler/parsers/direct_action.rb', line 20

def self.[](child, code)
  self.new(child, :code => code.strip)
end

.parsed(results, *_) ⇒ Object



25
26
27
28
29
# File 'lib/rattler/parsers/direct_action.rb', line 25

def self.parsed(results, *_) #:nodoc:
  optional_expr, code = results
  expr = optional_expr.first || ESymbol[]
  self[expr, code]
end

Instance Method Details

#bind(scope, bind_args) ⇒ Object



54
55
56
# File 'lib/rattler/parsers/direct_action.rb', line 54

def bind(scope, bind_args)
  bindable_code.bind(scope, bind_args)
end

#bindable_codeObject



50
51
52
# File 'lib/rattler/parsers/direct_action.rb', line 50

def bindable_code
  @bindable_code ||= create_bindable_code
end

#parse(scanner, rules, scope = {}) ⇒ Object

If the wrapped parser matches at the parse position, return the result of applying the symantic action, otherwise return a false value.

Returns:

  • the result of applying the symantic action, or a false value if the parse failed.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rattler/parsers/direct_action.rb', line 38

def parse(scanner, rules, scope = {})
  if result = parse_child(child, scanner, rules, scope) {|_| scope = _ }
    if not child.capturing?
      apply([])
    elsif result.respond_to?(:to_ary)
      apply(result, scope)
    else
      apply([result], scope)
    end
  end
end