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 = -12,
61 DSN_BAR = -11, // Also called pipe '|'
68 DSN_RIGHT = -4, // right bracket, ')'
69 DSN_LEFT = -3, // left bracket, '('
70 DSN_STRING = -2, // a quoted string, stripped of the quotes
71 DSN_EOF = -1 // special case for end of file
72};
73
74
81{
82public:
83
94 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
95 FILE* aFile, const wxString& aFileName );
96
106 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
107 const std::string& aSExpression, const wxString& aSource = wxEmptyString );
108
117 DSNLEXER( const std::string& aSExpression, const wxString& aSource = wxEmptyString );
118
131 DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
132 LINE_READER* aLineReader = nullptr );
133
134 virtual ~DSNLEXER();
135
141 void InitParserState();
142
152 bool SyncLineReaderWith( DSNLEXER& aLexer );
153
167 void SetSpecctraMode( bool aMode );
168
179 void PushReader( LINE_READER* aLineReader );
180
196 LINE_READER* PopReader();
197
209 int NextTok();
210
217 int NeedSYMBOL();
218
226 int NeedSYMBOLorNUMBER();
227
234 int NeedNUMBER( const char* aExpectation );
235
239 int CurTok() const
240 {
241 return curTok;
242 }
243
247 int PrevTok() const
248 {
249 return prevTok;
250 }
251
256 {
257 return findToken( curText );
258 }
259
267 char SetStringDelimiter( char aStringDelimiter )
268 {
269 char old = stringDelimiter;
270
271 if( specctraMode )
272 stringDelimiter = aStringDelimiter;
273
274 return old;
275 }
276
282 bool SetSpaceInQuotedTokens( bool val )
283 {
284 bool old = space_in_quoted_tokens;
285
286 if( specctraMode )
287 space_in_quoted_tokens = val;
288
289 return old;
290 }
291
298 bool SetCommentsAreTokens( bool val )
299 {
300 bool old = commentsAreTokens;
301 commentsAreTokens = val;
302 return old;
303 }
304
317 wxArrayString* ReadCommentLines();
318
325 static bool IsSymbol( int aTok );
326
333 void Expecting( int aTok ) const;
334
342 void Expecting( const char* aTokenList ) const;
343
351 void Unexpected( int aTok ) const;
352
359 void Unexpected( const char* aToken ) const;
360
369 void Duplicate( int aTok );
370
376 void NeedLEFT();
377
383 void NeedRIGHT();
384
390 void NeedBAR();
391
395 const char* GetTokenText( int aTok ) const;
396
400 wxString GetTokenString( int aTok ) const;
401
402 static const char* Syntax( int aTok );
403
407 const char* CurText() const
408 {
409 return curText.c_str();
410 }
411
415 const std::string& CurStr() const
416 {
417 return curText;
418 }
419
424 wxString FromUTF8() const
425 {
426 return wxString::FromUTF8( curText.c_str() );
427 }
428
432 int CurLineNumber() const
433 {
434 return reader->LineNumber();
435 }
436
440 const char* CurLine() const
441 {
442 return (const char*)(*reader);
443 }
444
450 const wxString& CurSource() const
451 {
452 return reader->GetSource();
453 }
454
460 int CurOffset() const
461 {
462 return curOffset + 1;
463 }
464
465#ifndef SWIG
466
467protected:
468 void init();
469
471 {
472 if( reader )
473 {
474 reader->ReadLine();
475
476 unsigned len = reader->Length();
477
478 // start may have changed in ReadLine(), which can resize and
479 // relocate reader's line buffer.
480 start = reader->Line();
481
482 next = start;
483 limit = next + len;
484
485 return len;
486 }
487 return 0;
488 }
489
497 int findToken( const std::string& aToken ) const;
498
499 bool isStringTerminator( char cc ) const
500 {
501 if( !space_in_quoted_tokens && cc == ' ' )
502 return true;
503
504 if( cc == stringDelimiter )
505 return true;
506
507 return false;
508 }
509
517 double parseDouble();
518
519 double parseDouble( const char* aExpected )
520 {
521 NeedNUMBER( aExpected );
522 return parseDouble();
523 }
524
525 template <typename T>
526 inline double parseDouble( T aToken )
527 {
528 return parseDouble( GetTokenText( aToken ) );
529 }
530
532 const char* start;
533 const char* next;
534 const char* limit;
535 char dummy[1];
536
537 typedef std::vector<LINE_READER*> READER_STACK;
538
540
543
549
552
554
557
558 int curTok;
559 std::string curText;
560
562 unsigned keywordCount;
564#endif // SWIG
565};
566
567#endif // DSNLEXER_H_
Implement a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:81
const char * next
Definition: dsnlexer.h:533
std::string curText
the text of the current token
Definition: dsnlexer.h:559
unsigned keywordCount
count of keywords table
Definition: dsnlexer.h:562
std::vector< LINE_READER * > READER_STACK
Definition: dsnlexer.h:537
bool SetSpaceInQuotedTokens(bool val)
Change the setting controlling whether a space in a quoted string isa terminator.
Definition: dsnlexer.h:282
bool commentsAreTokens
true if should return comments as tokens
Definition: dsnlexer.h:553
const char * CurText() const
Return a pointer to the current token's text.
Definition: dsnlexer.h:407
const char * start
Definition: dsnlexer.h:532
int curTok
the current token obtained on last NextTok()
Definition: dsnlexer.h:558
bool space_in_quoted_tokens
blank spaces within quoted strings
Definition: dsnlexer.h:551
int PrevTok() const
Return whatever NextTok() returned the 2nd to last time it was called.
Definition: dsnlexer.h:247
bool specctraMode
if true, then: 1) stringDelimiter can be changed 2) Kicad quoting protocol is not in effect 3) space_...
Definition: dsnlexer.h:544
int curOffset
offset within current line of the current token
Definition: dsnlexer.h:556
char stringDelimiter
Definition: dsnlexer.h:550
LINE_READER * reader
Definition: dsnlexer.h:542
const char * limit
Definition: dsnlexer.h:534
bool isStringTerminator(char cc) const
Definition: dsnlexer.h:499
const KEYWORD * keywords
table sorted by CMake for bsearch()
Definition: dsnlexer.h:561
int readLine()
Definition: dsnlexer.h:470
READER_STACK readerStack
all the LINE_READERs by pointer.
Definition: dsnlexer.h:539
wxString FromUTF8() const
Return the current token text as a wxString, assuming that the input byte stream is UTF8 encoded.
Definition: dsnlexer.h:424
const wxString & CurSource() const
Return the current LINE_READER source.
Definition: dsnlexer.h:450
const std::string & CurStr() const
Return a reference to current token in std::string form.
Definition: dsnlexer.h:415
const char * CurLine() const
Return the current line of text from which the CurText() would return its token.
Definition: dsnlexer.h:440
bool SetCommentsAreTokens(bool val)
Change the handling of comments.
Definition: dsnlexer.h:298
double parseDouble(T aToken)
Definition: dsnlexer.h:526
int GetCurStrAsToken() const
Used to support "loose" matches (quoted tokens).
Definition: dsnlexer.h:255
int CurTok() const
Return whatever NextTok() returned the last time it was called.
Definition: dsnlexer.h:239
double parseDouble(const char *aExpected)
Definition: dsnlexer.h:519
int CurOffset() const
Return the byte offset within the current line, using a 1 based index.
Definition: dsnlexer.h:460
char SetStringDelimiter(char aStringDelimiter)
Change the string delimiter from the default " to some other character and return the old value.
Definition: dsnlexer.h:267
int prevTok
curTok from previous NextTok() call.
Definition: dsnlexer.h:555
const KEYWORD_MAP * keywordsLookup
fast, specialized "C string" hashtable
Definition: dsnlexer.h:563
int CurLineNumber() const
Return the current line number within my LINE_READER.
Definition: dsnlexer.h:432
bool iOwnReaders
on readerStack, should I delete them?
Definition: dsnlexer.h:531
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:64
@ DSN_STRING_QUOTE
Definition: dsnlexer.h:63
@ DSN_LEFT
Definition: dsnlexer.h:69
@ DSN_RIGHT
Definition: dsnlexer.h:68
@ DSN_NUMBER
Definition: dsnlexer.h:67
@ DSN_NONE
Definition: dsnlexer.h:60
@ DSN_BAR
Definition: dsnlexer.h:61
@ DSN_DASH
Definition: dsnlexer.h:65
@ DSN_SYMBOL
Definition: dsnlexer.h:66
@ DSN_COMMENT
Definition: dsnlexer.h:62
@ DSN_STRING
Definition: dsnlexer.h:70
@ DSN_EOF
Definition: dsnlexer.h:71
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