Kami Documentation

The manual of Kami, a C library that reads source code and tells what the code means. This page says what Kami is, how its layers fit together, how to build it and use it in a program, and which rules every call follows. Then it lists the other pages of the manual. The reference of every type and function is made from the comments in Kami's headers and is part of the project's wiki, one page for each header.

1. What Kami is

Kami reads text and answers questions about it. It has no display and no toolkit, and it needs only libc, so an editor, a command-line checker or a language server can all use it. Unryu, the source code editor, is built on it. Kami answers these questions:

2. The layers

Each layer has its own header, and a layer uses only the layers below it. A program can include the one header it needs, or kami.h for all of them.

The text of a file bytes in memory, with the file's path and what is known of the build (include directories, -D and -U) Lexer (lexer.h) the exact tokens of C and C++: offset, length, line, column Language files (language.h) a language parsed from its file; the same lexer reads every language Preprocessor (preprocessor.h) translation phase 4: each token knows where it is written Highlighting, names and folds (language.h) a style for each token; symbols from the file's grammar or patterns Parser (parser.h) the C grammar; the C++ reader Braces (brace.h) pairs, ghosts, reasons Brace session (brace_session.h) a text being edited: new answers after each change Semantic model (semantic.h): each symbol, its declaration, its documentation, and what each name refers to
Each box links to its page. The left side reads C and C++ as the compiler reads them; the right side reads every other language from its language file. Both sides give the same kinds of answer (tokens with styles, a semantic model, brace answers), so a program uses one set of calls for every language.

3. What each header gives

HeaderWhat it givesGuide
lexer.hThe tokens of C and C++, exactly as the standard reads them: line splices, digraphs, trigraphs when asked for, raw strings in C++. Each token has its offset, length, line, column, kind, flags and style. The tab width (8, 4 or 2) is worked out from the file.The Lexer and Highlighting
preprocessor.hTranslation phase 4: #define, #if, #include, #embed and the rest. Each token that comes out knows where it is written and which macro use made it. The #if decisions that rest on a macro nothing defines are reported, and a program can force them the other way.The Preprocessor
parser.hThe C23 grammar with the GNU extensions, and some checks the grammar alone misses. A record of each declaration, each use of a name and each scope. For C++, the same record from a reader that checks no grammar.The Parser and the Semantic Model
semantic.hEach symbol the file declares, with its declaration, its documentation comment and its number of uses; for each token, the symbol it names; for each macro use, the text it becomes.The Parser and the Semantic Model
brace.hThe partner of each brace, how sure that is and why. A missing brace gets a range where it can be and a most likely place. Parentheses and brackets are paired too.Braces
brace_session.hA text being edited, with brace answers that are always those of the current text, and a compiler check that turns doubtful answers into certain ones.The Brace Session
language.hA language read from its language file: which files are in it, its lexical rules, its words, its blocks, its declarations and optionally its grammar. The style of each token, the semantic model of a file in the language, and its folds.Language Files, Grammars
kami.hAll the headers in one include, and KAMI_VERSION, the library's version as a string.This page

4. Building and installing

Kami is built with make. It needs a C compiler that understands -std=gnu17 (GCC or Clang), make and ar, and nothing else.

CommandWhat it does
makeBuilds the static library libkami.a. The object files are removed once the archive exists; make KEEP_OBJS=1 keeps them. The build treats every warning as an error (-Wall -Wextra -Wpedantic -Wconversion -Wshadow -Werror).
make installInstalls libkami.a to $(PREFIX)/lib and every header to $(PREFIX)/include/kami. PREFIX is /usr unless you set it, and DESTDIR=... stages the install for a package.
make demoBuilds demo/kami_demo, a small program that prints the symbols and the brace pairs of a C file (see An Example Program).
make cleanRemoves the object files, the library and the demo program.

A program links with -lkami (or names libkami.a) and includes <kami/kami.h> after an install, or adds -I for the src folder. It can also compile the files it uses from src/ straight into itself, as Unryu does. The library uses C11 threads (call_once) for its built-in languages, so on some systems the program also links with -lpthread.

5. Rules that every call follows

6. Known limitations

7. The pages of the manual