Class: Parslet::Atoms::Named

Inherits:
Base
  • Object
show all
Defined in:
lib/parslet/atoms/named.rb,
lib/parslet/atoms/visitor.rb

Overview

Names a match to influence tree construction.

Example:

str('foo')            # will return 'foo', 
str('foo').as(:foo)   # will return :foo => 'foo'

Constant Summary

Constants included from Precedence

Precedence::ALTERNATE, Precedence::BASE, Precedence::LOOKAHEAD, Precedence::OUTER, Precedence::REPETITION, Precedence::SEQUENCE

Instance Attribute Summary collapse

Attributes inherited from Base

#label

Instance Method Summary collapse

Methods inherited from Base

#cached?, #inspect, #parse, #parse_with_debug, precedence, #setup_and_apply, #to_s, #try

Methods included from CanFlatten

#flatten, #flatten_repetition, #flatten_sequence, #foldl, #merge_fold, #warn_about_duplicate_keys

Methods included from DSL

#>>, #absent?, #as, #capture, #ignore, #maybe, #present?, #repeat, #|

Constructor Details

#initialize(parslet, name) ⇒ Named

Returns a new instance of Named.



10
11
12
13
14
# File 'lib/parslet/atoms/named.rb', line 10

def initialize(parslet, name)
  super()

  @parslet, @name = parslet, name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/parslet/atoms/named.rb', line 9

def name
  @name
end

#parsletObject (readonly)

Returns the value of attribute parslet.



9
10
11
# File 'lib/parslet/atoms/named.rb', line 9

def parslet
  @parslet
end

Instance Method Details

#accept(visitor) ⇒ Object

Call back visitors #visit_named method. See parslet/export for an example.



33
34
35
# File 'lib/parslet/atoms/visitor.rb', line 33

def accept(visitor)
  visitor.visit_named(name, parslet)
end

#apply(source, context, consume_all) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/parslet/atoms/named.rb', line 16

def apply(source, context, consume_all)
  success, value = result = parslet.apply(source, context, consume_all)

  return result unless success
  succ(
    produce_return_value(
      value))
end

#to_s_inner(prec) ⇒ Object



25
26
27
# File 'lib/parslet/atoms/named.rb', line 25

def to_s_inner(prec)
  "#{name}:#{parslet.to_s(prec)}"
end