Class: Puppet::Pops::Parser::Lexer::Token

Inherits:
Object
  • Object
show all
Includes:
Util::MethodHelper
Defined in:
lib/puppet/pops/parser/lexer.rb

Constant Summary collapse

ALWAYS_ACCEPTABLE =
Proc.new { |context| true }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Constructor Details

#initialize(string) ⇒ Token #initialize(regex) ⇒ Token

Returns a new instance of Token.

Overloads:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppet/pops/parser/lexer.rb', line 43

def initialize(string_or_regex, name, options = {})
  if string_or_regex.is_a?(String)
    @name, @string = name, string_or_regex
    @regex = Regexp.new(Regexp.escape(string_or_regex))
  else
    @name, @regex = name, string_or_regex
  end

  set_options(options)
  @acceptable_when = ALWAYS_ACCEPTABLE
end

Instance Attribute Details

#nameObject



31
32
33
# File 'lib/puppet/pops/parser/lexer.rb', line 31

def name
  @name
end

#regexObject



31
32
33
# File 'lib/puppet/pops/parser/lexer.rb', line 31

def regex
  @regex
end

#skipObject Also known as: skip?



31
32
33
# File 'lib/puppet/pops/parser/lexer.rb', line 31

def skip
  @skip
end

#skip_textObject



31
32
33
# File 'lib/puppet/pops/parser/lexer.rb', line 31

def skip_text
  @skip_text
end

#stringObject



31
32
33
# File 'lib/puppet/pops/parser/lexer.rb', line 31

def string
  @string
end

Instance Method Details

#acceptable?(context = {}) ⇒ Boolean

Returns if the token is acceptable in the given context or not.

Parameters:

  • context (Hash) (defaults to: {})

    the lexing context

Returns:

  • (Boolean)

    if the token is acceptable in the given context or not.



63
64
65
# File 'lib/puppet/pops/parser/lexer.rb', line 63

def acceptable?(context={})
  @acceptable_when.call(context)
end

#acceptable_when(block) ⇒ Object

Defines when the token is able to match. This provides context that cannot be expressed otherwise, such as feature flags.

Parameters:

  • block (Proc)

    a proc that given a context returns a boolean



72
73
74
# File 'lib/puppet/pops/parser/lexer.rb', line 72

def acceptable_when(block)
  @acceptable_when = block
end

#to_sString

Returns human readable token reference; the String if literal, else the token name.

Returns:

  • (String)

    human readable token reference; the String if literal, else the token name



56
57
58
# File 'lib/puppet/pops/parser/lexer.rb', line 56

def to_s
  string or @name.to_s
end