Class: NexusParser::Tokens::RowVec

Inherits:
Token
  • Object
show all
Defined in:
lib/nexus_parser/tokens.rb

Instance Attribute Summary

Attributes inherited from Token

#value

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ RowVec

Returns a new instance of RowVec.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/nexus_parser/tokens.rb', line 133

def initialize(str)
  # We ignore commas outside (and inside) of groupings, it's fine.
  str.gsub!(/[\, \t]/, '')

  groupers = ['(', ')', '{', '}']
  openers = ['(', '{']
  closers = [')', '}']
  closer_for = { '(' => ')', '{' => '}' }

  a = []
  group = nil
  group_closer = nil
  str.each_char { |c|
    if groupers.include? c
      if ((openers.include?(c) && !group.nil?) ||
        (closers.include?(c) && (group.nil? || c != group_closer)))
        raise(NexusParser::ParseError,
          "Mismatched grouping in matrix row '#{str}'")
      end

      if openers.include? c
        group = []
        group_closer = closer_for[c]
      else # c is a closer
        if group.count == 1
          a << group.first
        elsif group.count > 1
          a << group
        end
        group = nil
        group_closer = nil
      end
    else
      if group.nil?
        a << c
      else
        group << c
      end
    end
  }

  raise(NexusParser::ParseError,
    "Unclosed grouping in matrix row '#{str}'") if !group.nil?

  @value = a
end