P4C
The P4 Compiler
Loading...
Searching...
No Matches
parserDriver.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_PARSERS_PARSERDRIVER_H_
9#define FRONTENDS_PARSERS_PARSERDRIVER_H_
10
11#include <cstdio>
12#include <iostream>
13#include <string>
14#include <string_view>
15
16#include "frontends/p4/symbol_table.h"
17#include "frontends/parsers/p4/abstractP4Lexer.hpp"
18#include "frontends/parsers/p4/p4AnnotationLexer.hpp"
19#include "ir/ir.h"
20#include "lib/cstring.h"
21#include "lib/source_file.h"
22
23namespace P4 {
24
25class P4Lexer;
26class P4Parser;
27
30class AbstractParserDriver {
31 public:
32 virtual ~AbstractParserDriver() = 0;
33
34 protected:
35 AbstractParserDriver();
36
38 // Callbacks.
40
46 void onReadComment(const char *text, bool lineComment);
47
49 void onReadToken(const char *text);
50
52 void onReadLineNumber(const char *text);
53
55 void onReadFileName(const char *text);
56
59
62 void onParseError(const Util::SourceInfo &location, const std::string &message);
63
65 // Shared state manipulated directly by the lexer and parser.
67
70
73
75 int saveState = -1;
76
77 private:
79 int lineDirectiveLine = 0;
80
82 cstring lineDirectiveFile;
83
85 cstring lastIdentifier;
86};
87
89class P4ParserDriver final : public AbstractParserDriver {
90 public:
104 static const IR::P4Program *parse(std::istream &in, std::string_view sourceFile,
105 unsigned sourceLine = 1);
106 static const IR::P4Program *parse(FILE *in, std::string_view sourceFile,
107 unsigned sourceLine = 1);
108
112 static std::pair<const IR::P4Program *, const Util::InputSources *> parseProgramSources(
113 std::istream &in, std::string_view sourceFile, unsigned sourceLine = 1);
114
115 static std::pair<const IR::P4Program *, const Util::InputSources *> parseProgramSources(
116 FILE *in, std::string_view sourceFile, unsigned sourceLine = 1);
117
124 // Lists /////////////////////////////////////////////////////////////////
126 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
127
128 static const IR::IndexedVector<IR::NamedExpression> *parseKvList(
129 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
130
131 static const IR::Vector<IR::Expression> *parseConstantList(
132 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
133
134 static const IR::Vector<IR::Expression> *parseConstantOrStringLiteralList(
135 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
136
137 static const IR::Vector<IR::Expression> *parseStringLiteralList(
138 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
139
140 // Singletons ////////////////////////////////////////////////////////////
141 static const IR::Expression *parseExpression(const Util::SourceInfo &srcInfo,
143
144 static const IR::Constant *parseConstant(const Util::SourceInfo &srcInfo,
146
147 static const IR::Expression *parseConstantOrStringLiteral(
148 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
149
150 static const IR::StringLiteral *parseStringLiteral(const Util::SourceInfo &srcInfo,
152
153 // Pairs /////////////////////////////////////////////////////////////////
154 static const IR::Vector<IR::Expression> *parseExpressionPair(
155 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
156
157 static const IR::Vector<IR::Expression> *parseConstantPair(
158 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
159
160 static const IR::Vector<IR::Expression> *parseStringLiteralPair(
161 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
162
163 // Triples ///////////////////////////////////////////////////////////////
164 static const IR::Vector<IR::Expression> *parseExpressionTriple(
165 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
166
167 static const IR::Vector<IR::Expression> *parseConstantTriple(
168 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
169
170 static const IR::Vector<IR::Expression> *parseStringLiteralTriple(
171 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
172
173 // P4Runtime Annotations /////////////////////////////////////////////////
174 static const IR::Vector<IR::Expression> *parseP4rtTranslationAnnotation(
175 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
176
177 protected:
178 friend class P4::P4Lexer;
179 friend class P4::P4Parser;
180
182 // @return true if this is the first error declaration, false if it has
183 // been combined into a previous one (and should be elided)
184 bool onReadErrorDeclaration(IR::Type_Error *error);
185
187 // Shared state manipulated directly by the lexer and parser.
189
192
195 IR::Node *result = nullptr;
196
199 std::string stringLiteral;
200
201 // flag to track when template args are expected, to adjust the precedence
202 // of '<'
203 bool template_args = false;
204
205 private:
206 P4ParserDriver();
207
209 bool parse(AbstractP4Lexer &lexer, std::string_view sourceFile, unsigned sourceLine = 1);
210
212 template <typename T>
213 const T *parse(P4AnnotationLexer::Type type, const Util::SourceInfo &srcInfo,
215
219 IR::Type_Error *allErrors = nullptr;
220};
221
222} // namespace P4
223
224namespace P4::V1 {
225
226class V1Lexer;
227class V1Parser;
228
230class V1ParserDriver final : public P4::AbstractParserDriver {
231 public:
245 static const IR::V1Program *parse(std::istream &in, std::string_view sourceFile,
246 unsigned sourceLine = 1);
247 static const IR::V1Program *parse(FILE *in, std::string_view sourceFile,
248 unsigned sourceLine = 1);
249
250 protected:
251 friend class V1::V1Lexer;
252 friend class V1::V1Parser;
253
262 IR::Constant *constantFold(IR::Expression *expr);
263
266 IR::Vector<IR::Expression> makeExpressionList(const IR::NameList *list);
267
269 void clearPragmas();
270
272 void addPragma(IR::Annotation *pragma);
273
276
278 // Shared state manipulated directly by the lexer and parser.
280
282 IR::V1Program *global = nullptr;
283
284 private:
286 IR::Vector<IR::Annotation> currentPragmas;
287
288 V1ParserDriver();
289};
290
291} // namespace P4::V1
292
293#endif /* FRONTENDS_PARSERS_PARSERDRIVER_H_ */
Definition parserDriver.h:30
void onParseError(const Util::SourceInfo &location, const std::string &message)
Definition parserDriver.cpp:105
Util::SourceInfo yylloc
The location of the most recent token.
Definition parserDriver.h:72
void onReadToken(const char *text)
Notify that the lexer read a token. @text is the matched source text.
Definition parserDriver.cpp:76
void onReadComment(const char *text, bool lineComment)
Definition parserDriver.cpp:94
void onReadFileName(const char *text)
Notify that the lexer read a filename from a line directive.
Definition parserDriver.cpp:98
void onReadLineNumber(const char *text)
Notify that the lexer read a line number from a line directive.
Definition parserDriver.cpp:83
int saveState
Scratch storage for the lexer to remember its previous state.
Definition parserDriver.h:75
Util::InputSources * sources
The input sources that comprise the P4 program we're parsing.
Definition parserDriver.h:69
void onReadIdentifier(cstring id)
Notify that the lexer read an identifier, @id.
Definition parserDriver.cpp:103
Definition indexed_vector.h:31
Definition node.h:53
Definition ir/vector.h:59
Util::ProgramStructure * structure
Semantic information about the program being parsed.
Definition parserDriver.h:191
static const IR::Vector< IR::Expression > * parseExpressionList(const Util::SourceInfo &srcInfo, const IR::Vector< IR::AnnotationToken > &body)
Definition parserDriver.cpp:193
static std::pair< const IR::P4Program *, const Util::InputSources * > parseProgramSources(std::istream &in, std::string_view sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:158
bool onReadErrorDeclaration(IR::Type_Error *error)
Notify that the parser parsed a P4 error declaration.
Definition parserDriver.cpp:300
IR::Node * result
Definition parserDriver.h:195
std::string stringLiteral
Definition parserDriver.h:199
static const IR::P4Program * parse(std::istream &in, std::string_view sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:138
Definition source_file.h:289
Definition symbol_table.h:39
Definition source_file.h:132
void addPragma(IR::Annotation *pragma)
Add @pragma to the list of active pragmas.
Definition parserDriver.cpp:357
IR::Vector< IR::Annotation > takePragmasAsVector()
Definition parserDriver.cpp:361
static const IR::V1Program * parse(std::istream &in, std::string_view sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:315
IR::Vector< IR::Expression > makeExpressionList(const IR::NameList *list)
Definition parserDriver.cpp:349
IR::V1Program * global
The root of the IR tree we're constructing.
Definition parserDriver.h:282
void clearPragmas()
Clear the list of active pragmas.
Definition parserDriver.cpp:355
IR::Constant * constantFold(IR::Expression *expr)
Definition parserDriver.cpp:343
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58