dtc-lexer.l 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. * USA
  19. */
  20. %option noyywrap nounput noinput never-interactive
  21. %x BYTESTRING
  22. %x PROPNODENAME
  23. %s V1
  24. PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
  25. PATHCHAR ({PROPNODECHAR}|[/])
  26. LABEL [a-zA-Z_][a-zA-Z0-9_]*
  27. STRING \"([^\\"]|\\.)*\"
  28. CHAR_LITERAL '([^']|\\')*'
  29. WS [[:space:]]
  30. COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
  31. LINECOMMENT "//".*\n
  32. %{
  33. #include "dtc.h"
  34. #include "srcpos.h"
  35. #include "dtc-parser.tab.h"
  36. YYLTYPE yylloc;
  37. extern bool treesource_error;
  38. /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
  39. #define YY_USER_ACTION \
  40. { \
  41. srcpos_update(&yylloc, yytext, yyleng); \
  42. }
  43. /*#define LEXDEBUG 1*/
  44. #ifdef LEXDEBUG
  45. #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
  46. #else
  47. #define DPRINT(fmt, ...) do { } while (0)
  48. #endif
  49. static int dts_version = 1;
  50. #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
  51. BEGIN(V1); \
  52. static void push_input_file(const char *filename);
  53. static bool pop_input_file(void);
  54. static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
  55. %}
  56. %%
  57. <*>"/include/"{WS}*{STRING} {
  58. char *name = strchr(yytext, '\"') + 1;
  59. yytext[yyleng-1] = '\0';
  60. push_input_file(name);
  61. }
  62. <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
  63. char *line, *fnstart, *fnend;
  64. struct data fn;
  65. /* skip text before line # */
  66. line = yytext;
  67. while (!isdigit((unsigned char)*line))
  68. line++;
  69. /* regexp ensures that first and list "
  70. * in the whole yytext are those at
  71. * beginning and end of the filename string */
  72. fnstart = memchr(yytext, '"', yyleng);
  73. for (fnend = yytext + yyleng - 1;
  74. *fnend != '"'; fnend--)
  75. ;
  76. assert(fnstart && fnend && (fnend > fnstart));
  77. fn = data_copy_escape_string(fnstart + 1,
  78. fnend - fnstart - 1);
  79. /* Don't allow nuls in filenames */
  80. if (memchr(fn.val, '\0', fn.len - 1))
  81. lexical_error("nul in line number directive");
  82. /* -1 since #line is the number of the next line */
  83. srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
  84. data_free(fn);
  85. }
  86. <*><<EOF>> {
  87. if (!pop_input_file()) {
  88. yyterminate();
  89. }
  90. }
  91. <*>{STRING} {
  92. DPRINT("String: %s\n", yytext);
  93. yylval.data = data_copy_escape_string(yytext+1,
  94. yyleng-2);
  95. return DT_STRING;
  96. }
  97. <*>"/dts-v1/" {
  98. DPRINT("Keyword: /dts-v1/\n");
  99. dts_version = 1;
  100. BEGIN_DEFAULT();
  101. return DT_V1;
  102. }
  103. <*>"/plugin/" {
  104. DPRINT("Keyword: /plugin/\n");
  105. return DT_PLUGIN;
  106. }
  107. <*>"/memreserve/" {
  108. DPRINT("Keyword: /memreserve/\n");
  109. BEGIN_DEFAULT();
  110. return DT_MEMRESERVE;
  111. }
  112. <*>"/bits/" {
  113. DPRINT("Keyword: /bits/\n");
  114. BEGIN_DEFAULT();
  115. return DT_BITS;
  116. }
  117. <*>"/delete-property/" {
  118. DPRINT("Keyword: /delete-property/\n");
  119. DPRINT("<PROPNODENAME>\n");
  120. BEGIN(PROPNODENAME);
  121. return DT_DEL_PROP;
  122. }
  123. <*>"/delete-node/" {
  124. DPRINT("Keyword: /delete-node/\n");
  125. DPRINT("<PROPNODENAME>\n");
  126. BEGIN(PROPNODENAME);
  127. return DT_DEL_NODE;
  128. }
  129. <*>"/omit-if-no-ref/" {
  130. DPRINT("Keyword: /omit-if-no-ref/\n");
  131. DPRINT("<PROPNODENAME>\n");
  132. BEGIN(PROPNODENAME);
  133. return DT_OMIT_NO_REF;
  134. }
  135. <*>{LABEL}: {
  136. DPRINT("Label: %s\n", yytext);
  137. yylval.labelref = xstrdup(yytext);
  138. yylval.labelref[yyleng-1] = '\0';
  139. return DT_LABEL;
  140. }
  141. <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
  142. char *e;
  143. DPRINT("Integer Literal: '%s'\n", yytext);
  144. errno = 0;
  145. yylval.integer = strtoull(yytext, &e, 0);
  146. if (*e && e[strspn(e, "UL")]) {
  147. lexical_error("Bad integer literal '%s'",
  148. yytext);
  149. }
  150. if (errno == ERANGE)
  151. lexical_error("Integer literal '%s' out of range",
  152. yytext);
  153. else
  154. /* ERANGE is the only strtoull error triggerable
  155. * by strings matching the pattern */
  156. assert(errno == 0);
  157. return DT_LITERAL;
  158. }
  159. <*>{CHAR_LITERAL} {
  160. struct data d;
  161. DPRINT("Character literal: %s\n", yytext);
  162. d = data_copy_escape_string(yytext+1, yyleng-2);
  163. if (d.len == 1) {
  164. lexical_error("Empty character literal");
  165. yylval.integer = 0;
  166. } else {
  167. yylval.integer = (unsigned char)d.val[0];
  168. if (d.len > 2)
  169. lexical_error("Character literal has %d"
  170. " characters instead of 1",
  171. d.len - 1);
  172. }
  173. data_free(d);
  174. return DT_CHAR_LITERAL;
  175. }
  176. <*>\&{LABEL} { /* label reference */
  177. DPRINT("Ref: %s\n", yytext+1);
  178. yylval.labelref = xstrdup(yytext+1);
  179. return DT_REF;
  180. }
  181. <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
  182. yytext[yyleng-1] = '\0';
  183. DPRINT("Ref: %s\n", yytext+2);
  184. yylval.labelref = xstrdup(yytext+2);
  185. return DT_REF;
  186. }
  187. <BYTESTRING>[0-9a-fA-F]{2} {
  188. yylval.byte = strtol(yytext, NULL, 16);
  189. DPRINT("Byte: %02x\n", (int)yylval.byte);
  190. return DT_BYTE;
  191. }
  192. <BYTESTRING>"]" {
  193. DPRINT("/BYTESTRING\n");
  194. BEGIN_DEFAULT();
  195. return ']';
  196. }
  197. <PROPNODENAME>\\?{PROPNODECHAR}+ {
  198. DPRINT("PropNodeName: %s\n", yytext);
  199. yylval.propnodename = xstrdup((yytext[0] == '\\') ?
  200. yytext + 1 : yytext);
  201. BEGIN_DEFAULT();
  202. return DT_PROPNODENAME;
  203. }
  204. "/incbin/" {
  205. DPRINT("Binary Include\n");
  206. return DT_INCBIN;
  207. }
  208. <*>{WS}+ /* eat whitespace */
  209. <*>{COMMENT}+ /* eat C-style comments */
  210. <*>{LINECOMMENT}+ /* eat C++-style comments */
  211. <*>"<<" { return DT_LSHIFT; };
  212. <*>">>" { return DT_RSHIFT; };
  213. <*>"<=" { return DT_LE; };
  214. <*>">=" { return DT_GE; };
  215. <*>"==" { return DT_EQ; };
  216. <*>"!=" { return DT_NE; };
  217. <*>"&&" { return DT_AND; };
  218. <*>"||" { return DT_OR; };
  219. <*>. {
  220. DPRINT("Char: %c (\\x%02x)\n", yytext[0],
  221. (unsigned)yytext[0]);
  222. if (yytext[0] == '[') {
  223. DPRINT("<BYTESTRING>\n");
  224. BEGIN(BYTESTRING);
  225. }
  226. if ((yytext[0] == '{')
  227. || (yytext[0] == ';')) {
  228. DPRINT("<PROPNODENAME>\n");
  229. BEGIN(PROPNODENAME);
  230. }
  231. return yytext[0];
  232. }
  233. %%
  234. static void push_input_file(const char *filename)
  235. {
  236. assert(filename);
  237. srcfile_push(filename);
  238. yyin = current_srcfile->f;
  239. yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
  240. }
  241. static bool pop_input_file(void)
  242. {
  243. if (srcfile_pop() == 0)
  244. return false;
  245. yypop_buffer_state();
  246. yyin = current_srcfile->f;
  247. return true;
  248. }
  249. static void lexical_error(const char *fmt, ...)
  250. {
  251. va_list ap;
  252. va_start(ap, fmt);
  253. srcpos_verror(&yylloc, "Lexical error", fmt, ap);
  254. va_end(ap);
  255. treesource_error = true;
  256. }