2 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
3 * See included license file for license details.
7 /* %option prefix="Elftosb" */
9 %option never-interactive
10 %option yyclass="ElftosbLexer"
14 #include "ElftosbLexer.h"
18 #include "HexValues.h"
21 using namespace elftosb;
23 //! Always executed before all other actions when a token is matched.
24 //! This action just assign the first and last lines of the token to
25 //! the current line. In most cases this is correct.
26 #define YY_USER_ACTION do { \
27 m_location.m_firstLine = m_line; \
28 m_location.m_lastLine = m_line; \
36 IDENT [a-zA-Z_][a-zA-Z0-9_]*
37 ESC \\(x{HEXDIGIT}{2}|.)
39 /* start conditions */
44 options { return TOK_OPTIONS; }
45 constants { return TOK_CONSTANTS; }
46 sources { return TOK_SOURCES; }
47 filters { return TOK_FILTERS; }
48 section { return TOK_SECTION; }
49 extern { return TOK_EXTERN; }
50 from { return TOK_FROM; }
51 raw { return TOK_RAW; }
52 load { return TOK_LOAD; }
53 jump { return TOK_JUMP; }
54 call { return TOK_CALL; }
55 mode { return TOK_MODE; }
57 else { return TOK_ELSE; }
58 defined { return TOK_DEFINED; }
59 info { return TOK_INFO; }
60 warning { return TOK_WARNING; }
61 error { return TOK_ERROR; }
62 sizeof { return TOK_SIZEOF; }
63 dcd { return TOK_DCD; }
64 hab { return TOK_HAB; }
65 ivt { return TOK_IVT; }
67 [whb]/[^a-zA-Z_0-9] { // must be followed by any non-ident char
75 theSize = kHalfWordSize;
81 m_symbolValue.m_int = new elftosb::SizedIntegerValue(0, theSize);
86 m_symbolValue.m_int = new elftosb::SizedIntegerValue(1, kWordSize);
87 return TOK_INT_LITERAL;
91 m_symbolValue.m_int = new elftosb::SizedIntegerValue(0, kWordSize);
92 return TOK_INT_LITERAL;
96 m_symbolValue.m_str = new std::string(yytext);
97 if (isSourceName(m_symbolValue.m_str))
99 return TOK_SOURCE_NAME;
107 ({DIGIT}+|0x{HEXDIGIT}+|0b{BINDIGIT}+)([ \t]*[GMK])? {
112 // check for binary number
113 if (yytext[0] == '0' && yytext[1] == 'b')
115 base = 2; // this is a binary number
116 yytext += 2; // skip over the "0b"
120 value = (uint32_t)strtoul(yytext, NULL, base);
123 switch (yytext[strlen(yytext) - 1])
126 mult = 1024 * 1024 * 1024;
139 // set resulting symbol value
140 m_symbolValue.m_int = new elftosb::SizedIntegerValue(value * mult, kWordSize);
141 return TOK_INT_LITERAL;
144 \'(.|ESC)\'|\'(.|ESC){2}\'|\'(.|ESC){4}\' {
147 int len = strlen(yytext);
155 value = (value << 8) | yytext[2];
156 theSize = kHalfWordSize;
160 value = (value << 8) | yytext[3];
161 value = (value << 8) | yytext[4];
164 m_symbolValue.m_int = new elftosb::SizedIntegerValue(value, theSize);
165 return TOK_INT_LITERAL;
168 \$[\.\*a-zA-Z0-9_\[\]\^\?\-]+ {
169 // remove $ from string
170 m_symbolValue.m_str = new std::string(&yytext[1]);
171 return TOK_SECTION_NAME;
175 "/*" { BEGIN(mlcmt); }
179 m_blobFirstLine = yylineno;
207 ".." { return TOK_DOT_DOT; }
223 "<<" { return TOK_LSHIFT; }
225 ">>" { return TOK_RSHIFT; }
231 "**" { return TOK_POWER; }
235 ">=" { return TOK_GEQ; }
237 "<=" { return TOK_LEQ; }
239 "==" { return TOK_EQ; }
241 "!=" { return TOK_NEQ; }
243 "&&" { return TOK_AND; }
245 "||" { return TOK_OR; }
252 yytext[strlen(yytext) - 1] = 0;
253 // processStringEscapes(yytext, yytext);
254 m_symbolValue.m_str = new std::string(yytext);
255 return TOK_STRING_LITERAL;
258 <blob>{HEXDIGIT}{2} {
259 uint8_t x = (hexCharToInt(yytext[0]) << 4) | hexCharToInt(yytext[1]);
260 m_blob->append(&x, 1);
265 m_symbolValue.m_blob = m_blob;
267 m_location.m_firstLine = m_blobFirstLine;
272 // end of multi-line comment, return to initial state
277 (#|\/\/).*$ /* absorb single-line comment */
279 <*>[ \t] /* eat up whitespace in all states */
282 /* eat up whitespace and count lines in all states */
286 <mlcmt>. /* ignore all other chars in a multi-line comment */
289 /* all other chars produce errors */
291 sprintf(msg, "unexpected character '%c' on line %d", yytext[0], m_line);
297 // verbatim code copied to the bottom of the output