dtc-lexer.l 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. <*>{LABEL}: {
  130. DPRINT("Label: %s\n", yytext);
  131. yylval.labelref = xstrdup(yytext);
  132. yylval.labelref[yyleng-1] = '\0';
  133. return DT_LABEL;
  134. }
  135. <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
  136. char *e;
  137. DPRINT("Integer Literal: '%s'\n", yytext);
  138. errno = 0;
  139. yylval.integer = strtoull(yytext, &e, 0);
  140. if (*e && e[strspn(e, "UL")]) {
  141. lexical_error("Bad integer literal '%s'",
  142. yytext);
  143. }
  144. if (errno == ERANGE)
  145. lexical_error("Integer literal '%s' out of range",
  146. yytext);
  147. else
  148. /* ERANGE is the only strtoull error triggerable
  149. * by strings matching the pattern */
  150. assert(errno == 0);
  151. return DT_LITERAL;
  152. }
  153. <*>{CHAR_LITERAL} {
  154. struct data d;
  155. DPRINT("Character literal: %s\n", yytext);
  156. d = data_copy_escape_string(yytext+1, yyleng-2);
  157. if (d.len == 1) {
  158. lexical_error("Empty character literal");
  159. yylval.integer = 0;
  160. } else {
  161. yylval.integer = (unsigned char)d.val[0];
  162. if (d.len > 2)
  163. lexical_error("Character literal has %d"
  164. " characters instead of 1",
  165. d.len - 1);
  166. }
  167. data_free(d);
  168. return DT_CHAR_LITERAL;
  169. }
  170. <*>\&{LABEL} { /* label reference */
  171. DPRINT("Ref: %s\n", yytext+1);
  172. yylval.labelref = xstrdup(yytext+1);
  173. return DT_REF;
  174. }
  175. <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
  176. yytext[yyleng-1] = '\0';
  177. DPRINT("Ref: %s\n", yytext+2);
  178. yylval.labelref = xstrdup(yytext+2);
  179. return DT_REF;
  180. }
  181. <BYTESTRING>[0-9a-fA-F]{2} {
  182. yylval.byte = strtol(yytext, NULL, 16);
  183. DPRINT("Byte: %02x\n", (int)yylval.byte);
  184. return DT_BYTE;
  185. }
  186. <BYTESTRING>"]" {
  187. DPRINT("/BYTESTRING\n");
  188. BEGIN_DEFAULT();
  189. return ']';
  190. }
  191. <PROPNODENAME>\\?{PROPNODECHAR}+ {
  192. DPRINT("PropNodeName: %s\n", yytext);
  193. yylval.propnodename = xstrdup((yytext[0] == '\\') ?
  194. yytext + 1 : yytext);
  195. BEGIN_DEFAULT();
  196. return DT_PROPNODENAME;
  197. }
  198. "/incbin/" {
  199. DPRINT("Binary Include\n");
  200. return DT_INCBIN;
  201. }
  202. <*>{WS}+ /* eat whitespace */
  203. <*>{COMMENT}+ /* eat C-style comments */
  204. <*>{LINECOMMENT}+ /* eat C++-style comments */
  205. <*>"<<" { return DT_LSHIFT; };
  206. <*>">>" { return DT_RSHIFT; };
  207. <*>"<=" { return DT_LE; };
  208. <*>">=" { return DT_GE; };
  209. <*>"==" { return DT_EQ; };
  210. <*>"!=" { return DT_NE; };
  211. <*>"&&" { return DT_AND; };
  212. <*>"||" { return DT_OR; };
  213. <*>. {
  214. DPRINT("Char: %c (\\x%02x)\n", yytext[0],
  215. (unsigned)yytext[0]);
  216. if (yytext[0] == '[') {
  217. DPRINT("<BYTESTRING>\n");
  218. BEGIN(BYTESTRING);
  219. }
  220. if ((yytext[0] == '{')
  221. || (yytext[0] == ';')) {
  222. DPRINT("<PROPNODENAME>\n");
  223. BEGIN(PROPNODENAME);
  224. }
  225. return yytext[0];
  226. }
  227. %%
  228. static void push_input_file(const char *filename)
  229. {
  230. assert(filename);
  231. srcfile_push(filename);
  232. yyin = current_srcfile->f;
  233. yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
  234. }
  235. static bool pop_input_file(void)
  236. {
  237. if (srcfile_pop() == 0)
  238. return false;
  239. yypop_buffer_state();
  240. yyin = current_srcfile->f;
  241. return true;
  242. }
  243. static void lexical_error(const char *fmt, ...)
  244. {
  245. va_list ap;
  246. va_start(ap, fmt);
  247. srcpos_verror(&yylloc, "Lexical error", fmt, ap);
  248. va_end(ap);
  249. treesource_error = true;
  250. }