Sha256: 8bc2625b415cc78292bd579c6c505039ad04627b6ca7cf659562d40b14556096

Contents?: true

Size: 1015 Bytes

Versions: 3

Compression:

Stored size: 1015 Bytes

Contents

module Treetop
  module Compiler    
    class CharacterClass < AtomicExpression
      def compile(address, builder, parent_expression = nil)
        super

        builder.if__ "has_terminal?(@regexps[gr = #{grounded_regexp(text_value)}] ||= Regexp.new(gr), :regexp, index)" do
          if address == 0 || decorated?
            assign_result "instantiate_node(#{node_class_name},input, index...(index + 1))"
            extend_result_with_inline_module parent_expression
          else
            assign_lazily_instantiated_node
          end
          builder << "@index += 1"  # Always one character
        end
        builder.else_ do
          builder << "terminal_parse_failure(#{expected})"
          assign_result 'nil'
        end
      end

      def expected
	single_quote('['+characters+']')
      end

      def grounded_regexp(string)
        # Double any backslashes, then backslash any single-quotes:
        "'\\A#{string.gsub(/\\/) { '\\\\' }.gsub(/'/) { "\\'"}}'"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
treetop-1.6.5 lib/treetop/compiler/node_classes/character_class.rb
treetop-1.6.4 lib/treetop/compiler/node_classes/character_class.rb
treetop-1.6.2 lib/treetop/compiler/node_classes/character_class.rb