Merge pull request #1949 from YosysHQ/eddie/select_blackbox
[yosys.git] / frontends / ilang / ilang_lexer.l
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * ---
19 *
20 * A very simple and straightforward frontend for the RTLIL text
21 * representation (as generated by the 'ilang' backend).
22 *
23 */
24
25 %{
26
27 #ifdef __clang__
28 // bison generates code using the 'register' storage class specifier
29 #pragma clang diagnostic ignored "-Wdeprecated-register"
30 #endif
31
32 #include <cstdlib>
33 #include "frontends/ilang/ilang_frontend.h"
34 #include "ilang_parser.tab.hh"
35
36 USING_YOSYS_NAMESPACE
37
38 #define YY_INPUT(buf,result,max_size) \
39 result = readsome(*ILANG_FRONTEND::lexin, buf, max_size)
40
41 %}
42
43 %option yylineno
44 %option noyywrap
45 %option nounput
46 %option prefix="rtlil_frontend_ilang_yy"
47
48 %x STRING
49
50 %%
51
52 "autoidx" { return TOK_AUTOIDX; }
53 "module" { return TOK_MODULE; }
54 "attribute" { return TOK_ATTRIBUTE; }
55 "parameter" { return TOK_PARAMETER; }
56 "signed" { return TOK_SIGNED; }
57 "real" { return TOK_REAL; }
58 "wire" { return TOK_WIRE; }
59 "memory" { return TOK_MEMORY; }
60 "width" { return TOK_WIDTH; }
61 "upto" { return TOK_UPTO; }
62 "offset" { return TOK_OFFSET; }
63 "size" { return TOK_SIZE; }
64 "input" { return TOK_INPUT; }
65 "output" { return TOK_OUTPUT; }
66 "inout" { return TOK_INOUT; }
67 "cell" { return TOK_CELL; }
68 "connect" { return TOK_CONNECT; }
69 "switch" { return TOK_SWITCH; }
70 "case" { return TOK_CASE; }
71 "assign" { return TOK_ASSIGN; }
72 "sync" { return TOK_SYNC; }
73 "low" { return TOK_LOW; }
74 "high" { return TOK_HIGH; }
75 "posedge" { return TOK_POSEDGE; }
76 "negedge" { return TOK_NEGEDGE; }
77 "edge" { return TOK_EDGE; }
78 "always" { return TOK_ALWAYS; }
79 "global" { return TOK_GLOBAL; }
80 "init" { return TOK_INIT; }
81 "update" { return TOK_UPDATE; }
82 "process" { return TOK_PROCESS; }
83 "end" { return TOK_END; }
84
85 [a-z]+ { return TOK_INVALID; }
86
87 "\\"[^ \t\r\n]+ { rtlil_frontend_ilang_yylval.string = strdup(yytext); return TOK_ID; }
88 "$"[^ \t\r\n]+ { rtlil_frontend_ilang_yylval.string = strdup(yytext); return TOK_ID; }
89 "."[0-9]+ { rtlil_frontend_ilang_yylval.string = strdup(yytext); return TOK_ID; }
90
91 [0-9]+'[01xzm-]* { rtlil_frontend_ilang_yylval.string = strdup(yytext); return TOK_VALUE; }
92 -?[0-9]+ {
93 char *end = nullptr;
94 long value = strtol(yytext, &end, 10);
95 if (end != yytext + strlen(yytext))
96 return TOK_INVALID; // literal out of range of long
97 if (value < INT_MIN || value > INT_MAX)
98 return TOK_INVALID; // literal out of range of int (relevant mostly for LP64 platforms)
99 rtlil_frontend_ilang_yylval.integer = value;
100 return TOK_INT;
101 }
102
103 \" { BEGIN(STRING); }
104 <STRING>\\. { yymore(); }
105 <STRING>\" {
106 BEGIN(0);
107 char *yystr = strdup(yytext);
108 yystr[strlen(yytext) - 1] = 0;
109 int i = 0, j = 0;
110 while (yystr[i]) {
111 if (yystr[i] == '\\' && yystr[i + 1]) {
112 i++;
113 if (yystr[i] == 'n')
114 yystr[i] = '\n';
115 else if (yystr[i] == 't')
116 yystr[i] = '\t';
117 else if ('0' <= yystr[i] && yystr[i] <= '7') {
118 yystr[i] = yystr[i] - '0';
119 if ('0' <= yystr[i + 1] && yystr[i + 1] <= '7') {
120 yystr[i + 1] = yystr[i] * 8 + yystr[i + 1] - '0';
121 i++;
122 }
123 if ('0' <= yystr[i + 1] && yystr[i + 1] <= '7') {
124 yystr[i + 1] = yystr[i] * 8 + yystr[i + 1] - '0';
125 i++;
126 }
127 }
128 }
129 yystr[j++] = yystr[i++];
130 }
131 yystr[j] = 0;
132 rtlil_frontend_ilang_yylval.string = yystr;
133 return TOK_STRING;
134 }
135 <STRING>. { yymore(); }
136
137 "#"[^\n]* /* ignore comments */
138 [ \t] /* ignore non-newline whitespaces */
139 [\r\n]+ { return TOK_EOL; }
140
141 . { return *yytext; }
142
143 %%
144
145 // this is a hack to avoid the 'yyinput defined but not used' error msgs
146 void *rtlil_frontend_ilang_avoid_input_warnings() {
147 return (void*)&yyinput;
148 }