spec_tokenizer.l 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <stdarg.h>
  22. #include "util.h"
  23. #include "spec_syntax.h"
  24. #include "spec_parser.tab.h"
  25. int colnum = 0;
  26. #define YY_USER_ACTION { \
  27. sp_lloc.first_line = sp_lineno; \
  28. sp_lloc.first_column = colnum; \
  29. colnum = colnum + sp_leng; \
  30. sp_lloc.last_column=colnum; \
  31. sp_lloc.last_line = sp_lineno; \
  32. }
  33. %}
  34. %option yylineno bison-bridge bison-locations
  35. ws [ \t]
  36. nl [\r\n]
  37. comment #[^\n]*
  38. hex_digit [0-9a-fA-F]
  39. hex_number 0x{hex_digit}+
  40. dec_number [0-9]+
  41. identifier [a-zA-Z][a-zA-Z0-9_]*
  42. byte Byte(\[|{ws})
  43. bit Bit(\[|{ws})
  44. br [\[\]\{\}]
  45. %%
  46. {identifier} {
  47. sp_lval.string = strdup(sp_text);
  48. return IDENTIFIER;
  49. }
  50. {dec_number} {
  51. sp_lval.string = strdup(sp_text);
  52. return DEC_NUM;
  53. }
  54. {hex_number} {
  55. sp_lval.string = strdup(sp_text);
  56. return HEX_NUM;
  57. }
  58. {byte} { yyless(sp_leng - 1); return BYTE; }
  59. {bit} { yyless(sp_leng - 1); return BIT; }
  60. {br}|[\.-] { return sp_text[0]; }
  61. {comment} {} // eat comments
  62. {ws}+ {} // eat white space
  63. {nl}+ { colnum = 0; } // eat new lines and reset column
  64. . {
  65. printf("Line: %d - Unrecognized input: %s\n", sp_lineno, sp_text);
  66. return 0;
  67. }
  68. %%
  69. // Report error, reset the line numbering and flush buffer
  70. void sp_error(const char* s, ...) {
  71. va_list ap;
  72. va_start(ap, s);
  73. sp_lerror(sp_lloc, s, ap);
  74. sp_lineno = 1;
  75. YY_FLUSH_BUFFER;
  76. }
  77. // Reset the line numbering and flush the buffer at the end of a file
  78. int sp_wrap() {
  79. sp_lineno = 1;
  80. YY_FLUSH_BUFFER;
  81. return 1;
  82. }