1 //===--- Language.h -------------------------------------------- -*- 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 #ifndef CLANG_PSEUDO_LANGUAGE_H
10 #define CLANG_PSEUDO_LANGUAGE_H
11 
12 #include "clang-pseudo/Token.h"
13 #include "clang-pseudo/grammar/Grammar.h"
14 #include "clang-pseudo/grammar/LRTable.h"
15 
16 namespace clang {
17 namespace pseudo {
18 class ForestNode;
19 class TokenStream;
20 class LRTable;
21 
22 struct GuardParams {
23   llvm::ArrayRef<const ForestNode *> RHS;
24   const TokenStream &Tokens;
25   // FIXME: use the index of Tokens.
26   SymbolID Lookahead;
27 };
28 // A guard restricts when a grammar rule can be used.
29 //
30 // The GLR parser will use the guard to determine whether a rule reduction will
31 // be conducted. For example, e.g. a guard may allow the rule
32 // `virt-specifier := IDENTIFIER` only if the identifier's text is 'override`.
33 //
34 // Return true if the guard is satisfied.
35 using RuleGuard = llvm::function_ref<bool(const GuardParams &)>;
36 
37 // A recovery strategy determines a region of code to skip when parsing fails.
38 //
39 // For example, given `class-def := CLASS IDENT { body [recover=Brackets] }`,
40 // if parsing fails while attempting to parse `body`, we may skip up to the
41 // matching `}` and assume everything between was a `body`.
42 //
43 // The provided index is the token where the skipped region begins.
44 // Returns the (excluded) end of the range, or Token::Invalid for no recovery.
45 using RecoveryStrategy =
46     llvm::function_ref<Token::Index(Token::Index Start, const TokenStream &)>;
47 
48 // Specify a language that can be parsed by the pseduoparser.
49 struct Language {
50   Grammar G;
51   LRTable Table;
52 
53   // Binding extension ids to corresponding implementations.
54   llvm::DenseMap<RuleID, RuleGuard> Guards;
55   llvm::DenseMap<ExtensionID, RecoveryStrategy> RecoveryStrategies;
56 
57   // FIXME: add clang::LangOptions.
58   // FIXME: add default start symbols.
59 };
60 
61 } // namespace pseudo
62 } // namespace clang
63 
64 #endif // CLANG_PSEUDO_LANGUAGE_H
65