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 (C) 2007-2024 Kicad Developers, see change_log.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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#ifndef DSNLEXER_H_
26#define DSNLEXER_H_
27
28#include <kicommon.h>
29#include <cstdio>
30#include <hashtables.h>
31#include <string>
32#include <vector>
33
34#include <richio.h>
35
36#ifndef SWIG
41{
42 const char* name;
43 int token;
44};
45#endif // SWIG
46
47// something like this macro can be used to help initialize a KEYWORD table.
48// see SPECCTRA_DB::keywords[] as an example.
49
50//#define TOKDEF(x) { #x, T_##x }
51
52
59{
60 DSN_NONE = -11,
67 DSN_RIGHT = -4, // right bracket, ')'
68 DSN_LEFT = -3, // left bracket, '('
69 DSN_STRING = -2, // a quoted string, stripped of the quotes
70 DSN_EOF = -1 // special case for end of file
71};
72
73
80{
81public:
82
93 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
94 FILE* aFile, const wxString& aFileName );
95
105 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
106 const std::string& aSExpression, const wxString& aSource = wxEmptyString );
107
116 DSNLEXER( const std::string& aSExpression, const wxString& aSource = wxEmptyString );
117
130 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
131 LINE_READER* aLineReader = nullptr );
132
133 virtual ~DSNLEXER();
134
140 void InitParserState();
141
151 bool SyncLineReaderWith( DSNLEXER& aLexer );
152
166 void SetSpecctraMode( bool aMode );
167
178 void PushReader( LINE_READER* aLineReader );
179
195 LINE_READER* PopReader();
196
208 int NextTok();
209
216 int NeedSYMBOL();
217
225 int NeedSYMBOLorNUMBER();
226
233 int NeedNUMBER( const char* aExpectation );
234
238 int CurTok() const
239 {
240 return curTok;
241 }
242
246 int PrevTok() const
247 {
248 return prevTok;
249 }
250
255 {
256 return findToken( curText );
257 }
258
266 char SetStringDelimiter( char aStringDelimiter )
267 {
268 char old = stringDelimiter;
269
270 if( specctraMode )
271 stringDelimiter = aStringDelimiter;
272
273 return old;
274 }
275
281 bool SetSpaceInQuotedTokens( bool val )
282 {
283 bool old = space_in_quoted_tokens;
284
285 if( specctraMode )
286 space_in_quoted_tokens = val;
287
288 return old;
289 }
290
297 bool SetCommentsAreTokens( bool val )
298 {
299 bool old = commentsAreTokens;
300 commentsAreTokens = val;
301 return old;
302 }
303
316 wxArrayString* ReadCommentLines();
317
324 static bool IsSymbol( int aTok );
325
332 void Expecting( int aTok ) const;
333
341 void Expecting( const char* aTokenList ) const;
342
350 void Unexpected( int aTok ) const;
351
358 void Unexpected( const char* aToken ) const;
359
368 void Duplicate( int aTok );
369
375 void NeedLEFT();
376
382 void NeedRIGHT();
383
387 const char* GetTokenText( int aTok ) const;
388
392 wxString GetTokenString( int aTok ) const;
393
394 static const char* Syntax( int aTok );
395
399 const char* CurText() const
400 {
401 return curText.c_str();
402 }
403
407 const std::string& CurStr() const
408 {
409 return curText;
410 }
411
416 wxString FromUTF8() const
417 {
418 return wxString::FromUTF8( curText.c_str() );
419 }
420
424 int CurLineNumber() const
425 {
426 return reader->LineNumber();
427 }
428
432 const char* CurLine() const
433 {
434 return (const char*)(*reader);
435 }
436
442 const wxString& CurSource() const
443 {
444 return reader->GetSource();
445 }
446
452 int CurOffset() const
453 {
454 return curOffset + 1;
455 }
456
457#ifndef SWIG
458
459protected:
460 void init();
461
463 {
464 if( reader )
465 {
466 reader->ReadLine();
467
468 unsigned len = reader->Length();
469
470 // start may have changed in ReadLine(), which can resize and
471 // relocate reader's line buffer.
472 start = reader->Line();
473
474 next = start;
475 limit = next + len;
476
477 return len;
478 }
479 return 0;
480 }
481
489 int findToken( const std::string& aToken ) const;
490
491 bool isStringTerminator( char cc ) const
492 {
493 if( !space_in_quoted_tokens && cc == ' ' )
494 return true;
495
496 if( cc == stringDelimiter )
497 return true;
498
499 return false;
500 }
501
509 double parseDouble();
510
511 double parseDouble( const char* aExpected )
512 {
513 NeedNUMBER( aExpected );
514 return parseDouble();
515 }
516
517 template <typename T>
518 inline double parseDouble( T aToken )
519 {
520 return parseDouble( GetTokenText( aToken ) );
521 }
522
524 const char* start;
525 const char* next;
526 const char* limit;
527 char dummy[1];
528
529 typedef std::vector<LINE_READER*> READER_STACK;
530
532
535
541
544
546
549
550 int curTok;
551 std::string curText;
552
554 unsigned keywordCount;
556#endif // SWIG
557};
558
559#endif // DSNLEXER_H_
Implement a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:80
const char * next
Definition: dsnlexer.h:525
std::string curText
the text of the current token
Definition: dsnlexer.h:551
unsigned keywordCount
count of keywords table
Definition: dsnlexer.h:554
std::vector< LINE_READER * > READER_STACK
Definition: dsnlexer.h:529
bool SetSpaceInQuotedTokens(bool val)
Change the setting controlling whether a space in a quoted string isa terminator.
Definition: dsnlexer.h:281
bool commentsAreTokens
true if should return comments as tokens
Definition: dsnlexer.h:545
const char * CurText() const
Return a pointer to the current token's text.
Definition: dsnlexer.h:399
const char * start
Definition: dsnlexer.h:524
int curTok
the current token obtained on last NextTok()
Definition: dsnlexer.h:550
bool space_in_quoted_tokens
blank spaces within quoted strings
Definition: dsnlexer.h:543
int PrevTok() const
Return whatever NextTok() returned the 2nd to last time it was called.
Definition: dsnlexer.h:246
bool specctraMode
if true, then: 1) stringDelimiter can be changed 2) Kicad quoting protocol is not in effect 3) space_...
Definition: dsnlexer.h:536
int curOffset
offset within current line of the current token
Definition: dsnlexer.h:548
char stringDelimiter
Definition: dsnlexer.h:542
LINE_READER * reader
Definition: dsnlexer.h:534
const char * limit
Definition: dsnlexer.h:526
bool isStringTerminator(char cc) const
Definition: dsnlexer.h:491
const KEYWORD * keywords
table sorted by CMake for bsearch()
Definition: dsnlexer.h:553
int readLine()
Definition: dsnlexer.h:462
READER_STACK readerStack
all the LINE_READERs by pointer.
Definition: dsnlexer.h:531
wxString FromUTF8() const
Return the current token text as a wxString, assuming that the input byte stream is UTF8 encoded.
Definition: dsnlexer.h:416
const wxString & CurSource() const
Return the current LINE_READER source.
Definition: dsnlexer.h:442
const std::string & CurStr() const
Return a reference to current token in std::string form.
Definition: dsnlexer.h:407
const char * CurLine() const
Return the current line of text from which the CurText() would return its token.
Definition: dsnlexer.h:432
bool SetCommentsAreTokens(bool val)
Change the handling of comments.
Definition: dsnlexer.h:297
double parseDouble(T aToken)
Definition: dsnlexer.h:518
int GetCurStrAsToken() const
Used to support "loose" matches (quoted tokens).
Definition: dsnlexer.h:254
int CurTok() const
Return whatever NextTok() returned the last time it was called.
Definition: dsnlexer.h:238
double parseDouble(const char *aExpected)
Definition: dsnlexer.h:511
int CurOffset() const
Return the byte offset within the current line, using a 1 based index.
Definition: dsnlexer.h:452
char SetStringDelimiter(char aStringDelimiter)
Change the string delimiter from the default " to some other character and return the old value.
Definition: dsnlexer.h:266
int prevTok
curTok from previous NextTok() call.
Definition: dsnlexer.h:547
const KEYWORD_MAP * keywordsLookup
fast, specialized "C string" hashtable
Definition: dsnlexer.h:555
int CurLineNumber() const
Return the current line number within my LINE_READER.
Definition: dsnlexer.h:424
bool iOwnReaders
on readerStack, should I delete them?
Definition: dsnlexer.h:523
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:93
DSN_SYNTAX_T
List all the DSN lexer's tokens that are supported in lexing.
Definition: dsnlexer.h:59
@ DSN_QUOTE_DEF
Definition: dsnlexer.h:63
@ DSN_STRING_QUOTE
Definition: dsnlexer.h:62
@ DSN_LEFT
Definition: dsnlexer.h:68
@ DSN_RIGHT
Definition: dsnlexer.h:67
@ DSN_NUMBER
Definition: dsnlexer.h:66
@ DSN_NONE
Definition: dsnlexer.h:60
@ DSN_DASH
Definition: dsnlexer.h:64
@ DSN_SYMBOL
Definition: dsnlexer.h:65
@ DSN_COMMENT
Definition: dsnlexer.h:61
@ DSN_STRING
Definition: dsnlexer.h:69
@ DSN_EOF
Definition: dsnlexer.h:70
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:95
#define KICOMMON_API
Definition: kicommon.h:28
CITER next(CITER it)
Definition: ptree.cpp:126
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 ...
std::vector< FAB_LAYER_COLOR > dummy
Hold a keyword string and its unique integer token.
Definition: dsnlexer.h:41
int token
a zero based index into an array of KEYWORDs
Definition: dsnlexer.h:43
const char * name
unique keyword.
Definition: dsnlexer.h:42