# frozen-string-literal: true
#
# Copyright (C) 2019 Thomas Baron
#
# This file is part of term_utils.
#
# term_utils is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# term_utils is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with term_utils. If not, see .
module TermUtils
module AP
# Represents a syntax element (abstract).
class Element
# @return [Symbol]
attr_accessor :id
# @return [Integer]
attr_accessor :min_occurs
# @return [Integer]
attr_accessor :max_occurs
# @return [Array]
attr_accessor :flags
# For dup method.
def initialize_dup(other)
if other.flags
@flags = []
other.flags.each do |f|
@flags << f.dup
end
end
super
end
# Finalizes this one. Internal use.
# @return [nil]
def finalize!(opts = {})
raise TermUtils::AP::SyntaxError, "min_occurs must be equal or greater than 0" unless (@min_occurs.is_a? Integer) && (@min_occurs >= 0)
raise TermUtils::AP::SyntaxError, "max_occurs must be equal or greater than min_occurs" unless !occur_bounded? || (@max_occurs >= @min_occurs)
unless @id
opts[:anonymous] += 1
@id = "anonymous#{opts[:anonymous]}".intern
end
end
# Tests whether this one has mutiple occurs.
# @return [Boolean]
def multiple_occurs?
(@max_occurs == nil) || (@max_occurs == :infinity) || ((@max_occurs.is_a? Integer) && (@max_occurs > 1))
end
# Tests whether the number of occurs are fixed.
# @return [Boolean]
def occur_bounded?
(@max_occurs != nil) && (@max_occurs != :infinity) && (@max_occurs.is_a? Integer)
end
# Tests whether this one is flagged.
# @return [Boolean]
def flagged?
!@flags.empty?
end
# Adds a new Flag to this one.
# @param opts [Hash]
# @option opts [String] :label
# @option opts [Symbol] :flavor `:anchor`, `:long`, `:short`.
# @return [TermUtils::AP::Flag]
def define_flag(opts = {}, &block)
new_flag = TermUtils::AP::Flag.new(opts)
@flags << new_flag
block.call(new_flag) if block
new_flag
end
end
end
end