Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
document_highlight.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/document_encoding.hpp>
6#include <portals/document_highlight_types.hpp>
7
8#include <array>
9#include <cstddef>
10#include <cstdint>
11#include <span>
12#include <string_view>
13
14namespace portals {
15
16namespace detail {
17
19enum class HighlightSqlMode {
20 TEXT,
21 WORD,
22 UNICODE_PREFIX,
23 IDENTIFIER,
24 NUMBER,
25 NUMBER_SUFFIX,
26 DASH,
27 SLASH,
28 LINE_COMMENT,
29 BLOCK_COMMENT,
30 SINGLE,
31 SINGLE_CLOSE,
32 DOUBLE,
33 DOUBLE_CLOSE,
34 BACKTICK,
35 BACKTICK_CLOSE,
36 MYSQL_DASH,
37 MYSQL_COMMENT_OPEN,
38 MYSQL_DIRECTIVE_GAP,
39 MYSQL_DIRECTIVE_VALUE,
40 MYSQL_DIRECTIVE_TRAIL,
41 DOLLAR_OPEN,
42 DOLLAR_BODY,
43 COPY_START,
44 COPY_ESCAPE,
45 COPY_DOT,
46 COPY_DATA
47};
48
50enum class HighlightMarkdownMode {
51 START,
52 TEXT,
53 HASH,
54 HEADING,
55 RUN,
56 INLINE,
57 INLINE_RUN,
58 FENCE_INFO,
59 FENCE_TEXT,
60 CLOSE_RUN,
61 CLOSE_SPACE
62};
63
65enum class HighlightMySqlComment {
66 NONE,
67 VERSION,
68 ACTIVE
69};
70
72enum class HighlightMySqlSet {
73 OFF,
74 TARGET,
75 TARGET_NAME,
76 VALUE,
77 VALUE_TAIL,
78 OTHER_VALUE
79};
80
82enum class HighlightMySqlTarget {
83 OTHER,
84 MODE,
85 SAVED_MODE
86};
87
90 std::array<char32_t, 64> terminator{U';'};
91 std::array<char32_t, 64> replacement{};
92 std::array<std::uint8_t, 64> matchedWidths{};
93 std::array<std::uint8_t, 64> fallback{};
94 std::array<char, 32> modeWord{};
97 std::uint32_t version{};
98 std::uint16_t setDepth{};
99 std::uint8_t terminatorLength{1};
100 std::uint8_t replacementLength{};
101 std::uint8_t matchedLength{};
102 std::uint8_t versionDigits{};
103 std::uint8_t modeWordLength{};
104 std::uint8_t atCount{};
105 HighlightMySqlComment comment{HighlightMySqlComment::NONE};
106 HighlightMySqlSet set{HighlightMySqlSet::OFF};
107 HighlightMySqlTarget target{HighlightMySqlTarget::OTHER};
112 bool lineStart{true};
114 bool starPending{};
116 bool routine{};
117 bool savedValid{};
120 bool modeLiteral{};
126};
127
131 std::uint64_t nextOffset{};
134 std::array<TokenRun, 8> queue{};
135 std::array<TokenRun, 8> speculative{};
136 std::uint8_t speculativeCount{};
137 std::uint8_t queued{};
138 std::uint8_t delivered{};
139 std::array<char32_t, 64> delimiter{};
140 std::array<char, 16> word{};
141 std::array<char, 16> infoWord{};
142 std::array<DecodedScalar, 2> suffix{};
143 std::uint8_t wordLength{};
144 std::uint8_t delimiterLength{};
145 std::uint8_t dollarMatch{};
146 std::uint8_t suffixLength{};
147 std::uint8_t numberPhase{};
148 std::uint16_t commentDepth{};
149 char32_t previous{};
150 char32_t runCharacter{};
151 char32_t fenceCharacter{};
152 std::uint8_t runLength{};
153 std::uint8_t fenceLength{};
154 std::uint8_t inlineLength{};
155 std::uint8_t indent{};
156 std::uint8_t infoLength{};
157 std::uint16_t parentheses{};
158 HighlightSqlMode sqlMode{HighlightSqlMode::TEXT};
159 HighlightMarkdownMode markdownMode{HighlightMarkdownMode::START};
161 bool escaped{};
163 bool fenced{};
164 bool sqlFence{};
167 bool capturing{};
168 bool infoStarted{};
169 bool infoEnded{};
170 bool statementStart{true};
171 bool copyHeader{};
172 bool copyFrom{};
173 bool copyStdin{};
177};
178
179} // namespace detail
180
181class DocumentHighlighter;
182
188 friend class DocumentHighlighter;
189
190 detail::HighlightState state_{};
191};
192
199public:
205 explicit DocumentHighlighter(DocumentHighlighterOptions options = {}, std::uint64_t initialByteOffset = 0) noexcept;
206
212
222 DocumentHighlightResult feed(std::span<const DecodedScalar> input, std::span<TokenRun> output,
223 bool endOfInput = false) noexcept;
224
229
233 std::uint64_t nextByteOffset() const noexcept;
234
235private:
236 detail::HighlightState state_{};
237};
238
239} // namespace portals
Definition document_highlight.hpp:187
DocumentHighlightResult feed(std::span< const DecodedScalar > input, std::span< TokenRun > output, bool endOfInput=false) noexcept
DocumentHighlightCheckpoint checkpoint() const noexcept
DocumentHighlighter(DocumentHighlighterOptions options={}, std::uint64_t initialByteOffset=0) noexcept
std::uint64_t nextByteOffset() const noexcept
DocumentHighlighter(const DocumentHighlightCheckpoint &checkpoint) noexcept
Public Portals API.
Definition action.hpp:8
DocumentLanguage
Explicit lexical profiles; unknown names resolve to PLAIN.
Definition document_highlight_types.hpp:11
@ POSTGRESQL_LEXICAL
Bounded PostgreSQL source profile, not a semantic parser.
Definition document_highlight_types.hpp:13
DocumentMySqlMode
MySQL lexical string modes supported initially and by source SET SQL_MODE.
Definition document_highlight_types.hpp:92
Decoded Unicode scalar with its exact interval in the original byte stream.
Definition document_encoding.hpp:45
Committed progress; errors leave their triggering transition uncommitted.
Definition document_highlight_types.hpp:84
Fixed-capacity policy copied into checkpoints.
Definition document_highlight_types.hpp:98
One nonempty semantic interval in original source bytes.
Definition document_highlight_types.hpp:65
Fixed MySQL client/source state, with no runtime or variable environment.
Definition document_highlight.hpp:89
bool wordVariable
Current bounded word follows @ and must not introduce a statement or keyword role.
Definition document_highlight.hpp:125
DecodedScalar star
Potential executable-comment closing star.
Definition document_highlight.hpp:96
bool modeNoBackslash
Current mode literal includes NO_BACKSLASH_ESCAPES.
Definition document_highlight.hpp:121
std::uint8_t terminatorLength
Current client delimiter length, in ASCII characters.
Definition document_highlight.hpp:99
bool lineStart
Only source-line indentation has been admitted.
Definition document_highlight.hpp:112
std::array< char32_t, 64 > replacement
DELIMITER value under construction before newline/EOF.
Definition document_highlight.hpp:91
std::array< std::uint8_t, 64 > matchedWidths
Original-byte widths of a delayed delimiter prefix.
Definition document_highlight.hpp:92
DocumentMySqlMode pendingMode
Mode to commit at the complete client terminator.
Definition document_highlight.hpp:110
std::uint32_t version
Accumulated executable-comment version gate.
Definition document_highlight.hpp:97
DocumentMySqlMode savedMode
Known @OLD_SQL_MODE value, if savedValid.
Definition document_highlight.hpp:109
bool variablePrefix
The last admitted source punctuation was an immediate variable-name @ prefix.
Definition document_highlight.hpp:124
std::array< char, 32 > modeWord
One bounded SQL_MODE item, never the whole literal.
Definition document_highlight.hpp:94
TokenRun matched
Original byte interval of the delayed delimiter prefix.
Definition document_highlight.hpp:95
bool pendingModeValid
A supported mode update awaits the client terminator.
Definition document_highlight.hpp:118
HighlightMySqlTarget target
Current assignment target.
Definition document_highlight.hpp:107
bool savedValid
One proven saved-mode slot is available.
Definition document_highlight.hpp:117
std::array< std::uint8_t, 64 > fallback
KMP proper-prefix lengths for bounded delimiter matching.
Definition document_highlight.hpp:93
bool pendingSavedValid
A supported save awaits the client terminator.
Definition document_highlight.hpp:119
HighlightMySqlComment comment
Executable wrapper phase.
Definition document_highlight.hpp:105
bool modeLiteral
A supported SQL_MODE string is being observed.
Definition document_highlight.hpp:120
bool modeWordEnded
Trailing whitespace began within a mode-list item.
Definition document_highlight.hpp:122
HighlightMySqlSet set
Observed SET clause phase.
Definition document_highlight.hpp:106
std::array< char32_t, 64 > terminator
Current bounded client statement delimiter.
Definition document_highlight.hpp:90
bool starPending
A closing star is delayed across a chunk boundary.
Definition document_highlight.hpp:114
bool routine
Current CREATE defines a stored program; SET is not executed during definition.
Definition document_highlight.hpp:116
std::uint8_t versionDigits
Retained version digit count; bound6.
Definition document_highlight.hpp:102
DocumentMySqlMode currentMode
Active session lexical mode.
Definition document_highlight.hpp:108
bool directiveSpace
Required whitespace after the DELIMITER keyword was admitted.
Definition document_highlight.hpp:123
DocumentMySqlMode pendingSavedMode
Saved mode to commit at the complete client terminator.
Definition document_highlight.hpp:111
std::uint8_t replacementLength
New directive delimiter characters retained.
Definition document_highlight.hpp:100
std::uint16_t setDepth
Parenthesis depth within unrelated SET expressions; bound65535.
Definition document_highlight.hpp:98
bool createStatement
Current statement began with CREATE.
Definition document_highlight.hpp:115
std::uint8_t matchedLength
Current terminator prefix length.
Definition document_highlight.hpp:101
bool wordLineStart
Current bounded word began at a source-line start.
Definition document_highlight.hpp:113
std::uint8_t atCount
At-sign qualification count, saturated at two.
Definition document_highlight.hpp:104
std::uint8_t modeWordLength
Retained mode-list item length; bound32.
Definition document_highlight.hpp:103
Trivially owned bounded state; size never depends on input/token/line length.
Definition document_highlight.hpp:129
HighlightMySqlState mysql
Fixed MySQL client/dump state.
Definition document_highlight.hpp:166
bool fenced
A Markdown fence is open.
Definition document_highlight.hpp:163
std::array< char, 16 > infoWord
Bounded fence language candidate, lowercased.
Definition document_highlight.hpp:141
bool infoEnded
Fence language word has ended or overflowed.
Definition document_highlight.hpp:169
TokenRun markdownPending
Delayed Markdown delimiter or close-line interval.
Definition document_highlight.hpp:133
bool eofFinalized
No further input may be admitted.
Definition document_highlight.hpp:176
std::uint8_t inlineLength
Required inline closing backtick count.
Definition document_highlight.hpp:154
std::uint16_t commentDepth
Currently open nested block comments.
Definition document_highlight.hpp:148
std::array< char32_t, 64 > delimiter
SQL tag characters, excluding dollar signs.
Definition document_highlight.hpp:139
bool escapeString
True only for explicit PostgreSQL E strings.
Definition document_highlight.hpp:160
bool lineCandidate
Delimiter began at a supported source line start.
Definition document_highlight.hpp:162
TokenRun pending
Delayed token prefix or delimiter interval.
Definition document_highlight.hpp:132
char32_t runCharacter
Current Markdown delimiter character.
Definition document_highlight.hpp:150
std::uint8_t numberPhase
Zero for integer, one for fraction, two for exponent digits.
Definition document_highlight.hpp:147
HighlightMarkdownMode markdownMode
Markdown scanner phase.
Definition document_highlight.hpp:159
std::uint8_t fenceLength
Required closing fence length.
Definition document_highlight.hpp:153
bool infoStarted
Fence info language word has begun.
Definition document_highlight.hpp:168
char32_t fenceCharacter
Opening fence character.
Definition document_highlight.hpp:151
char32_t previous
Previous comment delimiter candidate character.
Definition document_highlight.hpp:149
std::uint8_t delivered
Staged runs already copied to caller storage.
Definition document_highlight.hpp:138
std::uint8_t queued
Number of staged runs.
Definition document_highlight.hpp:137
HighlightSqlMode sqlMode
SQL scanner phase.
Definition document_highlight.hpp:158
std::array< TokenRun, 8 > speculative
SQL runs delayed while testing a fence close.
Definition document_highlight.hpp:135
bool statementStart
The next SQL word can introduce a COPY statement.
Definition document_highlight.hpp:170
bool copyHeader
A statement-start COPY header is active.
Definition document_highlight.hpp:171
bool copyHeaderCr
A header CR was admitted; an immediately following LF is still header whitespace.
Definition document_highlight.hpp:175
std::array< char, 16 > word
ASCII keyword candidate, uppercased.
Definition document_highlight.hpp:140
std::uint8_t indent
Source-line leading spaces seen, saturated at four.
Definition document_highlight.hpp:155
std::uint64_t nextOffset
Next required original-byte offset.
Definition document_highlight.hpp:131
std::uint8_t runLength
Current Markdown delimiter run length.
Definition document_highlight.hpp:152
DocumentLanguage embeddedLanguage
Recognized SQL fence language.
Definition document_highlight.hpp:165
std::uint8_t speculativeCount
Number of tentative SQL runs.
Definition document_highlight.hpp:136
bool copyStdin
COPY FROM STDIN was recognized outside parentheses.
Definition document_highlight.hpp:173
std::uint8_t suffixLength
Number of delayed numeric suffix scalars.
Definition document_highlight.hpp:146
bool copyFrom
A top-level COPY FROM token has been seen.
Definition document_highlight.hpp:172
std::uint16_t parentheses
SQL nesting depth used to identify COPY header direction.
Definition document_highlight.hpp:157
bool escaped
Next E-string scalar is escaped.
Definition document_highlight.hpp:161
std::uint8_t wordLength
Retained ASCII keyword characters.
Definition document_highlight.hpp:143
std::array< TokenRun, 8 > queue
Constant-capacity classified output staging.
Definition document_highlight.hpp:134
bool copyAwaitLine
Header semicolon was seen; next newline begins literal data.
Definition document_highlight.hpp:174
std::array< DecodedScalar, 2 > suffix
Pending numeric dot or exponent/sign.
Definition document_highlight.hpp:142
bool sqlFence
Current fence delegates its body to the same SQL scanner.
Definition document_highlight.hpp:164
std::uint8_t delimiterLength
Retained SQL tag characters or Markdown delimiter length.
Definition document_highlight.hpp:144
bool capturing
SQL output is currently speculative fence-close output.
Definition document_highlight.hpp:167
std::uint8_t infoLength
Retained fence info word length; excess is unknown/plain.
Definition document_highlight.hpp:156
std::uint8_t dollarMatch
Matched closing dollar delimiter prefix length.
Definition document_highlight.hpp:145
DocumentHighlighterOptions options
Immutable policy for this scanner generation.
Definition document_highlight.hpp:130