summaryrefslogtreecommitdiff
path: root/vtparse/src/vtparse_gen_c_tables.rb
blob: ed5655e387c0157c3292164f9c6f64da264aebee (plain)
1
2
3
4
5
6
7
8
9
10
11
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

require 'vtparse_tables'

class String
    def pad(len)
        self << (" " * (len - self.length))
    end
end

File.open("vtparse_table.h", "w") { |f|
    f.puts "typedef enum {"
    $states_in_order.each_with_index { |state, i|
        f.puts "   VTPARSE_STATE_#{state.to_s.upcase} = #{i},"
    }
    f.puts "} vtparse_state_t;"
    f.puts
    f.puts "typedef enum {"
    $actions_in_order.each_with_index { |action, i|
        f.puts "   VTPARSE_ACTION_#{action.to_s.upcase} = #{i+1},"
    }
    f.puts "} vtparse_action_t;"
    f.puts
    f.puts "typedef unsigned char state_change_t;"
    f.puts "extern state_change_t STATE_TABLE[#{$states_in_order.length}][256];"
    f.puts "extern vtparse_action_t ENTRY_ACTIONS[#{$states_in_order.length}];"
    f.puts "extern vtparse_action_t EXIT_ACTIONS[#{$states_in_order.length}];"
    f.puts "extern char *ACTION_NAMES[#{$actions_in_order.length+1}];"
    f.puts "extern char *STATE_NAMES[#{$states_in_order.length}];"
    f.puts
}

puts "Wrote vtparse_table.h"

File.open("vtparse_table.c", "w") { |f|
    f.puts
    f.puts '#include "vtparse_table.h"'
    f.puts
    f.puts "char *ACTION_NAMES[] = {"
    f.puts "   \"<no action>\","
    $actions_in_order.each { |action|
        f.puts "   \"#{action.to_s.upcase}\","
    }
    f.puts "};"
    f.puts
    f.puts "char *STATE_NAMES[] = {"
    $states_in_order.each { |state|
        f.puts "   \"#{state.to_s}\","
    }
    f.puts "};"
    f.puts
    f.puts "state_change_t STATE_TABLE[#{$states_in_order.length}][256] = {"
    $states_in_order.each { |state|
        f.puts "  {  /* VTPARSE_STATE_#{state.to_s.upcase} */"
        $state_tables[state].each_with_index { |state_change, i|
            if not state_change
                f.puts "    0,"
            else
                (action,) = state_change.find_all { |s| s.kind_of?(Symbol) }
                (state,)  = state_change.find_all { |s| s.kind_of?(StateTransition) }
                action_str = action ? "VTPARSE_ACTION_#{action.to_s.upcase}" : "0"
                state_str =  state ? "VTPARSE_STATE_#{state.to_state.to_s}" : "0"
                f.puts "/*#{i.to_s.pad(3)}*/  #{action_str.pad(33)} | (#{state_str.pad(33)} << 4),"
            end
        }
        f.puts "  },"
    }

    f.puts "};"
    f.puts
    f.puts "vtparse_action_t ENTRY_ACTIONS[] = {"
    $states_in_order.each { |state|
        actions = $states[state]
        if actions[:on_entry]
            f.puts "   VTPARSE_ACTION_#{actions[:on_entry].to_s.upcase}, /* #{state} */"
        else
            f.puts "   0  /* none for #{state} */,"
        end
    }
    f.puts "};"
    f.puts
    f.puts "vtparse_action_t EXIT_ACTIONS[] = {"
    $states_in_order.each { |state|
        actions = $states[state]
        if actions[:on_exit]
            f.puts "   VTPARSE_ACTION_#{actions[:on_exit].to_s.upcase}, /* #{state} */"
        else
            f.puts "   0  /* none for #{state} */,"
        end
    }
    f.puts "};"
    f.puts
}

puts "Wrote vtparse_table.c"