mclex.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* mclex.c -- lexer for Windows mc files parser.
  2. Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. Written by Kai Tietz, Onevision.
  4. This file is part of GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  16. 02110-1301, USA. */
  17. /* This is a lexer used by the Windows rc file parser.
  18. It basically just recognized a bunch of keywords. */
  19. #include "sysdep.h"
  20. #include "bfd.h"
  21. #include "bucomm.h"
  22. #include "libiberty.h"
  23. #include "safe-ctype.h"
  24. #include "windmc.h"
  25. #include "mcparse.h"
  26. #include <assert.h>
  27. /* Exported globals. */
  28. bfd_boolean mclex_want_nl = FALSE;
  29. bfd_boolean mclex_want_line = FALSE;
  30. bfd_boolean mclex_want_filename = FALSE;
  31. /* Local globals. */
  32. static unichar *input_stream = NULL;
  33. static unichar *input_stream_pos = NULL;
  34. static int input_line = 1;
  35. static const char *input_filename = NULL;
  36. void
  37. mc_set_content (const unichar *src)
  38. {
  39. if (!src)
  40. return;
  41. input_stream = input_stream_pos = unichar_dup (src);
  42. }
  43. void
  44. mc_set_inputfile (const char *name)
  45. {
  46. if (! name || *name == 0)
  47. input_filename = "-";
  48. else
  49. {
  50. const char *s1 = strrchr (name, '/');
  51. const char *s2 = strrchr (name, '\\');
  52. if (! s1)
  53. s1 = s2;
  54. if (s1 && s2 && s1 < s2)
  55. s1 = s2;
  56. if (! s1)
  57. s1 = name;
  58. else
  59. s1++;
  60. s1 = xstrdup (s1);
  61. input_filename = s1;
  62. }
  63. }
  64. static void
  65. show_msg (const char *kind, const char *msg, va_list argp)
  66. {
  67. fprintf (stderr, "In %s at line %d: %s: ", input_filename, input_line, kind);
  68. vfprintf (stderr, msg, argp);
  69. fprintf (stderr, ".\n");
  70. }
  71. void
  72. mc_warn (const char *s, ...)
  73. {
  74. va_list argp;
  75. va_start (argp, s);
  76. show_msg ("warning", s, argp);
  77. va_end (argp);
  78. }
  79. void
  80. mc_fatal (const char *s, ...)
  81. {
  82. va_list argp;
  83. va_start (argp, s);
  84. show_msg ("fatal", s, argp);
  85. va_end (argp);
  86. xexit (1);
  87. }
  88. int
  89. yyerror (const char *s, ...)
  90. {
  91. va_list argp;
  92. va_start (argp, s);
  93. show_msg ("parser", s, argp);
  94. va_end (argp);
  95. return 1;
  96. }
  97. static unichar *
  98. get_diff (unichar *end, unichar *start)
  99. {
  100. unichar *ret;
  101. unichar save = *end;
  102. *end = 0;
  103. ret = unichar_dup (start);
  104. *end = save;
  105. return ret;
  106. }
  107. static rc_uint_type
  108. parse_digit (unichar ch)
  109. {
  110. rc_uint_type base = 10, v = 0, c;
  111. if (ch == '0')
  112. {
  113. base = 8;
  114. switch (input_stream_pos[0])
  115. {
  116. case 'x': case 'X': base = 16; input_stream_pos++; break;
  117. case 'o': case 'O': base = 8; input_stream_pos++; break;
  118. case 'b': case 'B': base = 2; input_stream_pos++; break;
  119. }
  120. }
  121. else
  122. v = (rc_uint_type) (ch - '0');
  123. while ((ch = input_stream_pos[0]) != 0)
  124. {
  125. if (ch >= 'A' && ch <= 'F')
  126. c = (rc_uint_type) (ch - 'A') + 10;
  127. else if (ch >= 'a' && ch <= 'f')
  128. c = (rc_uint_type) (ch - 'a') + 10;
  129. else if (ch >= '0' && ch <= '9')
  130. c = (rc_uint_type) (ch - '0');
  131. else
  132. break;
  133. v *= base;
  134. v += c;
  135. ++input_stream_pos;
  136. }
  137. if (input_stream_pos[0] == 'U' || input_stream_pos[0] == 'u')
  138. input_stream_pos++;
  139. if (input_stream_pos[0] == 'L' || input_stream_pos[0] == 'l')
  140. input_stream_pos++;
  141. if (input_stream_pos[0] == 'L' || input_stream_pos[0] == 'l')
  142. input_stream_pos++;
  143. return v;
  144. }
  145. static mc_keyword *keyword_top = NULL;
  146. const mc_keyword *
  147. enum_facility (int e)
  148. {
  149. mc_keyword *h = keyword_top;
  150. while (h != NULL)
  151. {
  152. while (h && strcmp (h->group_name, "facility") != 0)
  153. h = h->next;
  154. if (e == 0)
  155. return h;
  156. --e;
  157. if (h)
  158. h = h->next;
  159. }
  160. return h;
  161. }
  162. const mc_keyword *
  163. enum_severity (int e)
  164. {
  165. mc_keyword *h = keyword_top;
  166. while (h != NULL)
  167. {
  168. while (h && strcmp (h->group_name, "severity") != 0)
  169. h = h->next;
  170. if (e == 0)
  171. return h;
  172. --e;
  173. if (h)
  174. h = h->next;
  175. }
  176. return h;
  177. }
  178. static void
  179. mc_add_keyword_ascii (const char *sz, int rid, const char *grp, rc_uint_type nv, const char *sv)
  180. {
  181. unichar *usz, *usv = NULL;
  182. rc_uint_type usz_len;
  183. unicode_from_codepage (&usz_len, &usz, sz, CP_ACP);
  184. if (sv)
  185. unicode_from_codepage (&usz_len, &usv, sv, CP_ACP);
  186. mc_add_keyword (usz, rid, grp, nv, usv);
  187. }
  188. void
  189. mc_add_keyword (unichar *usz, int rid, const char *grp, rc_uint_type nv, unichar *sv)
  190. {
  191. mc_keyword *p, *c, *n;
  192. size_t len = unichar_len (usz);
  193. c = keyword_top;
  194. p = NULL;
  195. while (c != NULL)
  196. {
  197. if (c->len > len)
  198. break;
  199. if (c->len == len)
  200. {
  201. int e = memcmp (usz, c->usz, len * sizeof (unichar));
  202. if (e < 0)
  203. break;
  204. if (! e)
  205. {
  206. if (! strcmp (grp, "keyword") || strcmp (c->group_name, grp) != 0)
  207. fatal (_("Duplicate symbol entered into keyword list."));
  208. c->rid = rid;
  209. c->nval = nv;
  210. c->sval = (!sv ? NULL : unichar_dup (sv));
  211. if (! strcmp (grp, "language"))
  212. {
  213. const wind_language_t *lag = wind_find_language_by_id ((unsigned) nv);
  214. if (lag == NULL)
  215. fatal ("Language ident 0x%lx is not resolvable.\n", (long) nv);
  216. memcpy (&c->lang_info, lag, sizeof (*lag));
  217. }
  218. return;
  219. }
  220. }
  221. c = (p = c)->next;
  222. }
  223. n = xmalloc (sizeof (mc_keyword));
  224. n->next = c;
  225. n->len = len;
  226. n->group_name = grp;
  227. n->usz = usz;
  228. n->rid = rid;
  229. n->nval = nv;
  230. n->sval = (!sv ? NULL : unichar_dup (sv));
  231. if (! strcmp (grp, "language"))
  232. {
  233. const wind_language_t *lag = wind_find_language_by_id ((unsigned) nv);
  234. if (lag == NULL)
  235. fatal ("Language ident 0x%lx is not resolvable.\n", (long) nv);
  236. memcpy (&n->lang_info, lag, sizeof (*lag));
  237. }
  238. if (! p)
  239. keyword_top = n;
  240. else
  241. p->next = n;
  242. }
  243. static int
  244. mc_token (const unichar *t, size_t len)
  245. {
  246. static int was_init = 0;
  247. mc_keyword *k;
  248. if (! was_init)
  249. {
  250. was_init = 1;
  251. mc_add_keyword_ascii ("OutputBase", MCOUTPUTBASE, "keyword", 0, NULL);
  252. mc_add_keyword_ascii ("MessageIdTypedef", MCMESSAGEIDTYPEDEF, "keyword", 0, NULL);
  253. mc_add_keyword_ascii ("SeverityNames", MCSEVERITYNAMES, "keyword", 0, NULL);
  254. mc_add_keyword_ascii ("FacilityNames", MCFACILITYNAMES, "keyword", 0, NULL);
  255. mc_add_keyword_ascii ("LanguageNames", MCLANGUAGENAMES, "keyword", 0, NULL);
  256. mc_add_keyword_ascii ("MessageId", MCMESSAGEID, "keyword", 0, NULL);
  257. mc_add_keyword_ascii ("Severity", MCSEVERITY, "keyword", 0, NULL);
  258. mc_add_keyword_ascii ("Facility", MCFACILITY, "keyword", 0, NULL);
  259. mc_add_keyword_ascii ("SymbolicName", MCSYMBOLICNAME, "keyword", 0, NULL);
  260. mc_add_keyword_ascii ("Language", MCLANGUAGE, "keyword", 0, NULL);
  261. mc_add_keyword_ascii ("Success", MCTOKEN, "severity", 0, NULL);
  262. mc_add_keyword_ascii ("Informational", MCTOKEN, "severity", 1, NULL);
  263. mc_add_keyword_ascii ("Warning", MCTOKEN, "severity", 2, NULL);
  264. mc_add_keyword_ascii ("Error", MCTOKEN, "severity", 3, NULL);
  265. mc_add_keyword_ascii ("System", MCTOKEN, "facility", 0xff, NULL);
  266. mc_add_keyword_ascii ("Application", MCTOKEN, "facility", 0xfff, NULL);
  267. mc_add_keyword_ascii ("English", MCTOKEN, "language", 0x409, "MSG00001");
  268. }
  269. k = keyword_top;
  270. if (!len || !t || *t == 0)
  271. return -1;
  272. while (k != NULL)
  273. {
  274. if (k->len > len)
  275. break;
  276. if (k->len == len)
  277. {
  278. if (! memcmp (k->usz, t, len * sizeof (unichar)))
  279. {
  280. if (k->rid == MCTOKEN)
  281. yylval.tok = k;
  282. return k->rid;
  283. }
  284. }
  285. k = k->next;
  286. }
  287. return -1;
  288. }
  289. int
  290. yylex (void)
  291. {
  292. unichar *start_token;
  293. unichar ch;
  294. if (! input_stream_pos)
  295. {
  296. fatal ("Input stream not setuped.\n");
  297. return -1;
  298. }
  299. if (mclex_want_line)
  300. {
  301. start_token = input_stream_pos;
  302. if (input_stream_pos[0] == '.'
  303. && (input_stream_pos[1] == '\n'
  304. || (input_stream_pos[1] == '\r' && input_stream_pos[2] == '\n')))
  305. {
  306. mclex_want_line = FALSE;
  307. while (input_stream_pos[0] != 0 && input_stream_pos[0] != '\n')
  308. ++input_stream_pos;
  309. if (input_stream_pos[0] == '\n')
  310. ++input_stream_pos;
  311. return MCENDLINE;
  312. }
  313. while (input_stream_pos[0] != 0 && input_stream_pos[0] != '\n')
  314. ++input_stream_pos;
  315. if (input_stream_pos[0] == '\n')
  316. ++input_stream_pos;
  317. yylval.ustr = get_diff (input_stream_pos, start_token);
  318. return MCLINE;
  319. }
  320. while ((ch = input_stream_pos[0]) <= 0x20)
  321. {
  322. if (ch == 0)
  323. return -1;
  324. ++input_stream_pos;
  325. if (ch == '\n')
  326. input_line += 1;
  327. if (mclex_want_nl && ch == '\n')
  328. {
  329. mclex_want_nl = FALSE;
  330. return NL;
  331. }
  332. }
  333. start_token = input_stream_pos;
  334. ++input_stream_pos;
  335. if (mclex_want_filename)
  336. {
  337. mclex_want_filename = FALSE;
  338. if (ch == '"')
  339. {
  340. start_token++;
  341. while ((ch = input_stream_pos[0]) != 0)
  342. {
  343. if (ch == '"')
  344. break;
  345. ++input_stream_pos;
  346. }
  347. yylval.ustr = get_diff (input_stream_pos, start_token);
  348. if (ch == '"')
  349. ++input_stream_pos;
  350. }
  351. else
  352. {
  353. while ((ch = input_stream_pos[0]) != 0)
  354. {
  355. if (ch <= 0x20 || ch == ')')
  356. break;
  357. ++input_stream_pos;
  358. }
  359. yylval.ustr = get_diff (input_stream_pos, start_token);
  360. }
  361. return MCFILENAME;
  362. }
  363. switch (ch)
  364. {
  365. case ';':
  366. ++start_token;
  367. while (input_stream_pos[0] != '\n' && input_stream_pos[0] != 0)
  368. ++input_stream_pos;
  369. if (input_stream_pos[0] == '\n')
  370. input_stream_pos++;
  371. yylval.ustr = get_diff (input_stream_pos, start_token);
  372. return MCCOMMENT;
  373. case '=':
  374. return '=';
  375. case '(':
  376. return '(';
  377. case ')':
  378. return ')';
  379. case '+':
  380. return '+';
  381. case ':':
  382. return ':';
  383. case '0': case '1': case '2': case '3': case '4':
  384. case '5': case '6': case '7': case '8': case '9':
  385. yylval.ival = parse_digit (ch);
  386. return MCNUMBER;
  387. default:
  388. if (ch >= 0x40)
  389. {
  390. int ret;
  391. while (input_stream_pos[0] >= 0x40 || (input_stream_pos[0] >= '0' && input_stream_pos[0] <= '9'))
  392. ++input_stream_pos;
  393. ret = mc_token (start_token, (size_t) (input_stream_pos - start_token));
  394. if (ret != -1)
  395. return ret;
  396. yylval.ustr = get_diff (input_stream_pos, start_token);
  397. return MCIDENT;
  398. }
  399. yyerror ("illegal character 0x%x.", ch);
  400. }
  401. return -1;
  402. }