]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/Highlighter.h
MFV: r351091
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / Highlighter.h
1 //===-- Highlighter.h -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_Highlighter_h_
11 #define liblldb_Highlighter_h_
12
13 #include <utility>
14 #include <vector>
15
16 #include "lldb/Utility/Stream.h"
17 #include "lldb/lldb-enumerations.h"
18 #include "llvm/ADT/StringRef.h"
19
20 namespace lldb_private {
21
22 //----------------------------------------------------------------------
23 /// Represents style that the highlighter should apply to the given source code.
24 /// Stores information about how every kind of token should be annotated.
25 //----------------------------------------------------------------------
26 struct HighlightStyle {
27
28   //----------------------------------------------------------------------
29   /// A pair of strings that should be placed around a certain token. Usually
30   /// stores color codes in these strings (the suffix string is often used for
31   /// resetting the terminal attributes back to normal).
32   //----------------------------------------------------------------------
33   class ColorStyle {
34     std::string m_prefix;
35     std::string m_suffix;
36
37   public:
38     ColorStyle() = default;
39     ColorStyle(llvm::StringRef prefix, llvm::StringRef suffix) {
40       Set(prefix, suffix);
41     }
42
43     /// Applies this style to the given value.
44     /// \param s
45     ///     The stream to which the result should be appended.
46     /// \param value
47     ///     The value that we should place our strings around.
48     void Apply(Stream &s, llvm::StringRef value) const;
49
50     /// Sets the prefix and suffix strings.
51     /// @param prefix
52     /// @param suffix
53     void Set(llvm::StringRef prefix, llvm::StringRef suffix);
54   };
55
56   /// The style for the token which is below the cursor of the user. Note that
57   /// this style is overwritten by the SourceManager with the values of
58   /// stop-show-column-ansi-prefix/stop-show-column-ansi-suffix.
59   ColorStyle selected;
60
61   /// Matches identifiers to variable or functions.
62   ColorStyle identifier;
63   /// Matches any string or character literals in the language: "foo" or 'f'
64   ColorStyle string_literal;
65   /// Matches scalar value literals like '42' or '0.1'.
66   ColorStyle scalar_literal;
67   /// Matches all reserved keywords in the language.
68   ColorStyle keyword;
69   /// Matches any comments in the language.
70   ColorStyle comment;
71   /// Matches commas: ','
72   ColorStyle comma;
73   /// Matches one colon: ':'
74   ColorStyle colon;
75   /// Matches any semicolon: ';'
76   ColorStyle semicolons;
77   /// Matches operators like '+', '-', '%', '&', '='
78   ColorStyle operators;
79
80   /// Matches '{' or '}'
81   ColorStyle braces;
82   /// Matches '[' or ']'
83   ColorStyle square_brackets;
84   /// Matches '(' or ')'
85   ColorStyle parentheses;
86
87   //-----------------------------------------------------------------------
88   // C language specific options
89   //-----------------------------------------------------------------------
90
91   /// Matches directives to a preprocessor (if the language has any).
92   ColorStyle pp_directive;
93
94   /// Returns a HighlightStyle that is based on vim's default highlight style.
95   static HighlightStyle MakeVimStyle();
96 };
97
98 //----------------------------------------------------------------------
99 /// Annotates source code with color attributes.
100 //----------------------------------------------------------------------
101 class Highlighter {
102 public:
103   Highlighter() = default;
104   virtual ~Highlighter() = default;
105   DISALLOW_COPY_AND_ASSIGN(Highlighter);
106
107   /// Returns a human readable name for the selected highlighter.
108   virtual llvm::StringRef GetName() const = 0;
109
110   /// Highlights the given line
111   /// \param options
112   /// \param line
113   ///     The user supplied line that needs to be highlighted.
114   /// \param cursor_pos
115   ///     The cursor position of the user in this line, starting at 0 (which
116   ///     means the cursor is on the first character in 'line').
117   /// \param previous_lines
118   ///     Any previous lines the user has written which we should only use
119   ///     for getting the context of the Highlighting right.
120   /// \param s
121   ///     The stream to which the highlighted version of the user string should
122   ///     be written.
123   virtual void Highlight(const HighlightStyle &options, llvm::StringRef line,
124                          llvm::Optional<size_t> cursor_pos,
125                          llvm::StringRef previous_lines, Stream &s) const = 0;
126
127   /// Utility method for calling Highlight without a stream.
128   std::string Highlight(const HighlightStyle &options, llvm::StringRef line,
129                         llvm::Optional<size_t> cursor_pos,
130                         llvm::StringRef previous_lines = "") const;
131 };
132
133 /// A default highlighter that only highlights the user cursor, but doesn't
134 /// do any other highlighting.
135 class DefaultHighlighter : public Highlighter {
136 public:
137   llvm::StringRef GetName() const override { return "none"; }
138
139   void Highlight(const HighlightStyle &options, llvm::StringRef line,
140                  llvm::Optional<size_t> cursor_pos,
141                  llvm::StringRef previous_lines, Stream &s) const override;
142 };
143
144 /// Manages the available highlighters.
145 class HighlighterManager {
146   DefaultHighlighter m_default;
147
148 public:
149   /// Queries all known highlighter for one that can highlight some source code.
150   /// \param language_type
151   ///     The language type that the caller thinks the source code was given in.
152   /// \param path
153   ///     The path to the file the source code is from. Used as a fallback when
154   ///     the user can't provide a language.
155   /// \return
156   ///     The highlighter that wants to highlight the source code. Could be an
157   ///     empty highlighter that does nothing.
158   const Highlighter &getHighlighterFor(lldb::LanguageType language_type,
159                                        llvm::StringRef path) const;
160   const Highlighter &getDefaultHighlighter() const { return m_default; }
161 };
162
163 } // namespace lldb_private
164
165 #endif // liblldb_Highlighter_h_