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

Included in:
HeredocSupport, Lexer2, SlurpSupport
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.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/puppet/pops/parser/lexer_support.rb', line 76

def assert_numeric(value, length)
  if value =~ /^0[xX].*$/
    lex_error(Puppet::Pops::Issues::INVALID_HEX_NUMBER, {:value => value}, length)     unless value =~ /^0[xX][0-9A-Fa-f]+$/

  elsif value =~ /^0[^.].*$/
    lex_error(Puppet::Pops::Issues::INVALID_OCTAL_NUMBER, {:value => value}, length)   unless value =~ /^0[0-7]+$/

  else
    lex_error(Puppet::Pops::Issues::INVALID_DECIMAL_NUMBER, {:value => value}, length) unless value =~ /0?\d+(?:\.\d+)?(?:[eE]-?\d+)?/
  end
end

#create_lex_error(issue, args = {}, pos = nil) ⇒ Puppet::ParseErrorWithIssue

Returns the created error.

Parameters:

Returns:



63
64
65
66
67
68
69
70
71
# File 'lib/puppet/pops/parser/lexer_support.rb', line 63

def create_lex_error(issue, args = {}, pos = nil)
  Puppet::ParseErrorWithIssue.new(
      issue.format(args),
      filename,
      line(pos),
      position(pos),
      nil,
      issue.issue_code)
end

#filenameObject



35
36
37
38
# File 'lib/puppet/pops/parser/lexer_support.rb', line 35

def filename
  file = @locator.file
  file.is_a?(String) && !file.empty? ? file : nil
end

#followed_byObject

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



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

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



17
18
19
20
21
22
23
# File 'lib/puppet/pops/parser/lexer_support.rb', line 17

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

#lex_error(issue, args = {}, pos = nil) ⇒ Object

Raises a Puppet::ParserErrorWithIssue with the given issue and arguments



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

def lex_error(issue, args = {}, pos=nil)
  raise create_lex_error(issue, args, pos)
end

#lex_error_without_pos(issue, args = {}) ⇒ Object

Raises a Puppet::LexError with the given message



26
27
28
# File 'lib/puppet/pops/parser/lexer_support.rb', line 26

def lex_error_without_pos(issue, args = {})
  raise Puppet::ParseErrorWithIssue.new(issue.format(args), nil, nil, nil, nil, issue.issue_code)
end

#lex_warning(issue, args = {}, pos = nil) ⇒ Object



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

def lex_warning(issue, args = {}, pos=nil)
  Puppet::Util::Log.create({
      :level => :warning,
      :message => issue.format(args),
      :issue_code => issue.issue_code,
      :file => filename,
      :line => line(pos),
      :pos => position(pos),
    })
end

#line(pos) ⇒ Object



40
41
42
# File 'lib/puppet/pops/parser/lexer_support.rb', line 40

def line(pos)
  @locator.line_for_offset(pos || @scanner.pos)
end

#position(pos) ⇒ Object



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

def position(pos)
  @locator.pos_on_line(pos || @scanner.pos)
end