mcl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * Wine Message Compiler lexical scanner
  3. *
  4. * Copyright 2000 Bertho A. Stultiens (BS)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include <assert.h>
  25. #include <string.h>
  26. #include "utils.h"
  27. #include "wmc.h"
  28. #include "lang.h"
  29. #include "mcy.tab.h"
  30. /*
  31. * Keywords are case insensitive. All normal input is treated as
  32. * being in codepage iso-8859-1 for ascii input files (unicode
  33. * page 0) and as equivalent unicode if unicode input is selected.
  34. * All normal input, which is not part of a message text, is
  35. * enforced to be unicode page 0. Otherwise an error will be
  36. * generated. The normal file data should only be ASCII because
  37. * that is the basic definition of the grammar.
  38. *
  39. * Byteorder or unicode input is determined automatically by
  40. * reading the first 8 bytes and checking them against unicode
  41. * page 0 byteorder (hibyte must be 0).
  42. * -- FIXME --
  43. * Alternatively, the input is checked against a special byte
  44. * sequence to identify the file.
  45. * -- FIXME --
  46. *
  47. *
  48. * Keywords:
  49. * Codepages
  50. * Facility
  51. * FacilityNames
  52. * LanguageNames
  53. * MessageId
  54. * MessageIdTypedef
  55. * Severity
  56. * SeverityNames
  57. * SymbolicName
  58. *
  59. * Default added identifiers for classes:
  60. * SeverityNames:
  61. * Success = 0x0
  62. * Informational = 0x1
  63. * Warning = 0x2
  64. * Error = 0x3
  65. * FacilityNames:
  66. * System = 0x0FF
  67. * Application = 0xFFF
  68. *
  69. * The 'Codepages' keyword is a wmc extension.
  70. */
  71. static const WCHAR ustr_application[] = { 'A', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', 0 };
  72. static const WCHAR ustr_codepages[] = { 'C', 'o', 'd', 'e', 'p', 'a', 'g', 'e', 's', 0 };
  73. static const WCHAR ustr_english[] = { 'E', 'n', 'g', 'l', 'i', 's', 'h', 0 };
  74. static const WCHAR ustr_error[] = { 'E', 'r', 'r', 'o', 'r', 0 };
  75. static const WCHAR ustr_facility[] = { 'F', 'a', 'c', 'i', 'l', 'i', 't', 'y', 0 };
  76. static const WCHAR ustr_facilitynames[] = { 'F', 'a', 'c', 'i', 'l', 'i', 't', 'y', 'N', 'a', 'm', 'e', 's', 0 };
  77. static const WCHAR ustr_informational[] = { 'I', 'n', 'f', 'o', 'r', 'm', 'a', 't', 'i', 'o', 'n', 'a', 'l', 0 };
  78. static const WCHAR ustr_language[] = { 'L', 'a', 'n', 'g', 'u', 'a', 'g', 'e', 0};
  79. static const WCHAR ustr_languagenames[] = { 'L', 'a', 'n', 'g', 'u', 'a', 'g', 'e', 'N', 'a', 'm', 'e', 's', 0};
  80. static const WCHAR ustr_messageid[] = { 'M', 'e', 's', 's', 'a', 'g', 'e', 'I', 'd', 0 };
  81. static const WCHAR ustr_messageidtypedef[] = { 'M', 'e', 's', 's', 'a', 'g', 'e', 'I', 'd', 'T', 'y', 'p', 'e', 'd', 'e', 'f', 0 };
  82. static const WCHAR ustr_outputbase[] = { 'O', 'u', 't', 'p', 'u', 't', 'B', 'a', 's', 'e', 0 };
  83. static const WCHAR ustr_severity[] = { 'S', 'e', 'v', 'e', 'r', 'i', 't', 'y', 0 };
  84. static const WCHAR ustr_severitynames[] = { 'S', 'e', 'v', 'e', 'r', 'i', 't', 'y', 'N', 'a', 'm', 'e', 's', 0 };
  85. static const WCHAR ustr_success[] = { 'S', 'u', 'c', 'c', 'e', 's', 's', 0 };
  86. static const WCHAR ustr_symbolicname[] = { 'S', 'y', 'm', 'b', 'o', 'l', 'i', 'c', 'N', 'a', 'm', 'e', 0 };
  87. static const WCHAR ustr_system[] = { 'S', 'y', 's', 't', 'e', 'm', 0 };
  88. static const WCHAR ustr_warning[] = { 'W', 'a', 'r', 'n', 'i', 'n', 'g', 0 };
  89. static const WCHAR ustr_msg00001[] = { 'm', 's', 'g', '0', '0', '0', '0', '1', 0 };
  90. /*
  91. * This table is to beat any form of "expression building" to check for
  92. * correct filename characters. It is also used for ident checks.
  93. * FIXME: use it more consistently.
  94. */
  95. #define CH_SHORTNAME 0x01
  96. #define CH_LONGNAME 0x02
  97. #define CH_IDENT 0x04
  98. #define CH_NUMBER 0x08
  99. /*#define CH_WILDCARD 0x10*/
  100. /*#define CH_DOT 0x20*/
  101. #define CH_PUNCT 0x40
  102. #define CH_INVALID 0x80
  103. static const char char_table[256] = {
  104. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, /* 0x00 - 0x07 */
  105. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, /* 0x08 - 0x0F */
  106. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, /* 0x10 - 0x17 */
  107. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, /* 0x18 - 0x1F */
  108. 0x80, 0x03, 0x80, 0x03, 0x03, 0x03, 0x03, 0x03, /* 0x20 - 0x27 " !"#$%&'" */
  109. 0x43, 0x43, 0x10, 0x80, 0x03, 0x03, 0x22, 0x80, /* 0x28 - 0x2F "()*+,-./" */
  110. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, /* 0x30 - 0x37 "01234567" */
  111. 0x0b, 0x0b, 0xc0, 0x80, 0x80, 0x80, 0x80, 0x10, /* 0x38 - 0x3F "89:;<=>?" */
  112. 0x03, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, /* 0x40 - 0x47 "@ABCDEFG" */
  113. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, /* 0x48 - 0x4F "HIJKLMNO" */
  114. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, /* 0x50 - 0x57 "PQRSTUVW" */
  115. 0x07, 0x07, 0x07, 0x80, 0x80, 0x80, 0x80, 0x07, /* 0x58 - 0x5F "XYZ[\]^_" */
  116. 0x03, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, /* 0x60 - 0x67 "`abcdefg" */
  117. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, /* 0x68 - 0x6F "hijklmno" */
  118. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, /* 0x70 - 0x77 "pqrstuvw" */
  119. 0x07, 0x07, 0x07, 0x03, 0x80, 0x03, 0x03, 0x80, /* 0x78 - 0x7F "xyz{|}~ " */
  120. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0x80 - 0x87 */
  121. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0x88 - 0x8F */
  122. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0x90 - 0x97 */
  123. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0x98 - 0x9F */
  124. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0xA0 - 0xA7 */
  125. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0xA8 - 0xAF */
  126. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0xB0 - 0xB7 */
  127. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0xB8 - 0xBF */
  128. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0xC0 - 0xC7 */
  129. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0xC8 - 0xCF */
  130. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0xD0 - 0xD7 */
  131. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0xD8 - 0xDF */
  132. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0xE0 - 0xE7 */
  133. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0xE8 - 0xEF */
  134. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 0xF0 - 0xF7 */
  135. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x80, /* 0xF8 - 0xFF */
  136. };
  137. static int isisochar(int ch)
  138. {
  139. return !(ch & (~0xff));
  140. }
  141. static int codepage;
  142. void set_codepage(int cp)
  143. {
  144. codepage = cp;
  145. }
  146. /*
  147. * Input functions
  148. */
  149. #define INPUTBUFFER_SIZE 2048 /* Must be larger than 4 and approx. large enough to hold a line */
  150. static int nungetstack = 0;
  151. static int allocungetstack = 0;
  152. static char *ungetstack = NULL;
  153. static int ninputbuffer = 0;
  154. static WCHAR inputbuffer[INPUTBUFFER_SIZE];
  155. /*
  156. * Fill the input buffer with *one* line of input.
  157. * The line is '\n' terminated so that scanning
  158. * messages with translation works as expected
  159. * (otherwise we cannot pre-translate because the
  160. * language is first known one line before the
  161. * actual message).
  162. */
  163. static int fill_inputbuffer(void)
  164. {
  165. static enum input_mode { INPUT_UNKNOWN, INPUT_ASCII, INPUT_UTF8, INPUT_UNICODE } mode;
  166. static int swapped;
  167. static unsigned char utf8_bom[3] = { 0xef, 0xbb, 0xbf };
  168. WCHAR *wbuf;
  169. int i, pos = 0, len = 0;
  170. char buffer[INPUTBUFFER_SIZE];
  171. if (mode == INPUT_UNKNOWN)
  172. {
  173. len = fread( buffer, 1, 8, yyin );
  174. wbuf = (WCHAR *)buffer;
  175. if (len >= 3 && !memcmp( buffer, utf8_bom, 3 ))
  176. {
  177. mode = INPUT_UTF8;
  178. memmove( buffer, buffer + 3, len - 3 );
  179. len -= 3;
  180. }
  181. else if (len == 8)
  182. {
  183. if (wbuf[0] == 0xfeff || wbuf[0] == 0xfffe)
  184. {
  185. mode = INPUT_UNICODE;
  186. pos = 1;
  187. swapped = (wbuf[0] == 0xfffe);
  188. }
  189. else if (!((wbuf[0] | wbuf[1] | wbuf[2] | wbuf[3]) & 0xff00))
  190. {
  191. mode = INPUT_UNICODE;
  192. }
  193. else if (!((wbuf[0] | wbuf[1] | wbuf[2] | wbuf[3]) & 0x00ff))
  194. {
  195. mode = INPUT_UNICODE;
  196. swapped = 1;
  197. }
  198. }
  199. if (mode == INPUT_UNICODE)
  200. {
  201. len = 4 - pos;
  202. memcpy( inputbuffer, wbuf + pos, len * sizeof(WCHAR) );
  203. }
  204. else if (mode == INPUT_UNKNOWN) mode = unicodein ? INPUT_UTF8 : INPUT_ASCII;
  205. }
  206. switch (mode)
  207. {
  208. case INPUT_ASCII:
  209. if (!fgets( buffer + len, sizeof(buffer) - len, yyin )) break;
  210. wbuf = codepage_to_unicode( codepage, buffer, strlen(buffer), &ninputbuffer );
  211. memcpy( inputbuffer, wbuf, ninputbuffer * sizeof(WCHAR) );
  212. free( wbuf );
  213. return 1;
  214. case INPUT_UTF8:
  215. if (!fgets( buffer + len, sizeof(buffer) - len, yyin )) break;
  216. wbuf = utf8_to_unicode( buffer, strlen(buffer), &ninputbuffer );
  217. memcpy( inputbuffer, wbuf, ninputbuffer * sizeof(WCHAR) );
  218. free( wbuf );
  219. return 1;
  220. case INPUT_UNICODE:
  221. len += fread( inputbuffer + len, sizeof(WCHAR), INPUTBUFFER_SIZE - len, yyin );
  222. if (!len) break;
  223. if (swapped) for (i = 0; i < len; i++) inputbuffer[i] = BYTESWAP_WORD( inputbuffer[i] );
  224. ninputbuffer = len;
  225. return 1;
  226. case INPUT_UNKNOWN:
  227. break;
  228. }
  229. if (ferror(yyin)) xyyerror( "Fatal: reading input failed\n" );
  230. return 0;
  231. }
  232. static int get_unichar(void)
  233. {
  234. static WCHAR *b = NULL;
  235. char_number++;
  236. if(nungetstack)
  237. return ungetstack[--nungetstack];
  238. if(!ninputbuffer)
  239. {
  240. if(!fill_inputbuffer())
  241. return EOF;
  242. b = inputbuffer;
  243. }
  244. ninputbuffer--;
  245. return *b++;
  246. }
  247. static void unget_unichar(int ch)
  248. {
  249. if(ch == EOF)
  250. return;
  251. char_number--;
  252. if(nungetstack == allocungetstack)
  253. {
  254. allocungetstack += 32;
  255. ungetstack = xrealloc(ungetstack, allocungetstack * sizeof(*ungetstack));
  256. }
  257. ungetstack[nungetstack++] = (WCHAR)ch;
  258. }
  259. /*
  260. * Normal character stack.
  261. * Used for number scanning.
  262. */
  263. static int ncharstack = 0;
  264. static int alloccharstack = 0;
  265. static char *charstack = NULL;
  266. static void empty_char_stack(void)
  267. {
  268. ncharstack = 0;
  269. }
  270. static void push_char(int ch)
  271. {
  272. if(ncharstack == alloccharstack)
  273. {
  274. alloccharstack += 32;
  275. charstack = xrealloc(charstack, alloccharstack * sizeof(*charstack));
  276. }
  277. charstack[ncharstack++] = (char)ch;
  278. }
  279. static int tos_char_stack(void)
  280. {
  281. if(!ncharstack)
  282. return 0;
  283. else
  284. return (int)(charstack[ncharstack-1] & 0xff);
  285. }
  286. static char *get_char_stack(void)
  287. {
  288. return charstack;
  289. }
  290. /*
  291. * Unicode character stack.
  292. * Used for general scanner.
  293. */
  294. static int nunicharstack = 0;
  295. static int allocunicharstack = 0;
  296. static WCHAR *unicharstack = NULL;
  297. static void empty_unichar_stack(void)
  298. {
  299. nunicharstack = 0;
  300. }
  301. static void push_unichar(int ch)
  302. {
  303. if(nunicharstack == allocunicharstack)
  304. {
  305. allocunicharstack += 128;
  306. unicharstack = xrealloc(unicharstack, allocunicharstack * sizeof(*unicharstack));
  307. }
  308. unicharstack[nunicharstack++] = (WCHAR)ch;
  309. }
  310. #if 0
  311. static int tos_unichar_stack(void)
  312. {
  313. if(!nunicharstack)
  314. return 0;
  315. else
  316. return (int)(unicharstack[nunicharstack-1] & 0xffff);
  317. }
  318. #endif
  319. static WCHAR *get_unichar_stack(void)
  320. {
  321. return unicharstack;
  322. }
  323. /*
  324. * Number scanner
  325. *
  326. * state | ch | next state
  327. * ------+-----------------+--------------------------
  328. * 0 | [0] | 1
  329. * 0 | [1-9] | 4
  330. * 0 | . | error (should never occur)
  331. * 1 | [xX] | 2
  332. * 1 | [0-7] | 3
  333. * 1 | [89a-wyzA-WYZ_] | error invalid digit
  334. * 1 | . | return 0
  335. * 2 | [0-9a-fA-F] | 2
  336. * 2 | [g-zG-Z_] | error invalid hex digit
  337. * 2 | . | return (hex-number) if TOS != [xX] else error
  338. * 3 | [0-7] | 3
  339. * 3 | [89a-zA-Z_] | error invalid octal digit
  340. * 3 | . | return (octal-number)
  341. * 4 | [0-9] | 4
  342. * 4 | [a-zA-Z_] | error invalid decimal digit
  343. * 4 | . | return (decimal-number)
  344. *
  345. * All non-identifier characters [^a-zA-Z_0-9] terminate the scan
  346. * and return the value. This is not entirely correct, but close
  347. * enough (should check punctuators as trailing context, but the
  348. * char_table is not adapted to that and it is questionable whether
  349. * it is worth the trouble).
  350. * All non-iso-8859-1 characters are an error.
  351. */
  352. static int scan_number(int ch)
  353. {
  354. int state = 0;
  355. int base = 10;
  356. empty_char_stack();
  357. while(1)
  358. {
  359. if(!isisochar(ch))
  360. xyyerror("Invalid digit\n");
  361. switch(state)
  362. {
  363. case 0:
  364. if(isdigit(ch))
  365. {
  366. push_char(ch);
  367. if(ch == '0')
  368. state = 1;
  369. else
  370. state = 4;
  371. }
  372. else
  373. internal_error(__FILE__, __LINE__, "Non-digit in first number-scanner state\n");
  374. break;
  375. case 1:
  376. if(ch == 'x' || ch == 'X')
  377. {
  378. push_char(ch);
  379. state = 2;
  380. }
  381. else if(ch >= '0' && ch <= '7')
  382. {
  383. push_char(ch);
  384. state = 3;
  385. }
  386. else if(isalpha(ch) || ch == '_')
  387. xyyerror("Invalid number digit\n");
  388. else
  389. {
  390. unget_unichar(ch);
  391. mcy_lval.num = 0;
  392. return tNUMBER;
  393. }
  394. break;
  395. case 2:
  396. if(isxdigit(ch))
  397. push_char(ch);
  398. else if(isalpha(ch) || ch == '_' || !isxdigit(tos_char_stack()))
  399. xyyerror("Invalid hex digit\n");
  400. else
  401. {
  402. base = 16;
  403. goto finish;
  404. }
  405. break;
  406. case 3:
  407. if(ch >= '0' && ch <= '7')
  408. push_char(ch);
  409. else if(isalnum(ch) || ch == '_')
  410. xyyerror("Invalid octal digit\n");
  411. else
  412. {
  413. base = 8;
  414. goto finish;
  415. }
  416. break;
  417. case 4:
  418. if(isdigit(ch))
  419. push_char(ch);
  420. else if(isalnum(ch) || ch == '_')
  421. xyyerror("Invalid decimal digit\n");
  422. else
  423. {
  424. base = 10;
  425. goto finish;
  426. }
  427. break;
  428. default:
  429. internal_error(__FILE__, __LINE__, "Invalid state in number-scanner\n");
  430. }
  431. ch = get_unichar();
  432. }
  433. finish:
  434. unget_unichar(ch);
  435. push_char(0);
  436. mcy_lval.num = strtoul(get_char_stack(), NULL, base);
  437. return tNUMBER;
  438. }
  439. static void newline(void)
  440. {
  441. line_number++;
  442. char_number = 1;
  443. }
  444. static int unisort(const void *p1, const void *p2)
  445. {
  446. return unistricmp(((const token_t *)p1)->name, ((const token_t *)p2)->name);
  447. }
  448. static token_t *tokentable = NULL;
  449. static int ntokentable = 0;
  450. token_t *lookup_token(const WCHAR *s)
  451. {
  452. token_t tok;
  453. tok.name = s;
  454. return (token_t *)bsearch(&tok, tokentable, ntokentable, sizeof(*tokentable), unisort);
  455. }
  456. void add_token(tok_e type, const WCHAR *name, int tok, int cp, const WCHAR *alias, int fix)
  457. {
  458. ntokentable++;
  459. tokentable = xrealloc(tokentable, ntokentable * sizeof(*tokentable));
  460. tokentable[ntokentable-1].type = type;
  461. tokentable[ntokentable-1].name = name;
  462. tokentable[ntokentable-1].token = tok;
  463. tokentable[ntokentable-1].codepage = cp;
  464. tokentable[ntokentable-1].alias = alias;
  465. tokentable[ntokentable-1].fixed = fix;
  466. qsort(tokentable, ntokentable, sizeof(*tokentable), unisort);
  467. }
  468. void get_tokentable(token_t **tab, int *len)
  469. {
  470. assert(tab != NULL);
  471. assert(len != NULL);
  472. *tab = tokentable;
  473. *len = ntokentable;
  474. }
  475. /*
  476. * The scanner
  477. *
  478. */
  479. int mcy_lex(void)
  480. {
  481. static const WCHAR ustr_dot1[] = { '.', '\n', 0 };
  482. static const WCHAR ustr_dot2[] = { '.', '\r', '\n', 0 };
  483. static int isinit = 0;
  484. int ch;
  485. if(!isinit)
  486. {
  487. isinit++;
  488. set_codepage(WMC_DEFAULT_CODEPAGE);
  489. add_token(tok_keyword, ustr_codepages, tCODEPAGE, 0, NULL, 0);
  490. add_token(tok_keyword, ustr_facility, tFACILITY, 0, NULL, 1);
  491. add_token(tok_keyword, ustr_facilitynames, tFACNAMES, 0, NULL, 1);
  492. add_token(tok_keyword, ustr_language, tLANGUAGE, 0, NULL, 1);
  493. add_token(tok_keyword, ustr_languagenames, tLANNAMES, 0, NULL, 1);
  494. add_token(tok_keyword, ustr_messageid, tMSGID, 0, NULL, 1);
  495. add_token(tok_keyword, ustr_messageidtypedef, tTYPEDEF, 0, NULL, 1);
  496. add_token(tok_keyword, ustr_outputbase, tBASE, 0, NULL, 1);
  497. add_token(tok_keyword, ustr_severity, tSEVERITY, 0, NULL, 1);
  498. add_token(tok_keyword, ustr_severitynames, tSEVNAMES, 0, NULL, 1);
  499. add_token(tok_keyword, ustr_symbolicname, tSYMNAME, 0, NULL, 1);
  500. add_token(tok_severity, ustr_error, 0x03, 0, NULL, 0);
  501. add_token(tok_severity, ustr_warning, 0x02, 0, NULL, 0);
  502. add_token(tok_severity, ustr_informational, 0x01, 0, NULL, 0);
  503. add_token(tok_severity, ustr_success, 0x00, 0, NULL, 0);
  504. add_token(tok_facility, ustr_application, 0xFFF, 0, NULL, 0);
  505. add_token(tok_facility, ustr_system, 0x0FF, 0, NULL, 0);
  506. add_token(tok_language, ustr_english, 0x409, 437, ustr_msg00001, 0);
  507. }
  508. empty_unichar_stack();
  509. while(1)
  510. {
  511. if(want_line)
  512. {
  513. while((ch = get_unichar()) != '\n')
  514. {
  515. if(ch == EOF)
  516. xyyerror("Unexpected EOF\n");
  517. push_unichar(ch);
  518. }
  519. newline();
  520. push_unichar(ch);
  521. push_unichar(0);
  522. if(!unistrcmp(ustr_dot1, get_unichar_stack()) || !unistrcmp(ustr_dot2, get_unichar_stack()))
  523. {
  524. want_line = 0;
  525. /* Reset the codepage to our default after each message */
  526. set_codepage(WMC_DEFAULT_CODEPAGE);
  527. return tMSGEND;
  528. }
  529. mcy_lval.str = xunistrdup(get_unichar_stack());
  530. return tLINE;
  531. }
  532. ch = get_unichar();
  533. if(ch == EOF)
  534. return EOF;
  535. if(ch == '\n')
  536. {
  537. newline();
  538. if(want_nl)
  539. {
  540. want_nl = 0;
  541. return tNL;
  542. }
  543. continue;
  544. }
  545. if(isisochar(ch))
  546. {
  547. if(want_file)
  548. {
  549. int n = 0;
  550. while(n < 8 && isisochar(ch))
  551. {
  552. int t = char_table[ch];
  553. if((t & CH_PUNCT) || !(t & CH_SHORTNAME))
  554. break;
  555. push_unichar(ch);
  556. n++;
  557. ch = get_unichar();
  558. }
  559. unget_unichar(ch);
  560. push_unichar(0);
  561. want_file = 0;
  562. mcy_lval.str = xunistrdup(get_unichar_stack());
  563. return tFILE;
  564. }
  565. if(char_table[ch] & CH_IDENT)
  566. {
  567. token_t *tok;
  568. while(isisochar(ch) && (char_table[ch] & (CH_IDENT|CH_NUMBER)))
  569. {
  570. push_unichar(ch);
  571. ch = get_unichar();
  572. }
  573. unget_unichar(ch);
  574. push_unichar(0);
  575. if(!(tok = lookup_token(get_unichar_stack())))
  576. {
  577. mcy_lval.str = xunistrdup(get_unichar_stack());
  578. return tIDENT;
  579. }
  580. switch(tok->type)
  581. {
  582. case tok_keyword:
  583. return tok->token;
  584. case tok_language:
  585. codepage = tok->codepage;
  586. /* Fall through */
  587. case tok_severity:
  588. case tok_facility:
  589. mcy_lval.tok = tok;
  590. return tTOKEN;
  591. default:
  592. internal_error(__FILE__, __LINE__, "Invalid token type encountered\n");
  593. }
  594. }
  595. if(isspace(ch)) /* Ignore space */
  596. continue;
  597. if(isdigit(ch))
  598. return scan_number(ch);
  599. }
  600. switch(ch)
  601. {
  602. case ':':
  603. case '=':
  604. case '+':
  605. case '(':
  606. case ')':
  607. return ch;
  608. case ';':
  609. while(ch != '\n' && ch != EOF)
  610. {
  611. push_unichar(ch);
  612. ch = get_unichar();
  613. }
  614. newline();
  615. push_unichar(ch); /* Include the newline */
  616. push_unichar(0);
  617. mcy_lval.str = xunistrdup(get_unichar_stack());
  618. return tCOMMENT;
  619. default:
  620. xyyerror("Invalid character '%c' (0x%04x)\n", isisochar(ch) && isprint(ch) ? ch : '.', ch);
  621. }
  622. }
  623. }