fpd.l 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. %{
  2. /*
  3. * fpd.l - FootPrint Definition language
  4. *
  5. * Written 2009, 2010, 2012, 2016 by Werner Almesberger
  6. * Copyright 2009, 2010, 2016 by Werner Almesberger
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <stdlib.h>
  14. #include "util.h"
  15. #include "coord.h"
  16. #include "expr.h"
  17. #include "error.h"
  18. #include "meas.h"
  19. #include "fpd.h"
  20. #include "y.tab.h"
  21. static int start_token = START_FPD;
  22. static int disable_keywords = 0;
  23. static int is_table = 0;
  24. void scan_empty(void)
  25. {
  26. yy_scan_string("");
  27. }
  28. void scan_file(void)
  29. {
  30. start_token = START_FPD;
  31. }
  32. void scan_expr(const char *s)
  33. {
  34. start_token = START_EXPR;
  35. yy_scan_string(s);
  36. }
  37. void scan_var(const char *s)
  38. {
  39. start_token = START_VAR;
  40. yy_scan_string(s);
  41. }
  42. void scan_values(const char *s)
  43. {
  44. start_token = START_VALUES;
  45. yy_scan_string(s);
  46. }
  47. %}
  48. /* keywords are only accepted at the beginning of a line */
  49. %s NOKEYWORD
  50. /* ALLOW is followed by special keywords (we could use NOKEYWORD as well) */
  51. %s ALLOW
  52. NUM [0-9]+\.?[0-9]*
  53. SP [\t ]*
  54. %%
  55. %{
  56. /*
  57. * Nice hack:
  58. *
  59. * http://www.gnu.org/software/bison/manual/bison.html#
  60. * Multiple-start_002dsymbols
  61. */
  62. if (start_token) {
  63. int tmp = start_token;
  64. start_token = 0;
  65. return tmp;
  66. }
  67. %}
  68. <INITIAL>"set" { BEGIN(NOKEYWORD);
  69. return TOK_SET; }
  70. <INITIAL>"loop" { BEGIN(NOKEYWORD);
  71. return TOK_LOOP; }
  72. <INITIAL>"part" { BEGIN(NOKEYWORD);
  73. return TOK_PACKAGE; }
  74. <INITIAL>"package" { BEGIN(NOKEYWORD);
  75. return TOK_PACKAGE; }
  76. <INITIAL>"frame" { BEGIN(NOKEYWORD);
  77. is_table = 0;
  78. return TOK_FRAME; }
  79. <INITIAL>"table" { BEGIN(NOKEYWORD);
  80. is_table = 1;
  81. return TOK_TABLE; }
  82. <INITIAL>"vec" { BEGIN(NOKEYWORD);
  83. return TOK_VEC; }
  84. <INITIAL>"pad" { BEGIN(NOKEYWORD);
  85. return TOK_PAD; }
  86. <INITIAL>"rpad" { BEGIN(NOKEYWORD);
  87. return TOK_RPAD; }
  88. <INITIAL>"hole" { BEGIN(NOKEYWORD);
  89. return TOK_HOLE; }
  90. <INITIAL>"rect" { BEGIN(NOKEYWORD);
  91. return TOK_RECT; }
  92. <INITIAL>"line" { BEGIN(NOKEYWORD);
  93. return TOK_LINE; }
  94. <INITIAL>"circ" { BEGIN(NOKEYWORD);
  95. return TOK_CIRC; }
  96. <INITIAL>"arc" { BEGIN(NOKEYWORD);
  97. return TOK_ARC; }
  98. <INITIAL>"meas" { BEGIN(NOKEYWORD);
  99. return TOK_MEAS; }
  100. <INITIAL>"measx" { BEGIN(NOKEYWORD);
  101. return TOK_MEASX; }
  102. <INITIAL>"measy" { BEGIN(NOKEYWORD);
  103. return TOK_MEASY; }
  104. <INITIAL>"unit" { BEGIN(NOKEYWORD);
  105. return TOK_UNIT; }
  106. <INITIAL>"allow" BEGIN(ALLOW);
  107. <ALLOW>"overlap" return TOK_ALLOW_OVERLAP;
  108. <ALLOW>"touch" return TOK_ALLOW_TOUCH;
  109. <ALLOW>"holes" return TOK_ALLOW_HOLES;
  110. <INITIAL>"%del" { BEGIN(NOKEYWORD);
  111. return TOK_DBG_DEL; }
  112. <INITIAL>"%move" { BEGIN(NOKEYWORD);
  113. return TOK_DBG_MOVE; }
  114. <INITIAL>"%frame" { BEGIN(NOKEYWORD);
  115. return TOK_DBG_FRAME; }
  116. <INITIAL>"%print" { BEGIN(NOKEYWORD);
  117. return TOK_DBG_PRINT; }
  118. <INITIAL>"%iprint" { BEGIN(NOKEYWORD);
  119. return TOK_DBG_IPRINT; }
  120. <INITIAL>"%meas" { BEGIN(NOKEYWORD);
  121. return TOK_DBG_MEAS; }
  122. <INITIAL>"%dump" { BEGIN(NOKEYWORD);
  123. return TOK_DBG_DUMP; }
  124. <INITIAL>"%exit" { BEGIN(NOKEYWORD);
  125. return TOK_DBG_EXIT; }
  126. <INITIAL>"%tsort" { BEGIN(NOKEYWORD);
  127. return TOK_DBG_TSORT; }
  128. <INITIAL>[a-zA-Z_][a-zA-Z_0-9]*: { *strchr(yytext, ':') = 0;
  129. yylval.id = unique(yytext);
  130. return LABEL; }
  131. [a-zA-Z_][a-zA-Z_0-9]* { yylval.id = unique(yytext);
  132. return ID; }
  133. {NUM}{SP}mm { yylval.num.type = nt_mm;
  134. yylval.num.exponent = 1;
  135. sscanf(yytext, "%lf", &yylval.num.n);
  136. return NUMBER; }
  137. {NUM}{SP}um { yylval.num.type = nt_um;
  138. yylval.num.exponent = 1;
  139. sscanf(yytext, "%lf", &yylval.num.n);
  140. return NUMBER; }
  141. {NUM}{SP}mil { yylval.num.type = nt_mil;
  142. yylval.num.exponent = 1;
  143. sscanf(yytext, "%lf", &yylval.num.n);
  144. return NUMBER; }
  145. {NUM} { yylval.num.type = nt_mm; /* mm or mil */
  146. yylval.num.exponent = 0;
  147. sscanf(yytext, "%lf", &yylval.num.n);
  148. return NUMBER; }
  149. \"(\\[^\n\t]|[^\\"\n\t])*\" { *strrchr(yytext, '"') = 0;
  150. yylval.str = stralloc(yytext+1);
  151. return STRING; }
  152. "->" return TOK_NEXT;
  153. "<-" return TOK_NEXT_INVERTED;
  154. ">>" return TOK_MAX;
  155. "<<" return TOK_MAX_INVERTED;
  156. {SP} ;
  157. \n { if (!disable_keywords)
  158. BEGIN(INITIAL);
  159. lineno++; }
  160. ; BEGIN(INITIAL);
  161. "{" { disable_keywords = is_table;
  162. BEGIN(disable_keywords ?
  163. NOKEYWORD : INITIAL);
  164. return '{'; }
  165. "}" { disable_keywords = 0;
  166. BEGIN(INITIAL);
  167. return '}'; }
  168. ^#\ [0-9]+\ \"[^"]*\"(\ [0-9]+)*\n {
  169. if (!disable_keywords)
  170. BEGIN(INITIAL);
  171. lineno = strtol(yytext+2, NULL, 0); }
  172. . return *yytext;