dtc-lexer.l 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. 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. /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
  38. #define YY_USER_ACTION \
  39. { \
  40. srcpos_update(&yylloc, yytext, yyleng); \
  41. }
  42. /*#define LEXDEBUG 1*/
  43. #ifdef LEXDEBUG
  44. #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
  45. #else
  46. #define DPRINT(fmt, ...) do { } while (0)
  47. #endif
  48. static int dts_version = 1;
  49. #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
  50. BEGIN(V1); \
  51. static void push_input_file(const char *filename);
  52. static int pop_input_file(void);
  53. %}
  54. %%
  55. <*>"/include/"{WS}*{STRING} {
  56. char *name = strchr(yytext, '\"') + 1;
  57. yytext[yyleng-1] = '\0';
  58. push_input_file(name);
  59. }
  60. <*><<EOF>> {
  61. if (!pop_input_file()) {
  62. yyterminate();
  63. }
  64. }
  65. <*>{STRING} {
  66. DPRINT("String: %s\n", yytext);
  67. yylval.data = data_copy_escape_string(yytext+1,
  68. yyleng-2);
  69. return DT_STRING;
  70. }
  71. <*>"/dts-v1/" {
  72. DPRINT("Keyword: /dts-v1/\n");
  73. dts_version = 1;
  74. BEGIN_DEFAULT();
  75. return DT_V1;
  76. }
  77. <*>"/memreserve/" {
  78. DPRINT("Keyword: /memreserve/\n");
  79. BEGIN_DEFAULT();
  80. return DT_MEMRESERVE;
  81. }
  82. <*>{LABEL}: {
  83. DPRINT("Label: %s\n", yytext);
  84. yylval.labelref = xstrdup(yytext);
  85. yylval.labelref[yyleng-1] = '\0';
  86. return DT_LABEL;
  87. }
  88. <V1>[0-9]+|0[xX][0-9a-fA-F]+ {
  89. yylval.literal = xstrdup(yytext);
  90. DPRINT("Literal: '%s'\n", yylval.literal);
  91. return DT_LITERAL;
  92. }
  93. <*>\&{LABEL} { /* label reference */
  94. DPRINT("Ref: %s\n", yytext+1);
  95. yylval.labelref = xstrdup(yytext+1);
  96. return DT_REF;
  97. }
  98. <*>"&{/"{PATHCHAR}+\} { /* new-style path reference */
  99. yytext[yyleng-1] = '\0';
  100. DPRINT("Ref: %s\n", yytext+2);
  101. yylval.labelref = xstrdup(yytext+2);
  102. return DT_REF;
  103. }
  104. <BYTESTRING>[0-9a-fA-F]{2} {
  105. yylval.byte = strtol(yytext, NULL, 16);
  106. DPRINT("Byte: %02x\n", (int)yylval.byte);
  107. return DT_BYTE;
  108. }
  109. <BYTESTRING>"]" {
  110. DPRINT("/BYTESTRING\n");
  111. BEGIN_DEFAULT();
  112. return ']';
  113. }
  114. <PROPNODENAME>{PROPNODECHAR}+ {
  115. DPRINT("PropNodeName: %s\n", yytext);
  116. yylval.propnodename = xstrdup(yytext);
  117. BEGIN_DEFAULT();
  118. return DT_PROPNODENAME;
  119. }
  120. "/incbin/" {
  121. DPRINT("Binary Include\n");
  122. return DT_INCBIN;
  123. }
  124. <*>{WS}+ /* eat whitespace */
  125. <*>{COMMENT}+ /* eat C-style comments */
  126. <*>{LINECOMMENT}+ /* eat C++-style comments */
  127. <*>. {
  128. DPRINT("Char: %c (\\x%02x)\n", yytext[0],
  129. (unsigned)yytext[0]);
  130. if (yytext[0] == '[') {
  131. DPRINT("<BYTESTRING>\n");
  132. BEGIN(BYTESTRING);
  133. }
  134. if ((yytext[0] == '{')
  135. || (yytext[0] == ';')) {
  136. DPRINT("<PROPNODENAME>\n");
  137. BEGIN(PROPNODENAME);
  138. }
  139. return yytext[0];
  140. }
  141. %%
  142. static void push_input_file(const char *filename)
  143. {
  144. assert(filename);
  145. srcfile_push(filename);
  146. yyin = current_srcfile->f;
  147. yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
  148. }
  149. static int pop_input_file(void)
  150. {
  151. if (srcfile_pop() == 0)
  152. return 0;
  153. yypop_buffer_state();
  154. yyin = current_srcfile->f;
  155. return 1;
  156. }