17#ifndef FRONTENDS_COMMON_PARSEINPUT_H_ 
   18#define FRONTENDS_COMMON_PARSEINPUT_H_ 
   20#include "frontends/common/options.h" 
   21#include "frontends/common/parser_options.h" 
   22#include "frontends/p4/fromv1.0/converters.h" 
   23#include "frontends/parsers/parserDriver.h" 
   32template <
typename Input, 
typename C = P4V1::Converter>
 
   33static const IR::P4Program *parseV1Program(Input stream, std::string_view sourceFile,
 
   35                                           std::optional<DebugHook> debugHook = std::nullopt) {
 
   39    if (debugHook) converter.addDebugHook(*debugHook, 
true);
 
   40    converter.loadModel();
 
   44    if (::errorCount() > 0 || v1 == 
nullptr) 
return nullptr;
 
   47    if (Log::verbose()) std::cerr << 
"Converting to P4-16" << std::endl;
 
   48    v1 = v1->apply(converter);
 
   49    if (::errorCount() > 0 || v1 == 
nullptr) 
return nullptr;
 
   50    BUG_CHECK(v1->
is<IR::P4Program>(), 
"Conversion returned %1%", v1);
 
   51    return v1->
to<IR::P4Program>();
 
   62template <
typename C = P4V1::Converter>
 
   65              "Parsing using options that don't match the current " 
   68    const IR::P4Program *result = 
nullptr;
 
   70        auto *file = fopen(options.
file.c_str(), 
"r");
 
   71        if (file == 
nullptr) {
 
   72            ::error(ErrorType::ERR_NOT_FOUND, 
"%1%: No such file or directory.", options.
file);
 
   75        result = options.
isv1() ? parseV1Program<FILE *, C>(file, options.
file.string(), 1,
 
   80        auto preprocessorResult = options.
preprocess();
 
   81        if (::errorCount() > 0 || !preprocessorResult.has_value()) {
 
   87                ? parseV1Program<FILE *, C>(preprocessorResult.value().get(), options.
file.string(),
 
   92    if (::errorCount() > 0) {
 
   93        ::error(ErrorType::ERR_OVERLIMIT, 
"%1% errors encountered, aborting compilation",
 
   97    BUG_CHECK(result != 
nullptr, 
"Parsing failed, but we didn't report an error");
 
 
  111const IR::P4Program *
parseP4String(
const char *sourceFile, 
unsigned sourceLine,
 
  112                                   const std::string &input,
 
  113                                   CompilerOptions::FrontendVersion version);
 
  115                                   CompilerOptions::FrontendVersion version);
 
static const IR::P4Program * parse(std::istream &in, std::string_view sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:135
 
static P4CContext & get()
Definition parser_options.cpp:530
 
Definition parser_options.h:40
 
DebugHook getDebugHook() const
Definition parser_options.cpp:524
 
std::optional< ParserOptions::PreprocessorResult > preprocess() const
Returns the output of the preprocessor.
Definition parser_options.cpp:420
 
std::filesystem::path file
file to compile (- for stdin)
Definition parser_options.h:69
 
bool isv1() const
True if we are compiling a P4 v1.0 or v1.1 program.
Definition parser_options.cpp:469
 
bool doNotPreprocess
if true skip preprocess
Definition parser_options.h:75
 
static const IR::V1Program * parse(std::istream &in, std::string_view sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:287
 
Definition applyOptionsPragmas.cpp:24
 
const IR::P4Program * parseP4String(const char *sourceFile, unsigned sourceLine, const std::string &input, CompilerOptions::FrontendVersion version)
Definition parseInput.cpp:28
 
const IR::P4Program * parseP4File(const ParserOptions &options)
Definition parseInput.h:63
 
T * to() noexcept
Definition rtti.h:226
 
bool is() const noexcept
Definition rtti.h:216