Class: Rattler::Parsers::SemanticAction

Inherits:
Parser show all
Includes:
Semantic
Defined in:
lib/rattler/parsers/semantic_action.rb

Overview

SemanticAction is a pseudo-parser that performs a semantic action by evaluating ruby code in the context of a parser scope.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Semantic

#bind, #bindable_code, #semantic?

Methods inherited from Parser

#&, #>>, #capturing?, #capturing_decidable?, #labeled?, #list, #one_or_more, #optional, #repeat, #semantic?, #sequence?, #skip, #variable_capture_count?, #with_ws, #zero_or_more, #|

Methods included from Runtime::ParserHelper

#select_captures

Methods inherited from Util::Node

#==, #[], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #initialize, #inspect, #method_missing, #name, #pretty_print, #pretty_print_cycle, #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

.[](code) ⇒ SemanticAction

Returns a pseudo-parser that performs a semantic action.

Parameters:

  • code (String)

    ruby code that can be bound to a parser scope to perform an action

Returns:

  • (SemanticAction)

    a pseudo-parser that performs a semantic action



16
17
18
# File 'lib/rattler/parsers/semantic_action.rb', line 16

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

.parsed(results, *_) ⇒ Object



9
10
11
# File 'lib/rattler/parsers/semantic_action.rb', line 9

def self.parsed(results, *_) #:nodoc:
  self[results.first]
end

Instance Method Details

#parse(scanner, rules, scope = ParserScope.empty) ⇒ Object

Perform the semantic action in the context of scope

Parameters:

  • scanner (StringScanner)

    the scanner for the current parse

  • rules (RuleSet)

    the grammar rules being used for the current parse

  • scope (ParserScope) (defaults to: ParserScope.empty)

    the scope of captured results

Returns:

  • the result of performing the semantic action in the context of scope



26
27
28
# File 'lib/rattler/parsers/semantic_action.rb', line 26

def parse(scanner, rules, scope = ParserScope.empty)
  apply scope
end