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, 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:
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
291 void SetKnowsBar( bool knowsBar = true )
292 {
293 // This is used to control whether the lexer knows about the DSN_BAR token.
294 // If it does not, then it will not return a DSN_BAR token, but rather
295 // treat it as a string.
296 // This is used to support the KiCad legacy format, which does not have a DSN_BAR
297 m_knowsBar = knowsBar;
298 }
299
306 bool SetCommentsAreTokens( bool val )
307 {
308 bool old = commentsAreTokens;
309 commentsAreTokens = val;
310 return old;
311 }
312
325 wxArrayString* ReadCommentLines();
326
333 static bool IsSymbol( int aTok );
334
335 static bool IsNumber( int aTok );
336
343 void Expecting( int aTok ) const;
344
352 void Expecting( const char* aTokenList ) const;
353
361 void Unexpected( int aTok ) const;
362
369 void Unexpected( const char* aToken ) const;
370
379 void Duplicate( int aTok );
380
386 void NeedLEFT();
387
393 void NeedRIGHT();
394
400 void NeedBAR();
401
405 const char* GetTokenText( int aTok ) const;
406
410 wxString GetTokenString( int aTok ) const;
411
412 static const char* Syntax( int aTok );
413
417 const char* CurText() const
418 {
419 return curText.c_str();
420 }
421
425 const std::string& CurStr() const
426 {
427 return curText;
428 }
429
430 const std::string& CurSeparator() const
431 {
432 return curSeparator;
433 }
434
439 wxString FromUTF8() const
440 {
441 return wxString::FromUTF8( curText.c_str() );
442 }
443
447 int CurLineNumber() const
448 {
449 return reader->LineNumber();
450 }
451
455 const char* CurLine() const
456 {
457 return (const char*)(*reader);
458 }
459
465 const wxString& CurSource() const
466 {
467 return reader->GetSource();
468 }
469
475 int CurOffset() const
476 {
477 return curOffset + 1;
478 }
479
480#ifndef SWIG
481
482protected:
483 void init();
484
485 inline bool isSep( char cc );
486
488 {
489 if( reader )
490 {
491 reader->ReadLine();
492
493 unsigned len = reader->Length();
494
495 // start may have changed in ReadLine(), which can resize and
496 // relocate reader's line buffer.
497 start = reader->Line();
498
499 next = start;
500 limit = next + len;
501
502 return len;
503 }
504 return 0;
505 }
506
514 int findToken( const std::string& aToken ) const;
515
516 bool isStringTerminator( char cc ) const
517 {
518 if( !space_in_quoted_tokens && cc == ' ' )
519 return true;
520
521 if( cc == stringDelimiter )
522 return true;
523
524 return false;
525 }
526
534 double parseDouble();
535
536 double parseDouble( const char* aExpected )
537 {
538 NeedNUMBER( aExpected );
539 return parseDouble();
540 }
541
542 template <typename T>
543 inline double parseDouble( T aToken )
544 {
545 return parseDouble( GetTokenText( aToken ) );
546 }
547
548protected:
550 const char* start;
551 const char* next;
552 const char* limit;
553 char dummy[1];
554
555 typedef std::vector<LINE_READER*> READER_STACK;
556
558
561
569
572
574
577
578 int curTok;
579 std::string curText;
580 std::string curSeparator;
581
583 unsigned keywordCount;
585#endif // SWIG
586};
587
588#endif // DSNLEXER_H_
Implement a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:81
const char * next
Definition: dsnlexer.h:551
std::string curText
The text of the current token.
Definition: dsnlexer.h:579
unsigned keywordCount
Count of keywords table.
Definition: dsnlexer.h:583
std::vector< LINE_READER * > READER_STACK
Definition: dsnlexer.h:555
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:573
const char * CurText() const
Return a pointer to the current token's text.
Definition: dsnlexer.h:417
const char * start
Definition: dsnlexer.h:550
int curTok
The current token obtained on last NextTok().
Definition: dsnlexer.h:578
std::string curSeparator
The text of the separator preceeding the current text.
Definition: dsnlexer.h:580
bool space_in_quoted_tokens
Blank spaces within quoted strings.
Definition: dsnlexer.h:571
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:562
const std::string & CurSeparator() const
Definition: dsnlexer.h:430
int curOffset
Offset within current line of the current token.
Definition: dsnlexer.h:576
char stringDelimiter
Definition: dsnlexer.h:570
LINE_READER * reader
No ownership. ownership is via readerStack, maybe, if iOwnReaders.
Definition: dsnlexer.h:560
const char * limit
Definition: dsnlexer.h:552
bool isStringTerminator(char cc) const
Definition: dsnlexer.h:516
const KEYWORD * keywords
Table sorted by CMake for bsearch().
Definition: dsnlexer.h:582
int readLine()
Definition: dsnlexer.h:487
READER_STACK readerStack
all the LINE_READERs by pointer.
Definition: dsnlexer.h:557
wxString FromUTF8() const
Return the current token text as a wxString, assuming that the input byte stream is UTF8 encoded.
Definition: dsnlexer.h:439
const wxString & CurSource() const
Return the current LINE_READER source.
Definition: dsnlexer.h:465
const std::string & CurStr() const
Return a reference to current token in std::string form.
Definition: dsnlexer.h:425
void SetKnowsBar(bool knowsBar=true)
Definition: dsnlexer.h:291
const char * CurLine() const
Return the current line of text from which the CurText() would return its token.
Definition: dsnlexer.h:455
bool SetCommentsAreTokens(bool val)
Change the handling of comments.
Definition: dsnlexer.h:306
double parseDouble(T aToken)
Definition: dsnlexer.h:543
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:536
int CurOffset() const
Return the byte offset within the current line, using a 1 based index.
Definition: dsnlexer.h:475
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:575
const KEYWORD_MAP * keywordsLookup
Fast, specialized "C string" hashtable.
Definition: dsnlexer.h:584
bool m_knowsBar
True if the lexer knows about the bar token.
Definition: dsnlexer.h:567
int CurLineNumber() const
Return the current line number within my LINE_READER.
Definition: dsnlexer.h:447
bool iOwnReaders
On readerStack, should I delete them?
Definition: dsnlexer.h:549
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:124
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 ...
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