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:
72  COLOR4D m_Color;
73 };
74 
75 
79 class SCH_SEXPR_PARSER : public SCHEMATIC_LEXER
80 {
82  int m_fieldId;
83  int m_unit;
84  int m_convert;
85  wxString m_symbolName;
86 
87  void parseHeader( TSCHEMATIC_T::T aHeaderType, int aFileVersion );
88 
89  inline long parseHex()
90  {
91  NextTok();
92  return strtol( CurText(), NULL, 16 );
93  }
94 
95  inline int parseInt()
96  {
97  return (int)strtol( CurText(), NULL, 10 );
98  }
99 
100  inline int parseInt( const char* aExpected )
101  {
102  NeedNUMBER( aExpected );
103  return parseInt();
104  }
105 
113  double parseDouble();
114 
115  inline double parseDouble( const char* aExpected )
116  {
117  NeedNUMBER( aExpected );
118  return parseDouble();
119  }
120 
121  inline double parseDouble( TSCHEMATIC_T::T aToken )
122  {
123  return parseDouble( GetTokenText( aToken ) );
124  }
125 
126  inline int parseInternalUnits()
127  {
128  auto retval = parseDouble() * IU_PER_MM;
129 
130  // Schematic internal units are represented as integers. Any values that are
131  // larger or smaller than the schematic units represent undefined behavior for
132  // the system. Limit values to the largest that can be displayed on the screen.
133  double int_limit = std::numeric_limits<int>::max() * 0.7071; // 0.7071 = roughly 1/sqrt(2)
134 
135  return KiROUND( Clamp<double>( -int_limit, retval, int_limit ) );
136  }
137 
138  inline int parseInternalUnits( const char* aExpected )
139  {
140  auto retval = parseDouble( aExpected ) * IU_PER_MM;
141 
142  double int_limit = std::numeric_limits<int>::max() * 0.7071;
143 
144  return KiROUND( Clamp<double>( -int_limit, retval, int_limit ) );
145  }
146 
147  inline int parseInternalUnits( TSCHEMATIC_T::T aToken )
148  {
149  return parseInternalUnits( GetTokenText( aToken ) );
150  }
151 
152  inline wxPoint parseXY()
153  {
154  wxPoint xy;
155 
156  xy.x = parseInternalUnits( "X coordinate" );
157  xy.y = parseInternalUnits( "Y coordinate" );
158 
159  return xy;
160  }
161 
162  bool parseBool();
163 
169  void parseStroke( STROKE_PARAMS& aStroke );
170 
171  void parseFill( FILL_PARAMS& aFill );
172 
173  void parseEDA_TEXT( EDA_TEXT* aText );
174  void parsePinNames( std::unique_ptr<LIB_PART>& aSymbol );
175 
176  void parseProperty( std::unique_ptr<LIB_PART>& aSymbol );
177 
178  LIB_ARC* parseArc();
181  LIB_PIN* parsePin();
184  LIB_TEXT* parseText();
185 
186  void parsePAGE_INFO( PAGE_INFO& aPageInfo );
187  void parseTITLE_BLOCK( TITLE_BLOCK& aTitleBlock );
188  void parseSchSymbolInstances( SCH_SCREEN* aScreen );
189 
191  SCH_FIELD* parseSchField( SCH_ITEM* aParent );
198  SCH_LINE* parseLine();
200  void parseBusAlias( SCH_SCREEN* aScreen );
201 
202 public:
203  SCH_SEXPR_PARSER( LINE_READER* aLineReader = nullptr );
204 
205  void ParseLib( LIB_PART_MAP& aSymbolLibMap );
206 
207  LIB_PART* ParseSymbol( LIB_PART_MAP& aSymbolLibMap,
208  int aFileVersion = SEXPR_SYMBOL_LIB_FILE_VERSION );
209 
211 
229  void ParseSchematic( SCH_SHEET* aSheet, bool aIsCopyablyOnly = false,
230  int aFileVersion = SEXPR_SCHEMATIC_FILE_VERSION );
231 
235  bool IsTooRecent() const;
236 };
237 
238 #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)
static constexpr double IU_PER_MM
Mock up a conversion function.
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)
T
enum T contains all this lexer's tokens.
#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:113
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)
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.
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
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:37
SCH_BITMAP * parseImage()
Simple container to manage line stroke parameters.
Definition: sch_item.h:153
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:68
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)
void parsePinNames(std::unique_ptr< LIB_PART > &aSymbol)
Definition for part library class.
#define SEXPR_SCHEMATIC_FILE_VERSION
Symbol library file version.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:187
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()