开发者

Parsing tcl arrays in ruby with treetop

开发者 https://www.devze.com 2022-12-23 12:20 出处:网络
I have a bunch of data in (what i think is) a tcl array. Basically it\'s in the form of {a {b c} d {e f} g}. It\'s only nested one deep, but isn\'t always nested, that is to say, a may just be a or it

I have a bunch of data in (what i think is) a tcl array. Basically it's in the form of {a {b c} d {e f} g}. It's only nested one deep, but isn't always nested, that is to say, a may just be a or it may be {aa bb} or possibly {}, but never {aa {bb cc}}. I want to extract this array so I can use it in ruby.

My first thought was, "No problem, I'll write a little grammar to parse this." I installed the treetop gem, and wrote up a parser, which seemed to work just fine. I started having problems when I tried to extract an array from the parsed tree. I would like to better understand the cause of the problems and what I am doing wrong.

Here is my parser code so far: (tcl_array.treetop)

grammar TCLArray
  rule array
    "{" [\s]* "}" {
      def content
        []
      end
    }
    /
    "{" [\s]* array_element_list [\s]* "}" {
      def content
        array_element_list.content
      end
    }
  end

  rule array_element_list
    array_element {
      def content
        [array_element.content]
      end
    }
    /
    array_element [\s]+ array_element_list {
      def 开发者_如何学编程content
        [array_element.content] + array_element_list.content
      end
    }
  end

  rule array_element
    [^{}\s]+ {
      def content
        return text_value
      end
    }
    /
    array {
      def content
        array.content
      end
    }
  end
end

Invoking p.parse("{a}").content yields tcl_array.rb:99:in 'content': undefined local variable or method 'array_element'

The first term in array_element_list (array_element) says that array_element is an undefined local variable, but accessor methods are supposed to be automatically defined according to the treetop documentation.

Earlier, I tried a solution that was based off of a grammar with fewer but slightly more complicated rules:

grammar TCLArray
  rule array
    "{" ([\s]* array_element ([\s]+ array_element)* )? [\s]* "}"
  end

  rule array_element
    [^{}\s]+ / array
  end
end

But with this grammar I had issues where the parser seemed to be creating several different expressions for the array rule even though it did not use any alternative expressions (/). The result was that I couldn't figure out how to access the various bits of the array rule to return them as a ruby array.


Maybe a parser generator is overkill in this case. Here's a simple hand-rolled recursive-descent parser based on this JSON parser by James Edward Gray II:

#!/usr/bin/env ruby
# based on James Edward Gray II's solution to the Parsing JSON
#   Ruby Quiz #155: <http://RubyQuiz.Com/quiz155.html>

require 'strscan'

class TclArrayParser < StringScanner
  def parse
    parse_value
  ensure
    eos? or error "Unexpected data: '#{rest}'"
  end

  private

  def parse_value
    trim_space
    parse_string or parse_array
  ensure
    trim_space
  end

  def parse_array
    return nil unless scan(/\{\s*/)
    array = []
    while contents = parse_value
      array << contents
    end
    scan(/\}/) or error('Unclosed array')
    array
  end

  def parse_string
    scan(/[^{}[:space:]]+/)
  end

  def trim_space
    skip(/\s*/)
  end

  def error(message)
    pos = if eos? then 'end of input' else "position #{self.pos}" end
    raise ParseError, "#{message} at #{pos}"
  end

  class ParseError < StandardError; end
end

Here's a testsuite:

require 'test/unit'
class TestTclArrayParser < Test::Unit::TestCase
  def test_that_an_empty_string_parses_to_nil
    assert_nil TclArrayParser.new('').parse
  end
  def test_that_a_whitespace_string_parses_to_nil
    assert_nil TclArrayParser.new("  \t  \n  ").parse
  end
  def test_that_an_empty_array_parses_to_an_empty_array
    assert_equal [], TclArrayParser.new('{}').parse
  end
  def test_that_an_empty_array_with_whitespace_at_the_front_parses_to_an_empty_array
    assert_equal [], TclArrayParser.new(' {}').parse
  end
  def test_that_an_empty_array_with_whitespace_at_the_end_parses_to_an_empty_array
    assert_equal [], TclArrayParser.new('{} ').parse
  end
  def test_that_an_empty_array_with_whitespace_inside_parses_to_an_empty_array
    assert_equal [], TclArrayParser.new('{ }').parse
  end
  def test_that_an_empty_array_surrounded_by_whitespace_parses_to_an_empty_array
    assert_equal [], TclArrayParser.new(' {} ').parse
  end
  def test_that_an_empty_array_with_whitespace_at_the_front_and_inside_parses_to_an_empty_array
    assert_equal [], TclArrayParser.new(' { }').parse
  end
  def test_that_an_empty_array_with_whitespace_at_the_end_and_inside_parses_to_an_empty_array
    assert_equal [], TclArrayParser.new('{ } ').parse
  end
  def test_that_an_empty_array_surrounded_by_whitespace_with_whitespace_inside_parses_to_an_empty_array
    assert_equal [], TclArrayParser.new(' { } ').parse
  end
  def test_that_a_sole_element_parses
    assert_equal 'a', TclArrayParser.new('a').parse
  end
  def test_that_an_array_with_one_element_parses
    assert_equal ['a'], TclArrayParser.new('{a}').parse
  end
  def test_that_a_nested_array_parses
    assert_equal [[]], TclArrayParser.new('{{}}').parse
  end
  def test_that_a_nested_array_with_one_element_parses
    assert_equal [['a']], TclArrayParser.new('{{a}}').parse
  end
  def test_that_whitespace_is_ignored
    assert_equal [], TclArrayParser.new('     {     }     ').parse
  end
  def test_that_complex_arrays_parse_correctly
    assert_equal ['a', %w[b c], 'd', %w[e f], 'g'], TclArrayParser.new('{a {b c} d {e f} g}').parse
    assert_equal [%w[aa bb], %w[b c], 'd', %w[e f], 'g'], TclArrayParser.new('{{aa bb} {b c} d {e f} g}').parse
    assert_equal [[], %w[b c], 'd', %w[e f], 'g'], TclArrayParser.new('{{} {b c} d {e f} g}').parse
    assert_equal [[], ['b', 'c'], 'd', ['e', 'f'], 'g'], TclArrayParser.new("\n{\n{\n}\n{\nb\nc\n}\nd\n{\ne\nf\n}\ng\n}\n").parse
  end
end


Noting this for reference, but I've just released a gem for parsing simple TCL.

https://github.com/julik/tickly

0

精彩评论

暂无评论...
验证码 换一张
取 消