brace.h
Which Brace Goes with Which: Answers and Matchers.
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 matcher reads a file's tokens and gives one BraceAnswer for each brace. brace_collect() lists the braces (a brace on a directive line is not paired: it belongs to a macro), and the matcher fills in each answer.
Features:
- For each brace: its partner, how sure that is, and the reasons
- A brace with no partner has a "ghost" partner: the range where the missing brace can be, and its most likely place
- Several matchers, the best first: proven by the language (C), by layout in every if configuration, by layout alone, by depth alone
- Parentheses and brackets, paired statement by statement
- Every bracket of a language other than C, paired by nesting when the file balances (
brace_match_language())
Positions:
- The partner of an answer is an index into the same answer array.
- The range and the ghost are token gaps: gap g is just before token g, and gap token_count is the end of the file. A ghost's range [range_low, range_high] holds the gaps where the missing partner can be; a real partner at token t is in the range when range_low <= t <= range_high.
#include "brace.h"
Types: BraceConfidence, BraceInput, BRACE_REASON_AGREE and the other values, BraceAnswer, BraceMatcher
Constants: BRACE_GHOST, BRACE_EXTRA, BRACE_IN_MACRO, BRACE_REASON_LAYOUT_DISAGREES, brace_matchers, brace_matcher_count
Functions: brace_reason_text, brace_is_pairable, brace_collect, brace_match_layout, brace_match_configurations, brace_match_preprocessor, brace_match_proven, brace_collect_delimiters, brace_match_delimiters, brace_match_language, brace_match_depth
Types
BraceConfidence
How sure an answer is.
| Name | Value | Description |
|---|---|---|
BRACE_CERTAIN |
the language, or the evidence, leaves no other pairing | |
BRACE_LIKELY |
the best answer, with a range that the partner is in | |
BRACE_AMBIGUOUS |
the evidence does not choose |
BraceInput
What a matcher reads: a file's tokens, and what is known of its build.
| Field | Type | Description |
|---|---|---|
tokens |
const Token * |
the file's tokens (lexer.h) |
token_count |
size_t |
how many tokens there are |
source |
const char * |
the file's text; a brace token may have length 0 (inserted by a program) |
cplusplus |
int |
1 to read C++ rules too (lambdas, Type{...}, the range for); normally from LEXER_CPLUSPLUS |
defines |
const char * |
what the build defines: lines "+NAME=value" (-D) and "-NAME" (-U), or NULL |
path |
const char * |
the file's path (its include "..." are looked for beside it), or NULL |
predefines |
const char * |
the compiler's own macros (the output of cc -dM -E), or NULL for a built-in set |
include_directories |
const char * |
the directories for include, one per line, or NULL |
BRACE_REASON_AGREE and the other values
Why an answer is what it is; a BraceAnswer carries a set of these bits. brace_reason_text() says each in a sentence.
| Name | Value | Description |
|---|---|---|
BRACE_REASON_AGREE |
1 << 0 |
nesting and indentation agree on the pair |
BRACE_REASON_UNMATCHED |
1 << 1 |
counting braces leaves it without a partner |
BRACE_REASON_CLOSER_COLUMN |
1 << 2 |
the } is not at the indentation of the line that opens the block |
BRACE_REASON_BODY_SHALLOW |
1 << 3 |
a line inside the block is not indented deeper than the block |
BRACE_REASON_CLOSER_SHARED |
1 << 4 |
the block spans lines, but its } shares a line with code |
BRACE_REASON_ONE_LINE_BODY |
1 << 5 |
the block opens and closes on one line, yet the next line is indented as its body |
BRACE_REASON_LAYOUT_PAIR |
1 << 6 |
indentation pairs it: the } sits at this block's indentation |
BRACE_REASON_LAYOUT_GHOST |
1 << 7 |
the block's indentation ends with no brace there, so one is missing |
BRACE_REASON_NO_LAYOUT |
1 << 8 |
indentation gives no evidence here |
BRACE_REASON_WALL_PAIR |
1 << 9 |
the range ends at a certain pair around it |
BRACE_REASON_WALL_TOP |
1 << 10 |
the range ends at a certain top-level declaration |
BRACE_REASON_LABEL_OUTSIDE |
1 << 11 |
paired this way, a case or default label would be outside every switch |
BRACE_REASON_BREAK_OUTSIDE |
1 << 12 |
paired this way, a break or continue would be outside every loop it needs (or, for a break, every switch) |
BRACE_REASON_ELSE_ALONE |
1 << 13 |
paired this way, an else would not follow an if |
BRACE_REASON_BARE_BLOCK |
1 << 14 |
a block of its own starts in the middle of a line, after a statement |
BRACE_REASON_SHARED_COLUMN |
1 << 15 |
its } lines up just as well with an enclosing block that is in doubt |
BRACE_REASON_OPEN_SHARED |
1 << 16 |
code follows the { on its line, yet the block runs on to later lines |
BRACE_REASON_SHARED_LINE |
1 << 17 |
another brace on its line is in doubt |
BRACE_REASON_BAD_PLACE |
1 << 18 |
C does not allow a { where this one is. |
BRACE_REASON_ODD_ROW |
1 << 19 |
a table row with a different number of values than the rows around it |
BRACE_REASON_CONFIGURATION |
1 << 20 |
it pairs differently in different if configurations |
BRACE_REASON_SCOPE |
1 << 21 |
a name its block declares is used after its } |
BRACE_REASON_SCOPE_GHOST |
1 << 22 |
the missing } goes after the last use of the block's own names |
BRACE_REASON_DELIMITER_BALANCED |
1 << 23 |
every ( ) [ ] of its statement balances |
BRACE_REASON_DELIMITER_UNCLOSED |
1 << 24 |
its statement ends while it is still open |
BRACE_REASON_DELIMITER_UNOPENED |
1 << 25 |
nothing in its statement opens it |
BRACE_REASON_DELIMITER_IN_DOUBT |
1 << 26 |
another ( ) [ ] of its statement does not balance |
BRACE_REASON_SAME_COLUMN_CLOSERS |
1 << 27 |
another } sits at the same column on the line right above or below it |
BRACE_REASON_LOST_BRACE_SIGNS |
1 << 28 |
text nearby that only a lost brace explains |
BRACE_REASON_COMPILER |
1 << 29 |
every repair the compiler accepts gives this pairing (brace_session_verify()) |
BRACE_REASON_PROVEN |
1 << 30 |
the code is valid C (in C++: its braces, parentheses and brackets pair), so the language pairs it so (every configuration that matters checked) |
BRACE_REASON_COUNT |
32 |
how many reason bits there are, BRACE_REASON_LAYOUT_DISAGREES included |
BraceAnswer
The answer for one brace: its partner, and how sure that is.
| Field | Type | Description |
|---|---|---|
token |
uint32_t |
the token index of this brace |
partner |
int32_t |
the partner's index in the answer array, or BRACE_GHOST, BRACE_EXTRA or BRACE_IN_MACRO |
other_partner |
int32_t |
its partner in another if configuration (an answer index), or BRACE_GHOST for none |
partner_token |
int32_t |
when partner is BRACE_IN_MACRO: the token of the macro use that holds the partner (otherwise it means nothing; brace_match_proven() sets it to -1) |
confidence |
uint8_t |
how sure the answer is: a BraceConfidence |
may_be_extra |
uint8_t |
1 when deleting the brace is also a repair: on an uncertain answer, or one the compiler check made certain as BRACE_EXTRA |
reasons |
uint64_t |
BRACE_REASON_* bits. |
range_low |
uint32_t |
when it is not certain: the first token gap where the partner can be |
range_high |
uint32_t |
and the last |
ghost_gap |
uint32_t |
for a ghost partner: its most likely gap, the token the missing brace goes before |
ghost_offset |
uint32_t |
the byte offset of the ghost: for a brace only a session fills it in (the start of the ghost_gap token, or a place kept from an earlier edit); brace_match_delimiters() fills it in for ( ) [ ] |
BraceMatcher
One way to pair braces, by name.
| Field | Type | Description |
|---|---|---|
name |
const char * |
its name ("proven", "preprocessor", "layout", "depth"), for a choice of matchers |
about |
const char * |
one sentence for people: what it uses, and how far to trust it |
match |
void (*)(const BraceInput *input, BraceAnswer *answers, size_t count) |
fills in the answers that brace_collect() listed |
Constants
BRACE_GHOST
#define BRACE_GHOST (-1)
a partner value: the partner is missing, and the range says where it can be
BRACE_EXTRA
#define BRACE_EXTRA (-2)
a partner value: the brace should not be there (only brace_session_verify() says this)
BRACE_IN_MACRO
#define BRACE_IN_MACRO (-3)
a partner value: the partner comes from a macro, whose use is at partner_token
BRACE_REASON_LAYOUT_DISAGREES
#define BRACE_REASON_LAYOUT_DISAGREES (1u << 31)
The reason bit that does not fit in an int enum: a warning that the indentation suggests another pairing.
brace_matchers
const BraceMatcher brace_matchers[][];
Every matcher, the best one first.
brace_matcher_count
const size_t brace_matcher_count;
How many matchers brace_matchers has.
Functions
brace_reason_text
const char *brace_reason_text(uint64_t bit);
One sentence for people about a reason.
| Parameter | Description |
|---|---|
bit |
One BRACE_REASON_* bit |
Returns: The sentence, or "" for a value that is not one reason bit
brace_is_pairable
int brace_is_pairable(const Token *token);
Whether a token is a brace that pairs: a { or } that is not on a preprocessor directive line.
brace_collect
size_t brace_collect(const BraceInput *input, BraceAnswer *answers);
List the braces that pair, in token order.
Call it once with NULL to count, allocate that many answers, and call it again to fill in each answer's token (the other fields are cleared).
| Parameter | Description |
|---|---|
input |
The file |
answers |
Filled in, or NULL to only count |
Returns: How many braces there are
brace_match_layout
void brace_match_layout(const BraceInput *input, BraceAnswer *answers, size_t count);
Pair braces by nesting and indentation, as two independent kinds of evidence.
A pair is certain only where the two agree. The range of an uncertain brace ends at the certain pairs around it, and indentation proposes partners and ghosts inside it. It may run on several threads at once, each with its own input and answers.
| Parameter | Description |
|---|---|
input |
The file |
answers |
The answers that brace_collect() listed, filled in |
count |
How many answers there are |
brace_match_configurations
void brace_match_configurations(
const BraceInput *input,
BraceAnswer *answers,
size_t count,
void(*)(const BraceInput *input, BraceAnswer *answers, size_t count)match);
Run a matcher in every if configuration that changes the braces, and merge the answers.
An answer is certain only when it is certain in each configuration, and other_partner is set where a brace pairs differently in another one.
| Parameter | Description |
|---|---|
input |
The file |
answers |
The answers that brace_collect() listed, filled in |
count |
How many answers there are |
match |
The matcher to run in each configuration |
brace_match_preprocessor
void brace_match_preprocessor(
const BraceInput *input,
BraceAnswer *answers,
size_t count);
brace_match_layout() in every if configuration that changes the braces
| Parameter | Description |
|---|---|
input |
The file |
answers |
The answers that brace_collect() listed, filled in |
count |
How many answers there are |
brace_match_proven
void brace_match_proven(const BraceInput *input, BraceAnswer *answers, size_t count);
Pair braces by proof where the code is valid, by layout where it is not.
The file is preprocessed (macros expanded, if decided) in every configuration that changes its braces. Where the configuration the compiler builds is valid C with balanced braces (for C++, whose grammar is not checked: its braces, parentheses and brackets pair and nest with each other), the pairing is the language's own, and certain (BRACE_REASON_PROVEN). What the proof cannot reach (a broken file, code in if 0) keeps the answers of brace_match_preprocessor(). Where the indentation disagrees with a proven pair, or cannot confirm it, BRACE_REASON_LAYOUT_DISAGREES warns. This is the best matcher for C and C++.
| Parameter | Description |
|---|---|
input |
The file |
answers |
The answers that brace_collect() listed, filled in |
count |
How many answers there are |
brace_collect_delimiters
size_t brace_collect_delimiters(const BraceInput *input, BraceAnswer *answers);
List every ( ) [ ] (those on directive lines too), in token order.
| Parameter | Description |
|---|---|
input |
The file |
answers |
Filled in (each answer's token), or NULL to only count |
Returns: How many there are
brace_match_delimiters
void brace_match_delimiters(const BraceInput *input, BraceAnswer *answers, size_t count);
Pair parentheses and brackets, statement by statement.
A pair is certain when every parenthesis and bracket of its statement balances. A statement that ends with one still open gets a ghost closer right before its end, and its other pairs are only likely.
| Parameter | Description |
|---|---|
input |
The file |
answers |
The answers that brace_collect_delimiters() listed, filled in |
count |
How many answers there are |
brace_match_language
int brace_match_language(
const BraceInput *input,
BraceAnswer *braces,
size_t brace_count,
BraceAnswer *delimiters,
size_t delimiter_count);
Pair the brackets of a language other than C.
The language's lexer has already taken strings and comments out. When every bracket of the file pairs by nesting, braces, parentheses and brackets pair by nesting, and every answer is certain. Otherwise (code being typed) brace_match_layout() pairs the braces and brace_match_delimiters() the rest.
| Parameter | Description |
|---|---|
input |
The file |
braces |
The answers that brace_collect() listed, filled in |
brace_count |
How many there are |
delimiters |
The answers that brace_collect_delimiters() listed, filled in |
delimiter_count |
How many there are |
Returns: 1 when every bracket paired by nesting, else 0
brace_match_depth
void brace_match_depth(const BraceInput *input, BraceAnswer *answers, size_t count);
The baseline: plain nesting depth from the start of the file.
Every answer is marked certain, which it is not: a brace with no partner has BRACE_GHOST and no useful range.
| Parameter | Description |
|---|---|
input |
The file |
answers |
The answers that brace_collect() listed, filled in |
count |
How many answers there are |
Kami API
The whole library
Reading C and C++
Braces
Any language