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:
88 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
89 FILE* aFile, const wxString& aFileName );
90
101 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
102 const std::string& aSExpression, const wxString& aSource = wxEmptyString );
103
112 DSNLEXER( const std::string& aSExpression, const wxString& aSource = wxEmptyString );
113
127 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
128 LINE_READER* aLineReader = nullptr );
129
130 virtual ~DSNLEXER();
131
137 void InitParserState();
138
149 bool SyncLineReaderWith( DSNLEXER& aLexer );
150
164 void SetSpecctraMode( bool aMode );
165
176 void PushReader( LINE_READER* aLineReader );
177
194
206 int NextTok();
207
214 int NeedSYMBOL();
215
223 int NeedSYMBOLorNUMBER();
224
231 int NeedNUMBER( const char* aExpectation );
232
236 int CurTok() const
237 {
238 return curTok;
239 }
240
244 int PrevTok() const
245 {
246 return prevTok;
247 }
248
253 {
254 return findToken( curText );
255 }
256
264 char SetStringDelimiter( char aStringDelimiter )
265 {
266 char old = stringDelimiter;
267
268 if( specctraMode )
269 stringDelimiter = aStringDelimiter;
270
271 return old;
272 }
273
279 bool SetSpaceInQuotedTokens( bool val )
280 {
281 bool old = space_in_quoted_tokens;
282
283 if( specctraMode )
285
286 return old;
287 }
288
289 void SetKnowsBar( bool knowsBar = true )
290 {
291 // This is used to control whether the lexer knows about the DSN_BAR token.
292 // If it does not, then it will not return a DSN_BAR token, but rather
293 // treat it as a string.
294 // This is used to support the KiCad legacy format, which does not have a DSN_BAR
295 m_knowsBar = knowsBar;
296 }
297
304 bool SetCommentsAreTokens( bool val )
305 {
306 bool old = commentsAreTokens;
307 commentsAreTokens = val;
308 return old;
309 }
310
323 wxArrayString* ReadCommentLines();
324
331 static bool IsSymbol( int aTok );
332
333 static bool IsNumber( int aTok );
334
341 void Expecting( int aTok ) const;
342
350 void Expecting( const char* aTokenList ) const;
351
359 void Unexpected( int aTok ) const;
360
367 void Unexpected( const char* aToken ) const;
368
377 void Duplicate( int aTok );
378
384 void NeedLEFT();
385
391 void NeedRIGHT();
392
398 void NeedBAR();
399
403 const char* GetTokenText( int aTok ) const;
404
408 wxString GetTokenString( int aTok ) const;
409
410 static const char* Syntax( int aTok );
411
415 const char* CurText() const
416 {
417 return curText.c_str();
418 }
419
423 const std::string& CurStr() const
424 {
425 return curText;
426 }
427
428 const std::string& CurSeparator() const
429 {
430 return curSeparator;
431 }
432
437 wxString FromUTF8() const
438 {
439 return wxString::FromUTF8( curText );
440 }
441
445 int CurLineNumber() const
446 {
447 return reader->LineNumber();
448 }
449
453 const char* CurLine() const
454 {
455 return (const char*)(*reader);
456 }
457
463 const wxString& CurSource() const
464 {
465 return reader->GetSource();
466 }
467
473 int CurOffset() const
474 {
475 return curOffset + 1;
476 }
477
478protected:
479 void init();
480
481 inline bool isSep( char cc );
482
484 {
485 if( reader )
486 {
487 reader->ReadLine();
488
489 unsigned len = reader->Length();
490
491 // start may have changed in ReadLine(), which can resize and
492 // relocate reader's line buffer.
493 start = reader->Line();
494
495 next = start;
496 limit = next + len;
497
498 return len;
499 }
500 return 0;
501 }
502
510 int findToken( const std::string& aToken ) const;
511
512 bool isStringTerminator( char cc ) const
513 {
514 if( !space_in_quoted_tokens && cc == ' ' )
515 return true;
516
517 if( cc == stringDelimiter )
518 return true;
519
520 return false;
521 }
522
530 double parseDouble();
531
532 double parseDouble( const char* aExpected )
533 {
534 NeedNUMBER( aExpected );
535 return parseDouble();
536 }
537
538 template <typename T>
539 inline double parseDouble( T aToken )
540 {
541 return parseDouble( GetTokenText( aToken ) );
542 }
543
544protected:
546 const char* start;
547 const char* next;
548 const char* limit;
549 char dummy[1];
550
551 typedef std::vector<LINE_READER*> READER_STACK;
552
554
557
565
568
570
573
574 int curTok;
575 std::string curText;
576 std::string curSeparator;
577
579 unsigned keywordCount;
581};
582
583#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:547
std::string curText
The text of the current token.
Definition dsnlexer.h:575
unsigned keywordCount
Count of keywords table.
Definition dsnlexer.h:579
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:551
bool SetSpaceInQuotedTokens(bool val)
Change the setting controlling whether a space in a quoted string isa terminator.
Definition dsnlexer.h:279
bool commentsAreTokens
True if should return comments as tokens.
Definition dsnlexer.h:569
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:415
const char * start
Definition dsnlexer.h:546
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:574
std::string curSeparator
The text of the separator preceeding the current text.
Definition dsnlexer.h:576
bool space_in_quoted_tokens
Blank spaces within quoted strings.
Definition dsnlexer.h:567
int PrevTok() const
Return whatever NextTok() returned the 2nd to last time it was called.
Definition dsnlexer.h:244
bool specctraMode
if true, then: 1) stringDelimiter can be changed 2) Kicad quoting protocol is not in effect 3) space_...
Definition dsnlexer.h:558
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:428
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:572
char stringDelimiter
Definition dsnlexer.h:566
LINE_READER * reader
No ownership. ownership is via readerStack, maybe, if iOwnReaders.
Definition dsnlexer.h:556
const char * limit
Definition dsnlexer.h:548
bool isStringTerminator(char cc) const
Definition dsnlexer.h:512
const KEYWORD * keywords
Table sorted by CMake for bsearch().
Definition dsnlexer.h:578
int readLine()
Definition dsnlexer.h:483
READER_STACK readerStack
all the LINE_READERs by pointer.
Definition dsnlexer.h:553
char dummy[1]
When there is no reader.
Definition dsnlexer.h:549
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:437
const wxString & CurSource() const
Return the current LINE_READER source.
Definition dsnlexer.h:463
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:423
void SetKnowsBar(bool knowsBar=true)
Definition dsnlexer.h:289
const char * CurLine() const
Return the current line of text from which the CurText() would return its token.
Definition dsnlexer.h:453
bool SetCommentsAreTokens(bool val)
Change the handling of comments.
Definition dsnlexer.h:304
double parseDouble(T aToken)
Definition dsnlexer.h:539
int GetCurStrAsToken() const
Used to support "loose" matches (quoted tokens).
Definition dsnlexer.h:252
int CurTok() const
Return whatever NextTok() returned the last time it was called.
Definition dsnlexer.h:236
double parseDouble(const char *aExpected)
Definition dsnlexer.h:532
int CurOffset() const
Return the byte offset within the current line, using a 1 based index.
Definition dsnlexer.h:473
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:264
int prevTok
curTok from previous NextTok() call.
Definition dsnlexer.h:571
const KEYWORD_MAP * keywordsLookup
Fast, specialized "C string" hashtable.
Definition dsnlexer.h:580
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:563
int CurLineNumber() const
Return the current line number within my LINE_READER.
Definition dsnlexer.h:445
bool iOwnReaders
On readerStack, should I delete them?
Definition dsnlexer.h:545
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:65
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