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 "clang-pseudo/grammar/Grammar.h" |
12 | #include "llvm/Support/CommandLine.h" |
13 | #include "llvm/Support/ErrorOr.h" |
14 | #include "llvm/Support/MemoryBuffer.h" |
15 | |
16 | static llvm::cl::opt<std::string> Grammar( |
17 | "grammar" , |
18 | llvm::cl::desc( |
19 | "Specify a BNF grammar file path, or a builtin language (cxx)." ), |
20 | llvm::cl::init(Val: "cxx" )); |
21 | |
22 | namespace clang { |
23 | namespace pseudo { |
24 | |
25 | const Language &getLanguageFromFlags() { |
26 | if (::Grammar == "cxx" ) |
27 | return cxx::getLanguage(); |
28 | |
29 | static Language *Lang = []() { |
30 | // Read from a bnf grammar file. |
31 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> GrammarText = |
32 | llvm::MemoryBuffer::getFile(Filename: ::Grammar); |
33 | if (std::error_code EC = GrammarText.getError()) { |
34 | llvm::errs() << "Error: can't read grammar file '" << ::Grammar |
35 | << "': " << EC.message() << "\n" ; |
36 | std::exit(status: 1); |
37 | } |
38 | std::vector<std::string> Diags; |
39 | auto G = Grammar::parseBNF(BNF: GrammarText->get()->getBuffer(), Diags); |
40 | for (const auto &Diag : Diags) |
41 | llvm::errs() << Diag << "\n" ; |
42 | auto Table = LRTable::buildSLR(G); |
43 | return new Language{ |
44 | .G: std::move(G), |
45 | .Table: std::move(Table), |
46 | .Guards: llvm::DenseMap<ExtensionID, RuleGuard>(), |
47 | .RecoveryStrategies: llvm::DenseMap<ExtensionID, RecoveryStrategy>(), |
48 | }; |
49 | }(); |
50 | return *Lang; |
51 | } |
52 | |
53 | } // namespace pseudo |
54 | } // namespace clang |
55 | |