Commit caf5fc8d authored by Dick Hollenbeck's avatar Dick Hollenbeck

DSNLEXER::NextTok() organizes the specctraMode code better, into one if block mostly.

This keeps it out of the KiCad mode path, making that leaner and less confusing about
what is supported in KiCad mode.  Within KiCad mode, treat quoted vs. non-quoted tokens
as the two general categories, with non-quoted having sub-categories.  Eliminate  
an unimplemented, unused function declaration in DSNLEXER.
 
Improve the output formatting of THROW_PARSE_ERROR().
parent 44d31a18
...@@ -196,19 +196,18 @@ int DSNLEXER::findToken( const std::string& tok ) ...@@ -196,19 +196,18 @@ int DSNLEXER::findToken( const std::string& tok )
if( findings ) if( findings )
return findings->token; return findings->token;
else else
return -1; return DSN_SYMBOL; // not a keyword, some arbitrary symbol.
} }
#else #else
int DSNLEXER::findToken( const std::string& tok ) inline int DSNLEXER::findToken( const std::string& tok )
{ {
KEYWORD_MAP::const_iterator it = keyword_hash.find( tok.c_str() ); KEYWORD_MAP::const_iterator it = keyword_hash.find( tok.c_str() );
if( it == keyword_hash.end() ) if( it != keyword_hash.end() )
return -1;
return it->second; return it->second;
return DSN_SYMBOL; // not a keyword, some arbitrary symbol.
} }
#endif #endif
...@@ -300,25 +299,24 @@ bool DSNLEXER::IsSymbol( int aTok ) ...@@ -300,25 +299,24 @@ bool DSNLEXER::IsSymbol( int aTok )
void DSNLEXER::Expecting( int aTok ) throw( IO_ERROR ) void DSNLEXER::Expecting( int aTok ) throw( IO_ERROR )
{ {
wxString errText; wxString errText = wxString::Format(
errText.Printf( _("Expecting '%s'"), GetChars( GetTokenString( aTok ) ) ); _("Expecting '%s'"), GetChars( GetTokenString( aTok ) ) );
THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() ); THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
} }
void DSNLEXER::Expecting( const char* text ) throw( IO_ERROR ) void DSNLEXER::Expecting( const char* text ) throw( IO_ERROR )
{ {
wxString errText; wxString errText = wxString::Format(
errText.Printf( _("Expecting '%s'"), _("Expecting '%s'"), GetChars( wxString::FromUTF8( text ) ) );
GetChars( wxString::FromUTF8( text ) ) );
THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() ); THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
} }
void DSNLEXER::Unexpected( int aTok ) throw( IO_ERROR ) void DSNLEXER::Unexpected( int aTok ) throw( IO_ERROR )
{ {
wxString errText; wxString errText = wxString::Format(
errText.Printf( _("Unexpected '%s'"), GetChars( GetTokenString( aTok ) ) ); _("Unexpected '%s'"), GetChars( GetTokenString( aTok ) ) );
THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() ); THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
} }
...@@ -333,9 +331,8 @@ void DSNLEXER::Duplicate( int aTok ) throw( IO_ERROR ) ...@@ -333,9 +331,8 @@ void DSNLEXER::Duplicate( int aTok ) throw( IO_ERROR )
void DSNLEXER::Unexpected( const char* text ) throw( IO_ERROR ) void DSNLEXER::Unexpected( const char* text ) throw( IO_ERROR )
{ {
wxString errText; wxString errText = wxString::Format(
errText.Printf( _("Unexpected '%s'"), _("Unexpected '%s'"), GetChars( wxString::FromUTF8( text ) ) );
GetChars( wxString::FromUTF8( text ) ) );
THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() ); THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
} }
...@@ -433,11 +430,11 @@ inline bool isSep( char cc ) ...@@ -433,11 +430,11 @@ inline bool isSep( char cc )
* at the first non-number character, even if it is not whitespace. * at the first non-number character, even if it is not whitespace.
* *
* @param cp is the start of the current token. * @param cp is the start of the current token.
* @param limit is the end of the current line of text. * @param limit is the end of the current token.
* @return const char* - if initial text was a number, then pointer to the text *
* after this number, else NULL if initial text was not a number. * @return bool - true if input token is a number, else false.
*/ */
static const char* isNumber( const char* cp, const char* limit ) static bool isNumber( const char* cp, const char* limit )
{ {
// regex for a float: "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?" i.e. any number, // regex for a float: "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?" i.e. any number,
// code traversal manually here: // code traversal manually here:
...@@ -483,7 +480,7 @@ static const char* isNumber( const char* cp, const char* limit ) ...@@ -483,7 +480,7 @@ static const char* isNumber( const char* cp, const char* limit )
} }
} }
return sawNumber ? cp : NULL; return sawNumber;
} }
...@@ -494,8 +491,9 @@ int DSNLEXER::NextTok() throw( IO_ERROR ) ...@@ -494,8 +491,9 @@ int DSNLEXER::NextTok() throw( IO_ERROR )
prevTok = curTok; prevTok = curTok;
if( curTok != DSN_EOF ) if( curTok == DSN_EOF )
{ goto exit;
if( cur >= limit ) if( cur >= limit )
{ {
L_read: L_read:
...@@ -565,63 +563,12 @@ L_read: ...@@ -565,63 +563,12 @@ L_read:
goto exit; goto exit;
} }
// 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:
THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
}
curText = cc;
head = cur+1;
if( head<limit && !isSep( *head ) )
{
THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
}
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.
*/
if( *cur == '-' && cur>start && !isSpace( cur[-1] ) )
{
curText = '-';
curTok = DSN_DASH;
head = cur+1;
goto exit;
}
if( ( head = isNumber( cur, limit ) ) != NULL &&
// token can only be a number if trailing text is a separator or line end
( head==limit || isSep( *head ) ) )
{
curText.clear();
curText.append( cur, head );
curTok = DSN_NUMBER;
goto exit;
}
// a quoted string, will return DSN_STRING
if( *cur == stringDelimiter )
{
// Non-specctraMode, understands and deciphers escaped \, \r, \n, and \". // Non-specctraMode, understands and deciphers escaped \, \r, \n, and \".
// Strips off leading and trailing double quotes // Strips off leading and trailing double quotes
if( !specctraMode ) 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. // copy the token, character by character so we can remove doubled up quotes.
curText.clear(); curText.clear();
...@@ -705,8 +652,53 @@ L_read: ...@@ -705,8 +652,53 @@ L_read:
wxString errtxt( _( "Un-terminated delimited string" ) ); wxString errtxt( _( "Un-terminated delimited string" ) );
THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() ); THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
} }
}
else // specctraMode DSN_STRING 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] ) )
{
curText = '-';
curTok = DSN_DASH;
head = cur+1;
goto exit;
}
// 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:
THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
}
curText = cc;
head = cur+1;
if( head<limit && !isSep( *head ) )
{
THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
}
curTok = DSN_QUOTE_DEF;
goto exit;
}
// specctraMode DSN_STRING
if( *cur == stringDelimiter )
{ {
++cur; // skip over the leading delimiter: ",', or $ ++cur; // skip over the leading delimiter: ",', or $
...@@ -729,31 +721,29 @@ L_read: ...@@ -729,31 +721,29 @@ L_read:
curTok = DSN_STRING; curTok = DSN_STRING;
goto exit; goto exit;
} }
} } // specctraMode
// 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 && !isSep( *head ) )
++head;
// non-quoted token, read it into curText.
curText.clear(); curText.clear();
curText.append( cur, head );
int found = findToken( curText ); head = cur;
while( head<limit && !isSep( *head ) )
curText += *head++;
if( found != -1 ) if( isNumber( curText.c_str(), curText.c_str() + curText.size() ) )
curTok = found; {
curTok = DSN_NUMBER;
goto exit;
}
else if( 0 == curText.compare( "string_quote" ) ) if( specctraMode && curText == "string_quote" )
{
curTok = DSN_STRING_QUOTE; curTok = DSN_STRING_QUOTE;
goto exit;
else // unrecogized token, call it a symbol
curTok = DSN_SYMBOL;
}
} }
curTok = findToken( curText );
exit: // single point of exit, no returns elsewhere please. exit: // single point of exit, no returns elsewhere please.
curOffset = cur - start; curOffset = cur - start;
...@@ -776,7 +766,9 @@ wxArrayString* DSNLEXER::ReadCommentLines() throw( IO_ERROR ) ...@@ -776,7 +766,9 @@ wxArrayString* DSNLEXER::ReadCommentLines() throw( IO_ERROR )
ret = new wxArrayString(); ret = new wxArrayString();
do do
{
ret->Add( FromUTF8() ); ret->Add( FromUTF8() );
}
while( ( tok = NextTok() ) == DSN_COMMENT ); while( ( tok = NextTok() ) == DSN_COMMENT );
} }
......
...@@ -134,19 +134,6 @@ protected: ...@@ -134,19 +134,6 @@ protected:
return 0; return 0;
} }
/**
* Function readLineOrCmt
* reads a line from the LINE_READER and returns either:
* <ol>
* <li> a positive line length (a +1 if empty line)
* <li> zero of end of file.
* <li> DSN_COMMENT if the line is a comment
* </ol>
*/
int readLineOrCmt();
/** /**
* Function findToken * Function findToken
* takes a string and looks up the string in the list of expected * takes a string and looks up the string in the list of expected
...@@ -154,7 +141,8 @@ protected: ...@@ -154,7 +141,8 @@ protected:
* *
* @param tok A string holding the token text to lookup, in an * @param tok A string holding the token text to lookup, in an
* unpredictable case: uppercase or lowercase * unpredictable case: uppercase or lowercase
* @return int - DSN_T or -1 if argument string is not a recognized token. * @return int - DSN_T matching the keyword text, or DSN_SYMBOL if argument
* string is not a recognized token.
*/ */
int findToken( const std::string& tok ); int findToken( const std::string& tok );
...@@ -168,6 +156,8 @@ protected: ...@@ -168,6 +156,8 @@ protected:
return false; return false;
} }
#endif #endif
public: public:
......
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
#define IO_FORMAT _( "IO_ERROR: %s\nfrom %s : %s" ) #define IO_FORMAT _( "IO_ERROR: %s\nfrom %s : %s" )
#define PARSE_FORMAT _( "PARSE_ERROR: %s in input/source \"%s\", line %d, offset %d\nfrom %s : %s" ) #define PARSE_FORMAT _( "PARSE_ERROR: %s in input/source\n'%s'\nline %d\noffset %d\nfrom %s : %s" )
// references: // references:
// http://stackoverflow.com/questions/2670816/how-can-i-use-the-compile-time-constant-line-in-a-string // http://stackoverflow.com/questions/2670816/how-can-i-use-the-compile-time-constant-line-in-a-string
......
...@@ -50,6 +50,9 @@ ...@@ -50,6 +50,9 @@
#include <class_board.h> #include <class_board.h>
#include <build_version.h> // LEGACY_BOARD_FILE_VERSION #include <build_version.h> // LEGACY_BOARD_FILE_VERSION
//#define USE_INSTRUMENTATION true
#define USE_INSTRUMENTATION false
static const wxString backupFileExtensionSuffix( wxT( "-bak" ) ); static const wxString backupFileExtensionSuffix( wxT( "-bak" ) );
static const wxString autosaveFilePrefix( wxT( "_autosave-" ) ); static const wxString autosaveFilePrefix( wxT( "_autosave-" ) );
...@@ -283,7 +286,7 @@ bool PCB_EDIT_FRAME::LoadOnePcbFile( const wxString& aFileName, bool aAppend, ...@@ -283,7 +286,7 @@ bool PCB_EDIT_FRAME::LoadOnePcbFile( const wxString& aFileName, bool aAppend,
props["page_width"] = wxString::Format( wxT( "%d" ), GetPageSizeIU().x ); props["page_width"] = wxString::Format( wxT( "%d" ), GetPageSizeIU().x );
props["page_height"] = wxString::Format( wxT( "%d" ), GetPageSizeIU().y ); props["page_height"] = wxString::Format( wxT( "%d" ), GetPageSizeIU().y );
#if 0 #if USE_INSTRUMENTATION
// measure the time to load a BOARD. // measure the time to load a BOARD.
unsigned startTime = GetRunningMicroSecs(); unsigned startTime = GetRunningMicroSecs();
#endif #endif
...@@ -291,7 +294,7 @@ bool PCB_EDIT_FRAME::LoadOnePcbFile( const wxString& aFileName, bool aAppend, ...@@ -291,7 +294,7 @@ bool PCB_EDIT_FRAME::LoadOnePcbFile( const wxString& aFileName, bool aAppend,
// load or append either: // load or append either:
loadedBoard = pi->Load( GetBoard()->GetFileName(), aAppend ? GetBoard() : NULL, &props ); loadedBoard = pi->Load( GetBoard()->GetFileName(), aAppend ? GetBoard() : NULL, &props );
#if 0 #if USE_INSTRUMENTATION
unsigned stopTime = GetRunningMicroSecs(); unsigned stopTime = GetRunningMicroSecs();
printf( "PLUGIN::Load(): %u usecs\n", stopTime - startTime ); printf( "PLUGIN::Load(): %u usecs\n", stopTime - startTime );
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment