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
|
|
|
*
|
|
|
|
* Copyright (C) 2007-2008 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>
|
|
|
|
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <dsnlexer.h>
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2010-12-27 21:22:12 +00:00
|
|
|
//#include "fctsys.h"
|
|
|
|
//#include "pcbnew.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
|
|
|
|
|
|
|
static int compare( const void* a1, const void* a2 )
|
|
|
|
{
|
|
|
|
const KEYWORD* k1 = (const KEYWORD*) a1;
|
|
|
|
const KEYWORD* k2 = (const KEYWORD*) a2;
|
|
|
|
|
|
|
|
int ret = strcmp( k1->name, k2->name );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----<DSNLEXER>-------------------------------------------------------------
|
|
|
|
|
2010-03-08 06:18: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;
|
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() ?
|
|
|
|
wxString( _( "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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
int DSNLEXER::findToken( const std::string& tok )
|
|
|
|
{
|
|
|
|
// convert to lower case once, this should be faster than using strcasecmp()
|
|
|
|
// for each test in compare().
|
|
|
|
lowercase.clear();
|
|
|
|
|
|
|
|
for( std::string::const_iterator iter = tok.begin(); iter!=tok.end(); ++iter )
|
|
|
|
lowercase += (char) tolower( *iter );
|
|
|
|
|
|
|
|
KEYWORD search;
|
|
|
|
|
|
|
|
search.name = lowercase.c_str();
|
|
|
|
|
|
|
|
// a boost hashtable might be a few percent faster, depending on
|
|
|
|
// hashtable size and quality of the hash function.
|
|
|
|
|
|
|
|
const KEYWORD* findings = (const KEYWORD*) bsearch( &search,
|
|
|
|
keywords, keywordCount,
|
|
|
|
sizeof(KEYWORD), compare );
|
|
|
|
if( findings )
|
|
|
|
return findings->token;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
wxString errText( _("Expecting") );
|
|
|
|
errText << wxT(" ") << 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
|
|
|
{
|
|
|
|
wxString errText( _("Expecting") );
|
2011-01-19 20:46:07 +00:00
|
|
|
errText << wxT(" '") << wxString::FromUTF8( text ) << wxT("'");
|
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
|
|
|
{
|
|
|
|
wxString errText( _("Unexpected") );
|
|
|
|
errText << wxT(" ") << 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 )
|
|
|
|
{
|
|
|
|
wxString errText;
|
|
|
|
|
|
|
|
errText.Printf( _("%s is a duplicate"), GetTokenString( aTok ).GetData() );
|
|
|
|
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
|
|
|
{
|
|
|
|
wxString errText( _("Unexpected") );
|
2011-01-19 20:46:07 +00:00
|
|
|
errText << wxT(" '") << wxString::FromUTF8( text ) << wxT("'");
|
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 )
|
|
|
|
{
|
|
|
|
wxString errText;
|
|
|
|
|
|
|
|
errText.Printf( _("need a NUMBER for '%s'"), wxString::FromUTF8( aExpectation ).GetData() );
|
|
|
|
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
|
|
|
*/
|
2012-10-08 19:34:46 +00:00
|
|
|
static bool isSpace( int cc )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2012-10-08 20:01:17 +00:00
|
|
|
// cc was signed extended from signed char, so it is often negative.
|
|
|
|
// Treat negative as large positive to exclude rapidly.
|
|
|
|
if( unsigned( cc ) <= ' ' )
|
2012-10-08 19:34:46 +00:00
|
|
|
{
|
|
|
|
switch( cc )
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if( curTok != DSN_EOF )
|
|
|
|
{
|
|
|
|
if( cur >= limit )
|
|
|
|
{
|
|
|
|
L_read:
|
2009-12-11 15:51:26 +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.
|
2009-12-11 04:55:24 +00:00
|
|
|
int len = readLine();
|
|
|
|
if( len == 0 )
|
|
|
|
{
|
2011-01-05 21:21:32 +00:00
|
|
|
cur = start; // after readLine(), since start can change, set cur offset to start
|
2009-12-11 04:55:24 +00:00
|
|
|
curTok = DSN_EOF;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2011-01-05 21:21:32 +00:00
|
|
|
cur = start; // after readLine() since start can change.
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
// skip leading whitespace
|
|
|
|
while( cur<limit && isSpace(*cur) )
|
|
|
|
++cur;
|
|
|
|
|
2010-02-20 19:13:30 +00:00
|
|
|
// If the first non-blank character is #, this line is a comment.
|
|
|
|
// Comments cannot follow any other token on the same line.
|
2009-12-11 04:55:24 +00:00
|
|
|
if( cur<limit && *cur=='#' )
|
2010-02-20 19:13:30 +00:00
|
|
|
{
|
|
|
|
if( commentsAreTokens )
|
|
|
|
{
|
|
|
|
// save the entire line, including new line as the current token.
|
|
|
|
// the '#' character may not be at offset zero.
|
2010-10-21 13:21:32 +00:00
|
|
|
curText = start; // entire line is the token
|
|
|
|
cur = start; // ensure a good curOffset below
|
2010-02-20 19:13:30 +00:00
|
|
|
curTok = DSN_COMMENT;
|
|
|
|
head = limit; // do a readLine() on next call in here.
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto L_read;
|
|
|
|
}
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// skip leading whitespace
|
|
|
|
while( cur<limit && isSpace(*cur) )
|
|
|
|
++cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cur >= limit )
|
|
|
|
goto L_read;
|
|
|
|
|
2012-04-30 02:57:48 +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
|
|
|
// 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;
|
|
|
|
|
|
|
|
if( head<limit && *head!=')' && *head!='(' && !isSpace(*head) )
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2010-10-21 13:21:32 +00:00
|
|
|
if( *cur == '-' && cur>start && !isSpace( cur[-1] ) )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2009-12-11 15:51:26 +00:00
|
|
|
curText = '-';
|
2009-12-11 04:55:24 +00:00
|
|
|
curTok = DSN_DASH;
|
2009-12-11 15:51:26 +00:00
|
|
|
head = cur+1;
|
2009-12-11 04:55:24 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle DSN_NUMBER
|
|
|
|
if( strchr( "+-.0123456789", *cur ) )
|
|
|
|
{
|
|
|
|
head = cur+1;
|
|
|
|
while( head<limit && strchr( ".0123456789", *head ) )
|
|
|
|
++head;
|
|
|
|
|
|
|
|
if( (head<limit && isSpace(*head)) || *head==')' || *head=='(' || head==limit )
|
|
|
|
{
|
|
|
|
curText.clear();
|
|
|
|
curText.append( cur, head );
|
|
|
|
curTok = DSN_NUMBER;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else it was something like +5V, fall through below
|
|
|
|
}
|
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
// a quoted string, will return DSN_STRING
|
2009-12-11 04:55:24 +00:00
|
|
|
if( *cur == stringDelimiter )
|
|
|
|
{
|
2011-01-30 19:26:03 +00:00
|
|
|
// Non-specctraMode, understands and deciphers escaped \, \r, \n, and \".
|
|
|
|
// Strips off leading and trailing double quotes
|
|
|
|
if( !specctraMode )
|
|
|
|
{
|
|
|
|
// copy the token, character by character so we can remove doubled up quotes.
|
|
|
|
curText.clear();
|
2010-02-20 19:13:30 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
++cur; // skip over the leading delimiter, which is always " in non-specctraMode
|
2010-02-20 19:13:30 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
head = cur;
|
|
|
|
|
|
|
|
while( head<limit )
|
2010-02-20 19:13:30 +00:00
|
|
|
{
|
2011-01-30 19:26:03 +00:00
|
|
|
// ESCAPE SEQUENCES:
|
|
|
|
if( *head =='\\' )
|
2010-02-20 19:13:30 +00:00
|
|
|
{
|
2011-01-30 19:26:03 +00:00
|
|
|
char tbuf[8];
|
|
|
|
char c;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if( ++head >= limit )
|
|
|
|
break; // throw exception at L_unterminated
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
curText += c;
|
2010-02-20 19:13:30 +00:00
|
|
|
}
|
2011-01-30 19:26:03 +00:00
|
|
|
|
|
|
|
else if( *head == '"' ) // end of the non-specctraMode DSN_STRING
|
2010-02-20 19:13:30 +00:00
|
|
|
{
|
2011-01-30 19:26:03 +00:00
|
|
|
curTok = DSN_STRING;
|
|
|
|
++head; // omit this trailing double quote
|
|
|
|
goto exit;
|
2010-02-20 19:13:30 +00:00
|
|
|
}
|
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
else
|
|
|
|
curText += *head++;
|
2010-02-20 19:13:30 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
} // while
|
2010-02-20 19:13:30 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
// L_unterminated:
|
|
|
|
wxString errtxt(_("Un-terminated delimited string") );
|
|
|
|
THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
2010-02-20 19:13:30 +00:00
|
|
|
}
|
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
else // specctraMode DSN_STRING
|
|
|
|
{
|
|
|
|
++cur; // skip over the leading delimiter: ",', or $
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
head = cur;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
while( head<limit && !isStringTerminator( *head ) )
|
|
|
|
++head;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
if( head >= limit )
|
|
|
|
{
|
|
|
|
wxString errtxt(_("Un-terminated delimited string") );
|
|
|
|
THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
|
|
|
|
}
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
curText.clear();
|
|
|
|
curText.append( cur, head );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
++head; // skip over the trailing delimiter
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
curTok = DSN_STRING;
|
|
|
|
goto exit;
|
|
|
|
}
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe it is a token we will find in the token table.
|
|
|
|
// If not, then call it a DSN_SYMBOL.
|
|
|
|
{
|
|
|
|
head = cur+1;
|
|
|
|
while( head<limit && !isSpace( *head ) && *head!=')' && *head!='(' )
|
|
|
|
++head;
|
|
|
|
|
|
|
|
curText.clear();
|
|
|
|
curText.append( cur, head );
|
|
|
|
|
|
|
|
int found = findToken( curText );
|
|
|
|
|
|
|
|
if( found != -1 )
|
|
|
|
curTok = found;
|
|
|
|
|
|
|
|
else if( 0 == curText.compare( "string_quote" ) )
|
|
|
|
curTok = DSN_STRING_QUOTE;
|
|
|
|
|
|
|
|
else // unrecogized token, call it a symbol
|
|
|
|
curTok = DSN_SYMBOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|