2009-12-11 04:55:24 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007-2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2021-07-15 19:26:35 +00:00
|
|
|
* Copyright (C) 2007-2021 Kicad Developers, see change_log.txt for contributors.
|
2009-12-11 04:55:24 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2010-12-27 21:22:12 +00:00
|
|
|
#ifndef DSNLEXER_H_
|
|
|
|
#define DSNLEXER_H_
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <hashtables.h>
|
2009-12-11 04:55:24 +00:00
|
|
|
#include <string>
|
2010-12-28 16:55:28 +00:00
|
|
|
#include <vector>
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <richio.h>
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2011-01-21 07:21:19 +00:00
|
|
|
#ifndef SWIG
|
2009-12-11 04:55:24 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Hold a keyword string and its unique integer token.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
|
|
|
struct KEYWORD
|
|
|
|
{
|
|
|
|
const char* name; ///< unique keyword.
|
|
|
|
int token; ///< a zero based index into an array of KEYWORDs
|
|
|
|
};
|
2020-11-18 19:50:36 +00:00
|
|
|
#endif // SWIG
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
// something like this macro can be used to help initialize a KEYWORD table.
|
|
|
|
// see SPECCTRA_DB::keywords[] as an example.
|
|
|
|
|
|
|
|
//#define TOKDEF(x) { #x, T_##x }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* List all the DSN lexer's tokens that are supported in lexing.
|
|
|
|
*
|
|
|
|
* It is up to the parser if it wants also to support them.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
enum DSN_SYNTAX_T
|
|
|
|
{
|
|
|
|
DSN_NONE = -11,
|
|
|
|
DSN_COMMENT = -10,
|
2009-12-11 04:55:24 +00:00
|
|
|
DSN_STRING_QUOTE = -9,
|
2020-11-18 19:50:36 +00:00
|
|
|
DSN_QUOTE_DEF = -8,
|
|
|
|
DSN_DASH = -7,
|
|
|
|
DSN_SYMBOL = -6,
|
|
|
|
DSN_NUMBER = -5,
|
|
|
|
DSN_RIGHT = -4, // right bracket, ')'
|
|
|
|
DSN_LEFT = -3, // left bracket, '('
|
|
|
|
DSN_STRING = -2, // a quoted string, stripped of the quotes
|
|
|
|
DSN_EOF = -1 // special case for end of file
|
2009-12-11 04:55:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Implement a lexical analyzer for the SPECCTRA DSN file format.
|
|
|
|
*
|
|
|
|
* It reads lexical tokens from the current #LINE_READER through the #NextTok() function.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
|
|
|
class DSNLEXER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Initialize a DSN lexer and prepares to read from aFile which is already open and has
|
|
|
|
* \a aFilename.
|
2009-12-11 04:55:24 +00:00
|
|
|
*
|
|
|
|
* @param aKeywordTable is an array of KEYWORDS holding \a aKeywordCount. This
|
|
|
|
* token table need not contain the lexer separators such as '(' ')', etc.
|
2011-06-07 15:29:01 +00:00
|
|
|
* @param aKeywordCount is the count of tokens in aKeywordTable.
|
2010-12-27 21:22:12 +00:00
|
|
|
* @param aFile is an open file, which will be closed when this is destructed.
|
|
|
|
* @param aFileName is the name of the file
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2010-12-27 21:22:12 +00:00
|
|
|
DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount,
|
|
|
|
FILE* aFile, const wxString& aFileName );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2010-12-28 16:55:28 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Initialize a DSN lexer and prepares to read from @a aSExpression.
|
2010-12-28 16:55:28 +00:00
|
|
|
*
|
|
|
|
* @param aKeywordTable is an array of KEYWORDS holding \a aKeywordCount. This
|
|
|
|
* token table need not contain the lexer separators such as '(' ')', etc.
|
2011-06-07 15:29:01 +00:00
|
|
|
* @param aKeywordCount is the count of tokens in aKeywordTable.
|
2010-12-28 16:55:28 +00:00
|
|
|
* @param aSExpression is text to feed through a STRING_LINE_READER
|
|
|
|
* @param aSource is a description of aSExpression, used for error reporting.
|
|
|
|
*/
|
2010-12-27 21:22:12 +00:00
|
|
|
DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount,
|
2010-12-28 16:55:28 +00:00
|
|
|
const std::string& aSExpression, const wxString& aSource = wxEmptyString );
|
2010-03-08 06:18:40 +00:00
|
|
|
|
2014-07-07 19:49:14 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Initialize a DSN lexer and prepares to read from @a aSExpression.
|
2014-07-07 19:49:14 +00:00
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* Use this one without a keyword table with the DOM parser in ptree.h.
|
|
|
|
*
|
|
|
|
* @param aSExpression is text to feed through a #STRING_LINE_READER
|
2014-07-07 19:49:14 +00:00
|
|
|
* @param aSource is a description of aSExpression, used for error reporting.
|
|
|
|
*/
|
|
|
|
DSNLEXER( const std::string& aSExpression, const wxString& aSource = wxEmptyString );
|
|
|
|
|
2010-12-28 16:55:28 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Initialize a DSN lexer and prepares to read from @a aLineReader which is already
|
|
|
|
* open, and may be in use by other DSNLEXERs also.
|
2010-12-28 16:55:28 +00:00
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* No ownership is taken of @a aLineReader. This enables it to be used by other DSNLEXERs.
|
2012-10-24 18:20:33 +00:00
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* @param aKeywordTable is an array of #KEYWORDS holding \a aKeywordCount. This
|
|
|
|
* token table need not contain the lexer separators such as '(' ')', etc.
|
2011-06-07 15:29:01 +00:00
|
|
|
* @param aKeywordCount is the count of tokens in aKeywordTable.
|
2010-12-28 16:55:28 +00:00
|
|
|
* @param aLineReader is any subclassed instance of LINE_READER, such as
|
2020-12-18 14:04:03 +00:00
|
|
|
* #STRING_LINE_READER or #FILE_LINE_READER. No ownership is taken.
|
2010-12-28 16:55:28 +00:00
|
|
|
*/
|
|
|
|
DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount,
|
2021-07-15 19:26:35 +00:00
|
|
|
LINE_READER* aLineReader = nullptr );
|
2010-12-28 16:55:28 +00:00
|
|
|
|
|
|
|
virtual ~DSNLEXER();
|
2010-03-02 19:15:30 +00:00
|
|
|
|
2014-12-04 19:15:26 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Usable only for DSN lexers which share the same #LINE_READER.
|
|
|
|
*
|
|
|
|
* Synchronizes the pointers handling the data read by the #LINE_READER. Allows 2
|
|
|
|
* #DNSLEXER objects to share the same current line, when switching from a #DNSLEXER
|
|
|
|
* to another #DNSLEXER
|
|
|
|
* @param aLexer the model.
|
|
|
|
* @return true if the sync can be made ( at least the same line reader ).
|
2014-12-04 19:15:26 +00:00
|
|
|
*/
|
|
|
|
bool SyncLineReaderWith( DSNLEXER& aLexer );
|
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Change the behavior of this lexer into or out of "specctra mode".
|
|
|
|
*
|
|
|
|
* If specctra mode, then:
|
|
|
|
* -#) stringDelimiter can be changed.
|
|
|
|
* -#) KiCad quoting protocol is not in effect.
|
|
|
|
* -#) space_in_quoted_tokens is functional else none of the above are true.
|
|
|
|
*
|
|
|
|
* The default mode is non-specctra mode, meaning:
|
|
|
|
* -#) stringDelimiter cannot be changed.
|
|
|
|
* -#) KiCad quoting protocol is in effect.
|
|
|
|
* -#) space_in_quoted_tokens is not functional.
|
2011-01-30 19:26:03 +00:00
|
|
|
*/
|
|
|
|
void SetSpecctraMode( bool aMode );
|
|
|
|
|
2010-10-05 07:15:29 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Manage a stack of LINE_READERs in order to handle nested file inclusion.
|
|
|
|
*
|
2010-10-05 13:38:15 +00:00
|
|
|
* This function pushes aLineReader onto the top of a stack of LINE_READERs and makes
|
2020-12-18 14:04:03 +00:00
|
|
|
* it the current #LINE_READER with its own #GetSource(), line number and line text.
|
2010-10-05 13:38:15 +00:00
|
|
|
* A grammar must be designed such that the "include" token (whatever its various names),
|
|
|
|
* and any of its parameters are not followed by anything on that same line,
|
|
|
|
* because PopReader always starts reading from a new line upon returning to
|
2020-12-18 14:04:03 +00:00
|
|
|
* the original #LINE_READER.
|
2010-10-05 07:15:29 +00:00
|
|
|
*/
|
|
|
|
void PushReader( LINE_READER* aLineReader );
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Delete the top most #LINE_READER from an internal stack of LINE_READERs and
|
|
|
|
* in the case of #FILE_LINE_READER this means the associated FILE is closed.
|
|
|
|
*
|
|
|
|
* The most recently used former #LINE_READER on the stack becomes the
|
|
|
|
* current #LINE_READER and its previous position in its input stream and the
|
2010-10-20 22:47:34 +00:00
|
|
|
* its latest line number should pertain. PopReader always starts reading
|
2020-12-18 14:04:03 +00:00
|
|
|
* from a new line upon returning to the previous #LINE_READER. A pop is only
|
|
|
|
* possible if there are at least 2 #LINE_READERs on the stack, since popping
|
2010-10-05 13:38:15 +00:00
|
|
|
* the last one is not supported.
|
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* @return the LINE_READER that was in use before the pop, or NULL
|
2011-01-19 20:46:07 +00:00
|
|
|
* if there was not at least two readers on the stack and therefore the
|
|
|
|
* pop failed.
|
2010-10-05 07:15:29 +00:00
|
|
|
*/
|
2011-01-19 20:46:07 +00:00
|
|
|
LINE_READER* PopReader();
|
2010-10-05 07:15:29 +00:00
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return the next token found in the input file or DSN_EOF when reaching the end of
|
|
|
|
* file.
|
|
|
|
*
|
|
|
|
* Users should wrap this function to return an enum to aid in grammar debugging while
|
|
|
|
* running under a debugger, but leave this lower level function returning an int (so
|
|
|
|
* the enum does not collide with another usage).
|
|
|
|
*
|
|
|
|
* @return the type of token found next.
|
|
|
|
* @throw IO_ERROR only if the #LINE_READER throws it.
|
2010-08-09 02:03:16 +00:00
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
int NextTok();
|
2010-08-09 02:03:16 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Call #NextTok() and then verifies that the token read in satisfies #IsSymbol().
|
|
|
|
*
|
|
|
|
* @return the actual token read in.
|
|
|
|
* @throw IO_ERROR if the next token does not satisfy IsSymbol().
|
2010-08-09 02:03:16 +00:00
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
int NeedSYMBOL();
|
2010-08-09 02:03:16 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Call #NextTok() and then verifies that the token read in satisfies bool IsSymbol() or
|
|
|
|
* the next token is #DSN_NUMBER.
|
|
|
|
*
|
|
|
|
* @return the actual token read in.
|
|
|
|
* @throw IO_ERROR if the next token does not satisfy the above test.
|
2010-08-09 02:03:16 +00:00
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
int NeedSYMBOLorNUMBER();
|
2010-08-09 02:03:16 +00:00
|
|
|
|
2011-02-14 06:39:51 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Call #NextTok() and then verifies that the token read is type #DSN_NUMBER.
|
|
|
|
*
|
|
|
|
* @return the actual token read in.
|
|
|
|
* @throw IO_ERROR if the next token does not satisfy the above test.
|
2011-02-14 06:39:51 +00:00
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
int NeedNUMBER( const char* aExpectation );
|
2011-02-14 06:39:51 +00:00
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return whatever #NextTok() returned the last time it was called.
|
2010-08-09 02:03:16 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
int CurTok() const
|
2010-08-09 02:03:16 +00:00
|
|
|
{
|
|
|
|
return curTok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return whatever NextTok() returned the 2nd to last time it was called.
|
2010-08-09 02:03:16 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
int PrevTok() const
|
2010-08-09 02:03:16 +00:00
|
|
|
{
|
|
|
|
return prevTok;
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:16:12 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Used to support "loose" matches (quoted tokens).
|
2020-08-06 22:16:12 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
int GetCurStrAsToken() const
|
2020-08-06 22:16:12 +00:00
|
|
|
{
|
|
|
|
return findToken( curText );
|
|
|
|
}
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Change the string delimiter from the default " to some other character and return
|
|
|
|
* the old value.
|
|
|
|
*
|
2009-12-11 04:55:24 +00:00
|
|
|
* @param aStringDelimiter The character in lowest 8 bits.
|
2020-12-18 14:04:03 +00:00
|
|
|
* @return The old delimiter in the lowest 8 bits.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2011-01-30 19:26:03 +00:00
|
|
|
char SetStringDelimiter( char aStringDelimiter )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
int old = stringDelimiter;
|
2020-12-18 14:04:03 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
if( specctraMode )
|
|
|
|
stringDelimiter = aStringDelimiter;
|
2020-12-18 14:04:03 +00:00
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Change the setting controlling whether a space in a quoted string isa terminator.
|
|
|
|
*
|
2009-12-11 04:55:24 +00:00
|
|
|
* @param val If true, means
|
|
|
|
*/
|
|
|
|
bool SetSpaceInQuotedTokens( bool val )
|
|
|
|
{
|
|
|
|
bool old = space_in_quoted_tokens;
|
2020-12-18 14:04:03 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
if( specctraMode )
|
|
|
|
space_in_quoted_tokens = val;
|
2020-12-18 14:04:03 +00:00
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2010-01-21 07:41:30 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Change the handling of comments.
|
|
|
|
*
|
|
|
|
* If set true, comments are returned as single line strings with a terminating newline.
|
|
|
|
* Otherwise they are consumed by the lexer and not returned.
|
2010-01-21 07:41:30 +00:00
|
|
|
*/
|
|
|
|
bool SetCommentsAreTokens( bool val )
|
|
|
|
{
|
2010-02-20 19:13:30 +00:00
|
|
|
bool old = commentsAreTokens;
|
|
|
|
commentsAreTokens = val;
|
2010-01-21 07:41:30 +00:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2013-06-23 19:18:33 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Check the next sequence of tokens and reads them into a wxArrayString if they are
|
|
|
|
* comments.
|
|
|
|
*
|
|
|
|
* Reading continues until a non-comment token is encountered, and such last read token
|
|
|
|
* remains as #CurTok() and as #CurText(). No push back or "un get" mechanism is used
|
|
|
|
* for this support. Upon return you simply avoid calling NextTok() for the next token,
|
|
|
|
* but rather #CurTok().
|
2013-06-23 19:18:33 +00:00
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* @return Heap allocated block of comments or NULL if none. The caller owns the
|
|
|
|
* allocation and must delete if not NULL.
|
2013-06-23 19:18:33 +00:00
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
wxArrayString* ReadCommentLines();
|
2013-06-23 19:18:33 +00:00
|
|
|
|
2010-06-18 16:00:16 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Test a token to see if it is a symbol.
|
|
|
|
*
|
|
|
|
* This means it cannot be a special delimiter character such as #DSN_LEFT, #DSN_RIGHT,
|
|
|
|
* #DSN_QUOTE, etc. It may however, coincidentally match a keyword and still be a symbol.
|
2010-06-18 16:00:16 +00:00
|
|
|
*/
|
|
|
|
static bool IsSymbol( int aTok );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Throw an #IO_ERROR exception with an input file specific error message.
|
|
|
|
*
|
2010-06-17 16:30:10 +00:00
|
|
|
* @param aTok is the token/keyword type which was expected at the current input location.
|
2010-11-09 15:45:11 +00:00
|
|
|
* @throw IO_ERROR with the location within the input file of the problem.
|
2010-06-17 16:30:10 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
void Expecting( int aTok ) const;
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Throw an #IO_ERROR exception with an input file specific error message.
|
|
|
|
*
|
2011-01-19 20:46:07 +00:00
|
|
|
* @param aTokenList is the token/keyword type which was expected at the
|
2020-12-18 14:04:03 +00:00
|
|
|
* current input location, e.g. "pin|graphic|property".
|
2010-11-09 15:45:11 +00:00
|
|
|
* @throw IO_ERROR with the location within the input file of the problem.
|
2010-06-17 16:30:10 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
void Expecting( const char* aTokenList ) const;
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Throw an #IO_ERROR exception with an input file specific error message.
|
|
|
|
*
|
2010-06-17 16:30:10 +00:00
|
|
|
* @param aTok is the token/keyword type which was not expected at the
|
2020-12-18 14:04:03 +00:00
|
|
|
* current input location.
|
2010-11-09 15:45:11 +00:00
|
|
|
* @throw IO_ERROR with the location within the input file of the problem.
|
2010-06-17 16:30:10 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
void Unexpected( int aTok ) const;
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2011-01-01 22:56:07 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Throw an #IO_ERROR exception with an input file specific error message.
|
|
|
|
*
|
|
|
|
* @param aToken is the token which was not expected at the current input location.
|
2011-01-01 22:56:07 +00:00
|
|
|
* @throw IO_ERROR with the location within the input file of the problem.
|
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
void Unexpected( const char* aToken ) const;
|
2011-01-01 22:56:07 +00:00
|
|
|
|
2010-06-17 16:30:10 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Throw an #IO_ERROR exception with a message saying specifically that \a aTok
|
2011-03-20 01:59:17 +00:00
|
|
|
* is a duplicate of one already seen in current context.
|
2020-12-18 14:04:03 +00:00
|
|
|
*
|
|
|
|
* @param aTok is the token/keyword type which was not expected at the current input
|
|
|
|
* location.
|
2010-11-09 15:45:11 +00:00
|
|
|
* @throw IO_ERROR with the location within the input file of the problem.
|
2010-06-17 16:30:10 +00:00
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
void Duplicate( int aTok );
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2010-06-18 16:00:16 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Call #NextTok() and then verifies that the token read in is a #DSN_LEFT.
|
|
|
|
*
|
|
|
|
* @throw IO_ERROR if the next token is not a #DSN_LEFT
|
2010-06-18 16:00:16 +00:00
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
void NeedLEFT();
|
2010-06-18 16:00:16 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Call #NextTok() and then verifies that the token read in is a #DSN_RIGHT.
|
|
|
|
*
|
|
|
|
* @throw IO_ERROR if the next token is not a #DSN_RIGHT
|
2010-06-18 16:00:16 +00:00
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
void NeedRIGHT();
|
2010-06-18 16:00:16 +00:00
|
|
|
|
2010-06-17 16:30:10 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return the C string representation of a #DSN_T value.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
const char* GetTokenText( int aTok ) const;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2010-06-17 16:30:10 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return a quote wrapped wxString representation of a token value.
|
2010-06-17 16:30:10 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
wxString GetTokenString( int aTok ) const;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2010-06-17 16:30:10 +00:00
|
|
|
static const char* Syntax( int aTok );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return a pointer to the current token's text.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
const char* CurText() const
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
return curText.c_str();
|
|
|
|
}
|
|
|
|
|
2013-09-26 05:29:54 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return a reference to current token in std::string form.
|
2013-09-26 05:29:54 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
const std::string& CurStr() const
|
2013-09-26 05:29:54 +00:00
|
|
|
{
|
|
|
|
return curText;
|
|
|
|
}
|
|
|
|
|
2011-01-01 22:56:07 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return the current token text as a wxString, assuming that the input byte stream
|
|
|
|
* is UTF8 encoded.
|
2011-01-01 22:56:07 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
wxString FromUTF8() const
|
2011-01-01 22:56:07 +00:00
|
|
|
{
|
|
|
|
return wxString::FromUTF8( curText.c_str() );
|
|
|
|
}
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return the current line number within my #LINE_READER.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
int CurLineNumber() const
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2010-03-02 19:15:30 +00:00
|
|
|
return reader->LineNumber();
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
2011-01-01 22:56:07 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return the current line of text from which the #CurText() would return its token.
|
2011-01-01 22:56:07 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
const char* CurLine() const
|
2011-01-01 22:56:07 +00:00
|
|
|
{
|
|
|
|
return (const char*)(*reader);
|
|
|
|
}
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return the current #LINE_READER source.
|
|
|
|
*
|
|
|
|
* @return source of the lines of text, e.g. a filename or "clipboard".
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
const wxString& CurSource() const
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2010-10-05 07:15:29 +00:00
|
|
|
return reader->GetSource();
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return the byte offset within the current line, using a 1 based index.
|
|
|
|
*
|
|
|
|
* @return a one based index into the current line.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2020-11-18 19:50:36 +00:00
|
|
|
int CurOffset() const
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
return curOffset + 1;
|
|
|
|
}
|
2020-12-18 14:04:03 +00:00
|
|
|
|
|
|
|
#ifndef SWIG
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void init();
|
|
|
|
|
|
|
|
int readLine()
|
|
|
|
{
|
|
|
|
if( reader )
|
|
|
|
{
|
|
|
|
reader->ReadLine();
|
|
|
|
|
|
|
|
unsigned len = reader->Length();
|
|
|
|
|
|
|
|
// start may have changed in ReadLine(), which can resize and
|
|
|
|
// relocate reader's line buffer.
|
|
|
|
start = reader->Line();
|
|
|
|
|
|
|
|
next = start;
|
|
|
|
limit = next + len;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take @a aToken string and looks up the string in the keywords table.
|
|
|
|
*
|
|
|
|
* @param aToken is a string to lookup in the keywords table.
|
|
|
|
* @return with a value from the enum #DSN_T matching the keyword text,
|
|
|
|
* or #DSN_SYMBOL if @a aToken is not in the keywords table.
|
|
|
|
*/
|
|
|
|
int findToken( const std::string& aToken ) const;
|
|
|
|
|
|
|
|
bool isStringTerminator( char cc ) const
|
|
|
|
{
|
|
|
|
if( !space_in_quoted_tokens && cc == ' ' )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( cc == stringDelimiter )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool iOwnReaders; ///< on readerStack, should I delete them?
|
|
|
|
const char* start;
|
|
|
|
const char* next;
|
|
|
|
const char* limit;
|
|
|
|
char dummy[1]; ///< when there is no reader.
|
|
|
|
|
|
|
|
typedef std::vector<LINE_READER*> READER_STACK;
|
|
|
|
|
|
|
|
READER_STACK readerStack; ///< all the LINE_READERs by pointer.
|
|
|
|
|
|
|
|
///< no ownership. ownership is via readerStack, maybe, if iOwnReaders
|
|
|
|
LINE_READER* reader;
|
|
|
|
|
|
|
|
bool specctraMode; ///< if true, then:
|
|
|
|
///< 1) stringDelimiter can be changed
|
|
|
|
///< 2) Kicad quoting protocol is not in effect
|
|
|
|
///< 3) space_in_quoted_tokens is functional
|
|
|
|
///< else not.
|
|
|
|
|
|
|
|
char stringDelimiter;
|
|
|
|
bool space_in_quoted_tokens; ///< blank spaces within quoted strings
|
|
|
|
|
|
|
|
bool commentsAreTokens; ///< true if should return comments as tokens
|
|
|
|
|
|
|
|
int prevTok; ///< curTok from previous NextTok() call.
|
|
|
|
int curOffset; ///< offset within current line of the current token
|
|
|
|
|
|
|
|
int curTok; ///< the current token obtained on last NextTok()
|
|
|
|
std::string curText; ///< the text of the current token
|
|
|
|
|
|
|
|
const KEYWORD* keywords; ///< table sorted by CMake for bsearch()
|
|
|
|
unsigned keywordCount; ///< count of keywords table
|
|
|
|
KEYWORD_MAP keyword_hash; ///< fast, specialized "C string" hashtable
|
|
|
|
#endif // SWIG
|
2009-12-11 04:55:24 +00:00
|
|
|
};
|
|
|
|
|
2010-12-27 21:22:12 +00:00
|
|
|
#endif // DSNLEXER_H_
|