Sha256: 1ffe65833554c20ad21a40fc1b6d5d2f0ae7f4ff8c572ab7ec21a19583909160
Contents?: true
Size: 1.33 KB
Versions: 1
Compression:
Stored size: 1.33 KB
Contents
module Yagg Error = Class.new(Exception) class Parser attr_accessor :header, :body, :tokens, :rules, :token_groups ParseError = Class.new(Exception) def initialize(text) @source = text end def result self end def setup_sections if @source =~ /([\w\W]*?)^%%\s*([\w\W]*?)^%%\s*/ self.header, self.body, * = $1, $2 else raise ParseError, "can't recognize format" end end def parse_tokens self.tokens = self.header.scan(/^%token(.*)/).flat_map{|x| x.flat_map{|y| y.split(" ")}} end def parse_rule(arr) arr = arr[0..-1] name = arr.shift arr.shift groups = [[]] arr.each do |t| if t == '|' groups.push [] else groups.last.push t end end {name => groups} end def parse_rules tokens = self.body.scan(/[^'\s]+|'.'/).to_a self.token_groups = [[]] tokens.each do |t| if t != ';' self.token_groups.last << t else self.token_groups << [] end end self.token_groups.pop if self.token_groups.last.empty? self.rules = self.token_groups.inject({}){|a, b| a.update(parse_rule(b))} end def parse setup_sections parse_tokens parse_rules end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
yagg-0.1.1 | lib/yagg/parsery.rb |