kicad/new/sch_part.cpp

359 lines
9.1 KiB
C++
Raw Normal View History

2010-12-27 16:49:39 +00:00
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2010 Kicad Developers, see change_log.txt for contributors.
*
* 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
*/
2011-01-01 22:56:43 +00:00
#include <wx/wx.h> // _()
2010-12-27 16:49:39 +00:00
#include <sch_part.h>
#include <sweet_lexer.h>
2011-01-01 22:56:43 +00:00
#include <sch_lpid.h>
#include <sch_lib_table.h>
2010-12-27 16:49:39 +00:00
using namespace SCH;
2011-01-03 17:47:08 +00:00
#define MAX_INHERITANCE_NESTING 10 // no problem going larger
2011-01-01 22:56:43 +00:00
2011-01-03 17:47:08 +00:00
//-----<temporary home for PART sub objects, move after stable>------------------
struct XY {};
struct AT {};
2011-01-01 22:56:43 +00:00
2011-01-04 05:20:27 +00:00
class POLY_LINE
{
};
2011-01-01 22:56:43 +00:00
//-----</temporary home for PART sub objects, move after stable>-----------------
/**
* Class PART_PARSER
* is a localized/hidden PART Parser. You get here through the public interface
* PART::Parse(). Advantages of private class declaration in this situation:
* 1) keeps all the recursive parsing helper functions out of the main public PART
* header file and so should speed up compilation.
* 2) Allows use of cost-less Java like inline functions, since nobody knows about
* them but this source file. Most are only used once and called from one place.
* <p>
* All the functions in this class throw PARSE_ERROR. If SWEET_LEXER throws, it
* may be an IO_ERROR, propogated from here also. The throws() statements are left off
* to keep the noise level down.
*/
class PART_PARSER
2010-12-27 16:49:39 +00:00
{
2011-01-01 22:56:43 +00:00
SWEET_LEXER* in;
LIB_TABLE* libs;
2011-01-03 17:47:08 +00:00
int contains; // separate from PART::contains until done
// so we can see what we inherited from base PART
2011-01-01 22:56:43 +00:00
public:
PART_PARSER( PART* aPart, SWEET_LEXER* aLexer, LIB_TABLE* aTable ) :
in( aLexer ),
libs( aTable ),
contains( 0 )
{
parsePart( aPart );
}
2011-01-03 17:47:08 +00:00
void parseXY( XY* me )
{
}
void parseAt( AT* me )
{
}
void parseExtends( PART* me )
{
PART* base;
int offset;
if( contains & PB(EXTENDS) )
in->Duplicate( T_extends );
in->NeedSYMBOLorNUMBER();
me->setExtends( new LPID() );
offset = me->extends->Parse( in->CurText() );
if( offset > -1 ) // -1 is success
THROW_PARSE_ERROR( _("invalid extends LPID"),
in->CurSource(),
in->CurLine(),
in->CurLineNumber(),
in->CurOffset() + offset );
base = libs->LookupPart( *me->extends, me->Owner() );
// we could be going in circles here, recursively, or too deep, set limits
// and disallow extending from self (even indirectly)
int extendsDepth = 0;
for( PART* ancestor = base; ancestor && extendsDepth<MAX_INHERITANCE_NESTING;
++extendsDepth, ancestor = ancestor->base )
{
if( ancestor == me )
{
THROW_PARSE_ERROR( _("'extends' may not have self as any ancestor"),
in->CurSource(),
in->CurLine(),
in->CurLineNumber(),
in->CurOffset() );
}
}
if( extendsDepth == MAX_INHERITANCE_NESTING )
{
THROW_PARSE_ERROR( _("max allowed extends depth exceeded"),
in->CurSource(),
in->CurLine(),
in->CurLineNumber(),
in->CurOffset() );
}
me->inherit( *base );
me->base = base;
contains |= PB(EXTENDS);
}
2011-01-01 22:56:43 +00:00
/// @param me = ja mir, the object getting stuffed, from its perspective
void parsePart( PART* me )
{
2011-01-04 05:20:27 +00:00
PART_T tok;
2011-01-01 22:56:43 +00:00
2011-01-04 05:20:27 +00:00
#if 0
2011-01-01 22:56:43 +00:00
// Be flexible regarding the starting point of the stream.
// Caller may not have read the first two tokens out of the
// stream: T_LEFT and T_part, so ignore them if seen here.
// The 1st two tokens T_LEFT and T_part are then optional in the grammar.
2011-01-03 17:47:08 +00:00
2011-01-04 05:20:27 +00:00
if( (tok = in->NextTok() ) == T_LEFT )
2011-01-01 22:56:43 +00:00
{
2011-01-03 17:47:08 +00:00
if( ( tok = in->NextTok() ) != T_part )
in->Expecting( T_part );
2011-01-01 22:56:43 +00:00
}
2011-01-04 05:20:27 +00:00
#else
// "( part" are not optional
in->NeedLEFT();
if( ( tok = in->NextTok() ) != T_part )
in->Expecting( T_part );
#endif
2011-01-03 17:47:08 +00:00
in->NeedSYMBOLorNUMBER(); // read in part NAME_HINT, and toss
tok = in->NextTok();
2011-01-01 22:56:43 +00:00
// extends must be _first_ thing, if it is present at all, after NAME_HINT
if( tok == T_extends )
{
2011-01-03 17:47:08 +00:00
parseExtends( me );
2011-01-01 22:56:43 +00:00
tok = in->NextTok();
}
2011-01-03 17:47:08 +00:00
for( ; tok!=T_RIGHT; tok = in->NextTok() )
2011-01-01 22:56:43 +00:00
{
2011-01-03 17:47:08 +00:00
if( tok==T_EOF )
in->Unexpected( _( "end of input" ) );
2011-01-01 22:56:43 +00:00
if( tok == T_LEFT )
tok = in->NextTok();
switch( tok )
{
default:
// describe what we expect at this level
in->Expecting( wxT(
"anchor|value|footprint|model|keywords|alternates\n"
"|property\n"
" |property_del\n"
"|pin\n"
" |pin_merge|pin_swap|pin_renum|pin_rename|route_pin_swap\n"
"|polyline|line|rectangle|circle|arc|bezier|text"
)
);
break;
case T_anchor:
if( contains & PB(ANCHOR) )
in->Duplicate( tok );
contains |= PB(ANCHOR);
break;
2011-01-04 05:20:27 +00:00
case T_line:
break;
2011-01-01 22:56:43 +00:00
/*
case T_value:
if( contains & PB(VALUE) )
in->Duplicate( tok );
contains |= PB(VALUE);
in->NeedSYMBOLorNUMBER();
// me->value = in->CurText();
in->NeedRIGHT();
break;
case T_footprint:
break;
case T_model:
break;
case T_keywords:
break;
case T_alternates:
break;
case T_property:
break;
case T_property_del:
break;
case T_pin:
break;
case T_pin_merge:
break;
case T_pin_swap:
break;
case T_pin_renum:
break;
case T_pin_rename:
break;
case T_route_pin_swap:
break;
case T_polyline:
break;
case T_rectangle:
break;
2011-01-01 22:56:43 +00:00
case T_circle:
break;
case T_arc:
break;
case T_bezier:
break;
case T_text:
break;
*/
// Not sure about reference in a PART, comes in at COMPONENT object.
// It is maybe just a hint here or a prefix.
case T_reference:
if( contains & PB(REFERENCE) )
in->Duplicate( tok );
contains |= PB(REFERENCE);
break;
}
}
2011-01-03 17:47:08 +00:00
contains |= PB(PARSED);
2011-01-01 22:56:43 +00:00
2011-01-04 05:20:27 +00:00
me->contains |= contains;
2011-01-01 22:56:43 +00:00
}
2011-01-03 17:47:08 +00:00
2011-01-01 22:56:43 +00:00
};
2011-01-03 17:47:08 +00:00
PART::PART( LIB* aOwner, const STRING& aPartNameAndRev ) :
2011-01-01 22:56:43 +00:00
owner( aOwner ),
contains( 0 ),
2011-01-03 17:47:08 +00:00
partNameAndRev( aPartNameAndRev ),
extends( 0 ),
base( 0 )
{
// Our goal is to have class LIB only instantiate what is needed, so print here
// what it is doing. It is the only class where PART can be instantiated.
D(printf("PART::PART(%s)\n", aPartNameAndRev.c_str() );)
}
2011-01-01 22:56:43 +00:00
PART::~PART()
{
delete extends;
}
void PART::setExtends( LPID* aLPID )
{
delete extends;
extends = aLPID;
2010-12-27 16:49:39 +00:00
}
2011-01-01 22:56:43 +00:00
void PART::inherit( const PART& other )
{
contains = other.contains;
2011-01-03 17:47:08 +00:00
// @todo copy the inherited drawables, properties, and pins here
2011-01-01 22:56:43 +00:00
}
PART& PART::operator=( const PART& other )
{
2011-01-03 17:47:08 +00:00
owner = other.owner;
partNameAndRev = other.partNameAndRev;
body = other.body;
base = other.base;
setExtends( other.extends ? new LPID( *other.extends ) : 0 );
2011-01-01 22:56:43 +00:00
2011-01-03 17:47:08 +00:00
// maintain in concert with inherit(), which is a partial assignment operator.
2011-01-01 22:56:43 +00:00
inherit( other );
return *this;
}
void PART::Parse( SWEET_LEXER* aLexer, LIB_TABLE* aTable ) throw( IO_ERROR )
{
PART_PARSER( this, aLexer, aTable );
}
#if 0 && defined(DEBUG)
2010-12-27 16:49:39 +00:00
int main( int argc, char** argv )
{
return 0;
}
#endif