dtc-lexer.l 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 yylineno
  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. WS [[:space:]]
  30. COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
  31. LINECOMMENT "//".*\n
  32. %{
  33. #include "dtc.h"
  34. #include "srcpos.h"
  35. #include "dtc-parser.tab.h"
  36. /*#define LEXDEBUG 1*/
  37. #ifdef LEXDEBUG
  38. #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
  39. #else
  40. #define DPRINT(fmt, ...) do { } while (0)
  41. #endif
  42. static int dts_version; /* = 0 */
  43. #define BEGIN_DEFAULT() if (dts_version == 0) { \
  44. DPRINT("<INITIAL>\n"); \
  45. BEGIN(INITIAL); \
  46. } else { \
  47. DPRINT("<V1>\n"); \
  48. BEGIN(V1); \
  49. }
  50. static void push_input_file(const char *filename);
  51. static int pop_input_file(void);
  52. %}
  53. %%
  54. <*>"/include/"{WS}*{STRING} {
  55. char *name = strchr(yytext, '\"') + 1;
  56. yytext[yyleng-1] = '\0';
  57. push_input_file(name);
  58. }
  59. <*><<EOF>> {
  60. if (!pop_input_file()) {
  61. yyterminate();
  62. }
  63. }
  64. <*>{STRING} {
  65. yylloc.file = srcpos_file;
  66. yylloc.first_line = yylineno;
  67. DPRINT("String: %s\n", yytext);
  68. yylval.data = data_copy_escape_string(yytext+1,
  69. yyleng-2);
  70. yylloc.first_line = yylineno;
  71. return DT_STRING;
  72. }
  73. <*>"/dts-v1/" {
  74. yylloc.file = srcpos_file;
  75. yylloc.first_line = yylineno;
  76. DPRINT("Keyword: /dts-v1/\n");
  77. dts_version = 1;
  78. BEGIN_DEFAULT();
  79. return DT_V1;
  80. }
  81. <*>"/memreserve/" {
  82. yylloc.file = srcpos_file;
  83. yylloc.first_line = yylineno;
  84. DPRINT("Keyword: /memreserve/\n");
  85. BEGIN_DEFAULT();
  86. return DT_MEMRESERVE;
  87. }
  88. <*>{LABEL}: {
  89. yylloc.file = srcpos_file;
  90. yylloc.first_line = yylineno;
  91. DPRINT("Label: %s\n", yytext);
  92. yylval.labelref = strdup(yytext);
  93. yylval.labelref[yyleng-1] = '\0';
  94. return DT_LABEL;
  95. }
  96. <INITIAL>[bodh]# {
  97. yylloc.file = srcpos_file;
  98. yylloc.first_line = yylineno;
  99. if (*yytext == 'b')
  100. yylval.cbase = 2;
  101. else if (*yytext == 'o')
  102. yylval.cbase = 8;
  103. else if (*yytext == 'd')
  104. yylval.cbase = 10;
  105. else
  106. yylval.cbase = 16;
  107. DPRINT("Base: %d\n", yylval.cbase);
  108. return DT_BASE;
  109. }
  110. <INITIAL>[0-9a-fA-F]+ {
  111. yylloc.file = srcpos_file;
  112. yylloc.first_line = yylineno;
  113. yylval.literal = strdup(yytext);
  114. DPRINT("Literal: '%s'\n", yylval.literal);
  115. return DT_LEGACYLITERAL;
  116. }
  117. <V1>[0-9]+|0[xX][0-9a-fA-F]+ {
  118. yylloc.file = srcpos_file;
  119. yylloc.first_line = yylineno;
  120. yylval.literal = strdup(yytext);
  121. DPRINT("Literal: '%s'\n", yylval.literal);
  122. return DT_LITERAL;
  123. }
  124. \&{LABEL} { /* label reference */
  125. yylloc.file = srcpos_file;
  126. yylloc.first_line = yylineno;
  127. DPRINT("Ref: %s\n", yytext+1);
  128. yylval.labelref = strdup(yytext+1);
  129. return DT_REF;
  130. }
  131. "&{/"{PATHCHAR}+\} { /* new-style path reference */
  132. yylloc.file = srcpos_file;
  133. yylloc.first_line = yylineno;
  134. yytext[yyleng-1] = '\0';
  135. DPRINT("Ref: %s\n", yytext+2);
  136. yylval.labelref = strdup(yytext+2);
  137. return DT_REF;
  138. }
  139. <INITIAL>"&/"{PATHCHAR}+ { /* old-style path reference */
  140. yylloc.file = srcpos_file;
  141. yylloc.first_line = yylineno;
  142. DPRINT("Ref: %s\n", yytext+1);
  143. yylval.labelref = strdup(yytext+1);
  144. return DT_REF;
  145. }
  146. <BYTESTRING>[0-9a-fA-F]{2} {
  147. yylloc.file = srcpos_file;
  148. yylloc.first_line = yylineno;
  149. yylval.byte = strtol(yytext, NULL, 16);
  150. DPRINT("Byte: %02x\n", (int)yylval.byte);
  151. return DT_BYTE;
  152. }
  153. <BYTESTRING>"]" {
  154. yylloc.file = srcpos_file;
  155. yylloc.first_line = yylineno;
  156. DPRINT("/BYTESTRING\n");
  157. BEGIN_DEFAULT();
  158. return ']';
  159. }
  160. <PROPNODENAME>{PROPNODECHAR}+ {
  161. yylloc.file = srcpos_file;
  162. yylloc.first_line = yylineno;
  163. DPRINT("PropNodeName: %s\n", yytext);
  164. yylval.propnodename = strdup(yytext);
  165. BEGIN_DEFAULT();
  166. return DT_PROPNODENAME;
  167. }
  168. "/incbin/" {
  169. yylloc.file = srcpos_file;
  170. yylloc.first_line = yylineno;
  171. DPRINT("Binary Include\n");
  172. return DT_INCBIN;
  173. }
  174. <*>{WS}+ /* eat whitespace */
  175. <*>{COMMENT}+ /* eat C-style comments */
  176. <*>{LINECOMMENT}+ /* eat C++-style comments */
  177. <*>. {
  178. yylloc.file = srcpos_file;
  179. yylloc.first_line = yylineno;
  180. DPRINT("Char: %c (\\x%02x)\n", yytext[0],
  181. (unsigned)yytext[0]);
  182. if (yytext[0] == '[') {
  183. DPRINT("<BYTESTRING>\n");
  184. BEGIN(BYTESTRING);
  185. }
  186. if ((yytext[0] == '{')
  187. || (yytext[0] == ';')) {
  188. DPRINT("<PROPNODENAME>\n");
  189. BEGIN(PROPNODENAME);
  190. }
  191. return yytext[0];
  192. }
  193. %%
  194. /*
  195. * Stack of nested include file contexts.
  196. */
  197. struct incl_file {
  198. struct dtc_file *file;
  199. YY_BUFFER_STATE yy_prev_buf;
  200. int yy_prev_lineno;
  201. struct incl_file *prev;
  202. };
  203. static struct incl_file *incl_file_stack;
  204. /*
  205. * Detect infinite include recursion.
  206. */
  207. #define MAX_INCLUDE_DEPTH (100)
  208. static int incl_depth = 0;
  209. static void push_input_file(const char *filename)
  210. {
  211. struct incl_file *incl_file;
  212. struct dtc_file *newfile;
  213. struct search_path search, *searchptr = NULL;
  214. assert(filename);
  215. if (incl_depth++ >= MAX_INCLUDE_DEPTH)
  216. die("Includes nested too deeply");
  217. if (srcpos_file) {
  218. search.dir = srcpos_file->dir;
  219. search.next = NULL;
  220. search.prev = NULL;
  221. searchptr = &search;
  222. }
  223. newfile = dtc_open_file(filename, searchptr);
  224. incl_file = xmalloc(sizeof(struct incl_file));
  225. /*
  226. * Save current context.
  227. */
  228. incl_file->yy_prev_buf = YY_CURRENT_BUFFER;
  229. incl_file->yy_prev_lineno = yylineno;
  230. incl_file->file = srcpos_file;
  231. incl_file->prev = incl_file_stack;
  232. incl_file_stack = incl_file;
  233. /*
  234. * Establish new context.
  235. */
  236. srcpos_file = newfile;
  237. yylineno = 1;
  238. yyin = newfile->file;
  239. yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  240. }
  241. static int pop_input_file(void)
  242. {
  243. struct incl_file *incl_file;
  244. if (incl_file_stack == 0)
  245. return 0;
  246. dtc_close_file(srcpos_file);
  247. /*
  248. * Pop.
  249. */
  250. --incl_depth;
  251. incl_file = incl_file_stack;
  252. incl_file_stack = incl_file->prev;
  253. /*
  254. * Recover old context.
  255. */
  256. yy_delete_buffer(YY_CURRENT_BUFFER);
  257. yy_switch_to_buffer(incl_file->yy_prev_buf);
  258. yylineno = incl_file->yy_prev_lineno;
  259. srcpos_file = incl_file->file;
  260. yyin = incl_file->file ? incl_file->file->file : NULL;
  261. /*
  262. * Free old state.
  263. */
  264. free(incl_file);
  265. return 1;
  266. }