module Puppet::Pops::Parser::LexerSupport

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.

Constants

BOM_BOCU
BOM_GB_18030
BOM_SCSU
BOM_UTF_1
BOM_UTF_16_1
BOM_UTF_16_2
BOM_UTF_32_1
BOM_UTF_32_2
BOM_UTF_8
BOM_UTF_EBCDIC
LONGEST_BOM
MM
MM_ANY

Public Instance Methods

assert_not_bom(content) click to toggle source
    # File lib/puppet/pops/parser/lexer_support.rb
167 def assert_not_bom(content)
168   name, size =
169   case bom = get_bom(content)
170 
171   when BOM_UTF_32_1, BOM_UTF_32_2
172     ['UTF-32', 4]
173 
174   when BOM_GB_18030
175     ['GB-18030', 4]
176 
177   when BOM_UTF_EBCDIC
178     ['UTF-EBCDIC', 4]
179 
180   when BOM_SCSU
181     ['SCSU', 3]
182 
183   when BOM_UTF_8
184     ['UTF-8', 3]
185 
186   when BOM_UTF_1
187     ['UTF-1', 3]
188 
189   when BOM_BOCU
190     ['BOCU', 3]
191 
192   when BOM_UTF_16_1, BOM_UTF_16_2
193     ['UTF-16', 2]
194 
195   else
196     return
197   end
198 
199   lex_error_without_pos(
200     Puppet::Pops::Issues::ILLEGAL_BOM,
201     { :format_name   => name,
202       :bytes  => "[#{bom.values[0,size].map {|b| "%X" % b}.join(" ")}]"
203     })
204 end
assert_numeric(value, pos) click to toggle source

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.

   # File lib/puppet/pops/parser/lexer_support.rb
83 def assert_numeric(value, pos)
84   if value =~ /^0[xX]/
85     lex_error(Issues::INVALID_HEX_NUMBER, {:value => value}, pos)     unless value =~ /^0[xX][0-9A-Fa-f]+$/
86 
87   elsif value =~ /^0[^.]/
88     lex_error(Issues::INVALID_OCTAL_NUMBER, {:value => value}, pos)   unless value =~ /^0[0-7]+$/
89 
90   elsif value =~ /^\d+[eE.]/
91     lex_error(Issues::INVALID_DECIMAL_NUMBER, {:value => value}, pos) unless value =~ /^\d+(?:\.\d+)?(?:[eE]-?\d+)?$/
92 
93   else
94     lex_error(Issues::ILLEGAL_NUMBER, {:value => value}, pos) unless value =~ /^\d+$/
95   end
96 end
create_lex_error(issue, args = {}, pos = nil) click to toggle source

@param issue [Issues::Issue] the issue @param args [Hash<Symbol,String>] Issue arguments @param pos [Integer] @return [Puppet::ParseErrorWithIssue] the created error

   # File lib/puppet/pops/parser/lexer_support.rb
69 def create_lex_error(issue, args = {}, pos = nil)
70   Puppet::ParseErrorWithIssue.new(
71       issue.format(args),
72       filename,
73       line(pos),
74       position(pos),
75       nil,
76       issue.issue_code,
77       args)
78 end
filename() click to toggle source
   # File lib/puppet/pops/parser/lexer_support.rb
41 def filename
42   file = @locator.file
43   file.is_a?(String) && !file.empty? ? file : nil
44 end
followed_by() click to toggle source

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

   # File lib/puppet/pops/parser/lexer_support.rb
13 def followed_by
14   return "<eof>" if @scanner.eos?
15   result = @scanner.rest[0,5] + "..."
16   result.gsub!("\t", '\t')
17   result.gsub!("\n", '\n')
18   result.gsub!("\r", '\r')
19   result
20 end
format_quote(q) click to toggle source

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

   # File lib/puppet/pops/parser/lexer_support.rb
23 def format_quote(q)
24   if q == "'"
25     '"\'"'
26   else
27     "'#{q}'"
28   end
29 end
get_bom(content) click to toggle source
    # File lib/puppet/pops/parser/lexer_support.rb
206 def get_bom(content)
207   # get 5 bytes as efficiently as possible (none of the string methods works since a bom consists of
208   # illegal characters on most platforms, and there is no get_bytes(n). Explicit calls are faster than
209   # looping with a lambda. The get_byte returns nil if there are too few characters, and they
210   # are changed to spaces
211   MM.new(
212     (content.getbyte(0) || ' '),
213     (content.getbyte(1) || ' '),
214     (content.getbyte(2) || ' '),
215     (content.getbyte(3) || ' ')
216     )
217 end
lex_error(issue, args = {}, pos=nil) click to toggle source

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

   # File lib/puppet/pops/parser/lexer_support.rb
37 def lex_error(issue, args = {}, pos=nil)
38   raise create_lex_error(issue, args, pos)
39 end
lex_error_without_pos(issue, args = {}) click to toggle source

Raises a Puppet::LexError with the given message

   # File lib/puppet/pops/parser/lexer_support.rb
32 def lex_error_without_pos(issue, args = {})
33   raise Puppet::ParseErrorWithIssue.new(issue.format(args), nil, nil, nil, nil, issue.issue_code, args)
34 end
lex_warning(issue, args = {}, pos=nil) click to toggle source
   # File lib/puppet/pops/parser/lexer_support.rb
54 def lex_warning(issue, args = {}, pos=nil)
55   Puppet::Util::Log.create({
56       :level => :warning,
57       :message => issue.format(args),
58       :issue_code => issue.issue_code,
59       :file => filename,
60       :line => line(pos),
61       :pos => position(pos),
62     })
63 end
line(pos) click to toggle source
   # File lib/puppet/pops/parser/lexer_support.rb
46 def line(pos)
47   @locator.line_for_offset(pos || @scanner.pos)
48 end
position(pos) click to toggle source
   # File lib/puppet/pops/parser/lexer_support.rb
50 def position(pos)
51   @locator.pos_on_line(pos || @scanner.pos)
52 end