KiCad PCB EDA Suite
pcb_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) 2012 CERN
5  * Copyright (C) 2012-2016 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #ifndef _PCBNEW_PARSER_H_
31 #define _PCBNEW_PARSER_H_
32 
33 #include <pcb_lexer.h>
34 #include <hashtables.h>
35 #include <layers_id_colors_and_visibility.h> // PCB_LAYER_ID
36 #include <common.h> // KiROUND
37 #include <convert_to_biu.h> // IU_PER_MM
38 #include <3d_cache/3d_info.h>
39 
40 #include <boost/unordered_map.hpp>
41 #include <boost/unordered_set.hpp>
42 
43 
44 class BOARD;
45 class BOARD_ITEM;
46 class D_PAD;
47 class DIMENSION;
48 class DRAWSEGMENT;
49 class EDA_TEXT;
50 class EDGE_MODULE;
51 class TEXTE_MODULE;
52 class TEXTE_PCB;
53 class TRACK;
54 class MODULE;
55 class PCB_TARGET;
56 class VIA;
57 class ZONE_CONTAINER;
58 struct LAYER;
59 
60 
66 class PCB_PARSER : public PCB_LEXER
67 {
68  typedef boost::unordered_map< std::string, PCB_LAYER_ID > LAYER_ID_MAP;
69  typedef boost::unordered_map< std::string, LSET > LSET_MAP;
70 
72  LAYER_ID_MAP m_layerIndices;
73  LSET_MAP m_layerMasks;
74  std::vector<int> m_netCodes;
75  bool m_tooRecent;
77 
80  inline int getNetCode( int aNetCode )
81  {
82  if( ( aNetCode >= 0 ) && ( aNetCode < (int) m_netCodes.size() ) )
83  return m_netCodes[aNetCode];
84 
85  return aNetCode;
86  }
87 
95  void pushValueIntoMap( int aIndex, int aValue );
96 
103  void init();
104 
105  void parseHeader();
106  void parseGeneralSection();
107  void parsePAGE_INFO();
108  void parseTITLE_BLOCK();
109 
110  void parseLayers();
111  void parseLayer( LAYER* aLayer );
112 
113  void parseSetup();
114  void parseNETINFO_ITEM();
115  void parseNETCLASS();
116 
120 
127  MODULE* parseMODULE( wxArrayString* aInitialComments = 0 );
128 
133  MODULE* parseMODULE_unchecked( wxArrayString* aInitialComments = 0 );
136  D_PAD* parseD_PAD( MODULE* aParent = NULL );
137  TRACK* parseTRACK();
138  VIA* parseVIA();
141  BOARD* parseBOARD();
142 
148 
149 
160  template<class T, class M>
161  T lookUpLayer( const M& aMap );
162 
172 
182 
194  wxPoint parseXY();
195 
196  void parseXY( int* aX, int* aY );
197 
205  void parseEDA_TEXT( EDA_TEXT* aText );
206 
208 
217  double parseDouble();
218 
219  inline double parseDouble( const char* aExpected )
220  {
221  NeedNUMBER( aExpected );
222  return parseDouble();
223  }
224 
225  inline double parseDouble( PCB_KEYS_T::T aToken )
226  {
227  return parseDouble( GetTokenText( aToken ) );
228  }
229 
230  inline int parseBoardUnits()
231  {
232  // There should be no major rounding issues here, since the values in
233  // the file are in mm and get converted to nano-meters.
234  // See test program tools/test-nm-biu-to-ascii-mm-round-tripping.cpp
235  // to confirm or experiment. Use a similar strategy in both places, here
236  // and in the test program. Make that program with:
237  // $ make test-nm-biu-to-ascii-mm-round-tripping
238  return KiROUND( parseDouble() * IU_PER_MM );
239  }
240 
241  inline int parseBoardUnits( const char* aExpected )
242  {
243  // Use here KiROUND, not KIROUND (see comments about them)
244  // when having a function as argument, because it will be called twice
245  // with KIROUND
246  return KiROUND( parseDouble( aExpected ) * IU_PER_MM );
247  }
248 
249  inline int parseBoardUnits( PCB_KEYS_T::T aToken )
250  {
251  return parseBoardUnits( GetTokenText( aToken ) );
252  }
253 
254  inline int parseInt()
255  {
256  return (int)strtol( CurText(), NULL, 10 );
257  }
258 
259  inline int parseInt( const char* aExpected )
260  {
261  NeedNUMBER( aExpected );
262  return parseInt();
263  }
264 
265  inline long parseHex()
266  {
267  NextTok();
268  return strtol( CurText(), NULL, 16 );
269  }
270 
271  bool parseBool();
272 
277  int parseVersion();
278 
279 public:
280 
281  PCB_PARSER( LINE_READER* aReader = NULL ) :
282  PCB_LEXER( aReader ),
283  m_board( 0 )
284  {
285  init();
286  }
287 
288  // ~PCB_PARSER() {}
289 
297  {
298  LINE_READER* ret = PopReader();
299  PushReader( aReader );
300  return ret;
301  }
302 
303  void SetBoard( BOARD* aBoard )
304  {
305  init();
306  m_board = aBoard;
307  }
308 
309  BOARD_ITEM* Parse();
310 
314  bool IsTooRecent()
315  {
316  return m_tooRecent;
317  }
318 
323  wxString GetRequiredVersion();
324 
325 };
326 
327 
328 #endif // _PCBNEW_PARSER_H_
void parseHeader()
Definition: pcb_parser.cpp:550
boost::unordered_map< std::string, LSET > LSET_MAP
Definition: pcb_parser.h:69
int parseVersion()
Parse a format version tag like (version 20160417) return the version.
Definition: pcb_parser.cpp:169
PCB_KEYS_T::T NextTok()
Function NextTok returns the next token found in the input file or T_EOF when reaching the end of fil...
Definition: pcb_lexer.h:302
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:78
int NeedNUMBER(const char *aExpectation)
Function NeedNUMBER calls NextTok() and then verifies that the token read is type DSN_NUMBER...
Definition: dsnlexer.cpp:427
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
void PushReader(LINE_READER *aLineReader)
Function PushReader manages a stack of LINE_READERs in order to handle nested file inclusion...
Definition: dsnlexer.cpp:192
LSET parseBoardItemLayersAsMask()
Function parseBoardItemLayersAsMask parses the layers definition of a BOARD_ITEM object.
Definition: pcb_parser.cpp:974
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
boost::unordered_map< std::string, PCB_LAYER_ID > LAYER_ID_MAP
Definition: pcb_parser.h:68
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
const char * GetTokenText(int aTok)
Function GetTokenText returns the C string representation of a DSN_T value.
Definition: dsnlexer.cpp:315
S3D_INFO * parse3DModel()
Definition: pcb_parser.cpp:335
T
enum T contains all this lexer's tokens.
TRACK * parseTRACK()
VIA * parseVIA()
Class PCB_PARSER reads a Pcbnew s-expression formatted LINE_READER object and returns the appropriate...
Definition: pcb_parser.h:66
wxPoint parseXY()
Function parseXY parses a coordinate pair (xy X Y) in board units (mm).
Definition: pcb_parser.cpp:206
void parseNETINFO_ITEM()
BOARD * parseBOARD()
Definition: pcb_parser.cpp:444
void parseEDA_TEXT(EDA_TEXT *aText)
Function parseEDA_TEXT parses the common settings for any object derived from EDA_TEXT.
Definition: pcb_parser.cpp:238
DIMENSION * parseDIMENSION()
bool m_tooRecent
true if version parses as later than supported
Definition: pcb_parser.h:75
void parseTITLE_BLOCK()
Definition: pcb_parser.cpp:688
defines the basic data associated with a single 3D model.
int parseBoardUnits(PCB_KEYS_T::T aToken)
Definition: pcb_parser.h:249
int parseBoardUnits()
Definition: pcb_parser.h:230
void parseLayers()
Definition: pcb_parser.cpp:813
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels...
Definition: eda_text.h:114
void parsePAGE_INFO()
Definition: pcb_parser.cpp:632
Class PCB_LEXER is an automatically generated class using the TokenList2DnsLexer.cmake technology...
Definition: pcb_lexer.h:238
bool parseBool()
Definition: pcb_parser.cpp:154
LINE_READER * PopReader()
Function PopReader deletes the top most LINE_READER from an internal stack of LINE_READERs and in the...
Definition: dsnlexer.cpp:204
PCB_LAYER_ID
A quick note on layer IDs:
wxString GetRequiredVersion()
Return a string representing the version of kicad required to open this file.
Definition: pcb_parser.cpp:182
Class LSET is a set of PCB_LAYER_IDs.
void SetBoard(BOARD *aBoard)
Definition: pcb_parser.h:303
PCB_PARSER(LINE_READER *aReader=NULL)
Definition: pcb_parser.h:281
MODULE * parseMODULE_unchecked(wxArrayString *aInitialComments=0)
Function parseMODULE_unchecked Parse a module, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERRO...
DRAWSEGMENT * parseDRAWSEGMENT()
PCB_TARGET * parsePCB_TARGET()
PCB_LAYER_ID parseBoardItemLayer()
Function parseBoardItemLayer parses the layer definition of a BOARD_ITEM object.
Definition: pcb_parser.cpp:959
TEXTE_PCB * parseTEXTE_PCB()
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:405
void parseNETCLASS()
LINE_READER * SetLineReader(LINE_READER *aReader)
Function SetLineReader sets aLineReader into the parser, and returns the previous one...
Definition: pcb_parser.h:296
int parseInt(const char *aExpected)
Definition: pcb_parser.h:259
BOARD * parseBOARD_unchecked()
Function parseBOARD_unchecked Parse a module, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERROR...
Definition: pcb_parser.cpp:460
ZONE_CONTAINER * parseZONE_CONTAINER()
void parseLayer(LAYER *aLayer)
Definition: pcb_parser.cpp:771
int m_requiredVersion
set to the KiCad format version this board requires
Definition: pcb_parser.h:76
bool IsTooRecent()
Return whether a version number, if any was parsed, was too recent.
Definition: pcb_parser.h:314
T
enum T contains all this lexer's tokens.
Definition: pcb_lexer.h:26
EDGE_MODULE * parseEDGE_MODULE()
Board layer functions and definitions.
Class LAYER holds information pertinent to a layer of a BOARD.
Definition: class_board.h:85
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
std::vector< int > m_netCodes
net codes mapping for boards being loaded
Definition: pcb_parser.h:74
int getNetCode(int aNetCode)
Converts net code using the mapping table if available, otherwise returns unchanged net code if < 0 o...
Definition: pcb_parser.h:80
BOARD * m_board
Definition: pcb_parser.h:71
void parseGeneralSection()
Definition: pcb_parser.cpp:586
void init()
Function init clears and re-establishes m_layerMap with the default layer names.
Definition: pcb_parser.cpp:56
void pushValueIntoMap(int aIndex, int aValue)
function pushValueIntoMap Add aValue value in netcode mapping (m_netCodes) at index aIndex ensure the...
Definition: pcb_parser.cpp:113
double parseDouble(PCB_KEYS_T::T aToken)
Definition: pcb_parser.h:225
The common library.
double parseDouble()
Function parseDouble parses the current token as an ASCII numeric string with possible leading whites...
Definition: pcb_parser.cpp:124
LAYER_ID_MAP m_layerIndices
map layer name to it's index
Definition: pcb_parser.h:72
TEXTE_MODULE * parseTEXTE_MODULE()
D_PAD * parseD_PAD(MODULE *aParent=NULL)
MODULE * parseMODULE(wxArrayString *aInitialComments=0)
Function parseMODULE.
long parseHex()
Definition: pcb_parser.h:265
int parseInt()
Definition: pcb_parser.h:254
const char * CurText()
Function CurText returns a pointer to the current token's text.
Definition: dsnlexer.h:479
LSET_MAP m_layerMasks
map layer names to their masks
Definition: pcb_parser.h:73
Class DIMENSION.
void parseSetup()
Definition: pcb_parser.cpp:992
int parseBoardUnits(const char *aExpected)
Definition: pcb_parser.h:241
T lookUpLayer(const M &aMap)
Function lookUpLayer parses the current token for the layer definition of a BOARD_ITEM object...
Definition: pcb_parser.cpp:926
double parseDouble(const char *aExpected)
Definition: pcb_parser.h:219