Sha256: 932fd87279903492146eadc304da1dba7fa5a87f46872299633a5890c0bba90e

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require_relative 'composite_node'

module Rley
  module RGN
    # A RGN syntax node representing an expression quantified by a ?, * or +.
    class RepetitionNode < CompositeNode
      # @return [Symbol] one of: :zero_or_one, :zero_or_more, :one_or_more
      attr_accessor :repetition

      Repetition2suffix = {
        zero_or_one: '_qmark',
        zero_or_more: '_star',
        exactly_one: '',
        one_or_more: '_plus'
      }.freeze

      # @param child [Array<ASTNode>] sequence of AST nodes
      # @param theRepetition [Symbol] how many times the child node can be repeated
      def initialize(child, theRepetition)
        super([child])
        @repetition = theRepetition
      end

      # @return [RGN::ASTNode]
      def child
        subnodes[0]
      end

      # @return [String]
      def name
        child_name = subnodes[0].name
        "rep_#{child_name}#{Repetition2suffix[repetition]}"
      end

      # @return [String]
      def to_text
        child_text = subnodes[0].to_text
        "rep_#{child_text}#{Repetition2suffix[repetition]}"
      end

      # Part of the 'visitee' role in Visitor design pattern.
      # @param visitor [RGN::ASTVisitor] the visitor
      def accept(visitor)
        visitor.visit_repetition_node(self)
      end

      def suffix_qmark
        Repetition2suffix[:zero_or_one]
      end

      def suffix_star
        Repetition2suffix[:zero_or_more]
      end

      def suffix_plus
        Repetition2suffix[:one_or_more]
      end
    end # class
  end # module
end # module

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rley-0.8.13 lib/rley/rgn/repetition_node.rb
rley-0.8.11 lib/rley/rgn/repetition_node.rb
rley-0.8.10 lib/rley/rgn/repetition_node.rb
rley-0.8.09 lib/rley/rgn/repetition_node.rb
rley-0.8.08 lib/rley/rgn/repetition_node.rb