Sha256: 74209090d07fc58ff6a536fe31c665642b5a29baef7b834f0c41a50a2b47554e

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

# A simple xml parser. It is simple in the respect as that it doesn't address
# any of the complexities of XML. This is ruby 1.9.

$:.unshift '../lib'

require 'pp'
require 'parslet'

module XML
  include Parslet

  root :document
  
  rule(:document) {
    tag(close: false).as(:o) >> document.as(:i) >> tag(close: true).as(:c) |
    text
  }
  
  # Perhaps we could have some syntax sugar to make this more easy?
  #
  def tag(opts={})
    close = opts[:close] || false
    
    parslet = str('<')
    parslet = parslet >> str('/') if close
    parslet = parslet >> (str('>').absnt? >> match("[a-zA-Z]")).repeat(1).as(:name)
    parslet = parslet >> str('>')
    
    parslet
  end
  
  rule(:text) {
    match('[^<>]').repeat(0)
  }
end

def check(xml)
  include XML
  r=parse(xml)

  # We'll validate the tree by reducing valid pairs of tags into simply the
  # string "verified". If the transformation ends on a string, then the
  # document was 'valid'. 
  #
  t = Parslet::Transform.new do
    rule(
      o: {name: simple(:tag)}, 
      c: {name: simple(:tag)}, 
      i: simple(:t)
    ) { 'verified' } 
  end

  t.apply(r)
end

pp check("<a><b>some text in the tags</b></a>")
pp check("<b><b>some text in the tags</b></a>")

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
parslet-1.1.1 example/simple_xml.rb
parslet-1.1.0 example/simple_xml.rb
parslet-1.0.1 example/simple_xml.rb