brace_session.h
A Text Being Edited, and Its Brace Answers (layer 3)
Copyright (c) 2016-2026 Michael Grey (liquibyte) liquibyte@gmail.com SPDX-License-Identifier: Artistic-2.0 Distributed under the Artistic License 2.0; see LICENSE for terms.
A session holds a text that is being edited, and the brace answers for it. Every answer comes from the text as it is: after each edit the text is lexed again and the matcher answers again, and nothing remembered from an earlier text (no pair carried from edit to edit, no history file, no earlier version) can overrule it. The same text always gets the same answers, however it was reached.
Features:
- Edits as replacements, or by handing over the whole new text
- A guessed ghost (where a missing brace most likely goes) stays where it is from one edit to the next while the matcher's range still allows that place, so it does not jump about while the user types. It never decides whether a brace is missing, only where the missing one is shown
brace_session_verify(): a compiler judges the repairs that the answers allow
Internal design notes:
- Positions are byte offsets in the text. Every edit is one replacement (an offset, the bytes removed, the text inserted);
brace_session_replace()finds that replacement between the old text and the new one itself - The whole text is lexed again after an edit: simpler than lexing only the changed stretch, and fast enough for the files an editor opens
#include "brace_session.h"
Types: BraceRepair, BraceGhostPlace, BraceMatchFunction, BraceSession, BraceCompileFunction, BraceCompileBatchFunction
Constants: BRACE_SESSION_LOST
Functions: brace_session_create, brace_session_destroy, brace_session_load, brace_session_edit, brace_session_replace, brace_session_verify, brace_session_verify_batch
Types
BraceRepair
A repair that the compiler accepted (brace_session_verify()). Offsets are in the session's text.
| Field | Type | Description |
|---|---|---|
at |
uint32_t |
the offset where text goes in (it means nothing when text is empty) |
text[3] |
char |
what goes in: " }" or " {" after code, "}\n" or "{\n" on a line of its own, "{" inside a word, or "" for none |
removed |
uint32_t |
the offset of a brace that the repair takes out, or BRACE_SESSION_LOST (a repair that moves a brace both takes out and puts in) |
partner |
uint32_t |
the offset of the brace that the inserted one pairs with, or BRACE_SESSION_LOST (always, for a move) |
BraceGhostPlace
Where a guessed ghost was put, so that it can stay there.
| Field | Type | Description |
|---|---|---|
brace |
uint32_t |
the offset of the brace the ghost belongs to |
ghost |
uint32_t |
the offset the ghost goes before |
BraceMatchFunction
typedef void (*BraceMatchFunction)(const BraceInput *input, BraceAnswer *answers, size_t count);
A matcher's function, as in BraceMatcher.match: fills in the answers for the braces of input.
BraceSession
A text being edited, its tokens and its answers. Read the fields; change only the settings, before loading.
| Field | Type | Description |
|---|---|---|
text |
char * |
the text, followed by a NUL (the session's own) |
length |
size_t |
its length in bytes |
lexer |
Lexer |
its tokens |
answers |
BraceAnswer * |
one answer for each brace that pairs, in token order |
answer_count |
size_t |
how many answers there are |
ghosts |
BraceGhostPlace * |
where the guessed ghosts are |
ghost_count |
size_t |
how many ghost places there are |
match |
BraceMatchFunction |
the matcher: every answer is its answer |
lexer_options |
unsigned |
a setting: the lexer options (lexer_options_for_path()); LEXER_CPLUSPLUS also makes the matcher read C++, with a language file too |
language |
const Language * |
a setting: the language file the text is lexed by (such as the user's c.lang), or NULL for Kami's built-in C or C++; one whose engine is c++ makes the matcher read C++ as LEXER_CPLUSPLUS does |
defines |
const char * |
a setting: the build's -D and -U, as in BraceInput.defines, or NULL (the caller's) |
path |
const char * |
a setting: the file's path, for the proof's include "..." (the caller's), or NULL |
predefines |
const char * |
a setting: the compiler's macros (the output of cc -dM -E) for the proof (the caller's), or NULL |
include_directories |
const char * |
a setting: the include directories, one per line (the caller's), or NULL |
repairs |
BraceRepair * |
after brace_session_verify(): the repairs that compile, the most likely first (the next edit clears them) |
repair_count |
size_t |
how many repairs there are |
BraceCompileFunction
typedef int (*BraceCompileFunction)(const char *text, size_t length, void *context);
A compiler, as brace_session_verify() calls it (for example cc -fsyntax-only on the text)
BraceCompileBatchFunction
typedef void (*BraceCompileBatchFunction)(const char *const *texts, const size_t *lengths, size_t count, int *compiles, void *context);
A compiler that takes several texts at once, as brace_session_verify_batch() calls it.
It may compile them side by side. It sets compiles[k] to 1 for each text k that compiles with no errors, and to 0 for the others.
Constants
BRACE_SESSION_LOST
#define BRACE_SESSION_LOST UINT32_MAX
No offset: the value of a field that names none.
Functions
brace_session_create
BraceSession *brace_session_create(BraceMatchFunction match);
Create a session, with an empty text and no answers.
| Parameter | Description |
|---|---|
match |
The matcher (such as brace_match_proven), or NULL for brace_match_preprocessor |
Returns: The session, or NULL when out of memory
brace_session_destroy
void brace_session_destroy(BraceSession *session);
Destroy a session and everything it holds.
| Parameter | Description |
|---|---|
session |
The session, or NULL (which does nothing) |
brace_session_load
int brace_session_load(BraceSession *session, const char *text, size_t length);
Start over with a text (a file was opened), and answer.
| Parameter | Description |
|---|---|
session |
The session |
text |
The text; the session keeps a copy |
length |
Its length in bytes |
Returns: 0, or -1 when out of memory or session is NULL
brace_session_edit
int brace_session_edit(
BraceSession *session,
size_t offset,
size_t removed,
const char *inserted,
size_t inserted_length);
Replace bytes of the text with other text, and answer again.
| Parameter | Description |
|---|---|
session |
The session |
offset |
Where the edit starts |
removed |
How many bytes it removes |
inserted |
The text it inserts (it may be NULL when inserted_length is 0) |
inserted_length |
The inserted text's length in bytes |
Returns: 0, or -1 when out of memory, or when the edit reaches outside the text
brace_session_replace
int brace_session_replace(BraceSession *session, const char *text, size_t length);
Make the text equal to a new text, and answer again.
For an editor that only says "the text changed". The answers are the same as brace_session_load() would give the new text; only where guessed ghosts are kept follows the change. The same text again does nothing.
| Parameter | Description |
|---|---|
session |
The session |
text |
The new text |
length |
Its length in bytes |
Returns: 0, or -1 when out of memory or session is NULL
brace_session_verify
int brace_session_verify(
BraceSession *session,
BraceCompileFunction compile,
void *context,
size_t budget);
Let a compiler judge the repairs that the answers allow.
When the text does not compile, every single-brace repair that the uncertain answers allow is compiled: the missing brace at each gap of a range, a { inside a word (where a deletion joined two words), and each uncertain brace taken out. When none of these compiles, each uncertain brace is moved to each gap of its range instead. What all the repairs that compile agree on for a brace becomes certain (BRACE_REASON_COMPILER). When that contradicts an answer that already is certain, no brace becomes certain. For every brace that does not become certain, a ghost moves to the nearest place that compiles, and may_be_extra is set exactly when taking the brace out compiles. This certainty assumes one mistake (a move counts as one). The changed answers and the repairs last until the next edit.
| Parameter | Description |
|---|---|
session |
The session |
compile |
The compiler |
context |
Passed to compile |
budget |
The most repairs to try: each is one compile (the text's own compile comes on top) |
Returns: How many changes were made to the answers (an answer can count twice: may_be_extra and its ghost); 0 when the text has no braces, when it compiles, when there are more repairs than the budget allows, or when nothing helped; -1 when out of memory
brace_session_verify_batch
int brace_session_verify_batch(
BraceSession *session,
BraceCompileBatchFunction compile_batch,
void *context,
size_t budget);
brace_session_verify(), handing the compiler up to 32 texts at a time
The repairs are tried the most likely first, and the search stops once nothing more can become certain and nine repairs that compile are known.
| Parameter | Description |
|---|---|
session |
The session |
compile_batch |
The compiler |
context |
Passed to compile_batch |
budget |
The most repairs to try |
Returns: As brace_session_verify()
Kami API
The whole library
Reading C and C++
Braces
Any language