Sha256: dcf4db91cf5e56043a21b8a99a7e02dc52e5b3399e1caa6e2e01a520fa312513
Contents?: true
Size: 1.29 KB
Versions: 10
Compression:
Stored size: 1.29 KB
Contents
# frozen_string_literal: true class Language class Atom class Repeat < Atom def initialize(parent: nil, minimum: 0, maximum: nil) @parent = parent @minimum = minimum @maximum = maximum end def parse(parser) return unless @parent @minimum.times { match(parser) } if @maximum.nil? begin loop { match(parser) } rescue Parser::Interuption end else begin (@maximum - @minimum).times { match(parser) } rescue Parser::Interuption end end end def to_s minimum = @minimum.zero? ? "" : @minimum.to_s maximum = @maximum.nil? ? "" : ", #{@maximum}" parenthesis = (minimum.empty? && maximum.empty?) ? "" : "(#{minimum}#{maximum})" @parent ? "(#{@parent}).repeat#{parenthesis}" : "repeat#{parenthesis}" end private def match(parser) clone = Parser.new( root: self, input: parser.input, cursor: parser.cursor, buffer: parser.buffer ) @parent.parse(clone) parser.cursor = clone.cursor parser.buffer = clone.buffer parser.output << clone.output end end end end
Version data entries
10 entries across 10 versions & 1 rubygems