dtc-lexer.l 6.1 KB

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