A C library that reads source code and says what it means. No display, no toolkit — only libc.
  • C 99.8%
  • Makefile 0.2%
Find a file
liquibyte ffe98e1a4d
Kami: comments name Unryu's maint.mk
- src/language_builtin.c and src/lexer.c: the comments that name Unryu's
  commands say "make -f maint.mk kami-builtin" and "make -f maint.mk
  check": Unryu's tests and generators moved from its Makefile into
  maint.mk. Comments only; the version stays 1.5.
2026-11-18 13:34:15 -06:00
artwork
demo
doc Kami 1.3: the bugs the manual's checks found are fixed 2026-11-17 15:48:19 -06:00
screenshots
src Kami: comments name Unryu's maint.mk 2026-11-18 13:34:15 -06:00
LICENSE
logo.png
Makefile Kami 1.3: the bugs the manual's checks found are fixed 2026-11-17 15:48:19 -06:00
README.md

Kami

A C library that reads source code and says what it means. No display, no toolkit — only libc.

Kami (紙) is Japanese for paper: the material. Unryu, the source code editor built on it, is named for one paper made from it (雲龍紙, "cloud dragon" paper, with its long fibers showing — as Unryu shows the structure of code). Other tools built on Kami can take other washi names.

What it does

Layer Header What it gives
Lexer lexer.h Exact tokens of C and C++: digraphs, trigraphs, line splices, raw strings, UCNs. Every token has its line and column; the tab width (8, 4 or 2) is picked per file. lexer_run() is the language lexer with the built-in c.lang and cpp.lang (language_builtin.c).
Preprocessor preprocessor.h Translation phase 4 — #define, #if, #include, #embed, __has_include and the rest — token for token what gcc -E gives. Every token knows where it is written and which macro use made it. #if is decided as the compiler decides it, from the compiler's own predefines and the build's -D/-U.
Parser parser.h The C23 grammar with the GNU extensions, plus checks the grammar alone misses (break/case placement, redeclaration, call arity, scope). Optionally a record of every declaration, every use of a name resolved to its declaration, and every scope. After an error it skips that declaration and reads the rest.
Semantic model semantic.h Every symbol the file declares — variables, functions, parameters, typedefs, tags, members, enumerators, labels, macros — with its declaration, its documentation comment and how often it is used, and for each token of the file the symbol it names.
Braces brace.h, brace_session.h Which { goes with which }: certain when the language proves it, the layout's best answer when the code is broken, where a missing brace most likely goes, live tracking across edits, repairs the compiler accepts. Parentheses and brackets too.
Languages language.h Any language described in a language file: how its files are found (extension, file name, #!), its lexical rules (strings with interpolation, heredocs, regexes, sigils, markup with embedded languages, Markdown and other documents), its words by class, its declarations and its blocks (braces, indentation, or words such as def ... end). One lexer reads every language, so the braces, folds and highlighting operate on all of them. language_token_style() says what each token is: the highlighting, for any toolkit. language_symbols_build() gives a language its names. doc/language_files.html (the wiki's Language Files page) has the format, and doc/grammars.html (Grammars) the [grammar] section.
C++ parser.h (parse_cpp_record()) The names of a C++ file, for semantic_build(): namespaces, classes with their bases, methods (also defined out of their class), templates, enums, aliases, lambdas, locals; every name resolved by C++'s scopes. It reads code that does not compile too: it checks no grammar.

kami.h includes all of it.

How sure the brace answers are

The default matcher, brace_match_proven, does not guess on code that compiles: it preprocesses the file as the compiler would, checks that the braces balance and that the C grammar accepts it, and then the nesting is the pairing. Other #if configurations are tried one decision at a time, so a brace that pairs differently in another build is reported too.

Measured on code it was not written against (the tests live in the brace project for now):

  • A stress file of every brace edge case, written by ten agents that never saw the library: 100% certain, 0 wrong
  • Clean corpora: 100% certain on the original and holdout sets, 99.5% on unseen projects (the rest lack include paths, so they fall back to the layout matcher)
  • One mistake in otherwise correct code: 0 answers wrong while certain
  • The preprocessor matches gcc -E on 260 files
  • The semantic model of sqlite3.c (27,000 symbols) builds in half a second; ordinary files in milliseconds

Using it

#include "kami.h"

/* What each name in a file means */
Lexer lexer;
lexer_run(&lexer, text, length, lexer_options_for_path(path));
PreprocessorOptions options = { .file_directory = directory };
SemanticModel model;
if (semantic_build(lexer.tokens, lexer.token_count, text, &options, &model) == 0) {
	for (size_t token = 0; token < model.token_count; token++) {
		int32_t symbol = model.symbol_of_token[token];
		if (symbol < 0) continue;
		const SemanticSymbol *named = &model.symbols[symbol];
		/* semantic_kind_name(named->kind), named->name, named->comment_first ... */
	}
	semantic_free(&model);
}
lexer_free(&lexer);

/* Braces, kept up to date as the text is edited */
BraceSession *session = brace_session_create(brace_match_proven);
brace_session_load(session, text, length);
for (size_t index = 0; index < session->answer_count; index++) {
	const BraceAnswer *answer = &session->answers[index];
	/* answer->partner, answer->confidence, answer->reasons ... */
}
brace_session_edit(session, offset, removed, inserted, inserted_length);
brace_session_destroy(session);

A demo

make demo builds demo/kami_demo, a small program that prints what Kami finds in a C file: its symbols with their documentation, and each brace with its partner and how sure the pairing is. demo/sample.c is a correct file; demo/broken.c is the same file with one brace lost.

make demo
demo/kami_demo demo/sample.c
demo/kami_demo demo/broken.c

kami_demo on a correct file and on one with a lost brace

Building

make            # libkami.a (KEEP_OBJS=1 keeps the object files)
make demo       # demo/kami_demo (see above)
make install    # headers to $(PREFIX)/include/kami, the library to $(PREFIX)/lib (PREFIX=/usr)
make clean

A program can also compile the files it uses from src/ straight in, as Unryu does.

Built with -Wall -Wextra -Wpedantic -Wconversion -Wshadow -Werror under -std=gnu17.

Documentation

The manual is in doc/: doc/index.html is the overview, with a diagram of the layers, and it links to the guides of language files, grammars, the lexer and highlighting, the preprocessor, the parser and the semantic model, braces, the brace session, and an example program. Every public type, field, constant and function in src/*.h has a Doxygen comment.

The project's wiki holds both: the same guides, one wiki page each, and an API page for each header, made from those comments.

KAMI_VERSION in kami.h is the library's version.

License

Artistic License 2.0.