Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:insignificant reason:build-tool
4
5#include "lalr.h"
6#include "dotgraph.h"
7#include "parsetable.h"
8#include "cppgenerator.h"
9#include "recognizer.h"
10
11#include <QtCore/qcoreapplication.h>
12#include <QtCore/qfile.h>
13#include <QtCore/qstringlist.h>
14#include <QtCore/qdebug.h>
15
16#include <cstdlib>
17
18#define QLALR_NO_DEBUG_TABLE
19#define QLALR_NO_DEBUG_DOT
20
21using namespace Qt::StringLiterals;
22
23static void help_me ()
24{
25 qerr() << "Usage: qlalr [options] [input file name]" << Qt::endl
26 << Qt::endl
27 << " --help, -h\t\tdisplay this help and exit" << Qt::endl
28 << " --verbose, -v\t\tverbose output" << Qt::endl
29 << " --no-debug\t\tno debug information" << Qt::endl
30 << " --no-lines\t\tno #line directives" << Qt::endl
31 << " --dot\t\t\tgenerate a graph" << Qt::endl
32 << " --use-pragma-once\tuse #pragma once instead of ifndef/define header guards" << Qt::endl
33 << " --qt-security={critical,significant,insignificant}[:<reason>]" << Qt::endl
34 << "\t\t\tmark the output files with Qt-Security (QUIP-23) headers" << Qt::endl
35 << "\t\t\twith given score and free-form <reason>" << Qt::endl
36 << " --qt\t\t\tadd the Qt copyright header and Qt-specific types and macros" << Qt::endl
37 << " --exit-on-warn\texit with status code 2 on warning" << Qt::endl
38 << Qt::endl;
39 exit (0);
40}
41
42int main (int argc, char *argv[])
43{
44 QCoreApplication app (argc, argv);
45
46 bool generate_dot = false;
47 bool generate_report = false;
48 bool no_lines = false;
49 bool debug_info = true;
50 bool use_pragma_once = false;
51 std::optional<CppGenerator::SecurityHeader> security;
52 bool qt_copyright = false;
53 bool warnings_are_errors = false;
54 QString file_name;
55
56 const QStringList args = app.arguments().mid(1);
57 for (const QString &arg : args) {
58 if (arg == "-h"_L1 || arg == "--help"_L1)
59 help_me ();
60
61 else if (arg == "-v"_L1 || arg == "--verbose"_L1)
62 generate_report = true;
63
64 else if (arg == "--dot"_L1)
65 generate_dot = true;
66
67 else if (arg == "--no-lines"_L1)
68 no_lines = true;
69
70 else if (arg == "--no-debug"_L1)
71 debug_info = false;
72
73 else if (arg == "--use-pragma-once"_L1)
74 use_pragma_once = true;
75
76 else if (constexpr auto qtsec = "--qt-security="_L1; arg.startsWith(qtsec))
77 {
78 auto sh = CppGenerator::SecurityHeader::parse(QStringView{arg}.slice(qtsec.size()));
79 if (!sh)
80 qerr() << "*** Warning. Could not parse `" << arg << "'" << Qt::endl;
81 else
82 security = std::move(sh);
83 }
84
85 else if (arg == "--qt"_L1)
86 qt_copyright = true;
87
88 else if (arg == "--exit-on-warn"_L1)
89 warnings_are_errors = true;
90
91 else if (file_name.isEmpty ())
92 file_name = arg;
93
94 else
95 qerr() << "*** Warning. Ignore argument `" << arg << "'" << Qt::endl;
96 }
97
98 if (file_name.isEmpty ())
99 {
100 help_me ();
101 exit (EXIT_SUCCESS);
102 }
103
104 Grammar grammar;
105 Recognizer p (&grammar, no_lines);
106
107 if (! p.parse (file_name))
108 exit (EXIT_FAILURE);
109
110 if (grammar.rules.empty())
111 {
112 qerr() << "*** Fatal. No rules!" << Qt::endl;
113 exit (EXIT_FAILURE);
114 }
115
116 else if (grammar.start == grammar.names.end ())
117 {
118 qerr() << "*** Fatal. No start symbol!" << Qt::endl;
119 exit (EXIT_FAILURE);
120 }
121
123 grammar.buildRuleMap ();
124
125 Automaton aut (&grammar);
126 aut.build ();
127
128 CppGenerator gen (p, grammar, aut, generate_report);
129 gen.setDebugInfo (debug_info);
130 gen.setUsePragmaOnce(use_pragma_once);
131 if (security)
132 gen.setSecurityHeader(std::move(*security));
133 gen.setCopyright (qt_copyright);
134 gen.setWarningsAreErrors (warnings_are_errors);
135 gen ();
136
137 if (generate_dot)
138 {
139 DotGraph genDotFile (qout());
140 genDotFile (&aut);
141 }
142
143 else if (generate_report)
144 {
145 ParseTable genParseTable (qout());
146 genParseTable(&aut);
147 }
148
149 return EXIT_SUCCESS;
150}
151
152QString Recognizer::expand (const QString &text) const
153{
154 QString code = text;
155
156 if (_M_grammar->start != _M_grammar->names.end ())
157 {
158 code = code.replace ("$start_id"_L1, QString::number (std::distance (_M_grammar->names.begin (), _M_grammar->start)));
159 code = code.replace ("$start"_L1, *_M_grammar->start);
160 }
161
162 code = code.replace ("$header"_L1, _M_grammar->table_name.toLower () + "_p.h"_L1);
163
164 code = code.replace ("$table"_L1, _M_grammar->table_name);
165 code = code.replace ("$parser"_L1, _M_grammar->table_name);
166
167 if (_M_current_rule != _M_grammar->rules.end ())
168 {
169 code = code.replace ("$rule_number"_L1, QString::number (std::distance (_M_grammar->rules.begin (), _M_current_rule)));
170 code = code.replace ("$rule"_L1, *_M_current_rule->lhs);
171 }
172
173 return code;
174}
Automaton(Grammar *g)
Definition lalr.cpp:237
void build()
Definition lalr.cpp:258
CppGenerator(const Recognizer &p, Grammar &grammar, Automaton &aut, bool verbose)
void setUsePragmaOnce(bool use)
void setWarningsAreErrors(bool e)
void setSecurityHeader(SecurityHeader header)
void setDebugInfo(bool d)
void setCopyright(bool t)
void operator()(Automaton *a)
Definition dotgraph.cpp:16
debug_infot rules
Definition lalr.h:242
Name start
Definition lalr.h:238
void buildRuleMap()
Definition lalr.cpp:194
void buildExtendedGrammar()
Definition lalr.cpp:214
void operator()(Automaton *a)
QTextStream & qout()
Definition qev.cpp:12
static void help_me()
Definition main.cpp:23
int main(int argc, char *argv[])
[ctor_close]