dtc-lexer.l 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. extern bool treesource_error;
  37. /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
  38. #define YY_USER_ACTION \
  39. { \
  40. srcpos_update(&yylloc, yytext, yyleng); \
  41. }
  42. /*#define LEXDEBUG 1*/
  43. #ifdef LEXDEBUG
  44. #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
  45. #else
  46. #define DPRINT(fmt, ...) do { } while (0)
  47. #endif
  48. static int dts_version = 1;
  49. #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
  50. BEGIN(V1); \
  51. static void push_input_file(const char *filename);
  52. static bool pop_input_file(void);
  53. static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
  54. %}
  55. %%
  56. <*>"/include/"{WS}*{STRING} {
  57. char *name = strchr(yytext, '\"') + 1;
  58. yytext[yyleng-1] = '\0';
  59. push_input_file(name);
  60. }
  61. <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
  62. char *line, *fnstart, *fnend;
  63. struct data fn;
  64. /* skip text before line # */
  65. line = yytext;
  66. while (!isdigit((unsigned char)*line))
  67. line++;
  68. /* regexp ensures that first and list "
  69. * in the whole yytext are those at
  70. * beginning and end of the filename string */
  71. fnstart = memchr(yytext, '"', yyleng);
  72. for (fnend = yytext + yyleng - 1;
  73. *fnend != '"'; fnend--)
  74. ;
  75. assert(fnstart && fnend && (fnend > fnstart));
  76. fn = data_copy_escape_string(fnstart + 1,
  77. fnend - fnstart - 1);
  78. /* Don't allow nuls in filenames */
  79. if (memchr(fn.val, '\0', fn.len - 1))
  80. lexical_error("nul in line number directive");
  81. /* -1 since #line is the number of the next line */
  82. srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
  83. data_free(fn);
  84. }
  85. <*><<EOF>> {
  86. if (!pop_input_file()) {
  87. yyterminate();
  88. }
  89. }
  90. <*>{STRING} {
  91. DPRINT("String: %s\n", yytext);
  92. yylval.data = data_copy_escape_string(yytext+1,
  93. yyleng-2);
  94. return DT_STRING;
  95. }
  96. <*>"/dts-v1/" {
  97. DPRINT("Keyword: /dts-v1/\n");
  98. dts_version = 1;
  99. BEGIN_DEFAULT();
  100. return DT_V1;
  101. }
  102. <*>"/plugin/" {
  103. DPRINT("Keyword: /plugin/\n");
  104. return DT_PLUGIN;
  105. }
  106. <*>"/memreserve/" {
  107. DPRINT("Keyword: /memreserve/\n");
  108. BEGIN_DEFAULT();
  109. return DT_MEMRESERVE;
  110. }
  111. <*>"/bits/" {
  112. DPRINT("Keyword: /bits/\n");
  113. BEGIN_DEFAULT();
  114. return DT_BITS;
  115. }
  116. <*>"/delete-property/" {
  117. DPRINT("Keyword: /delete-property/\n");
  118. DPRINT("<PROPNODENAME>\n");
  119. BEGIN(PROPNODENAME);
  120. return DT_DEL_PROP;
  121. }
  122. <*>"/delete-node/" {
  123. DPRINT("Keyword: /delete-node/\n");
  124. DPRINT("<PROPNODENAME>\n");
  125. BEGIN(PROPNODENAME);
  126. return DT_DEL_NODE;
  127. }
  128. <*>"/omit-if-no-ref/" {
  129. DPRINT("Keyword: /omit-if-no-ref/\n");
  130. DPRINT("<PROPNODENAME>\n");
  131. BEGIN(PROPNODENAME);
  132. return DT_OMIT_NO_REF;
  133. }
  134. <*>{LABEL}: {
  135. DPRINT("Label: %s\n", yytext);
  136. yylval.labelref = xstrdup(yytext);
  137. yylval.labelref[yyleng-1] = '\0';
  138. return DT_LABEL;
  139. }
  140. <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
  141. char *e;
  142. DPRINT("Integer Literal: '%s'\n", yytext);
  143. errno = 0;
  144. yylval.integer = strtoull(yytext, &e, 0);
  145. if (*e && e[strspn(e, "UL")]) {
  146. lexical_error("Bad integer literal '%s'",
  147. yytext);
  148. }
  149. if (errno == ERANGE)
  150. lexical_error("Integer literal '%s' out of range",
  151. yytext);
  152. else
  153. /* ERANGE is the only strtoull error triggerable
  154. * by strings matching the pattern */
  155. assert(errno == 0);
  156. return DT_LITERAL;
  157. }
  158. <*>{CHAR_LITERAL} {
  159. struct data d;
  160. DPRINT("Character literal: %s\n", yytext);
  161. d = data_copy_escape_string(yytext+1, yyleng-2);
  162. if (d.len == 1) {
  163. lexical_error("Empty character literal");
  164. yylval.integer = 0;
  165. } else {
  166. yylval.integer = (unsigned char)d.val[0];
  167. if (d.len > 2)
  168. lexical_error("Character literal has %d"
  169. " characters instead of 1",
  170. d.len - 1);
  171. }
  172. data_free(d);
  173. return DT_CHAR_LITERAL;
  174. }
  175. <*>\&{LABEL} { /* label reference */
  176. DPRINT("Ref: %s\n", yytext+1);
  177. yylval.labelref = xstrdup(yytext+1);
  178. return DT_REF;
  179. }
  180. <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
  181. yytext[yyleng-1] = '\0';
  182. DPRINT("Ref: %s\n", yytext+2);
  183. yylval.labelref = xstrdup(yytext+2);
  184. return DT_REF;
  185. }
  186. <BYTESTRING>[0-9a-fA-F]{2} {
  187. yylval.byte = strtol(yytext, NULL, 16);
  188. DPRINT("Byte: %02x\n", (int)yylval.byte);
  189. return DT_BYTE;
  190. }
  191. <BYTESTRING>"]" {
  192. DPRINT("/BYTESTRING\n");
  193. BEGIN_DEFAULT();
  194. return ']';
  195. }
  196. <PROPNODENAME>\\?{PROPNODECHAR}+ {
  197. DPRINT("PropNodeName: %s\n", yytext);
  198. yylval.propnodename = xstrdup((yytext[0] == '\\') ?
  199. yytext + 1 : yytext);
  200. BEGIN_DEFAULT();
  201. return DT_PROPNODENAME;
  202. }
  203. "/incbin/" {
  204. DPRINT("Binary Include\n");
  205. return DT_INCBIN;
  206. }
  207. <*>{WS}+ /* eat whitespace */
  208. <*>{COMMENT}+ /* eat C-style comments */
  209. <*>{LINECOMMENT}+ /* eat C++-style comments */
  210. <*>"<<" { return DT_LSHIFT; };
  211. <*>">>" { return DT_RSHIFT; };
  212. <*>"<=" { return DT_LE; };
  213. <*>">=" { return DT_GE; };
  214. <*>"==" { return DT_EQ; };
  215. <*>"!=" { return DT_NE; };
  216. <*>"&&" { return DT_AND; };
  217. <*>"||" { return DT_OR; };
  218. <*>. {
  219. DPRINT("Char: %c (\\x%02x)\n", yytext[0],
  220. (unsigned)yytext[0]);
  221. if (yytext[0] == '[') {
  222. DPRINT("<BYTESTRING>\n");
  223. BEGIN(BYTESTRING);
  224. }
  225. if ((yytext[0] == '{')
  226. || (yytext[0] == ';')) {
  227. DPRINT("<PROPNODENAME>\n");
  228. BEGIN(PROPNODENAME);
  229. }
  230. return yytext[0];
  231. }
  232. %%
  233. static void push_input_file(const char *filename)
  234. {
  235. assert(filename);
  236. srcfile_push(filename);
  237. yyin = current_srcfile->f;
  238. yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
  239. }
  240. static bool pop_input_file(void)
  241. {
  242. if (srcfile_pop() == 0)
  243. return false;
  244. yypop_buffer_state();
  245. yyin = current_srcfile->f;
  246. return true;
  247. }
  248. static void lexical_error(const char *fmt, ...)
  249. {
  250. va_list ap;
  251. va_start(ap, fmt);
  252. srcpos_verror(&yylloc, "Lexical error", fmt, ap);
  253. va_end(ap);
  254. treesource_error = true;
  255. }