Created
September 23, 2014 00:33
-
-
Save kangguru/31993974104109dc29ec to your computer and use it in GitHub Desktop.
MT940 treetop grammar - (proof of concept)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'treetop' | |
require 'active_support/all' | |
Treetop.load 'mt940' | |
parser = MT940Parser.new | |
parser.parse(':61:foo:86:bar:86:bza:61:baz:61:fogoo').elements.each do |st| | |
st.elements.each do |st| | |
if st.try(:entry?) | |
puts "Statement: #{st.text_value}" | |
elsif st.elements.any? | |
puts "-> Additions: #{st.elements.map(&:text_value)}" | |
end | |
end | |
puts "--" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# my_grammar.treetop | |
grammar MT940 | |
rule statement | |
(entry (additions)* )+ | |
end | |
rule entry | |
':61:' ([a-zA-Z])+ { | |
def content | |
[:text, text_value] | |
end | |
def entry? | |
true | |
end | |
} | |
end | |
rule additions | |
':86:' ([a-zA-Z])+ { | |
def entry? | |
false | |
end | |
} | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment