58template <
typename Rule>
66 std::stringstream ss( aString );
77#define DEFINE_CONTENT_TO_NUMBER_ACTION( Rule, StateVariable ) \
79struct CADSTAR_LIB_PARSER_ACTION<Rule> \
81 template <typename ActionInput> \
82 static void apply( const ActionInput& in, CADSTAR_LIB_PARSER_STATE& s ) \
84 assert( s.m_CurrentString == "" && s.m_CurrentAttrName == "" ); \
85 s.StateVariable = helperStringToLong( in.string() ); \
101 template <
typename ActionInput>
114#define DEFINE_CONTENT_TO_STRING_ACTION( Rule ) \
116struct CADSTAR_LIB_PARSER_ACTION<Rule> \
118 template <typename ActionInput> \
119 static void apply( const ActionInput& in, CADSTAR_LIB_PARSER_STATE& s ) \
121 assert( s.m_CurrentString == "" && s.m_CurrentAttrName == "" ); \
122 s.m_CurrentString = in.string(); \
132#define DEFINE_STRING_ACTION( Rule, StateVariable ) \
134struct CADSTAR_LIB_PARSER_ACTION<Rule> \
137 template <typename ActionInput> \
138 static void apply( const ActionInput& in, CADSTAR_LIB_PARSER_STATE& s ) \
140 assert( in.string().size() >= s.m_CurrentString.size() ); \
141 s.StateVariable = s.m_CurrentString; \
142 s.m_CurrentString = ""; \
172template <
typename... EXCLUSION_RULES>
175 template <
typename ActionInput>
278#define DECLARE_SINGLE_MATCH_RULE( Rule, ExtraCode ) \
280struct CADSTAR_LIB_PARSER_ACTION<Rule> \
282 template <typename ActionInput> \
283 static void apply( const ActionInput& in, CADSTAR_LIB_PARSER_STATE& s ) \
285 assert( s.m_CurrentString == "" && s.m_CurrentAttrName == "" ); \
287 if( s.m_CurrentElementsParsed.count( #Rule ) ) \
289 throw parse_error( #Rule \
290 " was already defined for this part!", \
294 s.m_CurrentElementsParsed.insert( #Rule ); \
342#define DEFINE_PIN_GROUP_ACTION( Rule, StateVariable ) \
344struct CADSTAR_LIB_PARSER_ACTION<Rule> \
346 static void apply0( CADSTAR_LIB_PARSER_STATE& s ) \
348 assert( s.m_CurrentString == "" && s.m_CurrentAttrName == "" ); \
349 s.StateVariable.push_back( s.m_CurrentPinEquivalenceGroup ); \
350 s.m_CurrentPinEquivalenceGroup.clear(); \
364#define DEFINE_SWAP_GROUP_ACTION( Rule, StateVariable ) \
366struct CADSTAR_LIB_PARSER_ACTION<Rule> \
368 static void apply0( CADSTAR_LIB_PARSER_STATE& s ) \
370 assert( s.m_CurrentString == "" && s.m_CurrentAttrName == "" ); \
371 s.StateVariable.push_back( s.m_CurrentSwapGroup ); \
372 s.m_CurrentSwapGroup = CADSTAR_SWAP_GROUP(); \
383 template <
typename ActionInput>
388 static const std::set<std::string> reservedWordsStarLines = {
"VALUE",
"PNM",
"PLB",
"EQU",
389 "SYM",
"INT",
"EXT",
"DFN",
390 "NGS",
"NPV",
"STM",
"MXP",
395 throw parse_error( fmt::format(
"Duplicate attribute name '{}'", s.
m_CurrentAttrName ),
403 "Invalid use of in-built attribute name '{}'. Either the attribute "
404 "was already defined for this part or it has an unexpected syntax.",
415#define DEFINE_ATTRIBUTE_ACTION( Rule, StateVariable ) \
417struct CADSTAR_LIB_PARSER_ACTION<Rule> \
419 template <typename ActionInput> \
420 static void apply( const ActionInput& in, CADSTAR_LIB_PARSER_STATE& s ) \
422 if( s.StateVariable.count( s.m_CurrentAttrName ) ) \
425 fmt::format( "Duplicate attribute name '{}'", s.m_CurrentAttrName ), \
429 CADSTAR_ATTRIBUTE_VALUE val; \
430 val.m_ReadOnly = s.m_ReadOnly; \
431 val.m_Value = s.m_CurrentString; \
433 s.StateVariable.insert( { s.m_CurrentAttrName, val } ); \
434 s.m_CurrentAttrName = ""; \
435 s.m_CurrentString = ""; \
436 s.m_ReadOnly = false; \
484 template <
typename ActionInput>
495 template <
typename ActionInput>
500 static const std::map<std::string, CADSTAR_PIN_TYPE> tokenToPinType = {
501 {
"U", CADSTAR_PIN_TYPE::UNCOMMITTED },
502 {
"I", CADSTAR_PIN_TYPE::INPUT },
503 {
"N", CADSTAR_PIN_TYPE::OUTPUT_NOT_OR },
504 {
"Y", CADSTAR_PIN_TYPE::OUTPUT_OR },
505 {
"Q", CADSTAR_PIN_TYPE::OUTPUT_NOT_NORM_OR },
506 {
"P", CADSTAR_PIN_TYPE::POWER },
507 {
"G", CADSTAR_PIN_TYPE::GROUND },
508 {
"T", CADSTAR_PIN_TYPE::TRISTATE_BIDIR },
509 {
"TI", CADSTAR_PIN_TYPE::TRISTATE_INPUT },
510 {
"TD", CADSTAR_PIN_TYPE::TRISTATE_DRIVER }
513 if( !tokenToPinType.count( in.string() ) )
514 throw parse_error( fmt::format(
"Unexpected pin type '{}'", in.string() ), in );
521template <
typename INPUT_TYPE>
526 if( !parse<VALID_HEADER>( aInput ) )
529 catch(
const parse_error& )
540 string_input in( aSource,
"from_content" );
547 file_input in( aPath );
552template<
typename INPUT_TYPE>
563 if( !parse<GRAMMAR, CADSTAR_LIB_PARSER_ACTION>( aInput, s ) )
564 printf(
"Some error occurred!\n" );
566 catch(
const parse_error& e )
568 const auto& p = e.positions().front();
569 std::cerr <<
"Error at line " << p.line <<
", column " << p.column << std::endl
570 << aInput.line_at( p ) << std::endl
571 << std::setw( p.column ) <<
'^' << std::endl
572 << e.message() << std::endl;
581 string_input in( aSource,
"from_content" );
589 file_input in( aPath );
CADSTAR_PIN_POSITION
Positioning of pin names can be in one of four quadrants.
bool checkHeaderHelper(INPUT_TYPE &aInput)
#define DEFINE_ATTRIBUTE_ACTION(Rule, StateVariable)
#define DEFINE_PIN_GROUP_ACTION(Rule, StateVariable)
long helperStringToLong(std::string aString)
#define DEFINE_CONTENT_TO_STRING_ACTION(Rule)
#define DEFINE_SWAP_GROUP_ACTION(Rule, StateVariable)
#define DEFINE_STRING_ACTION(Rule, StateVariable)
#define DECLARE_SINGLE_MATCH_RULE(Rule, ExtraCode)
#define DEFINE_CONTENT_TO_NUMBER_ACTION(Rule, StateVariable)
CADSTAR_PARTS_LIB_MODEL readCadstarHelper(INPUT_TYPE &aInput)
bool CheckFileHeader(const std::filesystem::path &aPath) const
CADSTAR_PARTS_LIB_MODEL ReadFile(const std::filesystem::path &aPath) const
CADSTAR_PARTS_LIB_MODEL ReadContent(const std::string &aSource) const
bool CheckContentHeader(const std::string &aSource) const
static void apply(const ActionInput &in, CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply(const ActionInput &in, CADSTAR_LIB_PARSER_STATE &s)
static void apply(const ActionInput &in, CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply(const ActionInput &in, CADSTAR_LIB_PARSER_STATE &s)
static void apply0(CADSTAR_LIB_PARSER_STATE &s)
static void apply(const ActionInput &in, CADSTAR_LIB_PARSER_STATE &s)
Struture that will be populated by the PEGTL parser.
std::string m_CurrentSignalName
CADSTAR_PART_NODE m_CurrentNode
CADSTAR_PARTS_LIB_MODEL m_ParsedModel
CADSTAR_PART_PIN m_CurrentPin
std::string m_CurrentString
CADSTAR_PART_SYMBOL_ENTRY m_CurrentSymbol
CADSTAR_PART_ENTRY m_CurrentPart
std::vector< long > m_CurrentPinEquivalenceGroup
CADSTAR_SWAP_GROUP m_CurrentSwapGroup
std::set< std::string > m_CurrentElementsParsed
std::vector< CADSTAR_PART_PIN > m_CurrentPinList
std::string m_CurrentAttrName
String segment( no line continuation ), with exclusion rules.
CADSTAR Parts Library (*.lib) model - a data structure describing the contents of the file format.
std::map< long, CADSTAR_PART_NODE > m_HierarchyNodes
std::vector< CADSTAR_PART_ENTRY > m_PartEntries
std::map< std::string, std::vector< CADSTAR_PART_PIN > > m_HiddenPins
Pins with an implied electrical connection to a net, not part of any symbol (Note: we probably will n...
std::vector< CADSTAR_PART_SYMBOL_ENTRY > m_Symbols
Symbols that form this part.
std::map< std::string, std::string > m_UserAttributes
Star (*) line *<User-defined name> This line is ignored by CADSTAR.
std::map< long, std::string > m_PinNamesMap
Map of pin identifiers to alphanumeric pin names Pin names can be a maximum of 10 characters (Typical...
std::map< long, std::string > m_PinLabelsMap
Map of pin identifiers to alphanumeric pin labels.
std::optional< long > m_ParentNodeIdx
std::vector< std::string > m_PartNames
Part names belonging to this hierarchy.
CADSTAR_PIN_POSITION m_Position
std::vector< CADSTAR_PART_PIN > m_Pins