Module: Puppet::Pops::Parser::LexerSupport

Included in:
Lexer2
Defined in:
lib/puppet/pops/parser/lexer_support.rb

Overview

This is an integral part of the Lexer. It is broken out into a separate module for maintainability of the code, and making the various parts of the lexer focused.

Defined Under Namespace

Classes: TokenValue

Instance Method Summary collapse

Instance Method Details

#assert_numeric(value, length) ⇒ Object

Asserts that the given string value is a float, or an integer in decimal, octal or hex form. An error is raised if the given value does not comply.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppet/pops/parser/lexer_support.rb', line 50

def assert_numeric(value, length)
  if value =~ /^0[xX].*$/
    lex_error("Not a valid hex number #{value}", length)     unless value =~ /^0[xX][0-9A-Fa-f]+$/

  elsif value =~ /^0[^.].*$/
    lex_error("Not a valid octal number #{value}", length)   unless value =~ /^0[0-7]+$/

  else
    lex_error("Not a valid decimal number #{value}", length) unless value =~ /0?\d+(?:\.\d+)?(?:[eE]-?\d+)?/
  end
end

#followed_byObject

Returns “<eof>” if at end of input, else the following 5 characters with n r t escaped



19
20
21
22
23
24
25
26
# File 'lib/puppet/pops/parser/lexer_support.rb', line 19

def followed_by
  return "<eof>" if @scanner.eos?
  result = @scanner.rest[0,5] + "..."
  result.gsub!("\t", '\t')
  result.gsub!("\n", '\n')
  result.gsub!("\r", '\r')
  result
end

#format_quote(q) ⇒ Object

Returns a quoted string using “ or ‘ depending on the given a strings’s content



29
30
31
32
33
34
35
# File 'lib/puppet/pops/parser/lexer_support.rb', line 29

def format_quote(q)
  if q == "'"
    '"\'"'
  else
    "'#{q}'"
  end
end

#lex_error(msg, pos = nil) ⇒ Object

Raises a Puppet::LexError with the given message

Raises:



43
44
45
# File 'lib/puppet/pops/parser/lexer_support.rb', line 43

def lex_error(msg, pos=nil)
  raise Puppet::LexError.new(positioned_message(msg, pos))
end

#lex_error_without_pos(msg) ⇒ Object

Raises a Puppet::LexError with the given message

Raises:



38
39
40
# File 'lib/puppet/pops/parser/lexer_support.rb', line 38

def lex_error_without_pos msg
  raise Puppet::LexError.new(msg)
end

#positioned_message(msg, pos = nil) ⇒ Object

Formats given message by appending file, line and position if available.



7
8
9
10
11
12
13
14
15
16
# File 'lib/puppet/pops/parser/lexer_support.rb', line 7

def positioned_message(msg, pos = nil)
  result = [msg]
  file = @locator.file
  line = @locator.line_for_offset(pos || @scanner.pos)
  pos =  @locator.pos_on_line(pos || @scanner.pos)

  result << "in file #{file}" if file && file.is_a?(String) && !file.empty?
  result << "at line #{line}:#{pos}"
  result.join(" ")
end