2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
/*
|
2011-09-30 18:15:37 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
2009-12-11 04:55:24 +00:00
|
|
|
*
|
2013-05-01 15:48:00 +00:00
|
|
|
* Copyright (C) 2007-2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2011-09-30 18:15:37 +00:00
|
|
|
* Copyright (C) 2007 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib> // bsearch()
|
|
|
|
#include <cctype>
|
|
|
|
|
2013-04-09 16:00:46 +00:00
|
|
|
#include <macros.h>
|
2013-05-01 15:48:00 +00:00
|
|
|
#include <fctsys.h>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <dsnlexer.h>
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
|
2010-03-08 06:18:40 +00:00
|
|
|
//#define STANDALONE 1 // enable this for stand alone testing.
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2014-07-07 19:49:14 +00:00
|
|
|
#define FMT_CLIPBOARD _( "clipboard" )
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
//-----<DSNLEXER>-------------------------------------------------------------
|
|
|
|
|
2013-07-01 01:05:40 +00:00
|
|
|
void DSNLEXER::init()
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2012-04-30 02:57:48 +00:00
|
|
|
curTok = DSN_NONE;
|
|
|
|
prevTok = DSN_NONE;
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
stringDelimiter = '"';
|
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
specctraMode = false;
|
|
|
|
space_in_quoted_tokens = false;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2010-02-20 19:13:30 +00:00
|
|
|
commentsAreTokens = false;
|
2013-07-01 01:05:40 +00:00
|
|
|
|
|
|
|
#if 1
|
|
|
|
if( keywordCount > 11 )
|
|
|
|
{
|
|
|
|
// resize the hashtable bucket count
|
|
|
|
keyword_hash.reserve( keywordCount );
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill the specialized "C string" hashtable from keywords[]
|
|
|
|
const KEYWORD* it = keywords;
|
|
|
|
const KEYWORD* end = it + keywordCount;
|
|
|
|
|
|
|
|
for( ; it < end; ++it )
|
|
|
|
{
|
|
|
|
keyword_hash[it->name] = it->token;
|
|
|
|
}
|
|
|
|
#endif
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
2010-10-05 07:15:29 +00:00
|
|
|
|
2010-12-27 21:22:12 +00:00
|
|
|
DSNLEXER::DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount,
|
|
|
|
FILE* aFile, const wxString& aFilename ) :
|
2010-12-28 16:55:28 +00:00
|
|
|
iOwnReaders( true ),
|
2010-03-08 06:18:40 +00:00
|
|
|
keywords( aKeywordTable ),
|
|
|
|
keywordCount( aKeywordCount )
|
|
|
|
{
|
2010-12-28 02:44:30 +00:00
|
|
|
FILE_LINE_READER* fileReader = new FILE_LINE_READER( aFile, aFilename );
|
2010-10-05 07:15:29 +00:00
|
|
|
PushReader( fileReader );
|
2010-03-08 06:18:40 +00:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-27 21:22:12 +00:00
|
|
|
DSNLEXER::DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount,
|
|
|
|
const std::string& aClipboardTxt, const wxString& aSource ) :
|
2010-12-28 16:55:28 +00:00
|
|
|
iOwnReaders( true ),
|
2010-03-08 06:18:40 +00:00
|
|
|
keywords( aKeywordTable ),
|
|
|
|
keywordCount( aKeywordCount )
|
|
|
|
{
|
2010-12-27 21:22:12 +00:00
|
|
|
STRING_LINE_READER* stringReader = new STRING_LINE_READER( aClipboardTxt, aSource.IsEmpty() ?
|
2014-07-07 19:49:14 +00:00
|
|
|
wxString( FMT_CLIPBOARD ) : aSource );
|
2010-10-05 07:15:29 +00:00
|
|
|
PushReader( stringReader );
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-28 16:55:28 +00:00
|
|
|
DSNLEXER::DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount,
|
|
|
|
LINE_READER* aLineReader ) :
|
|
|
|
iOwnReaders( false ),
|
|
|
|
keywords( aKeywordTable ),
|
|
|
|
keywordCount( aKeywordCount )
|
|
|
|
{
|
2011-01-19 20:46:07 +00:00
|
|
|
if( aLineReader )
|
|
|
|
PushReader( aLineReader );
|
2010-12-28 16:55:28 +00:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-07 19:49:14 +00:00
|
|
|
static const KEYWORD empty_keywords[1] = {};
|
|
|
|
|
|
|
|
DSNLEXER::DSNLEXER( const std::string& aSExpression, const wxString& aSource ) :
|
|
|
|
iOwnReaders( true ),
|
|
|
|
keywords( empty_keywords ),
|
|
|
|
keywordCount( 0 )
|
|
|
|
{
|
|
|
|
STRING_LINE_READER* stringReader = new STRING_LINE_READER( aSExpression, aSource.IsEmpty() ?
|
|
|
|
wxString( FMT_CLIPBOARD ) : aSource );
|
|
|
|
PushReader( stringReader );
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-28 16:55:28 +00:00
|
|
|
DSNLEXER::~DSNLEXER()
|
|
|
|
{
|
|
|
|
if( iOwnReaders )
|
|
|
|
{
|
|
|
|
// delete the LINE_READERs from the stack, since I own them.
|
|
|
|
for( READER_STACK::iterator it = readerStack.begin(); it!=readerStack.end(); ++it )
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
void DSNLEXER::SetSpecctraMode( bool aMode )
|
|
|
|
{
|
|
|
|
specctraMode = aMode;
|
|
|
|
if( aMode )
|
|
|
|
{
|
|
|
|
// specctra mode defaults, some of which can still be changed in this mode.
|
|
|
|
space_in_quoted_tokens = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
space_in_quoted_tokens = false;
|
|
|
|
stringDelimiter = '"';
|
|
|
|
}
|
|
|
|
}
|
2010-12-28 16:55:28 +00:00
|
|
|
|
2014-12-04 19:15:26 +00:00
|
|
|
|
|
|
|
bool DSNLEXER::SyncLineReaderWith( DSNLEXER& aLexer )
|
|
|
|
{
|
|
|
|
// Synchronize the pointers handling the data read by the LINE_READER
|
|
|
|
// only if aLexer shares the same LINE_READER, because only in this case
|
2014-12-11 12:00:59 +00:00
|
|
|
// the char buffer is be common
|
2014-12-04 19:15:26 +00:00
|
|
|
|
|
|
|
if( reader != aLexer.reader )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We can synchronize the pointers which handle the data currently read
|
|
|
|
start = aLexer.start;
|
|
|
|
next = aLexer.next;
|
|
|
|
limit = aLexer.limit;
|
|
|
|
|
|
|
|
// Sync these parameters is not mandatory, but could help
|
|
|
|
// for instance in debug
|
|
|
|
curText = aLexer.curText;
|
|
|
|
curOffset = aLexer.curOffset;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-05 07:15:29 +00:00
|
|
|
void DSNLEXER::PushReader( LINE_READER* aLineReader )
|
|
|
|
{
|
|
|
|
readerStack.push_back( aLineReader );
|
|
|
|
reader = aLineReader;
|
2011-01-12 04:51:29 +00:00
|
|
|
start = (const char*) (*reader);
|
2010-10-05 13:38:15 +00:00
|
|
|
|
|
|
|
// force a new readLine() as first thing.
|
2010-10-21 13:21:32 +00:00
|
|
|
limit = start;
|
|
|
|
next = start;
|
2010-10-05 07:15:29 +00:00
|
|
|
}
|
2010-03-08 06:18:40 +00:00
|
|
|
|
|
|
|
|
2011-01-19 20:46:07 +00:00
|
|
|
LINE_READER* DSNLEXER::PopReader()
|
2010-10-05 07:15:29 +00:00
|
|
|
{
|
2011-01-19 20:46:07 +00:00
|
|
|
LINE_READER* ret = 0;
|
|
|
|
|
|
|
|
if( readerStack.size() )
|
2010-10-05 13:38:15 +00:00
|
|
|
{
|
2011-01-19 20:46:07 +00:00
|
|
|
ret = reader;
|
2010-10-05 13:38:15 +00:00
|
|
|
readerStack.pop_back();
|
|
|
|
|
2011-01-19 20:46:07 +00:00
|
|
|
if( readerStack.size() )
|
|
|
|
{
|
|
|
|
reader = readerStack.back();
|
|
|
|
start = reader->Line();
|
|
|
|
|
|
|
|
// force a new readLine() as first thing.
|
|
|
|
limit = start;
|
|
|
|
next = start;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader = 0;
|
|
|
|
start = dummy;
|
|
|
|
limit = dummy;
|
|
|
|
limit = dummy;
|
|
|
|
}
|
2010-10-05 13:38:15 +00:00
|
|
|
}
|
2011-01-19 20:46:07 +00:00
|
|
|
return ret;
|
2010-03-08 06:18:40 +00:00
|
|
|
}
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 01:05:40 +00:00
|
|
|
#if 0
|
|
|
|
static int compare( const void* a1, const void* a2 )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2013-07-01 01:05:40 +00:00
|
|
|
const KEYWORD* k1 = (const KEYWORD*) a1;
|
|
|
|
const KEYWORD* k2 = (const KEYWORD*) a2;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 01:05:40 +00:00
|
|
|
int ret = strcmp( k1->name, k2->name );
|
|
|
|
return ret;
|
|
|
|
}
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 01:05:40 +00:00
|
|
|
int DSNLEXER::findToken( const std::string& tok )
|
|
|
|
{
|
2009-12-11 04:55:24 +00:00
|
|
|
KEYWORD search;
|
|
|
|
|
2013-07-01 01:05:40 +00:00
|
|
|
search.name = tok.c_str();
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
const KEYWORD* findings = (const KEYWORD*) bsearch( &search,
|
|
|
|
keywords, keywordCount,
|
|
|
|
sizeof(KEYWORD), compare );
|
|
|
|
if( findings )
|
|
|
|
return findings->token;
|
|
|
|
else
|
2013-07-01 06:47:36 +00:00
|
|
|
return DSN_SYMBOL; // not a keyword, some arbitrary symbol.
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
2013-07-01 01:05:40 +00:00
|
|
|
#else
|
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
inline int DSNLEXER::findToken( const std::string& tok )
|
2013-07-01 01:05:40 +00:00
|
|
|
{
|
|
|
|
KEYWORD_MAP::const_iterator it = keyword_hash.find( tok.c_str() );
|
2013-07-01 06:47:36 +00:00
|
|
|
if( it != keyword_hash.end() )
|
|
|
|
return it->second;
|
2013-07-01 01:05:40 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
return DSN_SYMBOL; // not a keyword, some arbitrary symbol.
|
2013-07-01 01:05:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
const char* DSNLEXER::Syntax( int aTok )
|
|
|
|
{
|
|
|
|
const char* ret;
|
|
|
|
|
|
|
|
switch( aTok )
|
|
|
|
{
|
|
|
|
case DSN_NONE:
|
|
|
|
ret = "NONE";
|
|
|
|
break;
|
2009-12-11 15:51:26 +00:00
|
|
|
case DSN_STRING_QUOTE:
|
|
|
|
ret = "string_quote"; // a special DSN syntax token, see specctra spec.
|
|
|
|
break;
|
2009-12-11 04:55:24 +00:00
|
|
|
case DSN_QUOTE_DEF:
|
|
|
|
ret = "quoted text delimiter";
|
|
|
|
break;
|
|
|
|
case DSN_DASH:
|
|
|
|
ret = "-";
|
|
|
|
break;
|
|
|
|
case DSN_SYMBOL:
|
|
|
|
ret = "symbol";
|
|
|
|
break;
|
|
|
|
case DSN_NUMBER:
|
|
|
|
ret = "number";
|
|
|
|
break;
|
|
|
|
case DSN_RIGHT:
|
|
|
|
ret = ")";
|
|
|
|
break;
|
|
|
|
case DSN_LEFT:
|
|
|
|
ret = "(";
|
|
|
|
break;
|
|
|
|
case DSN_STRING:
|
|
|
|
ret = "quoted string";
|
|
|
|
break;
|
|
|
|
case DSN_EOF:
|
2011-01-19 20:46:07 +00:00
|
|
|
ret = "end of input";
|
2009-12-11 04:55:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = "???";
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* DSNLEXER::GetTokenText( int aTok )
|
|
|
|
{
|
|
|
|
const char* ret;
|
|
|
|
|
|
|
|
if( aTok < 0 )
|
|
|
|
{
|
|
|
|
return Syntax( aTok );
|
|
|
|
}
|
|
|
|
else if( (unsigned) aTok < keywordCount )
|
|
|
|
{
|
|
|
|
ret = keywords[aTok].name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = "token too big";
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-17 16:30:10 +00:00
|
|
|
wxString DSNLEXER::GetTokenString( int aTok )
|
|
|
|
{
|
|
|
|
wxString ret;
|
|
|
|
|
2011-01-01 22:56:07 +00:00
|
|
|
ret << wxT("'") << wxString::FromUTF8( GetTokenText(aTok) ) << wxT("'");
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-18 16:00:16 +00:00
|
|
|
bool DSNLEXER::IsSymbol( int aTok )
|
|
|
|
{
|
|
|
|
// This is static and not inline to reduce code space.
|
|
|
|
|
|
|
|
// if aTok is >= 0, then it is a coincidental match to a keyword.
|
|
|
|
return aTok==DSN_SYMBOL
|
|
|
|
|| aTok==DSN_STRING
|
|
|
|
|| aTok>=0
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
void DSNLEXER::Expecting( int aTok ) throw( IO_ERROR )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2013-07-01 06:47:36 +00:00
|
|
|
wxString errText = wxString::Format(
|
|
|
|
_("Expecting '%s'"), GetChars( GetTokenString( aTok ) ) );
|
2011-01-01 22:56:07 +00:00
|
|
|
THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
2010-06-17 16:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-19 20:46:07 +00:00
|
|
|
void DSNLEXER::Expecting( const char* text ) throw( IO_ERROR )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2013-07-01 06:47:36 +00:00
|
|
|
wxString errText = wxString::Format(
|
|
|
|
_("Expecting '%s'"), GetChars( wxString::FromUTF8( text ) ) );
|
2011-01-01 22:56:07 +00:00
|
|
|
THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
2010-06-17 16:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
void DSNLEXER::Unexpected( int aTok ) throw( IO_ERROR )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2013-07-01 06:47:36 +00:00
|
|
|
wxString errText = wxString::Format(
|
|
|
|
_("Unexpected '%s'"), GetChars( GetTokenString( aTok ) ) );
|
2011-01-01 22:56:07 +00:00
|
|
|
THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
2010-06-17 16:30:10 +00:00
|
|
|
}
|
|
|
|
|
2011-01-19 20:46:07 +00:00
|
|
|
|
2011-01-01 22:56:07 +00:00
|
|
|
void DSNLEXER::Duplicate( int aTok ) throw( IO_ERROR )
|
|
|
|
{
|
2012-12-08 23:58:03 +00:00
|
|
|
wxString errText = wxString::Format(
|
|
|
|
_("%s is a duplicate"), GetTokenString( aTok ).GetData() );
|
2011-01-01 22:56:07 +00:00
|
|
|
THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
|
|
|
}
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2011-01-19 20:46:07 +00:00
|
|
|
|
|
|
|
void DSNLEXER::Unexpected( const char* text ) throw( IO_ERROR )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2013-07-01 06:47:36 +00:00
|
|
|
wxString errText = wxString::Format(
|
|
|
|
_("Unexpected '%s'"), GetChars( wxString::FromUTF8( text ) ) );
|
2011-01-01 22:56:07 +00:00
|
|
|
THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
2010-06-17 16:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
void DSNLEXER::NeedLEFT() throw( IO_ERROR )
|
2010-06-18 16:00:16 +00:00
|
|
|
{
|
|
|
|
int tok = NextTok();
|
|
|
|
if( tok != DSN_LEFT )
|
|
|
|
Expecting( DSN_LEFT );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
void DSNLEXER::NeedRIGHT() throw( IO_ERROR )
|
2010-06-18 16:00:16 +00:00
|
|
|
{
|
|
|
|
int tok = NextTok();
|
|
|
|
if( tok != DSN_RIGHT )
|
|
|
|
Expecting( DSN_RIGHT );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
int DSNLEXER::NeedSYMBOL() throw( IO_ERROR )
|
2010-06-18 16:00:16 +00:00
|
|
|
{
|
|
|
|
int tok = NextTok();
|
|
|
|
if( !IsSymbol( tok ) )
|
|
|
|
Expecting( DSN_SYMBOL );
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
int DSNLEXER::NeedSYMBOLorNUMBER() throw( IO_ERROR )
|
2010-06-18 16:00:16 +00:00
|
|
|
{
|
|
|
|
int tok = NextTok();
|
|
|
|
if( !IsSymbol( tok ) && tok!=DSN_NUMBER )
|
2011-01-19 20:46:07 +00:00
|
|
|
Expecting( "symbol|number" );
|
2010-06-18 16:00:16 +00:00
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2010-12-27 21:22:12 +00:00
|
|
|
|
2011-02-14 06:39:51 +00:00
|
|
|
int DSNLEXER::NeedNUMBER( const char* aExpectation ) throw( IO_ERROR )
|
|
|
|
{
|
|
|
|
int tok = NextTok();
|
|
|
|
if( tok != DSN_NUMBER )
|
|
|
|
{
|
2012-12-08 23:58:03 +00:00
|
|
|
wxString errText = wxString::Format(
|
|
|
|
_("need a NUMBER for '%s'"), wxString::FromUTF8( aExpectation ).GetData() );
|
2011-02-14 06:39:51 +00:00
|
|
|
THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
/**
|
2011-06-07 15:29:01 +00:00
|
|
|
* Function isSpace
|
2012-10-08 19:34:46 +00:00
|
|
|
* tests for whitespace. Our whitespace, by our definition, is a subset of ASCII,
|
|
|
|
* i.e. no bytes with MSB on can be considered whitespace, since they are likely part
|
|
|
|
* of a multibyte UTF8 character.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2013-06-03 13:09:37 +00:00
|
|
|
static bool isSpace( char cc )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2013-06-03 13:09:37 +00:00
|
|
|
// cc is signed, so it is often negative.
|
2012-10-08 20:01:17 +00:00
|
|
|
// Treat negative as large positive to exclude rapidly.
|
2013-06-03 13:09:37 +00:00
|
|
|
if( (unsigned char) cc <= ' ' )
|
2012-10-08 19:34:46 +00:00
|
|
|
{
|
2013-07-09 05:22:08 +00:00
|
|
|
switch( (unsigned char) cc )
|
2012-10-08 19:34:46 +00:00
|
|
|
{
|
|
|
|
case ' ':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\t':
|
|
|
|
case '\0': // PCAD s-expression files have this.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-03 13:09:37 +00:00
|
|
|
inline bool isDigit( char cc )
|
|
|
|
{
|
|
|
|
return '0' <= cc && cc <= '9';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// return true if @a cc is an s-expression separator character
|
|
|
|
inline bool isSep( char cc )
|
|
|
|
{
|
|
|
|
return isSpace( cc ) || cc=='(' || cc==')';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-31 00:49:22 +00:00
|
|
|
/**
|
|
|
|
* Function isNumber
|
2013-06-03 13:09:37 +00:00
|
|
|
* returns true if the next sequence of text is a number:
|
|
|
|
* either an integer, fixed point, or float with exponent. Stops scanning
|
|
|
|
* at the first non-number character, even if it is not whitespace.
|
2013-05-31 00:49:22 +00:00
|
|
|
*
|
|
|
|
* @param cp is the start of the current token.
|
2013-07-01 06:47:36 +00:00
|
|
|
* @param limit is the end of the current token.
|
|
|
|
*
|
|
|
|
* @return bool - true if input token is a number, else false.
|
2013-05-31 00:49:22 +00:00
|
|
|
*/
|
2013-07-01 06:47:36 +00:00
|
|
|
static bool isNumber( const char* cp, const char* limit )
|
2013-05-31 00:49:22 +00:00
|
|
|
{
|
2013-06-03 13:09:37 +00:00
|
|
|
// regex for a float: "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?" i.e. any number,
|
|
|
|
// code traversal manually here:
|
2013-05-31 00:49:22 +00:00
|
|
|
|
|
|
|
bool sawNumber = false;
|
|
|
|
|
2013-06-03 13:09:37 +00:00
|
|
|
if( cp < limit && ( *cp=='-' || *cp=='+' ) )
|
2013-05-31 00:49:22 +00:00
|
|
|
++cp;
|
|
|
|
|
2013-06-03 13:09:37 +00:00
|
|
|
while( cp < limit && isDigit( *cp ) )
|
2013-05-31 00:49:22 +00:00
|
|
|
{
|
|
|
|
++cp;
|
|
|
|
sawNumber = true;
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:24:56 +00:00
|
|
|
if( cp < limit && *cp == '.' )
|
2013-05-31 00:49:22 +00:00
|
|
|
{
|
2013-05-31 14:24:56 +00:00
|
|
|
++cp;
|
2013-05-31 00:49:22 +00:00
|
|
|
|
2013-06-03 13:09:37 +00:00
|
|
|
while( cp < limit && isDigit( *cp ) )
|
2013-05-31 00:49:22 +00:00
|
|
|
{
|
|
|
|
++cp;
|
2013-05-31 14:24:56 +00:00
|
|
|
sawNumber = true;
|
2013-05-31 00:49:22 +00:00
|
|
|
}
|
2013-05-31 14:24:56 +00:00
|
|
|
}
|
2013-05-31 00:49:22 +00:00
|
|
|
|
2013-05-31 14:24:56 +00:00
|
|
|
if( sawNumber )
|
|
|
|
{
|
2013-06-03 13:09:37 +00:00
|
|
|
if( cp < limit && ( *cp=='E' || *cp=='e' ) )
|
2013-05-31 00:49:22 +00:00
|
|
|
{
|
|
|
|
++cp;
|
|
|
|
|
|
|
|
sawNumber = false; // exponent mandates at least one digit thereafter.
|
|
|
|
|
2013-06-03 13:09:37 +00:00
|
|
|
if( cp < limit && ( *cp=='-' || *cp=='+' ) )
|
2013-05-31 00:49:22 +00:00
|
|
|
++cp;
|
|
|
|
|
2013-06-03 13:09:37 +00:00
|
|
|
while( cp < limit && isDigit( *cp ) )
|
2013-05-31 00:49:22 +00:00
|
|
|
{
|
|
|
|
++cp;
|
|
|
|
sawNumber = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 14:37:03 +00:00
|
|
|
return sawNumber && cp==limit;
|
2013-05-31 00:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
int DSNLEXER::NextTok() throw( IO_ERROR )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2011-01-12 04:51:29 +00:00
|
|
|
const char* cur = next;
|
|
|
|
const char* head = cur;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
prevTok = curTok;
|
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
if( curTok == DSN_EOF )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if( cur >= limit )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
L_read:
|
2013-07-01 06:47:36 +00:00
|
|
|
// blank lines are returned as "\n" and will have a len of 1.
|
|
|
|
// EOF will have a len of 0 and so is detectable.
|
|
|
|
int len = readLine();
|
|
|
|
if( len == 0 )
|
|
|
|
{
|
|
|
|
cur = start; // after readLine(), since start can change, set cur offset to start
|
|
|
|
curTok = DSN_EOF;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = start; // after readLine() since start can change.
|
|
|
|
|
|
|
|
// skip leading whitespace
|
|
|
|
while( cur<limit && isSpace( *cur ) )
|
|
|
|
++cur;
|
|
|
|
|
|
|
|
// If the first non-blank character is #, this line is a comment.
|
|
|
|
// Comments cannot follow any other token on the same line.
|
|
|
|
if( cur<limit && *cur=='#' )
|
|
|
|
{
|
|
|
|
if( commentsAreTokens )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2013-07-01 06:47:36 +00:00
|
|
|
// Grab the entire current line [excluding end of line char(s)] as the
|
|
|
|
// current token. The '#' character may not be at offset zero.
|
|
|
|
|
|
|
|
while( limit[-1] == '\n' || limit[-1] == '\r' )
|
|
|
|
--limit;
|
|
|
|
|
|
|
|
curText.clear();
|
|
|
|
curText.append( start, limit );
|
|
|
|
|
|
|
|
cur = start; // ensure a good curOffset below
|
|
|
|
curTok = DSN_COMMENT;
|
|
|
|
head = limit; // do a readLine() on next call in here.
|
2009-12-11 04:55:24 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2013-07-01 06:47:36 +00:00
|
|
|
else
|
|
|
|
goto L_read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// skip leading whitespace
|
|
|
|
while( cur<limit && isSpace( *cur ) )
|
|
|
|
++cur;
|
|
|
|
}
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
if( cur >= limit )
|
|
|
|
goto L_read;
|
2011-01-05 21:21:32 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
if( *cur == '(' )
|
|
|
|
{
|
|
|
|
curText = *cur;
|
|
|
|
curTok = DSN_LEFT;
|
|
|
|
head = cur+1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *cur == ')' )
|
|
|
|
{
|
|
|
|
curText = *cur;
|
|
|
|
curTok = DSN_RIGHT;
|
|
|
|
head = cur+1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
// Non-specctraMode, understands and deciphers escaped \, \r, \n, and \".
|
|
|
|
// Strips off leading and trailing double quotes
|
|
|
|
if( !specctraMode )
|
|
|
|
{
|
|
|
|
// a quoted string, will return DSN_STRING
|
|
|
|
if( *cur == stringDelimiter )
|
|
|
|
{
|
|
|
|
// copy the token, character by character so we can remove doubled up quotes.
|
|
|
|
curText.clear();
|
|
|
|
|
|
|
|
++cur; // skip over the leading delimiter, which is always " in non-specctraMode
|
|
|
|
|
|
|
|
head = cur;
|
|
|
|
|
|
|
|
while( head<limit )
|
2010-02-20 19:13:30 +00:00
|
|
|
{
|
2013-07-01 06:47:36 +00:00
|
|
|
// ESCAPE SEQUENCES:
|
|
|
|
if( *head =='\\' )
|
2010-02-20 19:13:30 +00:00
|
|
|
{
|
2013-07-01 06:47:36 +00:00
|
|
|
char tbuf[8];
|
|
|
|
char c;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if( ++head >= limit )
|
|
|
|
break; // throw exception at L_unterminated
|
2013-06-23 19:18:33 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
switch( *head++ )
|
|
|
|
{
|
|
|
|
case '"':
|
|
|
|
case '\\': c = head[-1]; break;
|
|
|
|
case 'a': c = '\x07'; break;
|
|
|
|
case 'b': c = '\x08'; break;
|
|
|
|
case 'f': c = '\x0c'; break;
|
|
|
|
case 'n': c = '\n'; break;
|
|
|
|
case 'r': c = '\r'; break;
|
|
|
|
case 't': c = '\x09'; break;
|
|
|
|
case 'v': c = '\x0b'; break;
|
|
|
|
|
|
|
|
case 'x': // 1 or 2 byte hex escape sequence
|
|
|
|
for( i=0; i<2; ++i )
|
|
|
|
{
|
|
|
|
if( !isxdigit( head[i] ) )
|
|
|
|
break;
|
|
|
|
tbuf[i] = head[i];
|
|
|
|
}
|
|
|
|
tbuf[i] = '\0';
|
|
|
|
if( i > 0 )
|
|
|
|
c = (char) strtoul( tbuf, NULL, 16 );
|
|
|
|
else
|
|
|
|
c = 'x'; // a goofed hex escape sequence, interpret as 'x'
|
|
|
|
head += i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // 1-3 byte octal escape sequence
|
|
|
|
--head;
|
|
|
|
for( i=0; i<3; ++i )
|
|
|
|
{
|
|
|
|
if( head[i] < '0' || head[i] > '7' )
|
|
|
|
break;
|
|
|
|
tbuf[i] = head[i];
|
|
|
|
}
|
|
|
|
tbuf[i] = '\0';
|
|
|
|
if( i > 0 )
|
|
|
|
c = (char) strtoul( tbuf, NULL, 8 );
|
|
|
|
else
|
|
|
|
c = '\\'; // a goofed octal escape sequence, interpret as '\'
|
|
|
|
head += i;
|
|
|
|
break;
|
|
|
|
}
|
2013-06-23 19:18:33 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
curText += c;
|
|
|
|
}
|
2013-06-23 19:18:33 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
else if( *head == '"' ) // end of the non-specctraMode DSN_STRING
|
|
|
|
{
|
|
|
|
curTok = DSN_STRING;
|
|
|
|
++head; // omit this trailing double quote
|
2010-02-20 19:13:30 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2013-07-01 06:47:36 +00:00
|
|
|
|
2010-02-20 19:13:30 +00:00
|
|
|
else
|
2013-07-01 06:47:36 +00:00
|
|
|
curText += *head++;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
} // while
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
// L_unterminated:
|
|
|
|
wxString errtxt( _( "Un-terminated delimited string" ) );
|
|
|
|
THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
2012-04-30 02:57:48 +00:00
|
|
|
}
|
2013-07-01 06:47:36 +00:00
|
|
|
}
|
2012-04-30 02:57:48 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
else // is specctraMode, tests in this block should not occur in KiCad mode.
|
|
|
|
{
|
|
|
|
/* get the dash out of a <pin_reference> which is embedded for example
|
|
|
|
like: U2-14 or "U2"-"14"
|
|
|
|
This is detectable by a non-space immediately preceeding the dash.
|
|
|
|
*/
|
|
|
|
if( *cur == '-' && cur>start && !isSpace( cur[-1] ) )
|
2012-04-30 02:57:48 +00:00
|
|
|
{
|
2013-07-01 06:47:36 +00:00
|
|
|
curText = '-';
|
|
|
|
curTok = DSN_DASH;
|
2012-04-30 02:57:48 +00:00
|
|
|
head = cur+1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
// switching the string_quote character
|
|
|
|
if( prevTok == DSN_STRING_QUOTE )
|
|
|
|
{
|
|
|
|
static const wxString errtxt( _("String delimiter must be a single character of ', \", or $"));
|
|
|
|
|
|
|
|
char cc = *cur;
|
|
|
|
switch( cc )
|
|
|
|
{
|
|
|
|
case '\'':
|
|
|
|
case '$':
|
|
|
|
case '"':
|
|
|
|
break;
|
|
|
|
default:
|
2011-01-01 22:56:07 +00:00
|
|
|
THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
2009-12-11 15:51:26 +00:00
|
|
|
curText = cc;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
head = cur+1;
|
|
|
|
|
2013-06-03 13:09:37 +00:00
|
|
|
if( head<limit && !isSep( *head ) )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2011-01-01 22:56:07 +00:00
|
|
|
THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
curTok = DSN_QUOTE_DEF;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
// specctraMode DSN_STRING
|
2009-12-11 04:55:24 +00:00
|
|
|
if( *cur == stringDelimiter )
|
|
|
|
{
|
2013-07-01 06:47:36 +00:00
|
|
|
++cur; // skip over the leading delimiter: ",', or $
|
2011-01-30 19:26:03 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
head = cur;
|
2010-02-20 19:13:30 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
while( head<limit && !isStringTerminator( *head ) )
|
|
|
|
++head;
|
2010-02-20 19:13:30 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
if( head >= limit )
|
|
|
|
{
|
2013-06-03 13:09:37 +00:00
|
|
|
wxString errtxt( _( "Un-terminated delimited string" ) );
|
2011-01-30 19:26:03 +00:00
|
|
|
THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
2010-02-20 19:13:30 +00:00
|
|
|
}
|
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
curText.clear();
|
|
|
|
curText.append( cur, head );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
++head; // skip over the trailing delimiter
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
curTok = DSN_STRING;
|
|
|
|
goto exit;
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
2013-07-01 06:47:36 +00:00
|
|
|
} // specctraMode
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
// non-quoted token, read it into curText.
|
|
|
|
curText.clear();
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
head = cur;
|
|
|
|
while( head<limit && !isSep( *head ) )
|
|
|
|
curText += *head++;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
if( isNumber( curText.c_str(), curText.c_str() + curText.size() ) )
|
|
|
|
{
|
|
|
|
curTok = DSN_NUMBER;
|
|
|
|
goto exit;
|
|
|
|
}
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
if( specctraMode && curText == "string_quote" )
|
|
|
|
{
|
|
|
|
curTok = DSN_STRING_QUOTE;
|
|
|
|
goto exit;
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
2013-07-01 06:47:36 +00:00
|
|
|
curTok = findToken( curText );
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
exit: // single point of exit, no returns elsewhere please.
|
|
|
|
|
2010-10-21 13:21:32 +00:00
|
|
|
curOffset = cur - start;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
next = head;
|
|
|
|
|
|
|
|
// printf("tok:\"%s\"\n", curText.c_str() );
|
|
|
|
return curTok;
|
|
|
|
}
|
|
|
|
|
2013-06-23 19:18:33 +00:00
|
|
|
|
|
|
|
wxArrayString* DSNLEXER::ReadCommentLines() throw( IO_ERROR )
|
|
|
|
{
|
|
|
|
wxArrayString* ret = 0;
|
|
|
|
bool cmt_setting = SetCommentsAreTokens( true );
|
|
|
|
int tok = NextTok();
|
|
|
|
|
|
|
|
if( tok == DSN_COMMENT )
|
|
|
|
{
|
|
|
|
ret = new wxArrayString();
|
|
|
|
|
|
|
|
do
|
2013-07-01 06:47:36 +00:00
|
|
|
{
|
2013-06-23 19:18:33 +00:00
|
|
|
ret->Add( FromUTF8() );
|
2013-07-01 06:47:36 +00:00
|
|
|
}
|
2013-06-23 19:18:33 +00:00
|
|
|
while( ( tok = NextTok() ) == DSN_COMMENT );
|
|
|
|
}
|
|
|
|
|
|
|
|
SetCommentsAreTokens( cmt_setting );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|