P4C
The P4 Compiler
Loading...
Searching...
No Matches
parserDriver.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef FRONTENDS_PARSERS_PARSERDRIVER_H_
18#define FRONTENDS_PARSERS_PARSERDRIVER_H_
19
20#include <cstdio>
21#include <iostream>
22#include <string>
23#include <string_view>
24
25#include "frontends/p4/symbol_table.h"
26#include "frontends/parsers/p4/abstractP4Lexer.hpp"
27#include "frontends/parsers/p4/p4AnnotationLexer.hpp"
28#include "ir/ir.h"
29#include "lib/cstring.h"
30#include "lib/source_file.h"
31
32namespace P4 {
33
34class P4Lexer;
35class P4Parser;
36
39class AbstractParserDriver {
40 public:
41 virtual ~AbstractParserDriver() = 0;
42
43 protected:
44 AbstractParserDriver();
45
47 // Callbacks.
49
55 void onReadComment(const char *text, bool lineComment);
56
58 void onReadToken(const char *text);
59
61 void onReadLineNumber(const char *text);
62
64 void onReadFileName(const char *text);
65
68
71 void onParseError(const Util::SourceInfo &location, const std::string &message);
72
74 // Shared state manipulated directly by the lexer and parser.
76
79
82
84 int saveState = -1;
85
86 private:
88 int lineDirectiveLine = 0;
89
91 cstring lineDirectiveFile;
92
94 cstring lastIdentifier;
95};
96
98class P4ParserDriver final : public AbstractParserDriver {
99 public:
113 static const IR::P4Program *parse(std::istream &in, std::string_view sourceFile,
114 unsigned sourceLine = 1);
115 static const IR::P4Program *parse(FILE *in, std::string_view sourceFile,
116 unsigned sourceLine = 1);
117
121 static std::pair<const IR::P4Program *, const Util::InputSources *> parseProgramSources(
122 std::istream &in, std::string_view sourceFile, unsigned sourceLine = 1);
123
124 static std::pair<const IR::P4Program *, const Util::InputSources *> parseProgramSources(
125 FILE *in, std::string_view sourceFile, unsigned sourceLine = 1);
126
133 // Lists /////////////////////////////////////////////////////////////////
135 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
136
137 static const IR::IndexedVector<IR::NamedExpression> *parseKvList(
138 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
139
140 static const IR::Vector<IR::Expression> *parseConstantList(
141 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
142
143 static const IR::Vector<IR::Expression> *parseConstantOrStringLiteralList(
144 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
145
146 static const IR::Vector<IR::Expression> *parseStringLiteralList(
147 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
148
149 // Singletons ////////////////////////////////////////////////////////////
150 static const IR::Expression *parseExpression(const Util::SourceInfo &srcInfo,
152
153 static const IR::Constant *parseConstant(const Util::SourceInfo &srcInfo,
155
156 static const IR::Expression *parseConstantOrStringLiteral(
157 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
158
159 static const IR::StringLiteral *parseStringLiteral(const Util::SourceInfo &srcInfo,
161
162 // Pairs /////////////////////////////////////////////////////////////////
163 static const IR::Vector<IR::Expression> *parseExpressionPair(
164 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
165
166 static const IR::Vector<IR::Expression> *parseConstantPair(
167 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
168
169 static const IR::Vector<IR::Expression> *parseStringLiteralPair(
170 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
171
172 // Triples ///////////////////////////////////////////////////////////////
173 static const IR::Vector<IR::Expression> *parseExpressionTriple(
174 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
175
176 static const IR::Vector<IR::Expression> *parseConstantTriple(
177 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
178
179 static const IR::Vector<IR::Expression> *parseStringLiteralTriple(
180 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
181
182 // P4Runtime Annotations /////////////////////////////////////////////////
183 static const IR::Vector<IR::Expression> *parseP4rtTranslationAnnotation(
184 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
185
186 protected:
187 friend class P4::P4Lexer;
188 friend class P4::P4Parser;
189
191 // @return true if this is the first error declaration, false if it has
192 // been combined into a previous one (and should be elided)
193 bool onReadErrorDeclaration(IR::Type_Error *error);
194
196 // Shared state manipulated directly by the lexer and parser.
198
201
204 IR::Node *result = nullptr;
205
208 std::string stringLiteral;
209
210 // flag to track when template args are expected, to adjust the precedence
211 // of '<'
212 bool template_args = false;
213
214 private:
215 P4ParserDriver();
216
218 bool parse(AbstractP4Lexer &lexer, std::string_view sourceFile, unsigned sourceLine = 1);
219
221 template <typename T>
222 const T *parse(P4AnnotationLexer::Type type, const Util::SourceInfo &srcInfo,
224
228 IR::Type_Error *allErrors = nullptr;
229};
230
231} // namespace P4
232
233namespace P4::V1 {
234
235class V1Lexer;
236class V1Parser;
237
239class V1ParserDriver final : public P4::AbstractParserDriver {
240 public:
254 static const IR::V1Program *parse(std::istream &in, std::string_view sourceFile,
255 unsigned sourceLine = 1);
256 static const IR::V1Program *parse(FILE *in, std::string_view sourceFile,
257 unsigned sourceLine = 1);
258
259 protected:
260 friend class V1::V1Lexer;
261 friend class V1::V1Parser;
262
271 IR::Constant *constantFold(IR::Expression *expr);
272
275 IR::Vector<IR::Expression> makeExpressionList(const IR::NameList *list);
276
278 void clearPragmas();
279
281 void addPragma(IR::Annotation *pragma);
282
285
287 // Shared state manipulated directly by the lexer and parser.
289
291 IR::V1Program *global = nullptr;
292
293 private:
295 IR::Vector<IR::Annotation> currentPragmas;
296
297 V1ParserDriver();
298};
299
300} // namespace P4::V1
301
302#endif /* FRONTENDS_PARSERS_PARSERDRIVER_H_ */
Definition parserDriver.h:39
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:81
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:84
Util::InputSources * sources
The input sources that comprise the P4 program we're parsing.
Definition parserDriver.h:78
void onReadIdentifier(cstring id)
Notify that the lexer read an identifier, @id.
Definition parserDriver.cpp:103
Definition indexed_vector.h:40
Definition node.h:53
Definition ir/vector.h:59
Util::ProgramStructure * structure
Semantic information about the program being parsed.
Definition parserDriver.h:200
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:204
std::string stringLiteral
Definition parserDriver.h:208
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:291
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:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58