/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2022 Roberto Fernandez Bautista * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ /** * A program to test a PEGTL grammar */ #include #include #include #include #include #include #include using namespace tao::pegtl; //-------------------- Grammar definition ---------------------------------------------------- struct LINE_CONTINUATION : seq, eol>{}; ///< Any text can span multiple lines using '&' /** * String segment( no line continuation ), with exclusion rules */ template struct STR_SEGMENT_EXCLUDING : plus>, any>{}; /** * String with optional line continuation and exclusion rules */ template struct STRING_EXCLUDING : plus, opt> {}; /** * Control character with or without preceding whitespace */ template struct spaced_ch : seq, one>{}; // Definition of "Format" // # Format struct CURRENT_FORMAT_NUMBER : plus {}; struct FORMAT : seq, CURRENT_FORMAT_NUMBER>{}; // Definition of part //._[()][:][;] // string filters: struct PART_NAME_FILTER : sor, spaced_ch<':'>, spaced_ch<';'>>{}; struct PART_NUMBER_FILTER : one<')'>{}; struct PART_VERSION_FILTER : spaced_ch<';'>{}; // part elements: struct PART_NAME : STRING_EXCLUDING {}; struct PART_NUMBER : STRING_EXCLUDING {}; struct PART_VERSION : STRING_EXCLUDING {}; struct PART_DESCRIPTION : STRING_EXCLUDING<> {}; // the part itself struct PART : seq < bol, one<'.'>, PART_NAME, opt, PART_NUMBER, one<')'>>>, opt, PART_VERSION>>, opt, PART_DESCRIPTION>> > {}; struct UNMATCHED_CONTENT : STRING_EXCLUDING {}; //@todo remove once parser is complete struct GRAMMAR : plus < sor, opt > {}; //--------------------------Simple selector definition ----------------------------- // disable all parse nodes by default template< typename PEGTL_RULE > struct SIMPLE_SELECTOR : std::false_type {}; // enable only nodes that match certain rules: template<> struct SIMPLE_SELECTOR< STR_SEGMENT_EXCLUDING< PART_NAME_FILTER > > : std::true_type {}; template<> struct SIMPLE_SELECTOR< STR_SEGMENT_EXCLUDING< PART_NUMBER_FILTER > > : std::true_type {}; template<> struct SIMPLE_SELECTOR< STR_SEGMENT_EXCLUDING< PART_VERSION_FILTER > > : std::true_type {}; template<> struct SIMPLE_SELECTOR< STR_SEGMENT_EXCLUDING<> > : std::true_type {}; template<> struct SIMPLE_SELECTOR< CURRENT_FORMAT_NUMBER > : std::true_type {}; template<> struct SIMPLE_SELECTOR< FORMAT > : std::true_type {}; template<> struct SIMPLE_SELECTOR< PART > : std::true_type {}; template<> struct SIMPLE_SELECTOR< PART_NAME > : std::true_type {}; template<> struct SIMPLE_SELECTOR< PART_NUMBER > : std::true_type {}; template<> struct SIMPLE_SELECTOR< PART_VERSION > : std::true_type {}; template<> struct SIMPLE_SELECTOR< PART_DESCRIPTION > : std::true_type {}; template<> struct SIMPLE_SELECTOR< UNMATCHED_CONTENT > : std::true_type {}; //@todo remove //--------------------------Complex selector definition ----------------------------- // Helper function to clean up the tree struct FOLD_CONTENT : parse_tree::apply { template static void transform( Node& n ) { if( n->children.size() == 1 ) { n->children.pop_back(); } else if( n->children.size() != 0 ) { n->remove_content(); } } }; template using COMPLEX_SELECTOR = parse_tree::selector< PEGTL_RULE, parse_tree::store_content::on< STR_SEGMENT_EXCLUDING< PART_NAME_FILTER >, STR_SEGMENT_EXCLUDING< PART_NUMBER_FILTER >, STR_SEGMENT_EXCLUDING< PART_VERSION_FILTER >, STR_SEGMENT_EXCLUDING<>, CURRENT_FORMAT_NUMBER, UNMATCHED_CONTENT //@todo remove when parser complete. For debugging only >, parse_tree::remove_content::on< FORMAT, PART >, parse_tree::apply< FOLD_CONTENT >::on< PART_NAME, PART_NUMBER, PART_VERSION, PART_DESCRIPTION > >; int main( int argc, char** argv ) { // .\test_pegtl.exe complex my_file.lib | dot -Tsvg > output.svg; .\output.svg if( argc != 3 ) { printf( "usage: %s ", argv[0] ); return -1; } std::string chosen_selector = argv[1]; std::filesystem::path filepath( argv[2] ); file_input in( filepath ); const std::size_t issues = tao::pegtl::analyze(); if( issues > 0 ) { std::cout << "\n***ERROR***: " << issues << " issues found in the grammar!\n"; return -1; } std::unique_ptr root; if( chosen_selector == "complex" ) { root = parse_tree::parse( in ); } else if( chosen_selector == "simple" ) { root = parse_tree::parse( in ); } else { printf( "Invalid selector '%s' requested. Valid values are: complex or simple. \n", argv[1] ); printf( "usage: %s \n", argv[0] ); } if( root ) { parse_tree::print_dot( std::cout, *root ); } else { std::cout << "\n***ERROR***: No root tree node!\n"; // lets print out the trace for debugging standard_trace( in ); } return 0; }