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
# File lib/puppet/pops/parser/lexer_support.rb 168 def assert_not_bom(content) 169 name, size = 170 case bom = get_bom(content) 171 172 when BOM_UTF_32_1, BOM_UTF_32_2 173 ['UTF-32', 4] 174 175 when BOM_GB_18030 176 ['GB-18030', 4] 177 178 when BOM_UTF_EBCDIC 179 ['UTF-EBCDIC', 4] 180 181 when BOM_SCSU 182 ['SCSU', 3] 183 184 when BOM_UTF_8 185 ['UTF-8', 3] 186 187 when BOM_UTF_1 188 ['UTF-1', 3] 189 190 when BOM_BOCU 191 ['BOCU', 3] 192 193 when BOM_UTF_16_1, BOM_UTF_16_2 194 ['UTF-16', 2] 195 196 else 197 return 198 end 199 200 lex_error_without_pos( 201 Puppet::Pops::Issues::ILLEGAL_BOM, 202 { :format_name => name, 203 :bytes => "[#{bom.values[0,size].map {|b| "%X" % b}.join(" ")}]" 204 }) 205 end
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 84 def assert_numeric(value, pos) 85 if value =~ /^0[xX]/ 86 lex_error(Issues::INVALID_HEX_NUMBER, {:value => value}, pos) unless value =~ /^0[xX][0-9A-Fa-f]+$/ 87 88 elsif value =~ /^0[^.]/ 89 lex_error(Issues::INVALID_OCTAL_NUMBER, {:value => value}, pos) unless value =~ /^0[0-7]+$/ 90 91 elsif value =~ /^\d+[eE.]/ 92 lex_error(Issues::INVALID_DECIMAL_NUMBER, {:value => value}, pos) unless value =~ /^\d+(?:\.\d+)?(?:[eE]-?\d+)?$/ 93 94 else 95 lex_error(Issues::ILLEGAL_NUMBER, {:value => value}, pos) unless value =~ /^\d+$/ 96 end 97 end
@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 70 def create_lex_error(issue, args = {}, pos = nil) 71 Puppet::ParseErrorWithIssue.new( 72 issue.format(args), 73 filename, 74 line(pos), 75 position(pos), 76 nil, 77 issue.issue_code, 78 args) 79 end
# File lib/puppet/pops/parser/lexer_support.rb 42 def filename 43 file = @locator.file 44 file.is_a?(String) && !file.empty? ? file : nil 45 end
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 14 def followed_by 15 return "<eof>" if @scanner.eos? 16 result = @scanner.rest[0,5] + "..." 17 result.gsub!("\t", '\t') 18 result.gsub!("\n", '\n') 19 result.gsub!("\r", '\r') 20 result 21 end
Returns a quoted string using “ or ' depending on the given a strings's content
# File lib/puppet/pops/parser/lexer_support.rb 24 def format_quote(q) 25 if q == "'" 26 '"\'"' 27 else 28 "'#{q}'" 29 end 30 end
# File lib/puppet/pops/parser/lexer_support.rb 207 def get_bom(content) 208 # get 5 bytes as efficiently as possible (none of the string methods works since a bom consists of 209 # illegal characters on most platforms, and there is no get_bytes(n). Explicit calls are faster than 210 # looping with a lambda. The get_byte returns nil if there are too few characters, and they 211 # are changed to spaces 212 MM.new( 213 (content.getbyte(0) || ' '), 214 (content.getbyte(1) || ' '), 215 (content.getbyte(2) || ' '), 216 (content.getbyte(3) || ' ') 217 ) 218 end
Raises a Puppet::ParserErrorWithIssue with the given issue and arguments
# File lib/puppet/pops/parser/lexer_support.rb 38 def lex_error(issue, args = {}, pos=nil) 39 raise create_lex_error(issue, args, pos) 40 end
Raises a Puppet::LexError with the given message
# File lib/puppet/pops/parser/lexer_support.rb 33 def lex_error_without_pos(issue, args = {}) 34 raise Puppet::ParseErrorWithIssue.new(issue.format(args), nil, nil, nil, nil, issue.issue_code, args) 35 end
# File lib/puppet/pops/parser/lexer_support.rb 55 def lex_warning(issue, args = {}, pos=nil) 56 Puppet::Util::Log.create({ 57 :level => :warning, 58 :message => issue.format(args), 59 :issue_code => issue.issue_code, 60 :file => filename, 61 :line => line(pos), 62 :pos => position(pos), 63 }) 64 end
# File lib/puppet/pops/parser/lexer_support.rb 47 def line(pos) 48 @locator.line_for_offset(pos || @scanner.pos) 49 end
# File lib/puppet/pops/parser/lexer_support.rb 51 def position(pos) 52 @locator.pos_on_line(pos || @scanner.pos) 53 end