mcparse.y 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. %{ /* mcparse.y -- parser for Windows mc files
  2. Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. Parser for Windows mc files
  4. Written by Kai Tietz, Onevision.
  5. This file is part of GNU Binutils.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. /* This is a parser for Windows rc files. It is based on the parser
  19. by Gunther Ebert <gunther.ebert@ixos-leipzig.de>. */
  20. #include "sysdep.h"
  21. #include "bfd.h"
  22. #include "bucomm.h"
  23. #include "libiberty.h"
  24. #include "windmc.h"
  25. #include "safe-ctype.h"
  26. static rc_uint_type mc_last_id = 0;
  27. static rc_uint_type mc_sefa_val = 0;
  28. static unichar *mc_last_symbol = NULL;
  29. static const mc_keyword *mc_cur_severity = NULL;
  30. static const mc_keyword *mc_cur_facility = NULL;
  31. static mc_node *cur_node = NULL;
  32. %}
  33. %union
  34. {
  35. rc_uint_type ival;
  36. unichar *ustr;
  37. const mc_keyword *tok;
  38. mc_node *nod;
  39. };
  40. %start input
  41. %token NL
  42. %token<ustr> MCIDENT MCFILENAME MCLINE MCCOMMENT
  43. %token<tok> MCTOKEN
  44. %token MCENDLINE
  45. %token MCLANGUAGENAMES MCFACILITYNAMES MCSEVERITYNAMES MCOUTPUTBASE MCMESSAGEIDTYPEDEF
  46. %token MCLANGUAGE MCMESSAGEID MCSEVERITY MCFACILITY MCSYMBOLICNAME
  47. %token <ival> MCNUMBER
  48. %type<ival> id vid sefasy_def
  49. %type<ustr> alias_name token lines comments
  50. %type<tok> lang
  51. %%
  52. input: entities
  53. ;
  54. entities:
  55. /* empty */
  56. | entities entity
  57. ;
  58. entity: global_section
  59. | message
  60. | comments
  61. {
  62. cur_node = mc_add_node ();
  63. cur_node->user_text = $1;
  64. }
  65. | error { mc_fatal ("syntax error"); }
  66. ;
  67. global_section:
  68. MCSEVERITYNAMES '=' '(' severitymaps ')'
  69. | MCSEVERITYNAMES '=' '(' severitymaps error { mc_fatal ("missing ')' in SeverityNames"); }
  70. | MCSEVERITYNAMES '=' error { mc_fatal ("missing '(' in SeverityNames"); }
  71. | MCSEVERITYNAMES error { mc_fatal ("missing '=' for SeverityNames"); }
  72. | MCLANGUAGENAMES '=' '(' langmaps ')'
  73. | MCLANGUAGENAMES '=' '(' langmaps error { mc_fatal ("missing ')' in LanguageNames"); }
  74. | MCLANGUAGENAMES '=' error { mc_fatal ("missing '(' in LanguageNames"); }
  75. | MCLANGUAGENAMES error { mc_fatal ("missing '=' for LanguageNames"); }
  76. | MCFACILITYNAMES '=' '(' facilitymaps ')'
  77. | MCFACILITYNAMES '=' '(' facilitymaps error { mc_fatal ("missing ')' in FacilityNames"); }
  78. | MCFACILITYNAMES '=' error { mc_fatal ("missing '(' in FacilityNames"); }
  79. | MCFACILITYNAMES error { mc_fatal ("missing '=' for FacilityNames"); }
  80. | MCOUTPUTBASE '=' MCNUMBER
  81. {
  82. if ($3 != 10 && $3 != 16)
  83. mc_fatal ("OutputBase allows 10 or 16 as value");
  84. mcset_out_values_are_decimal = ($3 == 10 ? 1 : 0);
  85. }
  86. | MCMESSAGEIDTYPEDEF '=' MCIDENT
  87. {
  88. mcset_msg_id_typedef = $3;
  89. }
  90. | MCMESSAGEIDTYPEDEF '=' error
  91. {
  92. mc_fatal ("MessageIdTypedef expects an identifier");
  93. }
  94. | MCMESSAGEIDTYPEDEF error
  95. {
  96. mc_fatal ("missing '=' for MessageIdTypedef");
  97. }
  98. ;
  99. severitymaps:
  100. severitymap
  101. | severitymaps severitymap
  102. | error { mc_fatal ("severity ident missing"); }
  103. ;
  104. severitymap:
  105. token '=' MCNUMBER alias_name
  106. {
  107. mc_add_keyword ($1, MCTOKEN, "severity", $3, $4);
  108. }
  109. | token '=' error { mc_fatal ("severity number missing"); }
  110. | token error { mc_fatal ("severity missing '='"); }
  111. ;
  112. facilitymaps:
  113. facilitymap
  114. | facilitymaps facilitymap
  115. | error { mc_fatal ("missing ident in FacilityNames"); }
  116. ;
  117. facilitymap:
  118. token '=' MCNUMBER alias_name
  119. {
  120. mc_add_keyword ($1, MCTOKEN, "facility", $3, $4);
  121. }
  122. | token '=' error { mc_fatal ("facility number missing"); }
  123. | token error { mc_fatal ("facility missing '='"); }
  124. ;
  125. langmaps:
  126. langmap
  127. | langmaps langmap
  128. | error { mc_fatal ("missing ident in LanguageNames"); }
  129. ;
  130. langmap:
  131. token '=' MCNUMBER lex_want_filename ':' MCFILENAME
  132. {
  133. mc_add_keyword ($1, MCTOKEN, "language", $3, $6);
  134. }
  135. | token '=' MCNUMBER lex_want_filename ':' error { mc_fatal ("missing filename in LanguageNames"); }
  136. | token '=' MCNUMBER error { mc_fatal ("missing ':' in LanguageNames"); }
  137. | token '=' error { mc_fatal ("missing language code in LanguageNames"); }
  138. | token error { mc_fatal ("missing '=' for LanguageNames"); }
  139. ;
  140. alias_name:
  141. /* empty */
  142. {
  143. $$ = NULL;
  144. }
  145. | ':' MCIDENT
  146. {
  147. $$ = $2;
  148. }
  149. | ':' error { mc_fatal ("illegal token in identifier"); $$ = NULL; }
  150. ;
  151. message:
  152. id sefasy_def
  153. {
  154. cur_node = mc_add_node ();
  155. cur_node->symbol = mc_last_symbol;
  156. cur_node->facility = mc_cur_facility;
  157. cur_node->severity = mc_cur_severity;
  158. cur_node->id = ($1 & 0xffffUL);
  159. cur_node->vid = ($1 & 0xffffUL) | mc_sefa_val;
  160. mc_last_id = $1;
  161. }
  162. lang_entities
  163. ;
  164. id: MCMESSAGEID '=' vid { $$ = $3; }
  165. | MCMESSAGEID '=' error { mc_fatal ("missing number in MessageId"); $$ = 0; }
  166. | MCMESSAGEID error { mc_fatal ("missing '=' for MessageId"); $$ = 0; }
  167. ;
  168. vid: /* empty */
  169. {
  170. $$ = ++mc_last_id;
  171. }
  172. | MCNUMBER
  173. {
  174. $$ = $1;
  175. }
  176. | '+' MCNUMBER
  177. {
  178. $$ = mc_last_id + $2;
  179. }
  180. | '+' error { mc_fatal ("missing number after MessageId '+'"); }
  181. ;
  182. sefasy_def:
  183. /* empty */
  184. {
  185. $$ = 0;
  186. mc_sefa_val = (mcset_custom_bit ? 1 : 0) << 29;
  187. mc_last_symbol = NULL;
  188. mc_cur_severity = NULL;
  189. mc_cur_facility = NULL;
  190. }
  191. | sefasy_def severity
  192. {
  193. if ($1 & 1)
  194. mc_warn (_("duplicate definition of Severity"));
  195. $$ = $1 | 1;
  196. }
  197. | sefasy_def facility
  198. {
  199. if ($1 & 2)
  200. mc_warn (_("duplicate definition of Facility"));
  201. $$ = $1 | 2;
  202. }
  203. | sefasy_def symbol
  204. {
  205. if ($1 & 4)
  206. mc_warn (_("duplicate definition of SymbolicName"));
  207. $$ = $1 | 4;
  208. }
  209. ;
  210. severity: MCSEVERITY '=' MCTOKEN
  211. {
  212. mc_sefa_val &= ~ (0x3UL << 30);
  213. mc_sefa_val |= (($3->nval & 0x3UL) << 30);
  214. mc_cur_severity = $3;
  215. }
  216. ;
  217. facility: MCFACILITY '=' MCTOKEN
  218. {
  219. mc_sefa_val &= ~ (0xfffUL << 16);
  220. mc_sefa_val |= (($3->nval & 0xfffUL) << 16);
  221. mc_cur_facility = $3;
  222. }
  223. ;
  224. symbol: MCSYMBOLICNAME '=' MCIDENT
  225. {
  226. mc_last_symbol = $3;
  227. }
  228. ;
  229. lang_entities:
  230. lang_entity
  231. | lang_entities lang_entity
  232. ;
  233. lang_entity:
  234. lang lex_want_line lines MCENDLINE
  235. {
  236. mc_node_lang *h;
  237. h = mc_add_node_lang (cur_node, $1, cur_node->vid);
  238. h->message = $3;
  239. if (mcset_max_message_length != 0 && unichar_len (h->message) > mcset_max_message_length)
  240. mc_warn ("message length to long");
  241. }
  242. ;
  243. lines: MCLINE
  244. {
  245. $$ = $1;
  246. }
  247. | lines MCLINE
  248. {
  249. unichar *h;
  250. rc_uint_type l1,l2;
  251. l1 = unichar_len ($1);
  252. l2 = unichar_len ($2);
  253. h = (unichar *) res_alloc ((l1 + l2 + 1) * sizeof (unichar));
  254. if (l1) memcpy (h, $1, l1 * sizeof (unichar));
  255. if (l2) memcpy (&h[l1], $2, l2 * sizeof (unichar));
  256. h[l1 + l2] = 0;
  257. $$ = h;
  258. }
  259. | error { mc_fatal ("missing end of message text"); $$ = NULL; }
  260. | lines error { mc_fatal ("missing end of message text"); $$ = $1; }
  261. ;
  262. comments: MCCOMMENT { $$ = $1; }
  263. | comments MCCOMMENT
  264. {
  265. unichar *h;
  266. rc_uint_type l1,l2;
  267. l1 = unichar_len ($1);
  268. l2 = unichar_len ($2);
  269. h = (unichar *) res_alloc ((l1 + l2 + 1) * sizeof (unichar));
  270. if (l1) memcpy (h, $1, l1 * sizeof (unichar));
  271. if (l2) memcpy (&h[l1], $2, l2 * sizeof (unichar));
  272. h[l1 + l2] = 0;
  273. $$ = h;
  274. }
  275. ;
  276. lang: MCLANGUAGE lex_want_nl '=' MCTOKEN NL
  277. {
  278. $$ = $4;
  279. }
  280. | MCLANGUAGE lex_want_nl '=' MCIDENT NL
  281. {
  282. $$ = NULL;
  283. mc_fatal (_("undeclared language identifier"));
  284. }
  285. | MCLANGUAGE lex_want_nl '=' token error
  286. {
  287. $$ = NULL;
  288. mc_fatal ("missing newline after Language");
  289. }
  290. | MCLANGUAGE lex_want_nl '=' error
  291. {
  292. $$ = NULL;
  293. mc_fatal ("missing ident for Language");
  294. }
  295. | MCLANGUAGE error
  296. {
  297. $$ = NULL;
  298. mc_fatal ("missing '=' for Language");
  299. }
  300. ;
  301. token: MCIDENT { $$ = $1; }
  302. | MCTOKEN { $$ = $1->usz; }
  303. ;
  304. lex_want_nl:
  305. /* Empty */ { mclex_want_nl = 1; }
  306. ;
  307. lex_want_line:
  308. /* Empty */ { mclex_want_line = 1; }
  309. ;
  310. lex_want_filename:
  311. /* Empty */ { mclex_want_filename = 1; }
  312. ;
  313. %%
  314. /* Something else. */