17#ifndef FRONTENDS_COMMON_PARSEINPUT_H_
18#define FRONTENDS_COMMON_PARSEINPUT_H_
20#include "frontends/common/options.h"
21#include "frontends/p4/fromv1.0/converters.h"
22#include "frontends/p4/frontend.h"
23#include "frontends/parsers/parserDriver.h"
25#include "lib/source_file.h"
33template <
typename Input,
typename C = P4V1::Converter>
34static const IR::P4Program *parseV1Program(Input &stream,
const char *sourceFile,
36 std::optional<DebugHook> debugHook = std::nullopt) {
40 if (debugHook) converter.addDebugHook(*debugHook,
true);
41 converter.loadModel();
45 if (::errorCount() > 0 || v1 ==
nullptr)
return nullptr;
48 if (Log::verbose()) std::cerr <<
"Converting to P4-16" << std::endl;
49 v1 = v1->apply(converter);
50 if (::errorCount() > 0 || v1 ==
nullptr)
return nullptr;
51 BUG_CHECK(v1->is<IR::P4Program>(),
"Conversion returned %1%", v1);
52 return v1->to<IR::P4Program>();
63template <
typename C = P4V1::Converter>
66 "Parsing using options that don't match the current "
69 if (options.doNotPreprocess) {
70 in = fopen(options.file,
"r");
72 ::error(ErrorType::ERR_NOT_FOUND,
"%1%: No such file or directory.", options.file);
76 in = options.preprocess();
77 if (::errorCount() > 0 || in ==
nullptr)
return nullptr;
80 auto result = options.isv1()
81 ? parseV1Program<FILE *, C>(in, options.file, 1, options.getDebugHook())
83 if (options.doNotPreprocess) {
86 options.closePreprocessedInput(in);
89 if (::errorCount() > 0) {
90 ::error(ErrorType::ERR_OVERLIMIT,
"%1% errors encountered, aborting compilation",
94 BUG_CHECK(result !=
nullptr,
"Parsing failed, but we didn't report an error");
108const IR::P4Program *
parseP4String(
const char *sourceFile,
unsigned sourceLine,
109 const std::string &input,
110 CompilerOptions::FrontendVersion version);
112 CompilerOptions::FrontendVersion version);
static const IR::P4Program * parse(std::istream &in, const char *sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:135
static P4CContext & get()
Definition parser_options.cpp:522
Definition parser_options.h:39
static const IR::V1Program * parse(std::istream &in, const char *sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:286
Definition applyOptionsPragmas.cpp:24
const IR::P4Program * parseP4String(const char *sourceFile, unsigned sourceLine, const std::string &input, CompilerOptions::FrontendVersion version)
Definition parseInput.cpp:32
const IR::P4Program * parseP4File(ParserOptions &options)
Definition parseInput.h:64