The Brace Session
A brace session holds a text that is being edited, and the brace answers for it. After every edit the answers are those of the text as it now is, so they can never go stale. A session also keeps a suggested missing brace from jumping about while the user types, and it can ask a compiler which repairs of a broken text work. The declarations are in brace_session.h; the answers themselves are described on Braces.
1. A session's life
BraceSession *session = brace_session_create(brace_match_proven);
session->lexer_options = lexer_options_for_path(path);
session->path = path; /* for #include "..." */
brace_session_load(session, text, length);
for (size_t index = 0; index < session->answer_count; index++) {
const BraceAnswer *answer = &session->answers[index];
const Token *brace = &session->lexer.tokens[answer->token];
/* answer->partner, answer->confidence, answer->ghost_offset ... */
}
brace_session_edit(session, offset, removed, inserted, inserted_length); /* after each edit */
brace_session_destroy(session);
| Call | What it does |
|---|---|
brace_session_create(match) |
Makes a session with an empty text. match is the matcher, such as brace_match_proven; NULL means brace_match_preprocessor. Returns NULL when out of memory. |
brace_session_load(session, text, length) |
Starts over with a text (a file was opened). The session keeps its own copy, followed by a NUL, in session->text. |
brace_session_edit(session, offset, removed, inserted, inserted_length) |
Replaces removed bytes at offset with the inserted text (which may be NULL when its length is 0). Fails with -1 when the edit reaches outside the text. |
brace_session_replace(session, text, length) |
For an editor that only says "the text changed": the session finds the one replacement between the old text and the new one (what the two share at the start and at the end stays) and makes that edit. The same text again does nothing. |
brace_session_destroy(session) |
Frees the session and everything in it. NULL does nothing. |
Each call that changes the text lexes the whole text again, runs the matcher, and returns 0, or -1 when out of memory. The settings are fields that the program sets before loading; they are read at every edit, and the strings stay the program's own:
| Setting | What it does |
|---|---|
lexer_options |
The lexer options (lexer_options_for_path()). LEXER_CPLUSPLUS also tells the matcher to read C++. |
language |
The language file the text is lexed by, such as a user's own c.lang, or NULL for Kami's built-in C or C++ (lexer_run()). A language whose engine is c++ makes the matcher read C++ too, as LEXER_CPLUSPLUS does. |
path, defines, predefines, include_directories |
What is known of the file and its build, as in BraceInput: the proof needs them to preprocess the file as the compiler does. |
The fields a program reads are text and length, lexer (the tokens), answers and answer_count (one answer for each brace that pairs, in token order), and after a compiler check repairs and repair_count. ghosts and ghost_count are where the guessed ghosts are (each a BraceGhostPlace: the brace's offset and the ghost's offset). A session's matcher reads C; for another language, call brace_match_language() on the language's tokens instead.
2. Answers from the text alone
Every answer comes from the text as it is. Nothing remembered from an earlier text, no pair carried from edit to edit, no history file and no earlier version, can overrule the matcher. The same text always gets the same answers, however it was reached: by typing, by undo, or by opening it again. Only one thing is kept from edit to edit, and it never decides whether a brace is missing:
One edit. The answers are exactly those that brace_session_load() would give the new text, apart from where a guessed ghost is shown.
Ghosts that stay. When a brace has a ghost (its partner is BRACE_GHOST), the matcher suggests a most likely place, and that place can change with every key the user presses. The session remembers each ghost's place, as byte offsets that move with every edit (a place inside the removed bytes moves to the edit's offset). After the next edit, a brace that still has a ghost keeps its earlier place, as long as the matcher's new range still holds it; otherwise it takes the matcher's new place. ghost_offset is where the ghost is; ghost_gap is the token at or after it.
3. The compiler check
When the code is broken, the layout can only guess. A compiler can do better: of all the ways to repair the text with one brace, it can say which compile. brace_session_verify() asks the program's own compiler function, so Kami never runs a program itself:
static int compiles(const char *text, size_t length, void *context) {
/* write the text to a file and run cc -fsyntax-only on it, in a sandbox */
return 1; /* when it compiled with no errors */
}
int changed = brace_session_verify(session, compiles, NULL, 200);
- The text itself is compiled first. When it compiles, or has no braces, nothing happens.
- Every single-brace repair that the uncertain answers allow is gathered: the missing brace at each gap of each range, a
{inside a word (where a deleted brace may have joined two words), and each uncertain brace taken out. A missing brace goes right after the code before its gap (" }"), or on a line of its own ("}\n") when what comes before is a directive or nothing. When there are more repairs thanbudget, the check gives up and claims nothing. - They are tried the most likely first: deletions, then a
{after a header (after any), as after the header of a function,if,for,whileorswitch, or afterelseordo), then the rest nearest the matcher's ghosts, and a{inside a word last. The search stops once nothing more can become certain and nine repairs that compile are known. - When no single-brace repair compiles, each uncertain brace is moved to each gap of its range instead: a brace in the wrong place is one mistake too.
- What all the repairs that compile agree on for a brace becomes certain, with
BRACE_REASON_COMPILER: its partner, its ghost's place, orBRACE_EXTRAwhen every repair deletes it. When what they agree on for any brace contradicts an answer that is already certain, no brace becomes certain. For every brace that does not become certain, a ghost moves to the nearest place that compiles (a place after a header first), andmay_be_extrais set exactly when deleting the brace compiles.
The certainty assumes one mistake, and a moved brace counts as one. The call returns how many changes it made to the answers (one answer can count twice: once for may_be_extra, once for its ghost); 0 when the text has no braces, when it compiles, when there are more repairs than the budget, or when nothing helped; and -1 when out of memory. The budget counts the repairs; the compile of the text itself comes on top. The changed answers last until the next edit.
The repairs. Afterwards session->repairs holds up to 64 repairs that compiled, ranked: a { after a header first, a { inside a word last, else the nearest to where the matcher put a ghost. Each BraceRepair says where text goes in (at, a byte offset), what goes in (text: " }" or " {" after code, "}\n" or "{\n" on a line of its own, "{" inside a word, or ""), which brace is taken out (removed, or BRACE_SESSION_LOST; a move does both), and which brace the inserted one pairs with (partner, or BRACE_SESSION_LOST; it is always BRACE_SESSION_LOST for a move). The next edit clears them.
Compiling side by side. brace_session_verify_batch() is the same check with a compiler function that takes up to 32 texts at once (BraceCompileBatchFunction): it sets compiles[k] to 1 for each text k that compiles. The function may compile them in parallel.
Two sessions can be loaded, edited and checked on two threads at the same time; one session must be used by one thread at a time.
Kami API
The whole library
Reading C and C++
Braces
Any language
