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
24#include "frontends/p4/symbol_table.h"
25#include "frontends/parsers/p4/abstractP4Lexer.hpp"
26#include "frontends/parsers/p4/p4AnnotationLexer.hpp"
27#include "ir/ir.h"
28#include "lib/cstring.h"
29#include "lib/source_file.h"
30
31namespace P4 {
32
33class P4Lexer;
34class P4Parser;
35
39 public:
40 virtual ~AbstractParserDriver() = 0;
41
42 protected:
44
46 // Callbacks.
48
54 void onReadComment(const char *text, bool lineComment);
55
57 void onReadToken(const char *text);
58
60 void onReadLineNumber(const char *text);
61
63 void onReadFileName(const char *text);
64
67
70 void onParseError(const Util::SourceInfo &location, const std::string &message);
71
73 // Shared state manipulated directly by the lexer and parser.
75
78
81
83 int saveState = -1;
84
85 private:
87 int lineDirectiveLine = 0;
88
90 cstring lineDirectiveFile;
91
93 cstring lastIdentifier;
94};
95
98 public:
112 static const IR::P4Program *parse(std::istream &in, const char *sourceFile,
113 unsigned sourceLine = 1);
114 static const IR::P4Program *parse(FILE *in, const char *sourceFile, unsigned sourceLine = 1);
115
122 // Lists /////////////////////////////////////////////////////////////////
124 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
125
126 static const IR::IndexedVector<IR::NamedExpression> *parseKvList(
127 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
128
129 static const IR::Vector<IR::Expression> *parseConstantList(
130 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
131
132 static const IR::Vector<IR::Expression> *parseConstantOrStringLiteralList(
133 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
134
135 static const IR::Vector<IR::Expression> *parseStringLiteralList(
136 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
137
138 // Singletons ////////////////////////////////////////////////////////////
139 static const IR::Expression *parseExpression(const Util::SourceInfo &srcInfo,
141
142 static const IR::Constant *parseConstant(const Util::SourceInfo &srcInfo,
144
145 static const IR::Expression *parseConstantOrStringLiteral(
146 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
147
148 static const IR::StringLiteral *parseStringLiteral(const Util::SourceInfo &srcInfo,
150
151 // Pairs /////////////////////////////////////////////////////////////////
152 static const IR::Vector<IR::Expression> *parseExpressionPair(
153 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
154
155 static const IR::Vector<IR::Expression> *parseConstantPair(
156 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
157
158 static const IR::Vector<IR::Expression> *parseStringLiteralPair(
159 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
160
161 // Triples ///////////////////////////////////////////////////////////////
162 static const IR::Vector<IR::Expression> *parseExpressionTriple(
163 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
164
165 static const IR::Vector<IR::Expression> *parseConstantTriple(
166 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
167
168 static const IR::Vector<IR::Expression> *parseStringLiteralTriple(
169 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
170
171 // P4Runtime Annotations /////////////////////////////////////////////////
172 static const IR::Vector<IR::Expression> *parseP4rtTranslationAnnotation(
173 const Util::SourceInfo &srcInfo, const IR::Vector<IR::AnnotationToken> &body);
174
175 protected:
176 friend class P4::P4Lexer;
177 friend class P4::P4Parser;
178
180 void onReadErrorDeclaration(IR::Type_Error *error);
181
183 // Shared state manipulated directly by the lexer and parser.
185
188
192
195 std::string stringLiteral;
196
197 // flag to track when template args are expected, to adjust the precedence
198 // of '<'
199 bool template_args = false;
200
201 private:
203
205 bool parse(AbstractP4Lexer &lexer, const char *sourceFile, unsigned sourceLine = 1);
206
208 template <typename T>
209 const T *parse(P4AnnotationLexer::Type type, const Util::SourceInfo &srcInfo,
211
215 IR::Type_Error *allErrors = nullptr;
216};
217
218} // namespace P4
219
220namespace V1 {
221
222class V1Lexer;
223class V1Parser;
224
227 public:
241 static const IR::V1Program *parse(std::istream &in, const char *sourceFile,
242 unsigned sourceLine = 1);
243 static const IR::V1Program *parse(FILE *in, const char *sourceFile, unsigned sourceLine = 1);
244
245 protected:
246 friend class V1::V1Lexer;
247 friend class V1::V1Parser;
248
257 IR::Constant *constantFold(IR::Expression *expr);
258
261 IR::Vector<IR::Expression> makeExpressionList(const IR::NameList *list);
262
264 void clearPragmas();
265
267 void addPragma(IR::Annotation *pragma);
268
271
274 const IR::Annotations *takePragmasAsAnnotations();
275
277 // Shared state manipulated directly by the lexer and parser.
279
281 IR::V1Program *global = nullptr;
282
283 private:
285 IR::Vector<IR::Annotation> currentPragmas;
286
288};
289
290} // namespace V1
291
292#endif /* FRONTENDS_PARSERS_PARSERDRIVER_H_ */
Definition externInstance.h:33
Definition parserDriver.h:38
void onParseError(const Util::SourceInfo &location, const std::string &message)
Definition parserDriver.cpp:101
Util::SourceInfo yylloc
The location of the most recent token.
Definition parserDriver.h:80
void onReadToken(const char *text)
Notify that the lexer read a token. @text is the matched source text.
Definition parserDriver.cpp:72
void onReadComment(const char *text, bool lineComment)
Definition parserDriver.cpp:90
void onReadFileName(const char *text)
Notify that the lexer read a filename from a #line directive.
Definition parserDriver.cpp:94
void onReadLineNumber(const char *text)
Notify that the lexer read a line number from a #line directive.
Definition parserDriver.cpp:79
int saveState
Scratch storage for the lexer to remember its previous state.
Definition parserDriver.h:83
Util::InputSources * sources
The input sources that comprise the P4 program we're parsing.
Definition parserDriver.h:77
void onReadIdentifier(cstring id)
Notify that the lexer read an identifier, @id.
Definition parserDriver.cpp:99
A ParserDriver that can parse P4-16 programs.
Definition parserDriver.h:97
Util::ProgramStructure * structure
Semantic information about the program being parsed.
Definition parserDriver.h:187
void onReadErrorDeclaration(IR::Type_Error *error)
Notify that the parser parsed a P4 error declaration.
Definition parserDriver.cpp:271
static const IR::Vector< IR::Expression > * parseExpressionList(const Util::SourceInfo &srcInfo, const IR::Vector< IR::AnnotationToken > &body)
Definition parserDriver.cpp:164
static const IR::P4Program * parse(std::istream &in, const char *sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:135
IR::Vector< IR::Node > * nodes
Definition parserDriver.h:191
std::string stringLiteral
Definition parserDriver.h:195
Definition source_file.h:263
Definition symbol_table.h:39
Definition source_file.h:126
A ParserDriver that can parse P4-14 programs.
Definition parserDriver.h:226
IR::Vector< IR::Expression > makeExpressionList(const IR::NameList *list)
Definition parserDriver.cpp:319
void clearPragmas()
Clear the list of active pragmas.
Definition parserDriver.cpp:325
const IR::Annotations * takePragmasAsAnnotations()
Definition parserDriver.cpp:337
IR::V1Program * global
The root of the IR tree we're constructing.
Definition parserDriver.h:281
IR::Vector< IR::Annotation > takePragmasAsVector()
Definition parserDriver.cpp:331
void addPragma(IR::Annotation *pragma)
Add @pragma to the list of active pragmas.
Definition parserDriver.cpp:327
static const IR::V1Program * parse(std::istream &in, const char *sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:286
IR::Constant * constantFold(IR::Expression *expr)
Definition parserDriver.cpp:313
Definition cstring.h:72
Definition applyOptionsPragmas.cpp:24