KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_kicad_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 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Wayne Stambaugh <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
26
27#ifndef SCH_IO_KICAD_SEXPR_PARSER_H_
28#define SCH_IO_KICAD_SEXPR_PARSER_H_
29
31#include <progress_reporter.h>
32#include <schematic_lexer.h>
33#include <sch_file_versions.h>
34#include <default_values.h> // For some default values
35#include <map>
36#include <set>
37#include <wx/string.h>
38
39
40class SCH_PIN;
41class PAGE_INFO;
42class SCH_BITMAP;
44class SCH_SYMBOL;
45class SCH_FIELD;
46class SCH_ITEM;
47class SCH_SHAPE;
48class SCH_RULE_AREA;
49class SCH_JUNCTION;
50class SCH_LINE;
51class SCH_NO_CONNECT;
52class SCH_SCREEN;
53class SCH_SHEET;
54class SCH_SHEET_PIN;
55class SCH_TEXT;
56class SCH_TEXTBOX;
57class SCH_TABLE;
58class SCH_TABLECELL;
59class TITLE_BLOCK;
60
61
71
72
76class SCH_IO_KICAD_SEXPR_PARSER : public SCHEMATIC_LEXER
77{
78public:
79 SCH_IO_KICAD_SEXPR_PARSER( LINE_READER* aLineReader = nullptr,
80 PROGRESS_REPORTER* aProgressReporter = nullptr, unsigned aLineCount = 0,
81 SCH_SHEET* aRootSheet = nullptr, bool aIsAppending = false );
82
83 void ParseLib( LIB_SYMBOL_MAP& aSymbolLibMap );
84
88 LIB_SYMBOL* ParseSymbol( LIB_SYMBOL_MAP& aSymbolLibMap,
89 int aFileVersion = SEXPR_SYMBOL_LIB_FILE_VERSION );
90
92
110 void ParseSchematic( SCH_SHEET* aSheet, bool aIsCopyablyOnly = false,
111 int aFileVersion = SEXPR_SCHEMATIC_FILE_VERSION );
112
114
119 const std::vector<wxString>& GetParseWarnings() const { return m_parseWarnings; }
120
121 // Access parsed per-chain netclass overrides (chain name -> netclass name).
122 const std::map<wxString, wxString>& GetNetChainNetClasses() const
123 {
125 }
126
127 // Access parsed per-chain colour overrides (chain name -> RGBA).
128 const std::map<wxString, COLOR4D>& GetNetChainColors() const
129 {
130 return m_netChainColors;
131 }
132
134 {
135 wxString ref;
136 wxString pin;
137 };
138 using CHAIN_TERMINALS = std::pair<CHAIN_TERMINAL, CHAIN_TERMINAL>;
139
140 const std::map<wxString, CHAIN_TERMINALS>& GetNetChainTerminalRefs() const { return m_netChainTerminalRefs; }
141
142 // Access parsed per-chain member-net lists. Used to reconstruct manually force-created
143 // chains on reload, since those have no underlying inferred potential.
144 const std::map<wxString, std::set<wxString>>& GetNetChainMemberNets() const
145 {
147 }
148
149private:
150 // Group membership info refers to other Uuids in the file.
151 // We don't want to rely on group declarations being last in the file, so
152 // we store info about the group declarations here during parsing and then resolve
153 // them into BOARD_ITEM* after we've parsed the rest of the file.
155 {
156 virtual ~GROUP_INFO() = default; // Make polymorphic
157
158 wxString name;
161 std::vector<KIID> memberUuids;
162 bool locked = false;
163 };
164
165 void checkpoint();
166
167 KIID parseKIID();
168
169 void parseHeader( TSCHEMATIC_T::T aHeaderType, int aFileVersion );
170
171 inline long parseHex()
172 {
173 NextTok();
174 return strtol( CurText(), nullptr, 16 );
175 }
176
177 inline int parseInt()
178 {
179 return (int)strtol( CurText(), nullptr, 10 );
180 }
181
182 inline int parseInt( const char* aExpected )
183 {
184 NeedNUMBER( aExpected );
185 return parseInt();
186 }
187
188 int parseInternalUnits();
189
190 int parseInternalUnits( const char* aExpected );
191
192 int parseInternalUnits( TSCHEMATIC_T::T aToken )
193 {
194 return parseInternalUnits( GetTokenText( aToken ) );
195 }
196
197 VECTOR2I parseXY( bool aInvertY = false )
198 {
199 VECTOR2I xy;
200
201 xy.x = parseInternalUnits( "X coordinate" );
202 xy.y = aInvertY ? -parseInternalUnits( "Y coordinate" )
203 : parseInternalUnits( "Y coordinate" );
204
205 return xy;
206 }
207
208 bool parseBool();
209
220 bool parseMaybeAbsentBool( bool aDefaultValue );
221
222 LIB_SYMBOL* parseLibSymbol( LIB_SYMBOL_MAP& aSymbolLibMap );
223
229 void parseStroke( STROKE_PARAMS& aStroke );
230
231 void parseFill( FILL_PARAMS& aFill );
232
233 void parseMargins( int& aLeft, int& aTop, int& aRight, int& aBottom )
234 {
235 aLeft = parseInternalUnits( "left margin" );
236 aTop = parseInternalUnits( "top margin" );
237 aRight = parseInternalUnits( "right margin" );
238 aBottom = parseInternalUnits( "bottom margin" );
239 }
240
241 void parseEDA_TEXT( EDA_TEXT* aText, bool aConvertOverbarSyntax, bool aEnforceMinTextSize = true );
242 void parseBodyStyles( std::unique_ptr<LIB_SYMBOL>& aSymbol );
243 void parsePinNames( std::unique_ptr<LIB_SYMBOL>& aSymbol );
244 void parsePinNumbers( std::unique_ptr<LIB_SYMBOL>& aSymbol );
245
246 SCH_FIELD* parseProperty( std::unique_ptr<LIB_SYMBOL>& aSymbol );
247
248 void parseEllipseBody( SCH_SHAPE* aShape, bool aIsArc, bool aIsSchematic, TSCHEMATIC_T::T aFirstTok );
249
260
261 void parsePAGE_INFO( PAGE_INFO& aPageInfo );
262 void parseTITLE_BLOCK( TITLE_BLOCK& aTitleBlock );
263 void parseSchSymbolInstances( SCH_SCREEN* aScreen );
264 void parseSchSheetInstances( SCH_SHEET* aRootSheet, SCH_SCREEN* aScreen );
265
266 void parseGroup();
267 void parseGroupMembers( GROUP_INFO& aGroupInfo );
268
270 SCH_FIELD* parseSchField( SCH_ITEM* aParent );
288 void parseSchTextBoxContent( SCH_TEXTBOX* aTextBox );
291 void parseBusAlias( SCH_SCREEN* aScreen );
292 void parseSchNetChain();
293
294 void resolveGroups( SCH_SCREEN* aParent );
295
305 void skipToBlockEnd( int aDepth = 1 );
306
307private:
310 int m_unit;
312 wxString m_symbolName;
314
315 std::set<KIID> m_uuids;
316
317 PROGRESS_REPORTER* m_progressReporter; // optional; may be nullptr
318 const LINE_READER* m_lineReader; // for progress reporting
320 unsigned m_lineCount; // for progress reporting
321
322 KIID m_rootUuid; // The UUID of the root schematic.
323
326
329
330 std::vector<GROUP_INFO> m_groupInfos;
331
332 std::vector<wxString> m_parseWarnings;
333
334 std::map<wxString, wxString> m_netChainNetClasses;
335 std::map<wxString, COLOR4D> m_netChainColors;
336 std::map<wxString, CHAIN_TERMINALS> m_netChainTerminalRefs;
337 std::map<wxString, std::set<wxString>> m_netChainMemberNets;
338};
339
340#endif // SCH_IO_KICAD_SEXPR_PARSER_H_
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:89
Simple container to manage fill parameters.
Definition kiid.h:44
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
Define a library symbol object.
Definition lib_symbol.h:79
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition richio.h:62
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
A progress reporter interface for use in multi-threaded environments.
Object to handle a bitmap image that can be inserted in a schematic.
Definition sch_bitmap.h:36
Class for a wire to bus entry.
bool m_appending
Appending load status.
SCH_FIELD * parseSchField(SCH_ITEM *aParent)
void parseSchSymbolInstances(SCH_SCREEN *aScreen)
void parsePinNumbers(std::unique_ptr< LIB_SYMBOL > &aSymbol)
LIB_SYMBOL * parseLibSymbol(LIB_SYMBOL_MAP &aSymbolLibMap)
void parseEllipseBody(SCH_SHAPE *aShape, bool aIsArc, bool aIsSchematic, TSCHEMATIC_T::T aFirstTok)
int parseInternalUnits(TSCHEMATIC_T::T aToken)
void parseBusAlias(SCH_SCREEN *aScreen)
void parseTITLE_BLOCK(TITLE_BLOCK &aTitleBlock)
void parseGroupMembers(GROUP_INFO &aGroupInfo)
void parseStroke(STROKE_PARAMS &aStroke)
Parse stroke definition aStroke.
int m_unit
The current unit being parsed.
std::map< wxString, std::set< wxString > > m_netChainMemberNets
const std::vector< wxString > & GetParseWarnings() const
Return any non-fatal parse warnings that occurred during parsing.
void parseEDA_TEXT(EDA_TEXT *aText, bool aConvertOverbarSyntax, bool aEnforceMinTextSize=true)
void resolveGroups(SCH_SCREEN *aParent)
const std::map< wxString, wxString > & GetNetChainNetClasses() const
std::map< wxString, wxString > m_netChainNetClasses
SCH_SHEET * m_rootSheet
The rootsheet for full project loads or null for importing a schematic.
void ParseLib(LIB_SYMBOL_MAP &aSymbolLibMap)
std::vector< GROUP_INFO > m_groupInfos
void parseHeader(TSCHEMATIC_T::T aHeaderType, int aFileVersion)
void parseBodyStyles(std::unique_ptr< LIB_SYMBOL > &aSymbol)
const std::map< wxString, COLOR4D > & GetNetChainColors() const
void parseMargins(int &aLeft, int &aTop, int &aRight, int &aBottom)
wxString m_symbolName
The current symbol name.
std::map< wxString, CHAIN_TERMINALS > m_netChainTerminalRefs
VECTOR2I parseXY(bool aInvertY=false)
void parsePinNames(std::unique_ptr< LIB_SYMBOL > &aSymbol)
std::vector< wxString > m_parseWarnings
Non-fatal warnings collected during parsing.
void ParseSchematic(SCH_SHEET *aSheet, bool aIsCopyablyOnly=false, int aFileVersion=SEXPR_SCHEMATIC_FILE_VERSION)
Parse the internal LINE_READER object into aSheet.
SCH_SHEET_PIN * parseSchSheetPin(SCH_SHEET *aSheet)
int m_bodyStyle
The current body style being parsed.
std::map< wxString, COLOR4D > m_netChainColors
std::pair< CHAIN_TERMINAL, CHAIN_TERMINAL > CHAIN_TERMINALS
SCH_FIELD * parseProperty(std::unique_ptr< LIB_SYMBOL > &aSymbol)
void parseSchTextBoxContent(SCH_TEXTBOX *aTextBox)
bool parseMaybeAbsentBool(bool aDefaultValue)
Parses a boolean flag inside a list that existed before boolean normalization.
void parsePAGE_INFO(PAGE_INFO &aPageInfo)
const std::map< wxString, CHAIN_TERMINALS > & GetNetChainTerminalRefs() const
int m_requiredVersion
Set to the symbol library file version required.
int m_maxError
Max deviation allowed when approximating bezier curves.
int parseInt(const char *aExpected)
LIB_SYMBOL * ParseSymbol(LIB_SYMBOL_MAP &aSymbolLibMap, int aFileVersion=SEXPR_SYMBOL_LIB_FILE_VERSION)
Parse internal LINE_READER object into symbols and return all found.
void skipToBlockEnd(int aDepth=1)
Skip tokens until we reach the end of the current S-expression block.
void parseSchSheetInstances(SCH_SHEET *aRootSheet, SCH_SCREEN *aScreen)
const std::map< wxString, std::set< wxString > > & GetNetChainMemberNets() const
SCH_IO_KICAD_SEXPR_PARSER(LINE_READER *aLineReader=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr, unsigned aLineCount=0, SCH_SHEET *aRootSheet=nullptr, bool aIsAppending=false)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:38
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
Schematic symbol object.
Definition sch_symbol.h:69
Simple container to manage line stroke parameters.
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition title_block.h:37
FILL_T
Definition eda_shape.h:59
#define SEXPR_SYMBOL_LIB_FILE_VERSION
This file contains the file format version information for the s-expression schematic and symbol libr...
#define SEXPR_SCHEMATIC_FILE_VERSION
Schematic file version.
std::map< wxString, LIB_SYMBOL *, LibSymbolMapSort > LIB_SYMBOL_MAP
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683