From ee5f9034f792e08d5e766d3ebdf7faeb9d6ed2e4 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Thu, 26 Aug 2021 19:38:20 +0200 Subject: [PATCH] pcb parser: ensure the parser is reinitialized before parsing a new fp file Because the same parser is used to read all footprint files of a library, the parser must be reinitialized (internal variable cleared) before reading a new file, otherwise the previous parser state is applied to the next file. Fixes #7627 https://gitlab.com/kicad/code/kicad/issues/7627 --- common/dsnlexer.cpp | 11 +++++++++++ include/dsnlexer.h | 7 +++++++ pcbnew/plugins/kicad/kicad_plugin.cpp | 2 ++ pcbnew/plugins/kicad/pcb_parser.cpp | 3 +++ 4 files changed, 23 insertions(+) diff --git a/common/dsnlexer.cpp b/common/dsnlexer.cpp index a70130a6c7..dc11ba4de2 100644 --- a/common/dsnlexer.cpp +++ b/common/dsnlexer.cpp @@ -146,6 +146,7 @@ DSNLEXER::~DSNLEXER() } } + void DSNLEXER::SetSpecctraMode( bool aMode ) { specctraMode = aMode; @@ -162,6 +163,16 @@ void DSNLEXER::SetSpecctraMode( bool aMode ) } +void DSNLEXER::InitParserState() +{ + curTok = DSN_NONE; + prevTok = DSN_NONE; + commentsAreTokens = false; + + curOffset = 0; +} + + bool DSNLEXER::SyncLineReaderWith( DSNLEXER& aLexer ) { // Synchronize the pointers handling the data read by the LINE_READER diff --git a/include/dsnlexer.h b/include/dsnlexer.h index d9f5a24bf9..26724d9298 100644 --- a/include/dsnlexer.h +++ b/include/dsnlexer.h @@ -131,6 +131,13 @@ public: virtual ~DSNLEXER(); + /** + * Reinit variables used during parsing, to ensure od states are not used in a new parsing + * must be called before parsing a new file after parsing an old file to avoid + * starting with some variables in a non initial state + */ + void InitParserState(); + /** * Usable only for DSN lexers which share the same #LINE_READER. * diff --git a/pcbnew/plugins/kicad/kicad_plugin.cpp b/pcbnew/plugins/kicad/kicad_plugin.cpp index bc77a6f313..1028bddb52 100644 --- a/pcbnew/plugins/kicad/kicad_plugin.cpp +++ b/pcbnew/plugins/kicad/kicad_plugin.cpp @@ -265,6 +265,8 @@ void FP_CACHE::Load() FILE_LINE_READER reader( fn.GetFullPath() ); m_owner->m_parser->SetLineReader( &reader ); + // Ensure a previous parsing does not interacts with the new parsing: + m_owner->m_parser->InitParserState(); FOOTPRINT* footprint = (FOOTPRINT*) m_owner->m_parser->Parse(); wxString fpName = fn.GetName(); diff --git a/pcbnew/plugins/kicad/pcb_parser.cpp b/pcbnew/plugins/kicad/pcb_parser.cpp index cbc6d8e4b6..58ee5bc9a0 100644 --- a/pcbnew/plugins/kicad/pcb_parser.cpp +++ b/pcbnew/plugins/kicad/pcb_parser.cpp @@ -637,6 +637,9 @@ BOARD_ITEM* PCB_PARSER::Parse() token = CurTok(); + if( token == -1 ) // EOF + Unexpected( token ); + if( token != T_LEFT ) Expecting( T_LEFT );