Initial import.
[vtparse.git] / vtparse_gen_c_tables.rb
blobed5655e387c0157c3292164f9c6f64da264aebee
2 require 'vtparse_tables'
4 class String
5     def pad(len)
6         self << (" " * (len - self.length))
7     end
8 end
10 File.open("vtparse_table.h", "w") { |f|
11     f.puts "typedef enum {"
12     $states_in_order.each_with_index { |state, i|
13         f.puts "   VTPARSE_STATE_#{state.to_s.upcase} = #{i},"
14     }
15     f.puts "} vtparse_state_t;"
16     f.puts
17     f.puts "typedef enum {"
18     $actions_in_order.each_with_index { |action, i|
19         f.puts "   VTPARSE_ACTION_#{action.to_s.upcase} = #{i+1},"
20     }
21     f.puts "} vtparse_action_t;"
22     f.puts
23     f.puts "typedef unsigned char state_change_t;"
24     f.puts "extern state_change_t STATE_TABLE[#{$states_in_order.length}][256];"
25     f.puts "extern vtparse_action_t ENTRY_ACTIONS[#{$states_in_order.length}];"
26     f.puts "extern vtparse_action_t EXIT_ACTIONS[#{$states_in_order.length}];"
27     f.puts "extern char *ACTION_NAMES[#{$actions_in_order.length+1}];"
28     f.puts "extern char *STATE_NAMES[#{$states_in_order.length}];"
29     f.puts
32 puts "Wrote vtparse_table.h"
34 File.open("vtparse_table.c", "w") { |f|
35     f.puts
36     f.puts '#include "vtparse_table.h"'
37     f.puts
38     f.puts "char *ACTION_NAMES[] = {"
39     f.puts "   \"<no action>\","
40     $actions_in_order.each { |action|
41         f.puts "   \"#{action.to_s.upcase}\","
42     }
43     f.puts "};"
44     f.puts
45     f.puts "char *STATE_NAMES[] = {"
46     $states_in_order.each { |state|
47         f.puts "   \"#{state.to_s}\","
48     }
49     f.puts "};"
50     f.puts
51     f.puts "state_change_t STATE_TABLE[#{$states_in_order.length}][256] = {"
52     $states_in_order.each { |state|
53         f.puts "  {  /* VTPARSE_STATE_#{state.to_s.upcase} */"
54         $state_tables[state].each_with_index { |state_change, i|
55             if not state_change
56                 f.puts "    0,"
57             else
58                 (action,) = state_change.find_all { |s| s.kind_of?(Symbol) }
59                 (state,)  = state_change.find_all { |s| s.kind_of?(StateTransition) }
60                 action_str = action ? "VTPARSE_ACTION_#{action.to_s.upcase}" : "0"
61                 state_str =  state ? "VTPARSE_STATE_#{state.to_state.to_s}" : "0"
62                 f.puts "/*#{i.to_s.pad(3)}*/  #{action_str.pad(33)} | (#{state_str.pad(33)} << 4),"
63             end
64         }
65         f.puts "  },"
66     }
68     f.puts "};"
69     f.puts
70     f.puts "vtparse_action_t ENTRY_ACTIONS[] = {"
71     $states_in_order.each { |state|
72         actions = $states[state]
73         if actions[:on_entry]
74             f.puts "   VTPARSE_ACTION_#{actions[:on_entry].to_s.upcase}, /* #{state} */"
75         else
76             f.puts "   0  /* none for #{state} */,"
77         end
78     }
79     f.puts "};"
80     f.puts
81     f.puts "vtparse_action_t EXIT_ACTIONS[] = {"
82     $states_in_order.each { |state|
83         actions = $states[state]
84         if actions[:on_exit]
85             f.puts "   VTPARSE_ACTION_#{actions[:on_exit].to_s.upcase}, /* #{state} */"
86         else
87             f.puts "   0  /* none for #{state} */,"
88         end
89     }
90     f.puts "};"
91     f.puts
94 puts "Wrote vtparse_table.c"