KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sexpr_parse.cpp
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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25
26#include <sexpr/sexpr_parser.h>
27
29
30#include <common.h>
31#include <core/profile.h>
32
33#include <wx/cmdline.h>
34
35#include <fstream>
36#include <iostream>
37
38
40{
41public:
42 QA_SEXPR_PARSER( bool aVerbose ) : m_verbose( aVerbose )
43 {
44 }
45
46 bool Parse( std::istream& aStream )
47 {
48 // Don't let the parser handle stream reading - we don't want to
49 // see how long the disk IO takes. Read to memory first (event the
50 // biggest files will fit in)
51 const std::string sexpr_str( std::istreambuf_iterator<char>( aStream ), {} );
52
53 PROF_TIMER timer;
54 // Perform the parse
55 std::unique_ptr<SEXPR::SEXPR> sexpr( m_parser.Parse( sexpr_str ) );
56
57 if( m_verbose )
58 std::cout << "S-Expression Parsing took " << timer.msecs() << "ms" << std::endl;
59
60 return sexpr != nullptr;
61 }
62
63private:
66};
67
68static const wxCmdLineEntryDesc g_cmdLineDesc[] = {
69 {
70 wxCMD_LINE_SWITCH,
71 "h",
72 "help",
73 _( "displays help on the command line parameters" ).mb_str(),
74 wxCMD_LINE_VAL_NONE,
75 wxCMD_LINE_OPTION_HELP,
76 },
77 {
78 wxCMD_LINE_SWITCH,
79 "v",
80 "verbose",
81 _( "print parsing information" ).mb_str(),
82 },
83 {
84 wxCMD_LINE_PARAM,
85 nullptr,
86 nullptr,
87 _( "input file" ).mb_str(),
88 wxCMD_LINE_VAL_STRING,
89 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE,
90 },
91 { wxCMD_LINE_NONE }
92};
93
94
99
100
101int sexpr_parser_func( int argc, char* argv[] )
102{
103 wxCmdLineParser cl_parser( argc, argv );
104 cl_parser.SetDesc( g_cmdLineDesc );
105 cl_parser.AddUsageText( _( "Tests parsing of S-Expression files" ) );
106
107 int cmd_parsed_ok = cl_parser.Parse();
108 if( cmd_parsed_ok != 0 )
109 {
110 // Help and invalid input both stop here
111 return ( cmd_parsed_ok == -1 ) ? KI_TEST::RET_CODES::OK : KI_TEST::RET_CODES::BAD_CMDLINE;
112 }
113
114 const auto file_count = cl_parser.GetParamCount();
115 const bool verbose = cl_parser.Found( "verbose" );
116
117 QA_SEXPR_PARSER qa_parser( verbose );
118
119 bool ok = true;
120
121 if( file_count == 0 )
122 {
123 // Parse the file provided on stdin - used by AFL to drive the
124 // program
125 qa_parser.Parse( std::cin );
126 }
127 else
128 {
129 // Parse 'n' files given on the command line
130 // (this is useful for input minimisation (e.g. afl-tmin) as
131 // well as manual testing
132 for( unsigned i = 0; i < file_count; i++ )
133 {
134 const auto filename = cl_parser.GetParam( i ).ToStdString();
135
136 if( verbose )
137 std::cout << "Parsing: " << filename << std::endl;
138
139 std::ifstream fin;
140 fin.open( filename );
141
142 ok = ok && qa_parser.Parse( fin );
143 }
144 }
145
146 if( !ok )
148
150}
151
152
154 "sexpr_parser",
155 "Benchmark s-expression parsing",
157} );
A small class to help profiling.
Definition profile.h:46
bool Parse(std::istream &aStream)
SEXPR::PARSER m_parser
QA_SEXPR_PARSER(bool aVerbose)
static bool Register(const KI_TEST::UTILITY_PROGRAM &aProgInfo)
Register a utility program factory function against an ID string.
The common library.
static bool registered
static const wxCmdLineEntryDesc g_cmdLineDesc[]
#define _(s)
@ OK
Tool exited OK.
@ TOOL_SPECIFIC
Tools can define their own statuses from here onwards.
@ BAD_CMDLINE
The command line was not correct for the tool.
PARSER_RET_CODES
@ PARSE_FAILED
int sexpr_parser_func(int argc, char *argv[])