1
2%{ /* -*- C++ -*- */
3/*
4 * cdlScanner.yy
5 *
6 * Copyright 2014-2018 D. Mitch Bailey  cvc at shuharisystem dot com
7 *
8 * This file is part of cvc.
9 *
10 * cvc is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * cvc is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with cvc.  If not, see <http://www.gnu.org/licenses/>.
22 *
23 * You can download cvc from https://github.com/d-m-bailey/cvc.git
24*/
25#include "Cvc.hh"
26
27# include "CCdlParserDriver.hh"
28#include "cdlParser.hh"
29
30
31// Work around an incompatibility in flex (at least versions
32// 2.5.31 through 2.5.33): it generates code that does
33// not conform to C89.  See Debian bug 333231
34// <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.
35# undef yywrap
36# define yywrap() 1
37
38// The location of the current token.
39static yy::location scanner_location;
40
41int cdlLineCount = 0;
42int cdlWarningCount = 0;
43
44typedef yy::CCdlParser::token token;
45
46#define YY_USER_ACTION yylloc->columns(yyleng);
47
48%}
49
50%s READING
51
52TEXT			[[:alnum:][:punct:]]
53
54/* %option noyywrap batch debug noinput */
55%option noyywrap batch noinput
56/* bison-bridge bison-locations */
57
58
59%%
60
61%{
62	yylloc->step();
63%}
64
65<INITIAL>^\.[Ss][Uu][Bb][Cc][Kk][Tt]/[[:space:]]	{
66		BEGIN(READING);
67#ifdef CDL_FLEX_DEBUG
68		cout << "FLEX DEBUG: found SUBCKT\n" << endl;
69#endif
70		return( token::SUBCKT );
71	};
72
73<INITIAL>^\.[Ee][Nn][Dd][Ss]/[[:space:]] {
74		BEGIN(READING);
75#ifdef CDL_FLEX_DEBUG
76		cout << "FLEX DEBUG: found ENDS\n" << endl;
77#endif
78		return( token::ENDS );
79	};
80
81<INITIAL>^[Xx]{TEXT}* {
82		BEGIN(READING);
83		yylval->charPtr = cdlCircuitList.cdlText.SetTextAddress(yytext);
84#ifdef CDL_FLEX_DEBUG
85		cout << "FLEX DEBUG: found subcircuit " << *(yylval->charPtr) << "\n" << endl;
86#endif
87		return( token::SUBCIRCUIT );
88	};
89
90<INITIAL>^[Mm]{TEXT}* {
91		BEGIN(READING);
92		yylval->charPtr = cdlCircuitList.cdlText.SetTextAddress(yytext);
93#ifdef CDL_FLEX_DEBUG
94		cout << "FLEX DEBUG: found mosfet " << *(yylval->charPtr) << "\n" << endl;
95#endif
96		return( token::MOSFET );
97	};
98
99<INITIAL>^[Dd]{TEXT}* {
100		BEGIN(READING);
101		yylval->charPtr = cdlCircuitList.cdlText.SetTextAddress(yytext);
102#ifdef CDL_FLEX_DEBUG
103		cout << "FLEX DEBUG: found diode " << *(yylval->charPtr) << "\n" << endl;
104#endif
105		return( token::DIODE );
106	};
107
108<INITIAL>^[Cc]{TEXT}* {
109		BEGIN(READING);
110		yylval->charPtr = cdlCircuitList.cdlText.SetTextAddress(yytext);
111#ifdef CDL_FLEX_DEBUG
112		cout << "FLEX DEBUG: found capacitor " << *(yylval->charPtr) << "\n" << endl;
113#endif
114		return( token::CAPACITOR );
115	};
116
117<INITIAL>^[Rr]{TEXT}* {
118		BEGIN(READING);
119		yylval->charPtr = cdlCircuitList.cdlText.SetTextAddress(yytext);
120#ifdef CDL_FLEX_DEBUG
121		cout << "FLEX DEBUG: found resistor " << *(yylval->charPtr) << "\n" << endl;
122#endif
123		return( token::RESISTOR );
124	};
125
126<INITIAL>^[Qq]{TEXT}* {
127		BEGIN(READING);
128		yylval->charPtr = cdlCircuitList.cdlText.SetTextAddress(yytext);
129#ifdef CDL_FLEX_DEBUG
130		cout << "FLEX DEBUG: found bipolar " << *(yylval->charPtr) << "\n" << endl;
131#endif
132		return( token::BIPOLAR );
133	};
134
135<INITIAL>^[Ll]{TEXT}*	{
136		BEGIN(READING);
137		yylval->charPtr = cdlCircuitList.cdlText.SetTextAddress(yytext);
138#ifdef CDL_FLEX_DEBUG
139		cout << "FLEX DEBUG: found inductor " << *(yylval->charPtr) << "\n" << endl;
140#endif
141		return( token::INDUCTOR );
142	};
143
144<INITIAL>^\+	{
145		/* continuation */
146//		cdlLineCount++;
147		fprintf(stderr, "** ERROR: Unexpected continuation at line %d **\n", yylloc->begin.line);
148		cdlCircuitList.errorCount++;
149#ifdef CDL_FLEX_DEBUG
150		cout << "FLEX DEBUG: found unexpected continuation\n" << endl;
151#endif
152	};
153
154<*>^[[:blank:]]+/{TEXT}	{
155		fprintf(stderr, "** ERROR: Unexpected leading blanks at line %d **\n", yylloc->begin.line);
156		cdlCircuitList.errorCount++;
157#ifdef CDL_FLEX_DEBUG
158		cout << "FLEX DEBUG: found unexpected leading blank\n" << endl;
159#endif
160	};
161
162<INITIAL>{TEXT}+	{
163		fprintf(stderr, "** ERROR: Unexpected text %s at line %d column %d **\n", yytext, yylloc->begin.line, yylloc->begin.column);
164		cdlCircuitList.errorCount++;
165#ifdef CDL_FLEX_DEBUG
166		cout << "FLEX DEBUG: found unexpected text " << yytext << "\n" << endl;
167#endif
168	};
169
170<READING>{TEXT}+	{
171		yylval->charPtr = cdlCircuitList.cdlText.SetTextAddress(yytext);
172#ifdef CDL_FLEX_DEBUG
173		cout << "FLEX DEBUG: found string " << *(yylval->charPtr) << "\n" << endl;
174#endif
175		return( token::STRING );
176	};
177
178<*>^\*.*	{
179		/* comment */
180#ifdef CDL_FLEX_DEBUG
181		cout << "FLEX DEBUG: found comment\n" << endl;
182#endif
183	};
184
185<*>�O[[:blank:]]*$	{
186		/* blank line */
187#ifdef CDL_FLEX_DEBUG
188		cout << "FLEX DEBUG: found blank line\n" << endl;
189#endif
190	};
191
192<INITIAL>\n	{
193		cdlLineCount++;
194		yylloc->lines(1); yylloc->step();
195#ifdef CDL_FLEX_DEBUG
196		cout << "FLEX DEBUG: found EOL outside subcircuit\n" << endl;
197#endif
198	};
199
200<READING>\n\+	{
201		/* continuation */
202		cdlLineCount++;
203		yylloc->lines(1); yylloc->step();
204#ifdef CDL_FLEX_DEBUG
205		cout << "FLEX DEBUG: found continuation\n" << endl;
206#endif
207	};
208
209<READING>\n/\*	{
210		/* skip comments */
211		cdlLineCount++;
212		yylloc->lines(1); yylloc->step();
213#ifdef CDL_FLEX_DEBUG
214		cout << "FLEX DEBUG: found comment in continuation\n" << endl;
215#endif
216	};
217
218<READING>\n/[[:space:]]	{
219		/* skip blank lines */
220		cdlLineCount++;
221		yylloc->lines(1); yylloc->step();
222#ifdef CDL_FLEX_DEBUG
223		cout << "FLEX DEBUG: found possible blank line in continuation\n" << endl;
224#endif
225	};
226
227<READING>\n	{
228		cdlLineCount++;
229		yylloc->lines(1); yylloc->step();
230		BEGIN(INITIAL);
231#ifdef CDL_FLEX_DEBUG
232		cout << "FLEX DEBUG: found EOL\n" << endl;
233#endif
234		return( token::EOL );
235	};
236
237<*><<EOF>>	{
238#ifdef CDL_FLEX_DEBUG
239		cout << "FLEX DEBUG: found EOF\n" << endl;
240#endif
241		if ( cdlCircuitList.errorCount > 0 ) {
242			fprintf(stderr, "** ERROR: Unexpected character or syntax in CDL file %s**\n", driver.filename.c_str());
243//			exit(1);
244		}
245		return( token::CDL_EOF );
246	};
247
248[[:space:]] {
249		/* no token */
250		yylloc->step();
251	};
252
253. {
254		cdlCircuitList.errorCount++;
255//		fprintf(stderr, "** ERROR: unexpected character '%s' at line %d\n", yytext, cdlLineCount);
256		fprintf(stderr, "** ERROR: unexpected character '%s' at line %d column %d\n", yytext, yylloc->begin.line, yylloc->begin.column);
257	};
258
259%%
260