dtc-lexer.l 6.6 KB

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