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.
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, 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
@param issue [Puppet::Pops::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, 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
# File lib/puppet/pops/parser/lexer_support.rb, line 35 def filename file = @locator.file file.is_a?(String) && !file.empty? ? file : nil 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, 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
Returns a quoted string using “ or ‘ depending on the given a strings’s content
# File lib/puppet/pops/parser/lexer_support.rb, line 17 def format_quote(q) if q == "'" '"\"' else "'#{q}'" end end
Raises a Puppet::ParserErrorWithIssue with the given issue and arguments
# 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
Raises a Puppet::LexError with the given message
# 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
# 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
# File lib/puppet/pops/parser/lexer_support.rb, line 40 def line(pos) @locator.line_for_offset(pos || @scanner.pos) end
# File lib/puppet/pops/parser/lexer_support.rb, line 44 def position(pos) @locator.pos_on_line(pos || @scanner.pos) end