dtc-lexer.l 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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, *tmp, *fn;
  64. /* skip text before line # */
  65. line = yytext;
  66. while (!isdigit((unsigned char)*line))
  67. line++;
  68. /* skip digits in line # */
  69. tmp = line;
  70. while (!isspace((unsigned char)*tmp))
  71. tmp++;
  72. /* "NULL"-terminate line # */
  73. *tmp = '\0';
  74. /* start of filename */
  75. fn = strchr(tmp + 1, '"') + 1;
  76. /* strip trailing " from filename */
  77. tmp = strchr(fn, '"');
  78. *tmp = 0;
  79. /* -1 since #line is the number of the next line */
  80. srcpos_set_line(xstrdup(fn), atoi(line) - 1);
  81. }
  82. <*><<EOF>> {
  83. if (!pop_input_file()) {
  84. yyterminate();
  85. }
  86. }
  87. <*>{STRING} {
  88. DPRINT("String: %s\n", yytext);
  89. yylval.data = data_copy_escape_string(yytext+1,
  90. yyleng-2);
  91. return DT_STRING;
  92. }
  93. <*>"/dts-v1/" {
  94. DPRINT("Keyword: /dts-v1/\n");
  95. dts_version = 1;
  96. BEGIN_DEFAULT();
  97. return DT_V1;
  98. }
  99. <*>"/memreserve/" {
  100. DPRINT("Keyword: /memreserve/\n");
  101. BEGIN_DEFAULT();
  102. return DT_MEMRESERVE;
  103. }
  104. <*>"/bits/" {
  105. DPRINT("Keyword: /bits/\n");
  106. BEGIN_DEFAULT();
  107. return DT_BITS;
  108. }
  109. <*>"/delete-property/" {
  110. DPRINT("Keyword: /delete-property/\n");
  111. DPRINT("<PROPNODENAME>\n");
  112. BEGIN(PROPNODENAME);
  113. return DT_DEL_PROP;
  114. }
  115. <*>"/delete-node/" {
  116. DPRINT("Keyword: /delete-node/\n");
  117. DPRINT("<PROPNODENAME>\n");
  118. BEGIN(PROPNODENAME);
  119. return DT_DEL_NODE;
  120. }
  121. <*>{LABEL}: {
  122. DPRINT("Label: %s\n", yytext);
  123. yylval.labelref = xstrdup(yytext);
  124. yylval.labelref[yyleng-1] = '\0';
  125. return DT_LABEL;
  126. }
  127. <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
  128. char *e;
  129. DPRINT("Integer Literal: '%s'\n", yytext);
  130. errno = 0;
  131. yylval.integer = strtoull(yytext, &e, 0);
  132. assert(!(*e) || !e[strspn(e, "UL")]);
  133. if (errno == ERANGE)
  134. lexical_error("Integer literal '%s' out of range",
  135. yytext);
  136. else
  137. /* ERANGE is the only strtoull error triggerable
  138. * by strings matching the pattern */
  139. assert(errno == 0);
  140. return DT_LITERAL;
  141. }
  142. <*>{CHAR_LITERAL} {
  143. struct data d;
  144. DPRINT("Character literal: %s\n", yytext);
  145. d = data_copy_escape_string(yytext+1, yyleng-2);
  146. if (d.len == 1) {
  147. lexical_error("Empty character literal");
  148. yylval.integer = 0;
  149. return DT_CHAR_LITERAL;
  150. }
  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. return DT_CHAR_LITERAL;
  157. }
  158. <*>\&{LABEL} { /* label reference */
  159. DPRINT("Ref: %s\n", yytext+1);
  160. yylval.labelref = xstrdup(yytext+1);
  161. return DT_REF;
  162. }
  163. <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
  164. yytext[yyleng-1] = '\0';
  165. DPRINT("Ref: %s\n", yytext+2);
  166. yylval.labelref = xstrdup(yytext+2);
  167. return DT_REF;
  168. }
  169. <BYTESTRING>[0-9a-fA-F]{2} {
  170. yylval.byte = strtol(yytext, NULL, 16);
  171. DPRINT("Byte: %02x\n", (int)yylval.byte);
  172. return DT_BYTE;
  173. }
  174. <BYTESTRING>"]" {
  175. DPRINT("/BYTESTRING\n");
  176. BEGIN_DEFAULT();
  177. return ']';
  178. }
  179. <PROPNODENAME>\\?{PROPNODECHAR}+ {
  180. DPRINT("PropNodeName: %s\n", yytext);
  181. yylval.propnodename = xstrdup((yytext[0] == '\\') ?
  182. yytext + 1 : yytext);
  183. BEGIN_DEFAULT();
  184. return DT_PROPNODENAME;
  185. }
  186. "/incbin/" {
  187. DPRINT("Binary Include\n");
  188. return DT_INCBIN;
  189. }
  190. <*>{WS}+ /* eat whitespace */
  191. <*>{COMMENT}+ /* eat C-style comments */
  192. <*>{LINECOMMENT}+ /* eat C++-style comments */
  193. <*>"<<" { return DT_LSHIFT; };
  194. <*>">>" { return DT_RSHIFT; };
  195. <*>"<=" { return DT_LE; };
  196. <*>">=" { return DT_GE; };
  197. <*>"==" { return DT_EQ; };
  198. <*>"!=" { return DT_NE; };
  199. <*>"&&" { return DT_AND; };
  200. <*>"||" { return DT_OR; };
  201. <*>. {
  202. DPRINT("Char: %c (\\x%02x)\n", yytext[0],
  203. (unsigned)yytext[0]);
  204. if (yytext[0] == '[') {
  205. DPRINT("<BYTESTRING>\n");
  206. BEGIN(BYTESTRING);
  207. }
  208. if ((yytext[0] == '{')
  209. || (yytext[0] == ';')) {
  210. DPRINT("<PROPNODENAME>\n");
  211. BEGIN(PROPNODENAME);
  212. }
  213. return yytext[0];
  214. }
  215. %%
  216. static void push_input_file(const char *filename)
  217. {
  218. assert(filename);
  219. srcfile_push(filename);
  220. yyin = current_srcfile->f;
  221. yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
  222. }
  223. static bool pop_input_file(void)
  224. {
  225. if (srcfile_pop() == 0)
  226. return false;
  227. yypop_buffer_state();
  228. yyin = current_srcfile->f;
  229. return true;
  230. }
  231. static void lexical_error(const char *fmt, ...)
  232. {
  233. va_list ap;
  234. va_start(ap, fmt);
  235. srcpos_verror(&yylloc, "Lexical error", fmt, ap);
  236. va_end(ap);
  237. treesource_error = true;
  238. }