32#include <wx/translation.h>
34#define FMT_CLIPBOARD _( "clipboard" )
56 FILE* aFile,
const wxString& aFilename ) :
62 keywords( aKeywordTable ),
63 keywordCount( aKeywordCount ),
64 keywordsLookup( aKeywordMap )
74 const std::string& aClipboardTxt,
const wxString& aSource ) :
80 keywords( aKeywordTable ),
81 keywordCount( aKeywordCount ),
82 keywordsLookup( aKeywordMap )
99 keywords( aKeywordTable ),
100 keywordCount( aKeywordCount ),
101 keywordsLookup( aKeywordMap )
119 keywordsLookup( nullptr )
233 KEYWORD_MAP::const_iterator it =
keywordsLookup->find( tok.c_str() );
253 ret =
"string_quote";
256 ret =
"quoted text delimiter";
274 ret =
"quoted string";
277 ret =
"end of input";
300 ret =
"token too big";
310 ret << wxT(
"'") << wxString::FromUTF8(
GetTokenText(aTok) ) << wxT(
"'");
327 wxString errText = wxString::Format(
335 wxString errText = wxString::Format(
336 _(
"Expecting '%s'" ), wxString::FromUTF8(
text ) );
343 wxString errText = wxString::Format(
351 wxString errText = wxString::Format(
359 wxString errText = wxString::Format(
360 _(
"Unexpected '%s'" ), wxString::FromUTF8(
text ) );
410 wxString errText = wxString::Format(
_(
"need a number for '%s'" ),
411 wxString::FromUTF8( aExpectation ).GetData() );
429 if( (
unsigned char) cc <=
' ' )
431 switch( (
unsigned char) cc )
448 return '0' <= cc && cc <=
'9';
455 return isSpace( cc ) || cc==
'(' || cc==
')';
468static bool isNumber(
const char* cp,
const char* limit )
473 bool sawNumber =
false;
475 if( cp < limit && ( *cp==
'-' || *cp==
'+' ) )
478 while( cp < limit &&
isDigit( *cp ) )
484 if( cp < limit && *cp ==
'.' )
488 while( cp < limit &&
isDigit( *cp ) )
497 if( cp < limit && ( *cp==
'E' || *cp==
'e' ) )
503 if( cp < limit && ( *cp==
'-' || *cp==
'+' ) )
506 while( cp < limit &&
isDigit( *cp ) )
514 return sawNumber && cp==limit;
520 const char* cur =
next;
521 const char* head = cur;
550 if( cur<
limit && *cur==
'#' )
557 while(
limit[-1] ==
'\n' ||
limit[-1] ==
'\r' )
623 if( ++head >=
limit )
629 case '\\': c = head[-1];
break;
630 case 'a': c =
'\x07';
break;
631 case 'b': c =
'\x08';
break;
632 case 'f': c =
'\x0c';
break;
633 case 'n': c =
'\n';
break;
634 case 'r': c =
'\r';
break;
635 case 't': c =
'\x09';
break;
636 case 'v': c =
'\x0b';
break;
639 for( i = 0; i < 2; ++i )
641 if( !isxdigit( head[i] ) )
650 c = (char) strtoul( tbuf,
nullptr, 16 );
662 if( head[i] <
'0' || head[i] >
'7' )
671 c = (char) strtoul( tbuf,
nullptr, 8 );
682 else if( *head ==
'"' )
695 wxString errtxt(
_(
"Un-terminated delimited string" ) );
717 static const wxString errtxt(
_(
"String delimiter must be a single character of "
756 wxString errtxt(
_(
"Un-terminated delimited string" ) );
803 wxArrayString* ret =
nullptr;
809 ret =
new wxArrayString();
826#if ( defined( __GNUC__ ) && __GNUC__ < 11 ) || ( defined( __clang__ ) && __clang_major__ < 13 )
834 double fval = strtod(
CurText(), &tmp );
839 error.Printf(
_(
"Invalid floating point number in\nfile: '%s'\nline: %d\noffset: %d" ),
848 error.Printf(
_(
"Missing floating point number in\nfile: '%s'\nline: %d\noffset: %d" ),
858 const std::string& str =
CurStr();
862 while( std::isspace( str[woff] ) && woff < str.length() )
868 std::from_chars_result
res =
869 std::from_chars( str.data() + woff, str.data() + str.size(), dval );
871 if(
res.ec != std::errc() )
Implement a lexical analyzer for the SPECCTRA DSN file format.
int NeedSYMBOLorNUMBER()
Call NextTok() and then verifies that the token read in satisfies bool IsSymbol() or the next token i...
void NeedLEFT()
Call NextTok() and then verifies that the token read in is a DSN_LEFT.
std::string curText
the text of the current token
unsigned keywordCount
count of keywords table
int NeedNUMBER(const char *aExpectation)
Call NextTok() and then verifies that the token read is type DSN_NUMBER.
bool commentsAreTokens
true if should return comments as tokens
int findToken(const std::string &aToken) const
Take aToken string and looks up the string in the keywords table.
const char * CurText() const
Return a pointer to the current token's text.
static bool IsSymbol(int aTok)
Test a token to see if it is a symbol.
const char * GetTokenText(int aTok) const
Return the C string representation of a DSN_T value.
wxArrayString * ReadCommentLines()
Check the next sequence of tokens and reads them into a wxArrayString if they are comments.
int curTok
the current token obtained on last NextTok()
bool space_in_quoted_tokens
blank spaces within quoted strings
bool specctraMode
if true, then: 1) stringDelimiter can be changed 2) Kicad quoting protocol is not in effect 3) space_...
int NextTok()
Return the next token found in the input file or DSN_EOF when reaching the end of file.
int NeedSYMBOL()
Call NextTok() and then verifies that the token read in satisfies IsSymbol().
int curOffset
offset within current line of the current token
bool isStringTerminator(char cc) const
const KEYWORD * keywords
table sorted by CMake for bsearch()
void NeedRIGHT()
Call NextTok() and then verifies that the token read in is a DSN_RIGHT.
READER_STACK readerStack
all the LINE_READERs by pointer.
char dummy[1]
when there is no reader.
double parseDouble()
Parse the current token as an ASCII numeric string with possible leading whitespace into a double pre...
void SetSpecctraMode(bool aMode)
Change the behavior of this lexer into or out of "specctra mode".
wxString FromUTF8() const
Return the current token text as a wxString, assuming that the input byte stream is UTF8 encoded.
void Expecting(int aTok) const
Throw an IO_ERROR exception with an input file specific error message.
const wxString & CurSource() const
Return the current LINE_READER source.
LINE_READER * PopReader()
Delete the top most LINE_READER from an internal stack of LINE_READERs and in the case of FILE_LINE_R...
void Duplicate(int aTok)
Throw an IO_ERROR exception with a message saying specifically that aTok is a duplicate of one alread...
const std::string & CurStr() const
Return a reference to current token in std::string form.
const char * CurLine() const
Return the current line of text from which the CurText() would return its token.
bool SetCommentsAreTokens(bool val)
Change the handling of comments.
wxString GetTokenString(int aTok) const
Return a quote wrapped wxString representation of a token value.
int CurOffset() const
Return the byte offset within the current line, using a 1 based index.
void InitParserState()
Reinit variables used during parsing, to ensure od states are not used in a new parsing must be calle...
int prevTok
curTok from previous NextTok() call.
const KEYWORD_MAP * keywordsLookup
fast, specialized "C string" hashtable
static const char * Syntax(int aTok)
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.
bool SyncLineReaderWith(DSNLEXER &aLexer)
Usable only for DSN lexers which share the same LINE_READER.
void Unexpected(int aTok) const
Throw an IO_ERROR exception with an input file specific error message.
int CurLineNumber() const
Return the current line number within my LINE_READER.
bool iOwnReaders
on readerStack, should I delete them?
void PushReader(LINE_READER *aLineReader)
Manage a stack of LINE_READERs in order to handle nested file inclusion.
A LINE_READER that reads from an open file.
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
char * Line() const
Return a pointer to the last line that was read in.
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
static bool isNumber(const char *cp, const char *limit)
Return true if the next sequence of text is a number: either an integer, fixed point,...
static bool isSpace(char cc)
Test for whitespace.
static const KEYWORD empty_keywords[1]
std::unordered_map< const char *, int, fnv_1a, iequal_to > KEYWORD_MAP
A hashtable made of a const char* and an int.
#define THROW_IO_ERROR(msg)
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Hold a keyword string and its unique integer token.
const char * name
unique keyword.