1 //===--- CLI.cpp - ----------------------------------------------*- C++-*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang-pseudo/cli/CLI.h" 10 #include "clang-pseudo/cxx/CXX.h" 11 #include "llvm/Support/CommandLine.h" 12 #include "llvm/Support/ErrorOr.h" 13 #include "llvm/Support/MemoryBuffer.h" 14 15 static llvm::cl::opt<std::string> Grammar( 16 "grammar", 17 llvm::cl::desc( 18 "Specify a BNF grammar file path, or a builtin language (cxx)."), 19 llvm::cl::init("cxx")); 20 21 namespace clang { 22 namespace pseudo { 23 24 const Language &getLanguageFromFlags() { 25 if (::Grammar == "cxx") 26 return cxx::getLanguage(); 27 28 static Language *Lang = []() { 29 // Read from a bnf grammar file. 30 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> GrammarText = 31 llvm::MemoryBuffer::getFile(::Grammar); 32 if (std::error_code EC = GrammarText.getError()) { 33 llvm::errs() << "Error: can't read grammar file '" << ::Grammar 34 << "': " << EC.message() << "\n"; 35 std::exit(1); 36 } 37 std::vector<std::string> Diags; 38 auto G = Grammar::parseBNF(GrammarText->get()->getBuffer(), Diags); 39 for (const auto &Diag : Diags) 40 llvm::errs() << Diag << "\n"; 41 auto Table = LRTable::buildSLR(G); 42 return new Language{std::move(G), std::move(Table)}; 43 }(); 44 return *Lang; 45 } 46 47 } // namespace pseudo 48 } // namespace clang 49