AsmGrader 0.0.0
Loading...
Searching...
No Matches
syntax_highlighter.hpp
Go to the documentation of this file.
1
4#pragma once
5
8
9#include <fmt/base.h>
10#include <fmt/color.h>
11#include <fmt/format.h>
12#include <libassert/assert.hpp>
13#include <range/v3/algorithm/contains.hpp>
14#include <range/v3/algorithm/find.hpp>
15#include <range/v3/algorithm/find_if.hpp>
16#include <range/v3/algorithm/fold_left.hpp>
17#include <range/v3/algorithm/transform.hpp>
18#include <range/v3/iterator/concepts.hpp>
19#include <range/v3/range/access.hpp>
20#include <range/v3/range/concepts.hpp>
21#include <range/v3/range/conversion.hpp>
22#include <range/v3/range/primitives.hpp>
23#include <range/v3/view/enumerate.hpp>
24#include <range/v3/view/join.hpp>
25#include <range/v3/view/split.hpp>
26#include <range/v3/view/split_when.hpp>
27#include <range/v3/view/transform.hpp>
28
29#include <array>
30#include <charconv>
31#include <cstddef>
32#include <functional>
33#include <span>
34#include <string>
35#include <string_view>
36#include <system_error>
37#include <utility>
38#include <vector>
39
41
42using Option = std::pair<inspection::Token::Kind, fmt::text_style>;
43
45
46struct Options
47{
48 static constexpr auto num_token_kinds = static_cast<std::size_t>(EndDelimiter);
49
50 struct Opt
51 {
53 fmt::text_style style;
56 std::function<fmt::text_style(std::string_view)> style_fn;
58 std::function<std::string(std::string_view)> transform_fn;
59
60 std::string apply(std::string_view str) const;
61 };
62
64 static fmt::text_style style_basic_ident_types(fmt::text_style type_style, fmt::text_style default_style,
65 std::string_view ident);
66 static fmt::text_style style_op_keywords(fmt::text_style keyword_style, fmt::text_style default_style,
67 std::string_view op);
69 static std::string basic_binary_op_spacing(std::string_view op);
70 // Add some spacing around unary and ternary ops
71 static std::string basic_op_spacing(std::string_view op);
72
74
78 std::array<Opt, num_token_kinds> token_opts{};
79
80 constexpr Opt& operator[](std::size_t idx) { return token_opts.at(idx); }
81
82 constexpr Opt& operator[](inspection::Token::Kind kind) { return token_opts.at(static_cast<std::size_t>(kind)); }
83
84 constexpr const Opt& operator[](std::size_t idx) const { return token_opts.at(idx); }
85
86 constexpr const Opt& operator[](inspection::Token::Kind kind) const {
87 return token_opts.at(static_cast<std::size_t>(kind));
88 }
89};
90
133std::string render_blocks(std::string_view str, bool skip_styling);
134
142std::string highlight(std::string_view str, const Options& opts = Options::get_default_options());
143
146std::string highlight(const inspection::Token& token, const Options& opts = Options::get_default_options());
147
150std::string highlight(std::span<const inspection::Token> tokens, const Options& opts = Options::get_default_options());
151
152} // namespace asmgrader::highlight
Definition syntax_highlighter.hpp:40
std::string render_blocks(std::string_view str, bool skip_styling)
Parses and renders literal blocks, returning a form meant for displaying to a console user,...
Definition syntax_highlighter.cpp:428
std::pair< inspection::Token::Kind, fmt::text_style > Option
Definition syntax_highlighter.hpp:42
Definition syntax_highlighter.hpp:51
std::function< fmt::text_style(std::string_view)> style_fn
Function to call to determine the style for a token kind with a specific string. May be null....
Definition syntax_highlighter.hpp:56
std::string apply(std::string_view str) const
Definition syntax_highlighter.cpp:37
std::function< std::string(std::string_view)> transform_fn
Function to call to transform a token string. May be null.
Definition syntax_highlighter.hpp:58
fmt::text_style style
Style for the token kind.
Definition syntax_highlighter.hpp:53
Definition syntax_highlighter.hpp:47
constexpr Opt & operator[](inspection::Token::Kind kind)
Definition syntax_highlighter.hpp:82
constexpr Opt & operator[](std::size_t idx)
Definition syntax_highlighter.hpp:80
static fmt::text_style style_basic_ident_types(fmt::text_style type_style, fmt::text_style default_style, std::string_view ident)
Custom styling functions for identifiers that are types (e.g., void, int, etc.)
Definition syntax_highlighter.cpp:52
static std::string basic_binary_op_spacing(std::string_view op)
Add spacing for binary ops.
Definition syntax_highlighter.cpp:74
constexpr const Opt & operator[](inspection::Token::Kind kind) const
Definition syntax_highlighter.hpp:86
constexpr const Opt & operator[](std::size_t idx) const
Definition syntax_highlighter.hpp:84
static Options get_default_options()
Definition syntax_highlighter.cpp:115
static std::string basic_op_spacing(std::string_view op)
Definition syntax_highlighter.cpp:95
static fmt::text_style style_op_keywords(fmt::text_style keyword_style, fmt::text_style default_style, std::string_view op)
Definition syntax_highlighter.cpp:63
std::array< Opt, num_token_kinds > token_opts
Each index is representative of the highlighting option for the token kind enumerator with a value ==...
Definition syntax_highlighter.hpp:78
static constexpr auto num_token_kinds
Definition syntax_highlighter.hpp:48
Token of a very basic C++ expression. The primary use case is for rudemtary console syntax coloring.
Definition expression_inspection.hpp:57
Kind
The kind of token.
Definition expression_inspection.hpp:73