dtc-lexer.l 6.8 KB

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