KiCad PCB EDA Suite
sch_sexpr_parser.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2020 CERN
5  *
6  * @author Wayne Stambaugh <stambaughw@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #ifndef __SCH_SEXPR_PARSER_H__
32 #define __SCH_SEXPR_PARSER_H__
33 
34 #include <convert_to_biu.h> // IU_PER_MM
35 #include <math/util.h> // KiROUND, Clamp
36 
37 #include <class_library.h>
38 #include <schematic_lexer.h>
39 #include <sch_file_versions.h>
40 #include <default_values.h> // For some default values
41 
42 
43 class LIB_ARC;
44 class LIB_BEZIER;
45 class LIB_CIRCLE;
46 class LIB_ITEM;
47 class LIB_PIN;
48 class LIB_POLYLINE;
49 class LIB_TEXT;
50 class PAGE_INFO;
51 class SCH_BITMAP;
52 class SCH_BUS_WIRE_ENTRY;
53 class SCH_COMPONENT;
54 class SCH_FIELD;
55 class SCH_JUNCTION;
56 class SCH_LINE;
57 class SCH_NO_CONNECT;
58 class SCH_SCREEN;
59 class SCH_SHEET;
60 class SCH_SHEET_PIN;
61 class SCH_TEXT;
62 class TITLE_BLOCK;
63 
64 
69 {
70 public:
71  int m_Width;
73  COLOR4D m_Color;
74 
75  STROKE_PARAMS( int aWidth = Mils2iu( DEFAULT_LINE_THICKNESS ),
77  const COLOR4D& aColor = COLOR4D::UNSPECIFIED ) :
78  m_Width( aWidth ),
79  m_Type( aType ),
80  m_Color( aColor )
81  {
82  }
83 };
84 
85 
90 {
91 public:
93  COLOR4D m_Color;
94 };
95 
96 
100 class SCH_SEXPR_PARSER : public SCHEMATIC_LEXER
101 {
103  int m_fieldId;
104  int m_unit;
105  int m_convert;
106  wxString m_symbolName;
107 
108  void parseHeader( TSCHEMATIC_T::T aHeaderType, int aFileVersion );
109 
110  inline long parseHex()
111  {
112  NextTok();
113  return strtol( CurText(), NULL, 16 );
114  }
115 
116  inline int parseInt()
117  {
118  return (int)strtol( CurText(), NULL, 10 );
119  }
120 
121  inline int parseInt( const char* aExpected )
122  {
123  NeedNUMBER( aExpected );
124  return parseInt();
125  }
126 
134  double parseDouble();
135 
136  inline double parseDouble( const char* aExpected )
137  {
138  NeedNUMBER( aExpected );
139  return parseDouble();
140  }
141 
142  inline double parseDouble( TSCHEMATIC_T::T aToken )
143  {
144  return parseDouble( GetTokenText( aToken ) );
145  }
146 
147  inline int parseInternalUnits()
148  {
149  auto retval = parseDouble() * IU_PER_MM;
150 
151  // Schematic internal units are represented as integers. Any values that are
152  // larger or smaller than the schematic units represent undefined behavior for
153  // the system. Limit values to the largest that can be displayed on the screen.
154  double int_limit = std::numeric_limits<int>::max() * 0.7071; // 0.7071 = roughly 1/sqrt(2)
155 
156  return KiROUND( Clamp<double>( -int_limit, retval, int_limit ) );
157  }
158 
159  inline int parseInternalUnits( const char* aExpected )
160  {
161  auto retval = parseDouble( aExpected ) * IU_PER_MM;
162 
163  double int_limit = std::numeric_limits<int>::max() * 0.7071;
164 
165  return KiROUND( Clamp<double>( -int_limit, retval, int_limit ) );
166  }
167 
168  inline int parseInternalUnits( TSCHEMATIC_T::T aToken )
169  {
170  return parseInternalUnits( GetTokenText( aToken ) );
171  }
172 
173  inline wxPoint parseXY()
174  {
175  wxPoint xy;
176 
177  xy.x = parseInternalUnits( "X coordinate" );
178  xy.y = parseInternalUnits( "Y coordinate" );
179 
180  return xy;
181  }
182 
188  void parseStroke( STROKE_PARAMS& aStroke );
189 
190  void parseFill( FILL_PARAMS& aFill );
191 
192  void parseEDA_TEXT( EDA_TEXT* aText );
193  void parsePinNames( std::unique_ptr<LIB_PART>& aSymbol );
194 
195  void parseProperty( std::unique_ptr<LIB_PART>& aSymbol );
196 
197  LIB_ARC* parseArc();
200  LIB_PIN* parsePin();
203  LIB_TEXT* parseText();
204 
205  void parsePAGE_INFO( PAGE_INFO& aPageInfo );
206  void parseTITLE_BLOCK( TITLE_BLOCK& aTitleBlock );
207  void parseSchSymbolInstances( SCH_SCREEN* aScreen );
208 
210  SCH_FIELD* parseSchField( SCH_ITEM* aParent );
217  SCH_LINE* parseLine();
219  void parseBusAlias( SCH_SCREEN* aScreen );
220 
221 public:
222  SCH_SEXPR_PARSER( LINE_READER* aLineReader = nullptr );
223 
224  void ParseLib( LIB_PART_MAP& aSymbolLibMap );
225 
226  LIB_PART* ParseSymbol( LIB_PART_MAP& aSymbolLibMap,
227  int aFileVersion = SEXPR_SYMBOL_LIB_FILE_VERSION );
228 
230 
248  void ParseSchematic( SCH_SHEET* aSheet, bool aIsCopyablyOnly = false,
249  int aFileVersion = SEXPR_SCHEMATIC_FILE_VERSION );
250 
254  bool IsTooRecent() const;
255 };
256 
257 #endif // __SCH_SEXPR_PARSER_H__
int m_fieldId
The current field ID.
void ParseLib(LIB_PART_MAP &aSymbolLibMap)
LIB_PART * ParseSymbol(LIB_PART_MAP &aSymbolLibMap, int aFileVersion=SEXPR_SYMBOL_LIB_FILE_VERSION)
SCH_FIELD instances are attached to a component and provide a place for the component's value,...
Definition: sch_field.h:52
LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived to re...
Definition: richio.h:81
SCH_SEXPR_PARSER(LINE_READER *aLineReader=nullptr)
void parseProperty(std::unique_ptr< LIB_PART > &aSymbol)
void parseEDA_TEXT(EDA_TEXT *aText)
Define a symbol library graphical text item.
Definition: lib_text.h:40
SCH_SHEET_PIN * parseSchSheetPin(SCH_SHEET *aSheet)
void parseBusAlias(SCH_SCREEN *aScreen)
LIB_POLYLINE * parsePolyLine()
std::map< wxString, LIB_PART *, LibPartMapSort > LIB_PART_MAP
Part map used by part library object.
int m_unit
The current unit being parsed.
void parseTITLE_BLOCK(TITLE_BLOCK &aTitleBlock)
#define SEXPR_SYMBOL_LIB_FILE_VERSION
This file contains the file format version information for the s-expression schematic and symbol libr...
TITLE_BLOCK holds the information shown in the lower right corner of a plot, printout,...
Definition: title_block.h:40
void parseSchSymbolInstances(SCH_SCREEN *aScreen)
SCH_BUS_WIRE_ENTRY * parseBusEntry()
LIB_RECTANGLE * parseRectangle()
The base class for drawable items used by schematic library components.
Definition: lib_item.h:61
int parseInternalUnits(const char *aExpected)
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:112
Simple container to manage fill parameters.
void parseHeader(TSCHEMATIC_T::T aHeaderType, int aFileVersion)
#define NULL
wxString m_symbolName
The current symbol name.
SCH_JUNCTION * parseJunction()
int m_convert
The current body style being parsed.
PAGE_INFO describes the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
SCH_NO_CONNECT * parseNoConnect()
LIB_BEZIER * parseBezier()
Define a library symbol object.
int parseInternalUnits(TSCHEMATIC_T::T aToken)
double parseDouble(const char *aExpected)
STROKE_PARAMS(int aWidth=Mils2iu(DEFAULT_LINE_THICKNESS), PLOT_DASH_TYPE aType=PLOT_DASH_TYPE::DEFAULT, const COLOR4D &aColor=COLOR4D::UNSPECIFIED)
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet.h:84
Object to parser s-expression symbol library and schematic file formats.
bool IsTooRecent() const
Return whether a version number, if any was parsed, was too recent.
#define DEFAULT_LINE_THICKNESS
The default wire width in mils. (can be changed in preference menu)
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:42
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:216
PLOT_DASH_TYPE
Enum for choosing dashed line type.
Definition: plotter.h:87
LIB_ITEM * ParseDrawItem()
void parsePAGE_INFO(PAGE_INFO &aPageInfo)
int m_requiredVersion
Set to the symbol library file version required.
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:38
SCH_BITMAP * parseImage()
Simple container to manage line stroke parameters.
double parseDouble(TSCHEMATIC_T::T aToken)
SCH_FIELD * parseSchField(SCH_ITEM *aParent)
Schematic symbol object.
Definition: sch_component.h:88
LIB_CIRCLE * parseCircle()
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:61
void parseFill(FILL_PARAMS &aFill)
const char * GetTokenText(T aTok)
Function GetTokenText is in the DSN namespace and returns the C string representing a SPECCTRA_DB::ke...
Definition: specctra.cpp:69
void ParseSchematic(SCH_SHEET *aSheet, bool aIsCopyablyOnly=false, int aFileVersion=SEXPR_SCHEMATIC_FILE_VERSION)
Parse the internal LINE_READER object into aSheet.
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:42
Class for a wire to bus entry.
SCH_TEXT * parseSchText()
double parseDouble()
Parse the current token as an ASCII numeric string with possible leading whitespace into a double pre...
int parseInt(const char *aExpected)
PLOT_DASH_TYPE m_Type
void parsePinNames(std::unique_ptr< LIB_PART > &aSymbol)
Definition for part library class.
#define SEXPR_SCHEMATIC_FILE_VERSION
Symbol library file version.
SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:151
SCH_COMPONENT * parseSchematicSymbol()
void parseStroke(STROKE_PARAMS &aStroke)
Parse stroke definition aStroke.
Define a bezier curve graphic body item.
Definition: lib_bezier.h:34
SCH_SHEET * parseSheet()