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
40 public:
41 virtual ~AbstractParserDriver() = 0;
42
43 protected:
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
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
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 void onReadErrorDeclaration(IR::Type_Error *error);
183
185 // Shared state manipulated directly by the lexer and parser.
187
190
194
197 std::string stringLiteral;
198
199 // flag to track when template args are expected, to adjust the precedence
200 // of '<'
201 bool template_args = false;
202
203 private:
205
207 bool parse(AbstractP4Lexer &lexer, std::string_view sourceFile, unsigned sourceLine = 1);
208
210 template <typename T>
211 const T *parse(P4AnnotationLexer::Type type, const Util::SourceInfo &srcInfo,
213
217 IR::Type_Error *allErrors = nullptr;
218};
219
220} // namespace P4
221
222namespace P4::V1 {
223
224class V1Lexer;
225class V1Parser;
226
229 public:
243 static const IR::V1Program *parse(std::istream &in, std::string_view sourceFile,
244 unsigned sourceLine = 1);
245 static const IR::V1Program *parse(FILE *in, std::string_view sourceFile,
246 unsigned sourceLine = 1);
247
248 protected:
249 friend class V1::V1Lexer;
250 friend class V1::V1Parser;
251
260 IR::Constant *constantFold(IR::Expression *expr);
261
264 IR::Vector<IR::Expression> makeExpressionList(const IR::NameList *list);
265
267 void clearPragmas();
268
270 void addPragma(IR::Annotation *pragma);
271
274
277 const IR::Annotations *takePragmasAsAnnotations();
278
280 // Shared state manipulated directly by the lexer and parser.
282
284 IR::V1Program *global = nullptr;
285
286 private:
288 IR::Vector<IR::Annotation> currentPragmas;
289
291};
292
293} // namespace P4::V1
294
295#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 node.h:52
Definition vector.h:58
A ParserDriver that can parse P4-16 programs.
Definition parserDriver.h:98
Util::ProgramStructure * structure
Semantic information about the program being parsed.
Definition parserDriver.h:189
void onReadErrorDeclaration(IR::Type_Error *error)
Notify that the parser parsed a P4 error declaration.
Definition parserDriver.cpp:276
static const IR::Vector< IR::Expression > * parseExpressionList(const Util::SourceInfo &srcInfo, const IR::Vector< IR::AnnotationToken > &body)
Definition parserDriver.cpp:169
IR::Vector< IR::Node > * nodes
Definition parserDriver.h:193
std::string stringLiteral
Definition parserDriver.h:197
static const IR::P4Program * parse(std::istream &in, std::string_view sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:139
Definition source_file.h:283
Definition symbol_table.h:39
Definition source_file.h:124
A ParserDriver that can parse P4-14 programs.
Definition parserDriver.h:228
void addPragma(IR::Annotation *pragma)
Add @pragma to the list of active pragmas.
Definition parserDriver.cpp:333
IR::Vector< IR::Annotation > takePragmasAsVector()
Definition parserDriver.cpp:337
static const IR::V1Program * parse(std::istream &in, std::string_view sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:291
IR::Vector< IR::Expression > makeExpressionList(const IR::NameList *list)
Definition parserDriver.cpp:325
const IR::Annotations * takePragmasAsAnnotations()
Definition parserDriver.cpp:343
IR::V1Program * global
The root of the IR tree we're constructing.
Definition parserDriver.h:284
void clearPragmas()
Clear the list of active pragmas.
Definition parserDriver.cpp:331
IR::Constant * constantFold(IR::Expression *expr)
Definition parserDriver.cpp:319
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 error.h:51