xref: /llvm-project/clang-tools-extra/clangd/support/Bracket.h (revision ed8f78827895050442f544edef2933a60d4a7935)
1*ed8f7882SAaron Ballman //===--- Bracket.h - Analyze bracket structure --------------------*-C++-*-===//
2*ed8f7882SAaron Ballman //
3*ed8f7882SAaron Ballman // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*ed8f7882SAaron Ballman // See https://llvm.org/LICENSE.txt for license information.
5*ed8f7882SAaron Ballman // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*ed8f7882SAaron Ballman //
7*ed8f7882SAaron Ballman //===----------------------------------------------------------------------===//
8*ed8f7882SAaron Ballman //
9*ed8f7882SAaron Ballman // Bracket structure (particularly braces) is key to isolating broken regions
10*ed8f7882SAaron Ballman // of code and preventing parsing from going "off the rails".
11*ed8f7882SAaron Ballman //
12*ed8f7882SAaron Ballman // For correct C++ code, brackets are well-nested and identifying pairs and
13*ed8f7882SAaron Ballman // therefore blocks is simple. In broken code, brackets are not properly nested.
14*ed8f7882SAaron Ballman // We cannot match them all and must choose which pairs to form.
15*ed8f7882SAaron Ballman //
16*ed8f7882SAaron Ballman // Rather than have the grammar-based parser make these choices, we pair
17*ed8f7882SAaron Ballman // brackets up-front based on textual features like indentation.
18*ed8f7882SAaron Ballman // This mirrors the way humans read code, and so is likely to produce the
19*ed8f7882SAaron Ballman // "correct" interpretation of broken code.
20*ed8f7882SAaron Ballman //
21*ed8f7882SAaron Ballman // This interpretation then guides the parse: a rule containing a bracket pair
22*ed8f7882SAaron Ballman // must match against paired bracket tokens.
23*ed8f7882SAaron Ballman //
24*ed8f7882SAaron Ballman //===----------------------------------------------------------------------===//
25*ed8f7882SAaron Ballman 
26*ed8f7882SAaron Ballman #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_BRACKET_H
27*ed8f7882SAaron Ballman #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_BRACKET_H
28*ed8f7882SAaron Ballman 
29*ed8f7882SAaron Ballman #include "Token.h"
30*ed8f7882SAaron Ballman 
31*ed8f7882SAaron Ballman namespace clang {
32*ed8f7882SAaron Ballman namespace clangd {
33*ed8f7882SAaron Ballman 
34*ed8f7882SAaron Ballman /// Identifies bracket token in the stream which should be paired.
35*ed8f7882SAaron Ballman /// Sets Token::Pair accordingly.
36*ed8f7882SAaron Ballman void pairBrackets(TokenStream &);
37*ed8f7882SAaron Ballman 
38*ed8f7882SAaron Ballman } // namespace clangd
39*ed8f7882SAaron Ballman } // namespace clang
40*ed8f7882SAaron Ballman 
41*ed8f7882SAaron Ballman #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_BRACKET_H
42