lexer.h
The C and C++ Lexer (layer 0)
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.
The lexer cuts C or C++ source text into tokens, as translation phases 1 to 3 of the C standard describe: a line splice (a backslash before a line end) vanishes anywhere, even inside a token; digraphs are the tokens they stand for; trigraphs are read when they are asked for; comments, string and character literals (with their prefixes) are single tokens; and every token on a preprocessor directive line is marked as part of a directive. A comment or literal with no end is marked, and lexing goes on. With LEXER_CPLUSPLUS the lexer also reads C++ raw strings, R"tag(...)tag", and the C++ rule for <::. lexer_options_for_path() chooses the options from a file name.
lexer_run() is the same lexer that reads every other language (language.h), driven by Kami's built-in C and C++ language files.
Features:
- Every token with its byte offset, length, line and visual column
- Line splices, digraphs and optional trigraphs, read as the standard says
- C++ raw strings and the <:: rule with
LEXER_CPLUSPLUS - The tab width of the file, worked out from its indentation
- A token's spelling with its splices removed (
lexer_spell())
Positions, which every layer above uses:
- offset is the byte offset of the token's first real character (a splice in front of the token is skipped); length runs to the end of its last character, so it includes any splice inside the token.
- line counts from 1. column is the visual column of offset, counted from 0: a tab moves to the next multiple of the tab width, and a UTF-8 continuation byte takes no column. The tab width is
LEXER_TAB_WIDTH(8), unless the braces of the file only line up at a width of 4 or 2, where some lines are indented with tabs and others with spaces (tab_width). - The layers above read indentation from line and column, not from the text, so two lines look aligned only when they are aligned once tabs are expanded.
#include "lexer.h"
Types: TokenKind, TOKEN_FLAG_DIRECTIVE and the other values, LEXER_TRIGRAPHS and the other values, Token, Lexer
Constants: LEXER_TAB_WIDTH
Functions: lexer_run, lexer_free, lexer_layout, lexer_options_for_path, lexer_spell
Types
TokenKind
What a token is. Every language uses these kinds (language.h); a token's style says more.
| Name | Value | Description |
|---|---|---|
TOKEN_LEFT_BRACE |
{ (also written as the digraph <%) | |
TOKEN_RIGHT_BRACE |
} (also written as %>) | |
TOKEN_LEFT_PARENTHESIS |
( | |
TOKEN_RIGHT_PARENTHESIS |
) | |
TOKEN_LEFT_BRACKET |
[ (also written as <:) | |
TOKEN_RIGHT_BRACKET |
] (also written as :>) | |
TOKEN_SEMICOLON |
; | |
TOKEN_HASH |
the # (or %:) that starts a directive line | |
TOKEN_IDENTIFIER |
a name or a keyword | |
TOKEN_NUMBER |
a number (in C, a preprocessing number) | |
TOKEN_STRING |
a string literal, with its prefix | |
TOKEN_CHARACTER |
a character literal, with its prefix | |
TOKEN_HEADER_NAME |
the <...> after include, include_next, import or embed | |
TOKEN_COMMENT |
a comment of either form | |
TOKEN_PUNCTUATOR |
any other punctuator or operator | |
TOKEN_OTHER |
a byte that starts no token of the language, such as @ or ` in C |
TOKEN_FLAG_DIRECTIVE and the other values
The bits of Token.flags.
| Name | Value | Description |
|---|---|---|
TOKEN_FLAG_DIRECTIVE |
1 << 0 |
the token is on a preprocessor directive line |
TOKEN_FLAG_LINE_START |
1 << 1 |
the token starts its line: no token before it ends on that line |
TOKEN_FLAG_UNTERMINATED |
1 << 2 |
a comment or literal that has no end |
TOKEN_FLAG_SPLICED |
1 << 3 |
the token's text holds a line end: a line splice, or a string that runs over lines (never set on a comment) |
TOKEN_FLAG_DIGRAPH |
1 << 4 |
the token is written as a digraph (<% for {) |
TOKEN_FLAG_TRIGRAPH |
1 << 5 |
the token holds a trigraph (only when trigraphs are read) |
TOKEN_FLAG_HEREDOC |
1 << 6 |
the token is part of a here-document's body or its end word: data, not code |
TOKEN_FLAG_EMBEDDED |
1 << 7 |
the token is code of another language inside a string (awk '{ print }' in a shell script), or that string's closing quote: the grammar of the language around it skips it |
LEXER_TRIGRAPHS and the other values
The options of lexer_run(), language_lex_options() and language_lex_set().
| Name | Value | Description |
|---|---|---|
LEXER_TRIGRAPHS |
1 << 0 |
read trigraphs: ??< is {, ??= is #, and so on |
LEXER_CPLUSPLUS |
1 << 1 |
lexer_run() reads C++: raw strings, and <:: as < and :: |
Token
One token: where it is in the text, and what it is.
| Field | Type | Description |
|---|---|---|
offset |
uint32_t |
the byte offset of its first character |
length |
uint32_t |
its length in bytes, any line splice inside it included |
line |
uint32_t |
its line, counted from 1 |
column |
uint32_t |
its visual column, counted from 0 (a tab moves to the next tab stop) |
kind |
uint8_t |
what it is: a TokenKind |
flags |
uint8_t |
TOKEN_FLAG_* bits. |
style |
uint8_t |
what it is for highlighting: a LanguageStyle (language.h), set by the lexer |
Lexer
A text and its tokens: what the lexer makes.
| Field | Type | Description |
|---|---|---|
source |
const char * |
the text (the caller's: it must stay alive as long as the tokens are used) |
size |
size_t |
the text's length in bytes |
tokens |
Token * |
the tokens, in the order of the text |
token_count |
size_t |
how many tokens there are |
line_start |
uint32_t * |
the byte offset where each line starts; line_start[0] is 0 |
line_count |
size_t |
how many lines there are: one more than the number of line ends |
options |
unsigned |
the LEXER_* options the text was read with |
tab_width |
uint32_t |
the tab width the columns were measured with: 8, 4 or 2 |
Constants
LEXER_TAB_WIDTH
#define LEXER_TAB_WIDTH 8
The tab width that columns are measured with, unless the file's indentation shows that 4 or 2 is meant.
Functions
lexer_run
int lexer_run(Lexer *lexer, const char *source, size_t size, unsigned options);
Lex a text as C, or as C++.
This is the lexer of language.h with Kami's built-in C language file, or with its C++ file when options has LEXER_CPLUSPLUS (see language_builtin()).
| Parameter | Description |
|---|---|
lexer |
Filled in; free it with lexer_free() |
source |
The text; it need not end with a NUL, and it must stay alive while the tokens are used |
size |
The text's length in bytes |
options |
LEXER_TRIGRAPHS, LEXER_CPLUSPLUS, both, or 0 |
Returns: 0, or -1 when out of memory
lexer_free
void lexer_free(Lexer *lexer);
Free the tokens and lines of a lexer, and clear it. The text stays the caller's.
| Parameter | Description |
|---|---|
lexer |
A lexer that lexer_run(), language_lex() or language_lex_set() filled in, or NULL (nothing is done) |
lexer_layout
int lexer_layout(Lexer *lexer);
Work out the lines, the tab width and each token's line and column.
For a program that made the tokens itself: fills line_start, line_count and tab_width, and sets each token's line, column and TOKEN_FLAG_LINE_START (and TOKEN_FLAG_SPLICED and TOKEN_FLAG_TRIGRAPH), as lexer_run() does.
| Parameter | Description |
|---|---|
lexer |
A lexer whose source, size, tokens, token_count and options are set, and whose line_start is NULL or an array from an earlier layout (it is freed and made again) |
Returns: 0, or -1 when out of memory or when lexer is NULL
lexer_options_for_path
unsigned lexer_options_for_path(const char *path);
The lexer options for a file name: LEXER_CPLUSPLUS for C++, 0 for C.
C++ is chosen for the endings .cc .cpp .cxx .c++ .C .hh .hpp .hxx .h++ .H .ipp .tpp .tcc .inl, and for .h, which may be either: reading C as C++ only changes a string right after an R prefix, and <::, which C code does not have.
| Parameter | Description |
|---|---|
path |
The file's path or name, or NULL |
Returns: LEXER_CPLUSPLUS or 0
lexer_spell
size_t lexer_spell(const Lexer *lexer, const Token *token, char *buffer, size_t capacity);
Copy a token's spelling, with its line splices removed and (when they are read) its trigraphs replaced.
| Parameter | Description |
|---|---|
lexer |
The lexer that made the token |
token |
The token |
buffer |
Where the spelling goes; it is cut to fit, and it always ends with a NUL when capacity is not 0 |
capacity |
The buffer's size in bytes |
Returns: The spelling's whole length, which may be more than fitted
Kami API
The whole library
Reading C and C++
Braces
Any language