magic_parser

The magic_parser ruby gem provides an easy way to do most simple parsing of text files. The grammar for a file format is written as a hierarchy of regular expression/tag pairs. Here is an example taken from the definition of xml:

xml.rb

“by require ‘magic_parser’

class XMLParser < Parser def initialize # XML definition attr_value_w_space = “([\]?\s[\w\-\.\/:]+\s[\]?)” attr_value_no_space = “(\s[\w\-\.\/:]+\s)” attr_name = “([\w:-]+)” tag_name = attr_name attr_value = “(#attr_value_w_space|#attr_value_no_space)” attribute = “#attr_name\s\=\s#attr_value”

	super() { {
		'tag' => [ /\s*(\<\??\s*#{tag_name}(\s*#{attribute}\s*)*\/?\s*\??\>)\s*/, {
			'name' => /\<\??\s*#{tag_name}/,
			'attribute' => [ /\s*(#{attribute})\s*/, {
				'name' => /(#{attr_name})\s*\=/,
				'value' => /\=\s*#{attr_value}/
			}]
		}],
		'close_tag' => [ /\s*(\<\s*\/#{tag_name}\s*\>)\s*/, {
			'name' => /\<\s*\/#{tag_name}/
		}],
		'content' => /\s*(\w.*)/
	} }
end

end

class XML < Document @@parser = XMLParser.new end

With this you can parse a simple bit of text:

“by text = ‘

xml = XML.new( text: text )

or an entire file. For a file called data.xml:

Empire Burlesque Bob Dylan USA Columbia 10.90 1985

your code can look like:

“by require ‘xml’

catalog = XML.new ‘data.xml’ catalog.print

which will output:

“ag, || name, |xml| attribute, |version=“1.0”| name, |version| value, |“1.0”| attribute, |encoding=“UTF-8”| name, |encoding| value, |“UTF-8”| tag,

| name, |catalog| tag, || name, |cd| tag, || name, |title| content, |Empire Burlesque| close_tag, || name, |title| tag, || name, |artist| content, |Bob Dylan| close_tag, || name, |artist| tag, || name, |country| content, |USA| close_tag, || name, |country| tag, || name, |company| content, |Columbia| close_tag, || name, |company| tag, || name, |price| content, |10.90| close_tag, || name, |price| tag, || name, |year| content, |1985| close_tag, || name, |year| close_tag, || name, |cd| close_tag, |

| name, |catalog|

The Token class is an Array of Tokens and has ‘name’ and ‘text’ attributes. A Document is a kind of Token. You can iterate through the parsed file examining and editing tags and content. Given the prior code:

“by

print a list of titles and artists

catalog.each {|title| puts title.name puts title.text

index = 0
title.each {|field|
	if field.name == 'tag' && field[0].name == 'artist'
		# this should be the artist name
		puts title[index+1].text
	end
	index += 1
}

}

It is important to note that in definitions order matters! The most specific cases must be matched before the more general ones.

Features

  • definitions are entirely based on regular expressions
  • definitions are stored as hash trees, so they can build on each other (e.g. HTML can be defined as an extension of XML)

Future Expansion

This will be the basis for further projects such as an xslt processor. A logical next step will be to add XPath features.

Installation

Install parser by running:

sudo gem install magic_parser

Contribute

  • Issue Tracker: https://github.com/pamirian7/magic_parser/issues
  • Source Code: https://github.com/pamirian7/magic_parser

License

(The MIT License)

Copyright © Paul Amirian

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.