1# tokenise the input file
2
3function parse_error(msg) {
4	printf("PARSE ERROR: %s\nLine "NR" : "$0"\n", msg);
5	exit 1;
6}
7
8# ignore multi-line C comments.
9{
10  if (t = index($0, "/*")) {
11    if (t > 1)
12      tmp = substr($0, 1, t - 1)
13    else
14      tmp = ""
15    u = index(substr($0, t + 2), "*/")
16    while (u == 0) {
17      getline
18      t = -1
19      u = index($0, "*/")
20    }
21    if (u <= length($0) - 2)
22      $0 = tmp substr($0, t + u + 3)
23    else
24      $0 = tmp
25  }
26}
27
28# ignore blank lines
29/^[ \t]*$/ {
30	next;
31}
32
33/^\#define.*/ {
34	split($0,a,"[ \t;]*");
35	parse_define(a[2], a[3]);
36	next;
37}
38
39# ignore comments
40/^[ \t]*\#/ {
41	next;
42}
43
44/^[ \t]*module/ {
45	{if (module!="") parse_error("you can only specify one module name");}
46	start_module($2);
47	next;
48}
49
50{if (module=="") parse_error("you must specify the module name first");}
51
52/^[ \t]*option/ {
53	set_option($2, $3);
54	next;
55}
56
57/^[ \t]*typedef struct.*\{/ {
58	{if (current_struct!="") parse_error("you cannot have nested structures");}
59	start_struct($3);
60	next;
61}
62
63/^[ \t]*struct.*\{/ {
64	{if (current_struct!="") parse_error("you cannot have nested structures");}
65	start_struct($2);
66	next;
67}
68
69/^[ \t]*typedef union.*\{/ {
70	{if (current_struct!="") parse_error("this cannot appear inside a structure");}
71	split($0,a,"[ \t;()]*");
72	start_union_encap(a[4], a[6], a[7], a[8]);
73	next;
74}
75
76/^[ \t]*void.*\(/ {
77	{if (current_struct!="") parse_error("you cannot have nested structures");}
78	split($0,a,"[ \t;()]*");
79	start_function(a[2], a[3]);
80	return_result="void";
81	next;
82}
83
84/^[ \t]*STATUS.*\(|^[ \t]*void.*\(/ {
85	{if (current_struct!="") parse_error("you cannot have nested structures");}
86	split($0,a,"[ \t;()]*");
87	start_function(a[2], a[3]);
88	return_result="STATUS";
89	next;
90}
91
92{if (current_struct=="") parse_error("this must appear inside a structure");}
93
94/^[ \t]*union.*\{/ {
95	{if (current_union!="") parse_error("you cannot have nested unions");}
96	start_union($2);
97	next;
98}
99
100/^[ \t]*\[switch_is.*union.*\{/ {
101	{if (current_union!="") parse_error("you cannot have nested unions");}
102	split($0,a,"[ \t;()]*");
103	start_union_notencap(a[3]);
104	next;
105}
106
107/^[ \t]*case.*;/ {
108	{if (current_union=="") parse_error("this must appear inide a union");}
109	split($0,a,"[ \t;]*");
110	parse_case(a[3],a[4],a[5]);
111	next;
112}
113
114/^[ \t]*\[case(.*)\].*;/ {
115	{if (current_union=="") parse_error("this must appear inide a union");}
116	split($0,a,"[ \t;()[\]]*");
117	parse_case(a[6],a[8],a[9]);
118	next;
119}
120
121/^[ \t]*\}$/ {
122	{if (current_union=="") parse_error("this must appear inside a union");}
123	end_union("");
124	next;
125}
126
127/^[ \t]*\} .*;/ {
128	if (current_union!="") {
129		split($2,a,"[ \t;]*");
130		end_union(a[1]);
131		next;
132	}
133}
134
135{if (current_union!="") parse_error("this cannot appear inside a union");}
136
137/^[ \t]*\};/ {
138	end_struct("");
139	next;
140}
141
142/^[ \t]*\} .*;/ {
143	split($2,a,"[ \t;]*");
144	end_struct(a[1]);
145	next;
146}
147
148/^[ \t]*\);/ {
149	end_function();
150	return_result="";
151	next;
152}
153
154/^.*size_is.*\*.*;/ {
155	split($0,a,"[ \t;()]*");
156	add_sizeis_array(a[3], a[5], a[6]);
157	next;
158}
159
160/^.*;/ {
161	split($0,a,"[ \t;]*");
162	add_struct_elem(a[2], a[3]);
163	next;
164}
165
166/^[\t ]*void/ {
167	next;
168}
169
170/^[ \t]*\[.*\].*/ {
171	split($0,a,"[ \t;]*");
172	split(a[4], b, "[,]");
173	add_function_param(a[2], a[3], b[1]);
174	next;
175}
176
177{
178	parse_error("Unknown construct.");
179}
180
181