KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dsnlexer.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (C) 2007-2010 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef DSNLEXER_H_
22#define DSNLEXER_H_
23
24#include <kicommon.h>
25#include <cstdio>
26#include <hashtables.h>
27#include <string>
28#include <vector>
29
30#include <richio.h>
31
36{
37 const char* name;
38 int token;
39};
40
41// something like this macro can be used to help initialize a KEYWORD table.
42// see SPECCTRA_DB::keywords[] as an example.
43
44//#define TOKDEF(x) { #x, T_##x }
45
46
53{
54 DSN_NONE = -12,
55 DSN_BAR = -11, // Also called pipe '|'
62 DSN_RIGHT = -4, // right bracket, ')'
63 DSN_LEFT = -3, // left bracket, '('
64 DSN_STRING = -2, // a quoted string, stripped of the quotes
65 DSN_EOF = -1 // special case for end of file
66};
67
68
75{
76public:
87 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
88 FILE* aFile, const wxString& aFileName );
89
99 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
100 const std::string& aSExpression, const wxString& aSource = wxEmptyString );
101
110 DSNLEXER( const std::string& aSExpression, const wxString& aSource = wxEmptyString );
111
124 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
125 LINE_READER* aLineReader = nullptr );
126
127 virtual ~DSNLEXER();
128
134 void InitParserState();
135
145 bool SyncLineReaderWith( DSNLEXER& aLexer );
146
160 void SetSpecctraMode( bool aMode );
161
172 void PushReader( LINE_READER* aLineReader );
173
190
202 int NextTok();
203
210 int NeedSYMBOL();
211
219 int NeedSYMBOLorNUMBER();
220
227 int NeedNUMBER( const char* aExpectation );
228
232 int CurTok() const
233 {
234 return curTok;
235 }
236
240 int PrevTok() const
241 {
242 return prevTok;
243 }
244
249 {
250 return findToken( curText );
251 }
252
260 char SetStringDelimiter( char aStringDelimiter )
261 {
262 char old = stringDelimiter;
263
264 if( specctraMode )
265 stringDelimiter = aStringDelimiter;
266
267 return old;
268 }
269
275 bool SetSpaceInQuotedTokens( bool val )
276 {
277 bool old = space_in_quoted_tokens;
278
279 if( specctraMode )
281
282 return old;
283 }
284
285 void SetKnowsBar( bool knowsBar = true )
286 {
287 // This is used to control whether the lexer knows about the DSN_BAR token.
288 // If it does not, then it will not return a DSN_BAR token, but rather
289 // treat it as a string.
290 // This is used to support the KiCad legacy format, which does not have a DSN_BAR
291 m_knowsBar = knowsBar;
292 }
293
300 bool SetCommentsAreTokens( bool val )
301 {
302 bool old = commentsAreTokens;
303 commentsAreTokens = val;
304 return old;
305 }
306
319 wxArrayString* ReadCommentLines();
320
327 static bool IsSymbol( int aTok );
328
329 static bool IsNumber( int aTok );
330
337 void Expecting( int aTok ) const;
338
346 void Expecting( const char* aTokenList ) const;
347
355 void Unexpected( int aTok ) const;
356
363 void Unexpected( const char* aToken ) const;
364
373 void Duplicate( int aTok );
374
380 void NeedLEFT();
381
387 void NeedRIGHT();
388
394 void NeedBAR();
395
399 const char* GetTokenText( int aTok ) const;
400
404 wxString GetTokenString( int aTok ) const;
405
406 static const char* Syntax( int aTok );
407
411 const char* CurText() const
412 {
413 return curText.c_str();
414 }
415
419 const std::string& CurStr() const
420 {
421 return curText;
422 }
423
424 const std::string& CurSeparator() const
425 {
426 return curSeparator;
427 }
428
433 wxString FromUTF8() const
434 {
435 return wxString::FromUTF8( curText );
436 }
437
441 int CurLineNumber() const
442 {
443 return reader->LineNumber();
444 }
445
449 const char* CurLine() const
450 {
451 return (const char*)(*reader);
452 }
453
459 const wxString& CurSource() const
460 {
461 return reader->GetSource();
462 }
463
469 int CurOffset() const
470 {
471 return curOffset + 1;
472 }
473
474protected:
475 void init();
476
477 inline bool isSep( char cc );
478
480 {
481 if( reader )
482 {
483 reader->ReadLine();
484
485 unsigned len = reader->Length();
486
487 // start may have changed in ReadLine(), which can resize and
488 // relocate reader's line buffer.
489 start = reader->Line();
490
491 next = start;
492 limit = next + len;
493
494 return len;
495 }
496 return 0;
497 }
498
506 int findToken( const std::string& aToken ) const;
507
508 bool isStringTerminator( char cc ) const
509 {
510 if( !space_in_quoted_tokens && cc == ' ' )
511 return true;
512
513 if( cc == stringDelimiter )
514 return true;
515
516 return false;
517 }
518
526 double parseDouble();
527
528 double parseDouble( const char* aExpected )
529 {
530 NeedNUMBER( aExpected );
531 return parseDouble();
532 }
533
534 template <typename T>
535 inline double parseDouble( T aToken )
536 {
537 return parseDouble( GetTokenText( aToken ) );
538 }
539
540protected:
542 const char* start;
543 const char* next;
544 const char* limit;
545 char dummy[1];
546
547 typedef std::vector<LINE_READER*> READER_STACK;
548
550
553
561
564
566
569
570 int curTok;
571 std::string curText;
572 std::string curSeparator;
573
575 unsigned keywordCount;
577};
578
579#endif // DSNLEXER_H_
int NeedSYMBOLorNUMBER()
Call NextTok() and then verifies that the token read in satisfies bool IsSymbol() or the next token i...
Definition dsnlexer.cpp:412
const char * next
Definition dsnlexer.h:543
std::string curText
The text of the current token.
Definition dsnlexer.h:571
unsigned keywordCount
Count of keywords table.
Definition dsnlexer.h:575
int NeedNUMBER(const char *aExpectation)
Call NextTok() and then verifies that the token read is type DSN_NUMBER.
Definition dsnlexer.cpp:423
std::vector< LINE_READER * > READER_STACK
Definition dsnlexer.h:547
bool SetSpaceInQuotedTokens(bool val)
Change the setting controlling whether a space in a quoted string isa terminator.
Definition dsnlexer.h:275
bool commentsAreTokens
True if should return comments as tokens.
Definition dsnlexer.h:565
int findToken(const std::string &aToken) const
Take aToken string and looks up the string in the keywords table.
Definition dsnlexer.cpp:237
const char * CurText() const
Return a pointer to the current token's text.
Definition dsnlexer.h:411
const char * start
Definition dsnlexer.h:542
const char * GetTokenText(int aTok) const
Return the C string representation of a DSN_T value.
Definition dsnlexer.cpp:298
int curTok
The current token obtained on last NextTok().
Definition dsnlexer.h:570
std::string curSeparator
The text of the separator preceeding the current text.
Definition dsnlexer.h:572
bool space_in_quoted_tokens
Blank spaces within quoted strings.
Definition dsnlexer.h:563
int PrevTok() const
Return whatever NextTok() returned the 2nd to last time it was called.
Definition dsnlexer.h:240
bool specctraMode
if true, then: 1) stringDelimiter can be changed 2) Kicad quoting protocol is not in effect 3) space_...
Definition dsnlexer.h:554
int NextTok()
Return the next token found in the input file or DSN_EOF when reaching the end of file.
Definition dsnlexer.cpp:537
const std::string & CurSeparator() const
Definition dsnlexer.h:424
int NeedSYMBOL()
Call NextTok() and then verifies that the token read in satisfies IsSymbol().
Definition dsnlexer.cpp:401
int curOffset
Offset within current line of the current token.
Definition dsnlexer.h:568
char stringDelimiter
Definition dsnlexer.h:562
LINE_READER * reader
No ownership. ownership is via readerStack, maybe, if iOwnReaders.
Definition dsnlexer.h:552
const char * limit
Definition dsnlexer.h:544
bool isStringTerminator(char cc) const
Definition dsnlexer.h:508
const KEYWORD * keywords
Table sorted by CMake for bsearch().
Definition dsnlexer.h:574
int readLine()
Definition dsnlexer.h:479
READER_STACK readerStack
all the LINE_READERs by pointer.
Definition dsnlexer.h:549
char dummy[1]
When there is no reader.
Definition dsnlexer.h:545
void SetSpecctraMode(bool aMode)
Change the behavior of this lexer into or out of "specctra mode".
Definition dsnlexer.cpp:146
wxString FromUTF8() const
Return the current token text as a wxString, assuming that the input byte stream is UTF8 encoded.
Definition dsnlexer.h:433
const wxString & CurSource() const
Return the current LINE_READER source.
Definition dsnlexer.h:459
LINE_READER * PopReader()
Delete the top most LINE_READER from an internal stack of LINE_READERs and in the case of FILE_LINE_R...
Definition dsnlexer.cpp:208
const std::string & CurStr() const
Return a reference to current token in std::string form.
Definition dsnlexer.h:419
void SetKnowsBar(bool knowsBar=true)
Definition dsnlexer.h:285
const char * CurLine() const
Return the current line of text from which the CurText() would return its token.
Definition dsnlexer.h:449
bool SetCommentsAreTokens(bool val)
Change the handling of comments.
Definition dsnlexer.h:300
double parseDouble(T aToken)
Definition dsnlexer.h:535
int GetCurStrAsToken() const
Used to support "loose" matches (quoted tokens).
Definition dsnlexer.h:248
int CurTok() const
Return whatever NextTok() returned the last time it was called.
Definition dsnlexer.h:232
double parseDouble(const char *aExpected)
Definition dsnlexer.h:528
int CurOffset() const
Return the byte offset within the current line, using a 1 based index.
Definition dsnlexer.h:469
void InitParserState()
Reinit variables used during parsing, to ensure od states are not used in a new parsing must be calle...
Definition dsnlexer.cpp:163
char SetStringDelimiter(char aStringDelimiter)
Change the string delimiter from the default " to some other character and return the old value.
Definition dsnlexer.h:260
int prevTok
curTok from previous NextTok() call.
Definition dsnlexer.h:567
const KEYWORD_MAP * keywordsLookup
Fast, specialized "C string" hashtable.
Definition dsnlexer.h:576
DSNLEXER(const KEYWORD *aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP *aKeywordMap, FILE *aFile, const wxString &aFileName)
Initialize a DSN lexer and prepares to read from aFile which is already open and has aFilename.
Definition dsnlexer.cpp:50
bool SyncLineReaderWith(DSNLEXER &aLexer)
Usable only for DSN lexers which share the same LINE_READER.
Definition dsnlexer.cpp:173
bool m_knowsBar
True if the lexer knows about the bar token.
Definition dsnlexer.h:559
int CurLineNumber() const
Return the current line number within my LINE_READER.
Definition dsnlexer.h:441
bool iOwnReaders
On readerStack, should I delete them?
Definition dsnlexer.h:541
void PushReader(LINE_READER *aLineReader)
Manage a stack of LINE_READERs in order to handle nested file inclusion.
Definition dsnlexer.cpp:196
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition richio.h:62
DSN_SYNTAX_T
List all the DSN lexer's tokens that are supported in lexing.
Definition dsnlexer.h:53
@ DSN_QUOTE_DEF
Definition dsnlexer.h:58
@ DSN_STRING_QUOTE
Definition dsnlexer.h:57
@ DSN_LEFT
Definition dsnlexer.h:63
@ DSN_RIGHT
Definition dsnlexer.h:62
@ DSN_NUMBER
Definition dsnlexer.h:61
@ DSN_NONE
Definition dsnlexer.h:54
@ DSN_BAR
Definition dsnlexer.h:55
@ DSN_DASH
Definition dsnlexer.h:59
@ DSN_SYMBOL
Definition dsnlexer.h:60
@ DSN_COMMENT
Definition dsnlexer.h:56
@ DSN_STRING
Definition dsnlexer.h:64
@ DSN_EOF
Definition dsnlexer.h:65
std::unordered_map< const char *, int, fnv_1a, iequal_to > KEYWORD_MAP
A hashtable made of a const char* and an int.
Definition hashtables.h:93
#define KICOMMON_API
Definition kicommon.h:27
static bool IsNumber(char x)
double parseDouble(LINE_READER &aReader, const char *aLine, const char **aOutput)
Parses an ASCII point string with possible leading whitespace into a double precision floating point ...
Hold a keyword string and its unique integer token.
Definition dsnlexer.h:36
int token
a zero based index into an array of KEYWORDs
Definition dsnlexer.h:38
const char * name
unique keyword.
Definition dsnlexer.h:37