dictionary_parser.l 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Copyright (C) 2011 Anders Sundman <anders@4zm.org>
  3. *
  4. * This file is part of mfterm.
  5. *
  6. * mfterm 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. *
  11. * mfterm 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with mfterm. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. %{
  20. #include <stdio.h>
  21. #include "dictionary.h"
  22. static uint8_t key_token[6];
  23. typedef enum {
  24. DT_KEY = 1,
  25. DT_ERR = 2
  26. } dict_token_t;
  27. %}
  28. %option yylineno
  29. hex_digit [0-9a-fA-F]
  30. key {hex_digit}{12}
  31. comment #[^\n]*
  32. %%
  33. {key} {
  34. read_key(key_token, dp_text);
  35. return DT_KEY;
  36. }
  37. {comment} {} // eat comments
  38. [ \t\r\n]+ {} // eat white space
  39. . {
  40. printf("Line: %d - Unrecognized input: %s\n", dp_lineno, dp_text);
  41. return DT_ERR;
  42. }
  43. %%
  44. // Reset the line numbering at the end of a file
  45. int dp_wrap() { dp_lineno = 1; return 1; }
  46. // Parser loop. Import keys to the dictionary as they are found.
  47. int dictionary_import(FILE* input) {
  48. dict_token_t token;
  49. int res = 0;
  50. int keys_count = 0;
  51. int imported_count = 0;
  52. dp_in = input;
  53. while((token = dp_lex())) {
  54. switch(token) {
  55. case DT_KEY:
  56. ++keys_count;
  57. if (dictionary_add(key_token))
  58. ++imported_count;
  59. break;
  60. case DT_ERR:
  61. default:
  62. res = -1;
  63. break;
  64. }
  65. }
  66. if (res)
  67. printf("There were errors.\n");
  68. printf("%d keys (of %d) imported.\n", imported_count, keys_count);
  69. return res;
  70. }