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:
- What are the tokens? The lexer cuts a text into tokens, each with its byte offset, length, line, column, kind and style. It reads C and C++ exactly as the C standard describes, and every other language by the rules of its language file.
- What does the compiler see? The preprocessor runs translation phase 4 on a C or C++ file, as the compiler does, and keeps for every token the place in the file where it is written.
- Is it valid, and what does it declare? The parser checks the C grammar and records every declaration, every use of a name and every scope. The semantic model turns that record into symbols: for every token of the file, which symbol it names, where that symbol is declared, and its documentation comment. A language file with a grammar gives the same model for its language.
- Which brace goes with which? In valid C code the pairing is proved by the language. In broken code Kami gives its best answer, says how sure it is and why, and says where a missing brace most likely goes. A brace session keeps these answers up to date while the text is edited.
- What color is each token? Every token has a style, such as
keywordorcomment. A program gives each style a color; Kami itself knows nothing of colors.
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.
3. What each header gives
| Header | What it gives | Guide |
|---|---|---|
lexer.h | The 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.h | Translation 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.h | The 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.h | Each 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.h | The 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.h | A 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.h | A 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.h | All 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.
| Command | What it does |
|---|---|
make | Builds 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 install | Installs 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 demo | Builds demo/kami_demo, a small program that prints the symbols and the brace
pairs of a C file (see An Example Program). |
make clean | Removes 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
- Return values. A function that can fail returns 0 when it worked and -1 when it ran out of memory (or was given a NULL it needs). A result that says something about the code, such as "the file does not parse", is a field of the result, not a failure of the call. The few exceptions are named on each page.
- Results and freeing. A call fills in a struct that the caller owns, such as a
Lexeror aSemanticModel. What Kami allocated inside it is freed by the_freefunction of that type:lexer_free(),preprocessed_free(),parse_record_free(),semantic_free(),language_free(),language_set_free(). A free function clears the struct, so a second free does nothing harmful. A brace session is created and destroyed as a whole. - The text stays yours. Kami never copies the text it reads, except into a brace session. Tokens point into the text by offset, so the text must stay alive and unchanged while its tokens and models are used. The text does not need a NUL at its end.
- Three kinds of position. A token index counts the tokens of a lexer (or of the preprocessor's
output, in the parser's record). A byte offset counts the bytes of the text. A token gap is the place
just before a token: gap g is before token g, and gap
token_countis the end of the text. Lines count from 1, columns from 0. Each page says which kind a field holds. - "None". A field that can name no token holds a constant for that:
PREPROCESSOR_NONE,PARSE_NONE,SEMANTIC_NONE(allUINT32_MAX) orBRACE_SESSION_LOST. An index into an array of symbols, scopes or answers is -1 when there is none. - No surprises from the disk. Kami opens files only in the preprocessor, to read the headers that a C
file includes and the files that
#embednames. It never runs a program; a brace session asks the caller's own function when it wants a compiler's opinion. - Untrusted text. No text can make the lexer stop or loop. The preprocessor and the grammar reader have limits on how much work one file may cost, and stop when a file goes past them.
- Threads. Separate results can be made on separate threads at the same time, brace answers and brace sessions included, as long as no two threads share one result or one session. The built-in C and C++ languages are parsed once, safely, the first time they are needed.
6. Known limitations
- Very deep nesting (hundreds of thousands of braces in one initializer) can exhaust the stack of the C parser and takes a long time in brace matching. Real code is far below that.
- The C semantic model holds the symbols of the file itself. The headers it includes add their macros, but their
declarations are not symbols of the model.
preprocess_with_headers()gives a program the headers' tokens, to read them on its own. - A member is found by the type of what it is taken from only when that type is declared in the same file.
- The C++ reader checks no grammar: a C++ file is "valid" when its brackets pair. For the same reason, the proof of the brace pairs of a C++ file rests on its braces, parentheses and brackets pairing and nesting after the preprocessor, without a grammar check.
- A file whose quoted
#includeis not found does not preprocess cleanly, so its braces are paired by layout and not proved, even when the code is correct. - A model is made from the whole text every time; there is no rebuild of only the changed part.
7. The pages of the manual
- Language Files
The format of a language file, every key of every section with its default, the styles, and the calls that parse a language and choose the language of a file.
- Grammars
The
[grammar]section: terms, annotations, scopes and bodies, recovery from broken code, the layout of lines and indentation, and the keys that tune them. - The Lexer and Highlighting
Tokens and their fields, lexing C and C++ and any other language, embedded languages, the tab width, styles for highlighting, and folds.
- The Preprocessor
What the compiler sees: options, predefined macros, include directories, decisions that rest on unknown macros, where each token comes from, and the limits.
- The Parser and the Semantic Model
The C grammar check, the C++ reader, the record, and the model: symbols, scopes, how names and members are resolved, documentation, macro expansions, and the model of other languages.
- Braces
The matchers, the answer for each brace, confidence, ghosts and ranges, every reason, parentheses and brackets, and other languages.
- The Brace Session
Keeping brace answers right while the text is edited, ghosts that stay in place, and the compiler check with its repairs.
- An Example Program
A short, complete program that uses every layer, and how to build it.