much more of sweet parser

This commit is contained in:
Dick Hollenbeck 2011-02-27 02:16:05 -06:00
parent bc16d9d7c3
commit 03495698cf
6 changed files with 409 additions and 36 deletions

View File

@ -20,15 +20,7 @@ int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize )
while( (cc = *aSource++) != 0 && aDest < limit ) while( (cc = *aSource++) != 0 && aDest < limit )
{ {
if( cc == '\\' ) if( cc == '"' )
{
cc = *aSource++;
if( inside )
*aDest++ = cc;
}
else if( cc == '"' )
{ {
if( inside ) if( inside )
break; // 2nd double quote is end of delimited text break; // 2nd double quote is end of delimited text
@ -38,7 +30,19 @@ int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize )
else if( inside ) else if( inside )
{ {
*aDest++ = cc; if( cc == '\\' )
{
cc = *aSource++;
// do no copy the escape byte if it is followed by \ or "
if( cc != '"' && cc != '\\' )
*aDest++ = '\\';
if( aDest < limit )
*aDest++ = cc;
}
else
*aDest++ = cc;
} }
} }

View File

@ -8,6 +8,10 @@ PARTS="eyes ears feet"
REVS="rev1 rev5 rev10" REVS="rev1 rev5 rev10"
LINE="(line (pts (xy 12 13)(xy 12 20))(line_width 1.5))"
RECT="(rectangle (start 4 5)(end 6 8)(line_width 2.3)(fill transparent))"
CIRCLE="(circle (center 1 0)(radius 5)(line_width 2.1)(fill none))"
ARC="(arc (pos 22 33)(radius 12)(start 2 4)(end 13 33)(line_width 2.3)(fill filled))"
for C in ${CATEGORIES}; do for C in ${CATEGORIES}; do
@ -16,10 +20,10 @@ for C in ${CATEGORIES}; do
for P in ${PARTS}; do for P in ${PARTS}; do
for R in ${REVS}; do for R in ${REVS}; do
echo "(part $C/$P (value 22)(footprint SM0805))" > $BASEDIR/$C/$P.part.$R echo "(part $C/$P (value 22)(footprint SM0805)(model Airplane)$LINE$RECT$CIRCLE$ARC)" > $BASEDIR/$C/$P.part.$R
done done
# also make the part without a rev: # also make the part without a rev:
echo "(part $C/$P (value 22)(footprint SM0805))" > $BASEDIR/$C/$P.part echo "(part $C/$P (value 22)(footprint SM0805)(model Airplane)$LINE$RECT$CIRCLE$ARC)" > $BASEDIR/$C/$P.part
done done
done done

View File

@ -34,6 +34,7 @@ public:
virtual ~BASE_GRAPHIC() {} virtual ~BASE_GRAPHIC() {}
}; };
typedef std::vector<POINT> POINTS;
class POLY_LINE : BASE_GRAPHIC class POLY_LINE : BASE_GRAPHIC
{ {
@ -41,8 +42,8 @@ class POLY_LINE : BASE_GRAPHIC
friend class SWEET_PARSER; friend class SWEET_PARSER;
protected: protected:
double width; double lineWidth;
std::vector<POINT> pts; POINTS pts;
public: public:
POLY_LINE( PART* aOwner ) : POLY_LINE( PART* aOwner ) :
@ -51,9 +52,64 @@ public:
}; };
class RECTANGLE : BASE_GRAPHIC
{
friend class PART;
friend class SWEET_PARSER;
protected:
double lineWidth;
int fillType; // T_none, T_filled, or T_transparent
POINT start;
POINT end;
public:
RECTANGLE( PART* aOwner ) :
BASE_GRAPHIC( aOwner )
{}
}; };
class CIRCLE : BASE_GRAPHIC
{
friend class PART;
friend class SWEET_PARSER;
protected:
double lineWidth;
int fillType; // T_none, T_filled, or T_transparent
POINT center;
int radius;
public:
CIRCLE( PART* aOwner ) :
BASE_GRAPHIC( aOwner )
{}
};
class ARC : BASE_GRAPHIC
{
friend class PART;
friend class SWEET_PARSER;
protected:
double lineWidth;
int fillType; // T_none, T_filled, or T_transparent
POINT pos;
int radius;
POINT start;
POINT end;
public:
ARC( PART* aOwner ) :
BASE_GRAPHIC( aOwner )
{}
};
} // namespace SCH
//-----</temporary home for PART sub objects, move after stable>----------------- //-----</temporary home for PART sub objects, move after stable>-----------------
@ -133,7 +189,12 @@ protected: // not likely to have C++ descendants, but protected none-the-le
/// Alternate body forms. /// Alternate body forms.
//ALTERNATES alternates; //ALTERNATES alternates;
// lots of other stuff, like the mandatory properties, but no units, since we went with dimensionless // mandatory properties
wxString value;
wxString footprint;
wxString model;
wxString keywords;
public: public:
@ -160,6 +221,33 @@ public:
*/ */
void Parse( SWEET_PARSER* aParser, LIB_TABLE* aLibTable ) throw( IO_ERROR, PARSE_ERROR ); void Parse( SWEET_PARSER* aParser, LIB_TABLE* aLibTable ) throw( IO_ERROR, PARSE_ERROR );
void SetValue( const wxString& aValue )
{
value = aValue;
}
const wxString& GetValue()
{
return value;
}
void SetFootprint( const wxString& aFootprint )
{
footprint = aFootprint;
}
const wxString& GetFootprint()
{
return footprint;
}
void SetModel( const wxString& aModel )
{
model = aModel;
}
const wxString& GetModel()
{
return model;
}
/* /*
void SetBody( const STR_UTF& aSExpression ) void SetBody( const STR_UTF& aSExpression )
{ {

View File

@ -27,6 +27,8 @@
#include <sch_lib_table.h> #include <sch_lib_table.h>
#include <sch_lpid.h> #include <sch_lpid.h>
#include <macros.h> // FROM_UTF8()
using namespace SCH; using namespace SCH;
using namespace PR; using namespace PR;
@ -203,29 +205,66 @@ void SWEET_PARSER::Parse( PART* me, LIB_TABLE* aTable ) throw( IO_ERROR, PARSE_E
break; break;
case T_line: case T_line:
case T_polyline:
POLY_LINE* pl; POLY_LINE* pl;
pl = new POLY_LINE( me ); pl = new POLY_LINE( me );
me->graphics.push_back( pl ); me->graphics.push_back( pl );
parsePolyLine( pl ); parsePolyLine( pl );
break; break;
case T_rectangle:
RECTANGLE* rect;
rect = new RECTANGLE( me );
me->graphics.push_back( rect );
parseRectangle( rect );
break;
case T_circle:
CIRCLE* circ;
circ = new CIRCLE( me );
me->graphics.push_back( circ );
parseCircle( circ );
break;
case T_arc:
ARC* arc;
arc = new ARC( me );
me->graphics.push_back( arc );
parseArc( arc );
break;
/*
case T_value: case T_value:
if( contains & PB(VALUE) ) if( contains & PB(VALUE) )
Duplicate( tok ); Duplicate( tok );
contains |= PB(VALUE); contains |= PB(VALUE);
NeedSYMBOLorNUMBER(); NeedSYMBOLorNUMBER();
// me->value = CurText(); me->SetValue( FROM_UTF8( CurText() ) );
// @todo handle optional (effects..) here
NeedRIGHT(); NeedRIGHT();
break; break;
case T_footprint: case T_footprint:
if( contains & PB(FOOTPRINT) )
Duplicate( tok );
contains |= PB(FOOTPRINT);
NeedSYMBOLorNUMBER();
me->SetFootprint( FROM_UTF8( CurText() ) );
// @todo handle optional (effects..) here
NeedRIGHT();
break; break;
case T_model: case T_model:
if( contains & PB(MODEL) )
Duplicate( tok );
contains |= PB(MODEL);
NeedSYMBOLorNUMBER();
me->SetModel( FROM_UTF8( CurText() ) );
// @todo handle optional (effects..) here
NeedRIGHT();
break; break;
/*
case T_keywords: case T_keywords:
break; break;
@ -256,18 +295,6 @@ void SWEET_PARSER::Parse( PART* me, LIB_TABLE* aTable ) throw( IO_ERROR, PARSE_E
case T_route_pin_swap: case T_route_pin_swap:
break; break;
case T_polyline:
break;
case T_rectangle:
break;
case T_circle:
break;
case T_arc:
break;
case T_bezier: case T_bezier:
break; break;
@ -299,14 +326,15 @@ void SWEET_PARSER::parsePolyLine( POLY_LINE* me )
NeedLEFT(); NeedLEFT();
while( ( tok = NextTok() ) != T_RIGHT ) while( ( tok = NextTok() ) != T_RIGHT )
{ {
NeedLEFT(); if( tok == T_LEFT )
tok = NextTok();
tok = NextTok();
switch( tok ) switch( tok )
{ {
case T_line_width: case T_line_width:
NeedNUMBER( "line_width" ); NeedNUMBER( "line_width" );
me->width = strtod( CurText(), NULL ); me->lineWidth = strtod( CurText(), NULL );
NeedRIGHT();
break; break;
case T_pts: case T_pts:
@ -319,13 +347,20 @@ void SWEET_PARSER::parsePolyLine( POLY_LINE* me )
if( tok != T_xy ) if( tok != T_xy )
Expecting( T_xy ); Expecting( T_xy );
/* @todo resume here, its late me->pts.push_back( POINT() );
NeedNUMBER();
*/ NeedNUMBER( "x" );
me->pts.back().x = internal( CurText() );
NeedNUMBER( "y" );
me->pts.back().y = internal( CurText() );
NeedRIGHT();
} }
break; break;
case T_fill: case T_fill:
// @todo figure this out, maybe spit into polygon
break; break;
default: default:
@ -334,3 +369,238 @@ void SWEET_PARSER::parsePolyLine( POLY_LINE* me )
} }
} }
void SWEET_PARSER::parseRectangle( RECTANGLE* me )
{
T tok;
bool sawStart = false;
bool sawEnd = false;
bool sawWidth = false;
bool sawFill = false;
NeedLEFT();
while( ( tok = NextTok() ) != T_RIGHT )
{
if( tok == T_LEFT )
tok = NextTok();
switch( tok )
{
case T_line_width:
if( sawWidth )
Duplicate( tok );
NeedNUMBER( "line_width" );
me->lineWidth = strtod( CurText(), NULL );
sawWidth = true;
NeedRIGHT();
break;
case T_fill:
if( sawFill )
Duplicate( tok );
tok = NeedSYMBOL();
switch( tok )
{
case T_none:
case T_filled:
case T_transparent:
me->fillType = tok;
break;
default:
Expecting( "none|filled|transparent" );
}
NeedRIGHT();
sawFill = true;
break;
case T_start:
if( sawStart )
Duplicate( tok );
NeedNUMBER( "x" );
me->start.x = internal( CurText() );
NeedNUMBER( "y" );
me->start.y = internal( CurText() );
NeedRIGHT();
sawStart = true;
break;
case T_end:
if( sawEnd )
Duplicate( tok );
NeedNUMBER( "x" );
me->end.x = internal( CurText() );
NeedNUMBER( "y" );
me->end.y = internal( CurText() );
NeedRIGHT();
sawEnd = true;
break;
default:
Expecting( "start|end|line_width|fill" );
}
}
}
void SWEET_PARSER::parseCircle( CIRCLE* me )
{
T tok;
bool sawCenter = false;
bool sawRadius = false;
bool sawWidth = false;
bool sawFill = false;
NeedLEFT();
while( ( tok = NextTok() ) != T_RIGHT )
{
if( tok == T_LEFT )
tok = NextTok();
switch( tok )
{
case T_line_width:
if( sawWidth )
Duplicate( tok );
NeedNUMBER( "line_width" );
me->lineWidth = strtod( CurText(), NULL );
sawWidth = true;
NeedRIGHT();
break;
case T_fill:
if( sawFill )
Duplicate( tok );
tok = NeedSYMBOL();
switch( tok )
{
case T_none:
case T_filled:
case T_transparent:
me->fillType = tok;
break;
default:
Expecting( "none|filled|transparent" );
}
NeedRIGHT();
sawFill = true;
break;
case T_center:
if( sawCenter )
Duplicate( tok );
NeedNUMBER( "center x" );
me->center.x = internal( CurText() );
NeedNUMBER( "center y" );
me->center.y = internal( CurText() );
NeedRIGHT();
sawCenter = true;
break;
case T_radius:
if( sawRadius )
Duplicate( tok );
NeedNUMBER( "radius" );
me->radius = internal( CurText() );
NeedRIGHT();
sawRadius = true;
break;
default:
Expecting( "center|radius|line_width|fill" );
}
}
}
void SWEET_PARSER::parseArc( ARC* me )
{
T tok;
bool sawPos = false;
bool sawStart = false;
bool sawEnd = false;
bool sawRadius = false;
bool sawWidth = false;
bool sawFill = false;
NeedLEFT();
while( ( tok = NextTok() ) != T_RIGHT )
{
if( tok == T_LEFT )
tok = NextTok();
switch( tok )
{
case T_line_width:
if( sawWidth )
Duplicate( tok );
NeedNUMBER( "line_width" );
me->lineWidth = strtod( CurText(), NULL );
sawWidth = true;
NeedRIGHT();
break;
case T_fill:
if( sawFill )
Duplicate( tok );
tok = NeedSYMBOL();
switch( tok )
{
case T_none:
case T_filled:
case T_transparent:
me->fillType = tok;
break;
default:
Expecting( "none|filled|transparent" );
}
NeedRIGHT();
sawFill = true;
break;
case T_pos:
if( sawPos )
Duplicate( tok );
NeedNUMBER( "pos x" );
me->pos.x = internal( CurText() );
NeedNUMBER( "pos y" );
me->pos.y = internal( CurText() );
NeedRIGHT();
sawPos = true;
break;
case T_radius:
if( sawRadius )
Duplicate( tok );
NeedNUMBER( "radius" );
me->radius = internal( CurText() );
NeedRIGHT();
sawRadius = true;
break;
case T_start:
if( sawStart )
Duplicate( tok );
NeedNUMBER( "start x" );
me->start.x = internal( CurText() );
NeedNUMBER( "start y" );
me->start.y = internal( CurText() );
NeedRIGHT();
sawStart = true;
break;
case T_end:
if( sawEnd )
Duplicate( tok );
NeedNUMBER( "end x" );
me->end.x = internal( CurText() );
NeedNUMBER( "end y" );
me->end.y = internal( CurText() );
NeedRIGHT();
sawEnd = true;
break;
default:
Expecting( "center|radius|line_width|fill" );
}
}
}

View File

@ -34,6 +34,9 @@ namespace SCH {
class LIB_TABLE; class LIB_TABLE;
class PART; class PART;
class POLY_LINE; class POLY_LINE;
class RECTANGLE;
class CIRCLE;
class ARC;
/** /**
@ -54,7 +57,9 @@ class SWEET_PARSER : public SWEET_LEXER
void parseExtends( PART* me ); void parseExtends( PART* me );
void parsePolyLine( POLY_LINE* me ); void parsePolyLine( POLY_LINE* me );
void parseRectangle( RECTANGLE* me );
void parseCircle( CIRCLE* me );
void parseArc( ARC* me );
public: public:

View File

@ -14,6 +14,7 @@ end
end_angle end_angle
extends extends
fill fill
filled
font font
footprint footprint
input input
@ -56,6 +57,7 @@ size
start start
start_angle start_angle
text text
transparent
tristate tristate
unconnected unconnected
units units