Commit | Line | Data |
---|---|---|
e98a2d6e PP |
1 | %{ |
2 | /* | |
06a626b8 | 3 | * lexer.l |
e98a2d6e PP |
4 | * |
5 | * Common Trace Formal Lexer | |
6 | * | |
7 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8 | * | |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | */ | |
27 | ||
f73367f8 PP |
28 | #define BT_LOG_TAG "PLUGIN-CTF-METADATA-LEXER" |
29 | #include "logging.h" | |
30 | ||
e98a2d6e PP |
31 | #include <stdio.h> |
32 | #include <ctype.h> | |
06a626b8 JG |
33 | #include "scanner.h" |
34 | #include "parser.h" | |
35 | #include "ast.h" | |
e98a2d6e | 36 | |
f73367f8 PP |
37 | #define YY_FATAL_ERROR(_msg) BT_LOGF_STR(_msg) |
38 | ||
e98a2d6e PP |
39 | #define PARSE_INTEGER_LITERAL(base) \ |
40 | do { \ | |
41 | errno = 0; \ | |
42 | yylval->ull = strtoull(yytext, NULL, base); \ | |
43 | if (errno) { \ | |
f73367f8 PP |
44 | _BT_LOGE_LINENO(yylineno, \ |
45 | "Cannot parser constant integer: " \ | |
46 | "base=%d, text=\"%s\"", base, yytext); \ | |
47 | return CTF_ERROR; \ | |
e98a2d6e PP |
48 | } \ |
49 | } while (0) | |
50 | ||
51 | BT_HIDDEN | |
52 | void setstring(struct ctf_scanner *scanner, YYSTYPE *lvalp, const char *src); | |
53 | ||
54 | static void yyunput (int c, register char * yy_bp , yyscan_t yyscanner) | |
55 | __attribute__((unused)); | |
56 | static int input (yyscan_t yyscanner) __attribute__((unused)); | |
57 | ||
58 | BT_HIDDEN | |
59 | int import_string(struct ctf_scanner *scanner, YYSTYPE *lvalp, const char *src, char delim); | |
60 | ||
61 | %} | |
62 | ||
63 | %x comment_ml comment_sl string_lit char_const | |
64 | %option reentrant yylineno noyywrap bison-bridge | |
65 | %option extra-type="struct ctf_scanner *" | |
66 | /* bison-locations */ | |
67 | INTEGER_SUFFIX (U|UL|ULL|LU|LLU|Ul|Ull|lU|llU|u|uL|uLL|Lu|LLu|ul|ull|lu|llu) | |
68 | DIGIT [0-9] | |
69 | NONDIGIT [a-zA-Z_] | |
70 | HEXDIGIT [0-9A-Fa-f] | |
71 | OCTALDIGIT [0-7] | |
72 | UCHARLOWERCASE \\u{HEXDIGIT}{4} | |
73 | UCHARUPPERCASE \\U{HEXDIGIT}{8} | |
74 | ID_NONDIGIT {NONDIGIT}|{UCHARLOWERCASE}|{UCHARUPPERCASE} | |
75 | IDENTIFIER {ID_NONDIGIT}({ID_NONDIGIT}|{DIGIT})* | |
76 | %% | |
77 | ||
78 | /* | |
79 | * Using start conditions to deal with comments | |
80 | * and strings. | |
f73367f8 | 81 | */ |
e98a2d6e PP |
82 | |
83 | "/*" BEGIN(comment_ml); | |
84 | <comment_ml>[^*\n]* /* eat anything that's not a '*' */ | |
85 | <comment_ml>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */ | |
86 | <comment_ml>\n | |
87 | <comment_ml>"*"+"/" BEGIN(INITIAL); | |
88 | ||
89 | "//"[^\n]*\n /* skip comment */ | |
90 | ||
d4c6eae5 MJ |
91 | L?\"(\\.|[^\\"])*\" { if (import_string(yyextra, yylval, yytext, '\"') < 0) return CTF_ERROR; else return CTF_STRING_LITERAL; } |
92 | L?\'(\\.|[^\\'])*\' { if (import_string(yyextra, yylval, yytext, '\'') < 0) return CTF_ERROR; else return CTF_CHARACTER_LITERAL; } | |
e98a2d6e | 93 | |
d4c6eae5 MJ |
94 | "[" return CTF_LSBRAC; |
95 | "]" return CTF_RSBRAC; | |
96 | "(" return CTF_LPAREN; | |
97 | ")" return CTF_RPAREN; | |
98 | "{" return CTF_LBRAC; | |
99 | "}" return CTF_RBRAC; | |
100 | "->" return CTF_RARROW; | |
101 | "*" return CTF_STAR; | |
102 | "+" return CTF_PLUS; | |
103 | "-" return CTF_MINUS; | |
104 | "<" return CTF_LT; | |
105 | ">" return CTF_GT; | |
106 | := return CTF_TYPEASSIGN; | |
107 | : return CTF_COLON; | |
108 | ; return CTF_SEMICOLON; | |
109 | "..." return CTF_DOTDOTDOT; | |
110 | "." return CTF_DOT; | |
111 | = return CTF_EQUAL; | |
112 | "," return CTF_COMMA; | |
113 | align setstring(yyextra, yylval, yytext); return CTF_TOK_ALIGN; | |
114 | const setstring(yyextra, yylval, yytext); return CTF_CONST; | |
115 | char setstring(yyextra, yylval, yytext); return CTF_CHAR; | |
116 | clock setstring(yyextra, yylval, yytext); return CTF_CLOCK; | |
117 | double setstring(yyextra, yylval, yytext); return CTF_DOUBLE; | |
118 | enum setstring(yyextra, yylval, yytext); return CTF_ENUM; | |
119 | env setstring(yyextra, yylval, yytext); return CTF_ENV; | |
120 | event setstring(yyextra, yylval, yytext); return CTF_EVENT; | |
121 | floating_point setstring(yyextra, yylval, yytext); return CTF_FLOATING_POINT; | |
122 | float setstring(yyextra, yylval, yytext); return CTF_FLOAT; | |
123 | integer setstring(yyextra, yylval, yytext); return CTF_INTEGER; | |
124 | int setstring(yyextra, yylval, yytext); return CTF_INT; | |
125 | long setstring(yyextra, yylval, yytext); return CTF_LONG; | |
126 | short setstring(yyextra, yylval, yytext); return CTF_SHORT; | |
127 | signed setstring(yyextra, yylval, yytext); return CTF_SIGNED; | |
128 | stream setstring(yyextra, yylval, yytext); return CTF_STREAM; | |
129 | string setstring(yyextra, yylval, yytext); return CTF_STRING; | |
130 | struct setstring(yyextra, yylval, yytext); return CTF_STRUCT; | |
131 | trace setstring(yyextra, yylval, yytext); return CTF_TRACE; | |
132 | callsite setstring(yyextra, yylval, yytext); return CTF_CALLSITE; | |
133 | typealias setstring(yyextra, yylval, yytext); return CTF_TYPEALIAS; | |
134 | typedef setstring(yyextra, yylval, yytext); return CTF_TYPEDEF; | |
135 | unsigned setstring(yyextra, yylval, yytext); return CTF_UNSIGNED; | |
136 | variant setstring(yyextra, yylval, yytext); return CTF_VARIANT; | |
137 | void setstring(yyextra, yylval, yytext); return CTF_VOID; | |
138 | _Bool setstring(yyextra, yylval, yytext); return CTF_BOOL; | |
139 | _Complex setstring(yyextra, yylval, yytext); return CTF_COMPLEX; | |
140 | _Imaginary setstring(yyextra, yylval, yytext); return CTF_IMAGINARY; | |
141 | [1-9]{DIGIT}*{INTEGER_SUFFIX}? PARSE_INTEGER_LITERAL(10); return CTF_INTEGER_LITERAL; | |
142 | 0{OCTALDIGIT}*{INTEGER_SUFFIX}? PARSE_INTEGER_LITERAL(8); return CTF_INTEGER_LITERAL; | |
143 | 0[xX]{HEXDIGIT}+{INTEGER_SUFFIX}? PARSE_INTEGER_LITERAL(16); return CTF_INTEGER_LITERAL; | |
e98a2d6e | 144 | |
f73367f8 | 145 | {IDENTIFIER} BT_LOGV("Got identifier: id=\"%s\"", yytext); setstring(yyextra, yylval, yytext); if (is_type(yyextra, yytext)) return ID_TYPE; else return IDENTIFIER; |
e98a2d6e | 146 | [ \t\r\n] ; /* ignore */ |
f73367f8 | 147 | . _BT_LOGE_LINENO(yylineno, "Invalid character: char=\"%c\", val=0x%02x", isprint(yytext[0]) ? yytext[0] : '\0', yytext[0]); return CTF_ERROR; |
e98a2d6e | 148 | %% |