Sha256: 19034bf929c85207f016ee88826283f0a0d99d21368a9d3b59f3987184bec2a1

Contents?: true

Size: 971 Bytes

Versions: 18

Compression:

Stored size: 971 Bytes

Contents

# $Id$
#
# convert Array-like string into Ruby's Array.

class ArrayParser

rule

array   : '[' contents ']'
            {
              result = val[1]
            }
        | '[' ']'
            {
              result = []
            }

contents: ITEM
            {
              result = val
            }
        | contents ',' ITEM
            {
              result.push val[2]
            }

---- inner

  def parse(str)
    str = str.strip
    @q = []
    until str.empty?
      case str
      when /\A\s+/
        str = $'
      when /\A\w+/
        @q.push [:ITEM, $&]
        str = $'
      else
        c = str[0,1]
        @q.push [c, c]
        str = str[1..-1]
      end
    end
    @q.push [false, '$']   # is optional from Racc 1.3.7
    do_parse
  end

  def next_token
    @q.shift
  end

---- footer

if $0 == __FILE__
  src = <<EOS
[
  a, b, c,
  d,
  e ]
EOS
  puts 'parsing:'
  print src
  puts
  puts 'result:'
  p ArrayParser.new.parse(src)
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
racc-1.4.16-java sample/array.y
racc-1.4.16 sample/array.y
racc-1.4.15-java sample/array.y
racc-1.4.15 sample/array.y
racc-1.4.14-java sample/array.y
racc-1.4.14 sample/array.y
racc-1.4.13-java sample/array.y
racc-1.4.13 sample/array.y
racc-1.4.12-java sample/array.y
racc-1.4.12 sample/array.y
racc-1.4.11 sample/array.y
gd-racc-1.4.11 sample/array.y
racc-1.4.10 sample/array.y
racc-1.4.9-java sample/array.y
racc-1.4.9 sample/array.y
racc-1.4.8 sample/array.y
racc-1.4.7 sample/array.y
racc-1.4.6 sample/array.y