dtc-lexer.l 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 INCLUDE
  22. %x BYTESTRING
  23. %x PROPNODENAME
  24. %s V1
  25. PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
  26. PATHCHAR ({PROPNODECHAR}|[/])
  27. LABEL [a-zA-Z_][a-zA-Z0-9_]*
  28. STRING \"([^\\"]|\\.)*\"
  29. CHAR_LITERAL '([^']|\\')*'
  30. WS [[:space:]]
  31. COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
  32. LINECOMMENT "//".*\n
  33. %{
  34. #include "dtc.h"
  35. #include "srcpos.h"
  36. #include "dtc-parser.tab.h"
  37. extern YYLTYPE yylloc;
  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 int pop_input_file(void);
  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)?{WS}+[0-9]+{WS}+{STRING}({WS}+[0-9]+)? {
  62. char *line, *tmp, *fn;
  63. /* skip text before line # */
  64. line = yytext;
  65. while (!isdigit(*line))
  66. line++;
  67. /* skip digits in line # */
  68. tmp = line;
  69. while (!isspace(*tmp))
  70. tmp++;
  71. /* "NULL"-terminate line # */
  72. *tmp = '\0';
  73. /* start of filename */
  74. fn = strchr(tmp + 1, '"') + 1;
  75. /* strip trailing " from filename */
  76. tmp = strchr(fn, '"');
  77. *tmp = 0;
  78. /* -1 since #line is the number of the next line */
  79. srcpos_set_line(xstrdup(fn), atoi(line) - 1);
  80. }
  81. <*><<EOF>> {
  82. if (!pop_input_file()) {
  83. yyterminate();
  84. }
  85. }
  86. <*>{STRING} {
  87. DPRINT("String: %s\n", yytext);
  88. yylval.data = data_copy_escape_string(yytext+1,
  89. yyleng-2);
  90. return DT_STRING;
  91. }
  92. <*>"/dts-v1/" {
  93. DPRINT("Keyword: /dts-v1/\n");
  94. dts_version = 1;
  95. BEGIN_DEFAULT();
  96. return DT_V1;
  97. }
  98. <*>"/memreserve/" {
  99. DPRINT("Keyword: /memreserve/\n");
  100. BEGIN_DEFAULT();
  101. return DT_MEMRESERVE;
  102. }
  103. <*>"/bits/" {
  104. DPRINT("Keyword: /bits/\n");
  105. BEGIN_DEFAULT();
  106. return DT_BITS;
  107. }
  108. <*>"/delete-property/" {
  109. DPRINT("Keyword: /delete-property/\n");
  110. DPRINT("<PROPNODENAME>\n");
  111. BEGIN(PROPNODENAME);
  112. return DT_DEL_PROP;
  113. }
  114. <*>"/delete-node/" {
  115. DPRINT("Keyword: /delete-node/\n");
  116. DPRINT("<PROPNODENAME>\n");
  117. BEGIN(PROPNODENAME);
  118. return DT_DEL_NODE;
  119. }
  120. <*>{LABEL}: {
  121. DPRINT("Label: %s\n", yytext);
  122. yylval.labelref = xstrdup(yytext);
  123. yylval.labelref[yyleng-1] = '\0';
  124. return DT_LABEL;
  125. }
  126. <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
  127. yylval.literal = xstrdup(yytext);
  128. DPRINT("Literal: '%s'\n", yylval.literal);
  129. return DT_LITERAL;
  130. }
  131. <*>{CHAR_LITERAL} {
  132. yytext[yyleng-1] = '\0';
  133. yylval.literal = xstrdup(yytext+1);
  134. DPRINT("Character literal: %s\n", yylval.literal);
  135. return DT_CHAR_LITERAL;
  136. }
  137. <*>\&{LABEL} { /* label reference */
  138. DPRINT("Ref: %s\n", yytext+1);
  139. yylval.labelref = xstrdup(yytext+1);
  140. return DT_REF;
  141. }
  142. <*>"&{/"{PATHCHAR}+\} { /* new-style path reference */
  143. yytext[yyleng-1] = '\0';
  144. DPRINT("Ref: %s\n", yytext+2);
  145. yylval.labelref = xstrdup(yytext+2);
  146. return DT_REF;
  147. }
  148. <BYTESTRING>[0-9a-fA-F]{2} {
  149. yylval.byte = strtol(yytext, NULL, 16);
  150. DPRINT("Byte: %02x\n", (int)yylval.byte);
  151. return DT_BYTE;
  152. }
  153. <BYTESTRING>"]" {
  154. DPRINT("/BYTESTRING\n");
  155. BEGIN_DEFAULT();
  156. return ']';
  157. }
  158. <PROPNODENAME>\\?{PROPNODECHAR}+ {
  159. DPRINT("PropNodeName: %s\n", yytext);
  160. yylval.propnodename = xstrdup((yytext[0] == '\\') ?
  161. yytext + 1 : yytext);
  162. BEGIN_DEFAULT();
  163. return DT_PROPNODENAME;
  164. }
  165. "/incbin/" {
  166. DPRINT("Binary Include\n");
  167. return DT_INCBIN;
  168. }
  169. <*>{WS}+ /* eat whitespace */
  170. <*>{COMMENT}+ /* eat C-style comments */
  171. <*>{LINECOMMENT}+ /* eat C++-style comments */
  172. <*>"<<" { return DT_LSHIFT; };
  173. <*>">>" { return DT_RSHIFT; };
  174. <*>"<=" { return DT_LE; };
  175. <*>">=" { return DT_GE; };
  176. <*>"==" { return DT_EQ; };
  177. <*>"!=" { return DT_NE; };
  178. <*>"&&" { return DT_AND; };
  179. <*>"||" { return DT_OR; };
  180. <*>. {
  181. DPRINT("Char: %c (\\x%02x)\n", yytext[0],
  182. (unsigned)yytext[0]);
  183. if (yytext[0] == '[') {
  184. DPRINT("<BYTESTRING>\n");
  185. BEGIN(BYTESTRING);
  186. }
  187. if ((yytext[0] == '{')
  188. || (yytext[0] == ';')) {
  189. DPRINT("<PROPNODENAME>\n");
  190. BEGIN(PROPNODENAME);
  191. }
  192. return yytext[0];
  193. }
  194. %%
  195. static void push_input_file(const char *filename)
  196. {
  197. assert(filename);
  198. srcfile_push(filename);
  199. yyin = current_srcfile->f;
  200. yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
  201. }
  202. static int pop_input_file(void)
  203. {
  204. if (srcfile_pop() == 0)
  205. return 0;
  206. yypop_buffer_state();
  207. yyin = current_srcfile->f;
  208. return 1;
  209. }