Add Julian Scheid to credits, and add TODO note about character set support.
[vtparse.git] / vtparse_gen_c_tables.rb
blob811706bccbbf0974e670e8a5c28f2f584d5cdbfb
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+1},"
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+1}];"
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     f.puts "   \"<no state>\","
47     $states_in_order.each { |state|
48         f.puts "   \"#{state.to_s}\","
49     }
50     f.puts "};"
51     f.puts
52     f.puts "state_change_t STATE_TABLE[#{$states_in_order.length}][256] = {"
53     $states_in_order.each_with_index { |state, i|
54         f.puts "  {  /* VTPARSE_STATE_#{state.to_s.upcase} = #{i} */"
55         $state_tables[state].each_with_index { |state_change, i|
56             if not state_change
57                 f.puts "    0,"
58             else
59                 (action,) = state_change.find_all { |s| s.kind_of?(Symbol) }
60                 (state,)  = state_change.find_all { |s| s.kind_of?(StateTransition) }
61                 action_str = action ? "VTPARSE_ACTION_#{action.to_s.upcase}" : "0"
62                 state_str =  state ? "VTPARSE_STATE_#{state.to_state.to_s}" : "0"
63                 f.puts "/*#{i.to_s.pad(3)}*/  #{action_str.pad(33)} | (#{state_str.pad(33)} << 4),"
64             end
65         }
66         f.puts "  },"
67     }
69     f.puts "};"
70     f.puts
71     f.puts "vtparse_action_t ENTRY_ACTIONS[] = {"
72     $states_in_order.each { |state|
73         actions = $states[state]
74         if actions[:on_entry]
75             f.puts "   VTPARSE_ACTION_#{actions[:on_entry].to_s.upcase}, /* #{state} */"
76         else
77             f.puts "   0  /* none for #{state} */,"
78         end
79     }
80     f.puts "};"
81     f.puts
82     f.puts "vtparse_action_t EXIT_ACTIONS[] = {"
83     $states_in_order.each { |state|
84         actions = $states[state]
85         if actions[:on_exit]
86             f.puts "   VTPARSE_ACTION_#{actions[:on_exit].to_s.upcase}, /* #{state} */"
87         else
88             f.puts "   0  /* none for #{state} */,"
89         end
90     }
91     f.puts "};"
92     f.puts
95 puts "Wrote vtparse_table.c"