class Hiera::Backend::Eyaml::Parser::Parser

Attributes

token_types[R]

Public Class Methods

new(token_types) click to toggle source
# File lib/hiera/backend/eyaml/parser/parser.rb, line 31
def initialize(token_types)
  @token_types = token_types
end

Public Instance Methods

parse(text) click to toggle source
# File lib/hiera/backend/eyaml/parser/parser.rb, line 35
def parse(text)
  parse_scanner(StringScanner.new(text)).reverse
end
parse_scanner(s) click to toggle source
# File lib/hiera/backend/eyaml/parser/parser.rb, line 39
def parse_scanner(s)
  if s.eos?
    []
  else
    # Check if the scanner currently matches a regex
    current_match = @token_types.find do |token_type|
      s.match?(token_type.regex)
    end

    token =
      if current_match.nil?
        # No regex matches here. Find the earliest match.
        next_match_indexes = @token_types.map do |token_type|
          next_match = s.check_until(token_type.regex)
          if next_match.nil?
            nil
          else
            next_match.length - s.matched.length
          end
        end.reject { |i| i.nil? }
        non_match_size =
          if next_match_indexes.length == 0
            s.rest_size
          else
            next_match_indexes.min
          end
        non_match = s.peek(non_match_size)
        # advance scanner
        s.pos = s.pos + non_match_size
        NonMatchToken.new(non_match)
      else
        # A regex matches so create a token and do a recursive call with the advanced scanner
        current_match.create_token s.scan(current_match.regex)
      end

    parse_scanner(s) << token
  end
end