Class: GenMachine::SpecParser
- Inherits:
-
Object
- Object
- GenMachine::SpecParser
- Defined in:
- lib/genmachine/spec_parser.rb
Overview
This is a quick and dirty parser used for bootstrapping. Which means it’ll eventually be replaced when the real parser is written as a genmachine table.
Constant Summary collapse
- ESCAPES =
{'\t' => "\t", '\n' => "\n", '\r' => "\r", '\f' => "\f", '\b' => "\b", '\a' => "\a", '\e' => "\e", '\s' => " ", "\\\\" => '\\'}
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize(files, opts) ⇒ SpecParser
constructor
A new instance of SpecParser.
- #parse_combine_ranges(raw, input) ⇒ Object
- #parse_input(val, inputs = nil) ⇒ Object
-
#process_states(instates) ⇒ Object
consolidate same-name states and (eventually) combine / optimize where appropriate.
Constructor Details
#initialize(files, opts) ⇒ SpecParser
Returns a new instance of SpecParser.
6 7 8 9 10 |
# File 'lib/genmachine/spec_parser.rb', line 6 def initialize(files,opts) @table = [] @files = files @opts = opts end |
Instance Method Details
#build ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/genmachine/spec_parser.rb', line 12 def build c_name = c_type = c_args = c_cmds = c_first_state = c_states = nil new_fun = false @files.each do |fname| File.new(fname,'r').each_with_index do |line, line_no| line = line.strip det = line[0..0] if det == '|' or det == ':' re = (det=='|' ? '\|' : det) + '(?: |$)' cols = line.split(/#{re}/,-1)[1..-1].map(&:strip) if det=='|' && cols[0].include?('(') new_fun = true unless c_name.nil? @table << [c_name, c_type, c_args, c_cmds, c_first_state, process_states(c_states)] end parts = cols[0].split('(') c_name = parts.shift.to_underscored c_args, c_type = parts.join('(').split(/::(U|\[\]|\{\})$/) c_args = c_args[0..-2].split(',') c_type ||= '[]' c_states = [] c_cmds = (cols[3]||'').split(';') c_first_state = cols[4] # TODO: error if cols[1] or cols[2] have anything elsif det == ':' && new_fun c_args += cols[0].sub(/\)$/,'').split(',') c_cmds += (cols[3]||'').split(';') c_first_state += (cols[4]||'') elsif det == '|' new_fun = false conditionals, inputs = parse_input(cols[1]) c_states << {:name => cols[0], :input => inputs, :cond => conditionals, :acc => cols[2], :exprs => (cols[3]||'').split(';').map(&:strip), :next => cols[4]} elsif det == ':' && (c_states.size > 0) conditionals, inputs = parse_input(cols[1],c_states[-1][:input]) c_states[-1][:name] += (cols[0]||'') c_states[-1][:input] = inputs c_states[-1][:cond] += conditionals c_states[-1][:acc] += cols[2] c_states[-1][:exprs]+= (cols[3]||'').split(';').map(&:strip) c_states[-1][:next] += cols[4] end end end unless c_name.nil? @table << [c_name, c_type, c_args, c_cmds, c_first_state, process_states(c_states)] end end if @opts[:debug] require 'pp' pp @table end return @table end |
#parse_combine_ranges(raw, input) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/genmachine/spec_parser.rb', line 118 def parse_combine_ranges(raw, input) raw.gsub!(/\\[tnrfbaes\\]/){|m| ESCAPES[m]} raw.gsub!('<left-square-bracket>', '[') raw.gsub!('<right-square-bracket>', ']') if raw =~ /((?:.-.)*)((?:.)*)/um ranges = $1 singles = $2 if ranges.length > 0 _, range, ranges = ranges.partition /.-./um input << range end while ranges.length > 0 singles.scan(/./um).each{|s| input << s} end end |
#parse_input(val, inputs = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/genmachine/spec_parser.rb', line 83 def parse_input(val,inputs=nil) iters = 0 conds = [] val.gsub! /([^\\])\\\[/, '\1<left-square-bracket>' val.gsub! /([^\\])\\\]/, '\1<right-square-bracket>' while val.strip.length > 0 && iters < 100 case when val =~ /--+/um val.sub!($&,'') when val =~ /\s*\{([^\}]+)\}\s*/um conds << $1 val.sub!($&, '') when val =~ /\s*\[\^([^\]]+)\]\s*/um inputs ||= CharSet.new(:exclude) parse_combine_ranges($1, inputs) val.sub!($&, '') when val =~ /\s*\[([^\]]+)\]\s*/um inputs ||= CharSet.new(:include) parse_combine_ranges($1, inputs) val.sub!($&, '') when val =~ /^\s*\./um inputs ||= CharSet.new(:include) inputs << :any val.sub!($&, '') end iters += 1 end return conds, inputs end |
#process_states(instates) ⇒ Object
consolidate same-name states and (eventually) combine / optimize where appropriate.
73 74 75 76 77 78 79 80 81 |
# File 'lib/genmachine/spec_parser.rb', line 73 def process_states(instates) outstates = {} instates.each do |inst| name = inst.delete(:name) outstates[name] ||= [] outstates[name] << inst end return outstates end |