1 language
liquibyte edited this page 2026-11-19 16:23:29 -06:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

language.h

Languages Described in Files, and Their Highlighting.

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 language file tells Kami everything it needs to read a language: how to know its files, every lexical rule it has, how its blocks are made, its words by class, what its declarations look like, optionally its whole grammar, and how an editor comments a line. One lexer reads every language from its file, C and C++ too: lexer_run() is that lexer with Kami's built-in C and C++ files (language_builtin()). The engine key says only what reads the tokens after that: Kami's C preprocessor, parser and semantic model (engine = c or c++), or the file's own [grammar] and [symbols] (generic).

Features:

  • [language]: the name, extensions, file names (with * and ? patterns), #! programs and detect words; the engine; how blocks are made (braces, indent, keywords, none), and for keywords the words that open, close and join blocks, with block-rule tests on when they count (Ada's procedure P is ... begin ... end P, Fortran's if (x) then); the layout and other keys that a grammar uses
  • [lexical]: names (their characters, \u escapes, case, a style by prefix such as CSS's –name), comments of every kind, strings of every kind (raw, multi-line, doubled delimiters, prefixes, C++ R"x(...)x", Rust r#"..."#, Lua [==[ ]==], heredocs, interpolated code and variables), characters, regex literals, Perl's quote operators, Ruby's % literals, variables by sigil and by delimiters, command substitution, symbols, macros by suffix, attributes, directives, line splices and continuations, trigraphs, digraphs, header names, numbers, and operators (the longest first)
  • [lines]: a line that starts with a prefix is one token of a style (diff)
  • Markup: tags, attributes, entities, comments, CDATA, declarations, and embedded languages by tag (<script>) and code regions (<?php ?>)
  • Markdown and other documents: headings, fences in their own language, quotes, lists, rules, code spans, emphasis, strong text and links
  • [words]: the words of each class, by any style's name (keyword, type ...)
  • [symbols]: declarations as patterns, such as "function = def NAME", nested by the language's blocks (a method in its class)
  • [grammar]: a parsing expression grammar over the tokens, with annotations that say what each name declares or uses
  • [editing]: comment-toggle, indent-after, semicolon-after, unicode-escape, spelling and the import keys (import-words, import-separator, import-files), for an editor
  • Pairs besides brackets (pairs, keyword-pair, named-pair, text-pairs, void-tags, optional-end-tags): language_pairs() finds a string's quotes, a comment's ends, interpolation, tags, the < > of generics, keyword blocks, if chains and Unicode brackets in prose, with a ghost for an end that is missing
  • language_unusual_characters(): bidirectional controls, invisible characters and look-alike spaces in a text
  • language_po_messages(), language_po_directives_match(): a gettext catalog's messages, and whether a translation's s and d are the original's
  • language_spelling_ranges(): the runs of a text a spelling checker reads (the spelling key: code, prose, markup or none)
  • A LanguageSet: several language files in one text, for a file that embeds other languages
  • language_token_style(): what each token is, for highlighting; an editor gives each style a color

Internal design notes:

  • No display, no toolkit and no files: a language is parsed from text, so a sandboxed process can parse it too, and any program can ship its own files
  • A list is separated by spaces; "..." keeps a space in a marker ("( " is Forth's comment). A key that may come more than once (block-comment) adds an entry each time
  • Words are kept sorted and found by binary search; a language that is not case-sensitive keeps them in lower case
#include "language.h"

Types: LanguageEngine, LanguageBlocks, LanguageStyle, LanguagePattern, LanguageWord, LanguageMarker, LanguageMarkers, LanguageOperators, LanguageTriples, LanguageBlockTest, LanguageBlockRule, LanguageGrammar, LanguageLayout, Language, LanguageSet, LanguagePairKind, LanguagePair, LanguageSpelling, LanguageRange, LanguagePoMessage, LanguageUnusualKind, LanguageUnusual, LanguageFold

Constants: LANGUAGE_LIST_MAX, LANGUAGE_MARKER_MAX, LANGUAGE_OPERATORS_MAX, LANGUAGE_SYMBOLS_MAX, LANGUAGE_PATTERN_MAX, LANGUAGE_BLOCK_RULES_MAX, LANGUAGE_BLOCK_WORDS_MAX, LANGUAGE_PAIR_NOWHERE, LANGUAGE_PO_FORMS

Functions: language_parse, language_free, language_set_parse, language_set_free, language_set_find, language_builtin, language_builtin_text, language_match, language_detect_score, language_lex, language_lex_set, language_lex_options, language_word_style, language_conditional_word, language_token_style, language_style_name, language_style_from_name, language_symbols_build, language_grammar_compile, language_grammar_free, language_grammar_build, language_pairs, language_keyword_pairs, language_spelling_ranges, language_po_messages, language_po_directives_match, language_unusual_characters, language_indent_folds, language_keyword_folds

Types

LanguageEngine

What reads the tokens after the lexer.

Name Value Description
LANGUAGE_ENGINE_GENERIC the [symbols] patterns
LANGUAGE_ENGINE_C Kami's C: preprocessor, grammar, semantics.
LANGUAGE_ENGINE_CPP the same, as C++

LanguageBlocks

How a language makes its blocks: for brace matching, folding and nesting.

Name Value Description
LANGUAGE_BLOCKS_BRACES { }
LANGUAGE_BLOCKS_INDENT indentation (Python)
LANGUAGE_BLOCKS_NONE none
LANGUAGE_BLOCKS_KEYWORDS words: def ... end (Ruby), function ... end (Lua)

LanguageStyle

What a piece of text is: the classes an editor colors.

Name Value Description
LANGUAGE_STYLE_DEFAULT text with no other style
LANGUAGE_STYLE_COMMENT a block comment
LANGUAGE_STYLE_COMMENT_LINE a line comment
LANGUAGE_STYLE_COMMENT_DOC a block comment that opens with a doc opener
LANGUAGE_STYLE_COMMENT_LINE_DOC a line comment that opens with a doc opener
LANGUAGE_STYLE_NUMBER
LANGUAGE_STYLE_STRING a string, a heredoc
LANGUAGE_STYLE_CHARACTER
LANGUAGE_STYLE_KEYWORD
LANGUAGE_STYLE_TYPE
LANGUAGE_STYLE_CONSTANT a constant word, a symbol (:name)
LANGUAGE_STYLE_BUILTIN
LANGUAGE_STYLE_PREPROCESSOR a directive
LANGUAGE_STYLE_OPERATOR
LANGUAGE_STYLE_BRACE { } ( ) [ ]
LANGUAGE_STYLE_IDENTIFIER any other name
LANGUAGE_STYLE_REGEX a regex literal
LANGUAGE_STYLE_VARIABLE a name by its sigil: $x, @x, x%
LANGUAGE_STYLE_LABEL a tick name ('a), a label line
LANGUAGE_STYLE_MACRO a macro call: name!
LANGUAGE_STYLE_TAG a markup tag, its < > </ />
LANGUAGE_STYLE_ENTITY &
LANGUAGE_STYLE_HEADING a markdown or document heading
LANGUAGE_STYLE_EMPHASIS emphasis
LANGUAGE_STYLE_STRONG strong
LANGUAGE_STYLE_LINK [text](url), <url>
LANGUAGE_STYLE_CODE code, a fenced block with no language
LANGUAGE_STYLE_INSERTED a diff's + line
LANGUAGE_STYLE_DELETED a diff's - line
LANGUAGE_STYLE_CHANGED a diff's @ and file lines
LANGUAGE_STYLE_COUNT

LanguagePattern

One [symbols] pattern: words around the name it declares. A word * is any name that is not a keyword (a type: int NAME (); a word (...) or [...] is one group in brackets.

Field Type Description
kind uint8_t SemanticKind.
words[LANGUAGE_PATTERN_MAX][32] char the pattern's words, in order
word_count int how many
name_at int where NAME stands among the words: "def NAME" 1, "NAME :" 0

LanguageWord

A word and its class.

Field Type Description
word const char * into the language's word text
style uint8_t LanguageStyle.

LanguageMarker

typedef char LanguageMarker[LANGUAGE_MARKER_MAX];

One marker: a short text that opens, closes or names something (a comment's "//", a string's quote).

LanguageMarkers

A list of markers.

Field Type Description
items[LANGUAGE_LIST_MAX] LanguageMarker the markers
count int how many

LanguageOperators

The operators: a longer list.

Field Type Description
items[LANGUAGE_OPERATORS_MAX] LanguageMarker the operators, the longest first where two start alike
count int how many

LanguageTriples

Pairs: an opener and its closer (block comments, digraphs ...), or a delimiter and two markers.

Field Type Description
first[LANGUAGE_LIST_MAX] LanguageMarker each rule's first marker (what opens it)
second[LANGUAGE_LIST_MAX] LanguageMarker its second (what closes it)
third[LANGUAGE_LIST_MAX] LanguageMarker its third, for a rule that has one
count int how many rules

LanguageBlockTest

What a block-rule tests: when a block word works (opens or closes).

Name Value Description
LANGUAGE_BLOCK_ALONE only alone on its line (Oracle's DECLARE)
LANGUAGE_BLOCK_NOT_ALONE only with more on its line (FreeBasic's End: alone it ends the program)
LANGUAGE_BLOCK_NEXT_NOT not when the next token is one of the words (0: a number, ": a string)
LANGUAGE_BLOCK_NOT_AFTER not after one of the words on its line (Declare Sub, else if)
LANGUAGE_BLOCK_LINE_NOT not on a line that opens with one of the words (elsif x generate)
LANGUAGE_BLOCK_ENDS only when its statement ends with one of the words (if (x) then)
LANGUAGE_BLOCK_HAS only when its statement holds one of the words (procedure P is)
LANGUAGE_BLOCK_HAS_NOT not when its statement holds one of them (component c port map)
LANGUAGE_BLOCK_AFTER_NOT not when the first word is followed by one of the rest (is new, is null)
LANGUAGE_BLOCK_GROUP only when its statement is it and one ( ) group (Fortran's where (m))

LanguageBlockRule

One block-rule: a block word, its test, and the test's words.

Field Type Description
word LanguageMarker the block word the rule is for
test uint8_t LanguageBlockTest.
count uint8_t how many words the test has
words[LANGUAGE_BLOCK_WORDS_MAX] LanguageMarker the words the test looks for

LanguageGrammar

typedef struct LanguageGrammar LanguageGrammar;

A language's [grammar], compiled (language_grammar.c).

LanguageLayout

How the grammar sees line ends: none, NEWLINE tokens, or NEWLINE, INDENT and DEDENT.

Name Value Description
LANGUAGE_LAYOUT_NONE line ends do not count
LANGUAGE_LAYOUT_LINES a NEWLINE at the end of each line that has tokens
LANGUAGE_LAYOUT_INDENT NEWLINE, and INDENT and DEDENT by indentation (Python)
LANGUAGE_LAYOUT_OFFSIDE Haskell's offside rule: a block opens at the next token's column after a layout-open word.

Language

A language, parsed.

Field Type Description
id[64] char the file's name without .lang ("python")
name[64] char for people ("Python")
extensions[512] char space-separated, without the dot
filenames[512] char whole file names ("Makefile"), or patterns with * ("makefile.*")
shebangs[256] char programs in a #! line ("python3")
detect[256] char words that, near a file's top, win a tie of extensions (.m: @interface is Objective-C)
engine uint8_t LanguageEngine.
blocks uint8_t LanguageBlocks.
block_starts LanguageMarkers blocks = keywords: words that open a block at a statement's start (if, def)
block_opens LanguageMarkers words that open one anywhere (do, function)
block_ends LanguageMarkers words that close one (end, until)
block_endless LanguageMarkers openers with no closer when = follows their name (Ruby's def area = x)
block_joins LanguageMarkers words that join the block above, not open one: Ada's begin after procedure P is
block_join_to LanguageMarkers the openers a join word joins (procedure, declare, process)
block_rules[LANGUAGE_BLOCK_RULES_MAX] LanguageBlockRule when a block word works
block_rule_count int how many
continuation_column int fixed form: a character in this column (from 1) joins the line to the last (Fortran 77: 6)
statement_lines int a statement ends at a line's end (no line-continuation), not only at ; (Fortran)
grammar LanguageGrammar * NULL: no [grammar] (the names come from [symbols])
grammar_problem[160] char why the [grammar] could not be compiled; "" when it was
layout uint8_t LanguageLayout.
layout_ignore_in LanguageMarkers brackets inside which a line end is no NEWLINE: ( [ {
layout_continue_after[512] char space-separated: a line that ends with one of these goes on (Go's +, Ruby's ,)
layout_continue_before[256] char space-separated: a line that starts with one of these goes on the line above (.)
layout_block_after[64] char space-separated: a line that ends with one of these over a deeper line opens a block, in brackets that hide line ends (Nim: = : do) and outside them for a word that goes on too (Scala: = =>)
operator_characters[40] char any run of these is one operator (Haskell: <$ >=> .:)
underscore_operators int a name that ends in _ goes on with a run of operator_characters (Scala: unary_!, tpe_*)
digit_names int digits, then letters that make no number, are a name (Vala: 3DES_CBC)
dotted_operators int .name. is one operator (Fortran: .det., .myop.)
continuation_in_strings int line_continuation at a line end inside a string continues it; the next line's own one is skipped (Fortran)
layout_open[128] char space-separated: the words after which an offside block opens (where let do of)
layout_restart[512] char space-separated: a line in brackets that opens with one of these, no deeper than they, ends them (def)
soft_keywords[1024] char space-separated: keywords that are names where the grammar wants a NAME (Python's match)
split_operators LanguageMarkers tokens a grammar literal may take apart: >> in Vec<Vec<T>>
self_words LanguageMarkers self, this: self.x is a member of the class around it
class_scope_hidden int a method does not see its class's names without self (Python)
sigil_free_names int $x and ${x} name the variable x (the shell's x=1)
loose_strings int a string with no end is text, no fault (an INI value)
assign_local int an :assign declares in its own scope unless global says otherwise (Python)
pair_kinds unsigned pairs: a bit (1 << LanguagePairKind) for each kind the language has
keyword_pairs LanguageTriples keyword-pair: an opening word and its closing word (the shell's if fi), for a language whose blocks are not keywords
named_pairs LanguageTriples named-pair: an opening command and its closing one, whose { } names must be the same (LaTeX's \begin \end)
text_pairs LanguageMarkers text-pairs: opening and closing characters in turn, matched in prose (« » “ ”)
void_tags[512] char space-separated: elements that have no end tag (br img)
optional_end_tags[256] char space-separated: elements whose end tag may be left out (li p)
identifier_start[32] uint8_t a bit set of bytes: those a name can start with
identifier_part[32] uint8_t a bit set of bytes: those a name can go on with
case_sensitive int 0: its words are found in any case (SQL, Pascal)
unicode_spaces int Unicode space separators (no-break space ...) are skipped as white space (JavaScript); a name ends at one in every language.
identifier_escapes LanguageMarkers \u \U: a name may hold them (C's UCNs)
uppercase_style uint8_t a name that starts upper case has this style (Erlang: variable, Haskell: type); 0 for none
name_prefixes LanguageTriples prefix, style: a name that is no word and starts with the prefix has the style (CSS's –accent)
line_comments LanguageMarkers the markers of a comment that runs to the end of its line (//, #, REM)
block_comments LanguageTriples open, close
nested_comments int a block comment can hold another (Rust, Haskell)
documentation_openers LanguageMarkers a comment that opens with one is documentation
directive_comments LanguageMarkers a comment that opens with one is a directive: {$ (Pascal), { 1. (Haskell)
comment_at_word_start int a line comment opens only where a word may (shell: a::b is a word)
string_at_word_start int a string opens only where a word may (Tcl: a"b is a word)
regex_after_space int after a name, a space and a slash with no space after it open a regex (CoffeeScript: f /re/)
closer_takes_run int a string's closer of more than one character takes a longer run of it ("""a"""" in Nim)
any_string_prefix int any name right before a " is the string's prefix (Julia: doc"x", L"x")
backtick_names int a name is one name (Swift, Kotlin, Scala)
line_block_comments LanguageTriples open, close: each the whole of a line (=begin =end, %{ %})
line_block_open_end int such a comment may run to the file's end with no closer (Perl's POD)
column_comments[32] char in comment_column, one of these makes the line a comment (Fortran 77: C c *)
comment_column int from 1; 0 for none
shebang_comment int a first line #! is a comment
strings LanguageMarkers the quotes of a string
raw_strings LanguageMarkers the quotes of a string with no escapes
multiline_strings LanguageMarkers the quotes of a string that can go over lines
characters LanguageMarkers the quotes of a character literal
string_prefixes LanguageMarkers letters before a quote that are part of the string (f, b, u8)
raw_prefixes LanguageMarkers prefixes that make it a string with no escapes (r, R)
delimited_strings LanguageTriples opener, closer, flags ("raw multiline"): " " (C#), ." " (Forth)
hash_colors int fff ff8000 is a number (CSS, config files)
number_prefixes[8] char $FF %1010 &17: a number that starts with one of these (Pascal)
cpp_raw_prefixes LanguageMarkers R: R"tag( ... )tag".
hash_raw_prefixes LanguageMarkers r br: r#" ... "#, any number of #
hash_raw_strings int #" ... "# with no prefix (Swift)
long_brackets int [==[ ]==] strings (Lua)
long_bracket_comment LanguageMarker – before a long bracket makes it a comment
string_doubling int '' inside '...' is one quote (SQL, Pascal)
heredocs LanguageMarkers << <<- <<~ <<<
heredoc_joined int the word follows the opener with no space (Ruby's class << self is no heredoc)
command_embeds LanguageTriples command-embed: a command (first) and the language of its code argument (second); the rest of the rule in command_embed_options
command_embed_options[LANGUAGE_LIST_MAX][128] char per command-embed: first, after OPTIONS, skip OPTIONS (an option's value count after a colon: –arg:2), none OPTIONS (the code is in a file)
case_patterns LanguageMarkers a word, the word before the patterns, the closing word, then the item ends (case in esac ;; ;& ;;&): the ) after a pattern is no bracket
interpolations LanguageTriples delimiter, open, close: code inside a string (" ${ })
interpolation_prefixes LanguageMarkers only a string with one of these interpolates (f, s); none: every one
interpolation_variables LanguageTriples delimiter, sigil: name inside a string (" )
escape char 0 for none
escape_braces[8] char letters after the escape whose { } is part of it: Python's \N{NAME}
escape_in_code int the escape quotes the next character outside strings too (the shell's \;)
raw_escaped_quote int in a raw string, the escape before the delimiter keeps it open (Python's r'\'')
interpolation_format char ':' in an interpolation's code starts a format spec, text with fields of its own (Python)
line_continuation char a line that ends with it goes on (a directive, a statement)
line_splice char it and a line end vanish anywhere, inside tokens too (C's )
trigraphs int ??= and the rest (C, off by default)
digraphs LanguageTriples alternative, standard: <% {
less_colon_colon int C++: <:: is < :: unless : or > follows.
directive char a line that opens with it (or its digraph) is a directive
directive_first_column int only where it is the line's first character (Haskell's CPP; ### deeper is an operator)
conditional_words[96] char after the directive character: if, else-if, else, end, each with | alternatives (Haxe: if elseif else end; FreeBASIC: if|ifdef|ifndef elseif else endif); the grammar reads the first branch only
conditional_in_line int such a mark ends after its condition, the rest of its line is code (Haxe: if a x else y end)
comment_strings int a string in a block comment is read as one: a closer in it ends nothing (OCaml)
literate int a line not indented by a tab or four spaces is prose: a comment (Literate CoffeeScript)
smart_quotes int a typographic quote ends a string as the plain one does (PowerShell: " and “ ” „, ' and ‘ ’ ‚ ‛)
comment_definition[64] char words that, in order in a : NAME ... ; body, make NAME a line comment (Forth: SOURCE >IN !)
name_definition[64] char the same for a word that takes the next word as a name (Forth: BL WORD|PARSE-NAME)
name_words LanguageMarkers words after which the next blank-delimited word is a name, whatever it holds (Forth: ' POSTPONE [CHAR])
header_words LanguageMarkers in a directive, after one of these, <...> is a header name
directive_word_prefix char `define: the prefix and a word are a directive anywhere (Verilog)
attribute_prefix[8] char "@": "\@name" is an attribute; several (CSS's "# .": id, .class)
attribute_where_operand int only where an operand may stand: after one, @ is an operator (Python's a@b)
attribute_brackets LanguageTriples open, close: #[ ] is one attribute token
macro_suffix char '!': "name!" is a macro call (Rust)
symbol_prefix char ':': ":name" is a constant (Ruby, Lisp)
symbol_operators LanguageMarkers :[] :<=> :+: operators that are symbols too, and :name= (Ruby)
tick_style uint8_t a ' and a name that is no character constant: this style
number_separator char '_' or '\'' inside numbers
exponent_letters[8] char letters that may follow a number's dot as its exponent: "eE" when empty; Fortran's dD
number_characters[16] char more characters a number may hold: # (16FF#)
tick_numbers int 4'b1010: a ' and a base letter go on a number (Verilog)
numbered_character char '#': #13 is a character (Pascal)
character_prefixes LanguageMarkers $a (Erlang, Smalltalk), ?a (Ruby, Emacs Lisp); one that ends in \ takes a name too (#\space, \newline)
regex char '/': a regex literal where an operand may stand
regex_flags[32] char letters that can follow a regex literal (gimsuy)
regex_multiline int a regex may span lines (Ruby's /x)
regex_after LanguageMarkers words after which a regex may stand (return, typeof ...)
quote_operators LanguageMarkers q qq qw: a string with any delimiter
quote_in_quotes int q'[...]': the delimiter sits in quotes (Oracle)
regex_operators LanguageMarkers m qr: a regex with any delimiter
substitution_operators LanguageMarkers s tr y: two parts
percent_literals LanguageMarkers w q Q ...: Ruby's literals
percent_regexes LanguageMarkers r
variable_prefixes[8] char $ @ %: a sigil and a name are a variable
variable_specials[64] char $? $# $1: the characters a sigil may stand before alone
variable_braces LanguageTriples open, close: ${name} is one variable token
variable_delimiters LanguageTriples open, close: name% (Batch)
command_substitutions LanguageTriples open, close: $( ... ) holds code
quote_transpose int ' after an operand is an operator (Matlab's transpose)
operand_at_line_start int a line's first token may be an operand: a regex, w[] (Ruby)
full_stop int a . then a space ends a statement, not a number (Smalltalk's 1000.)
operators LanguageOperators longest first
line_prefixes LanguageMarkers [lines]: the prefixes that make a line one token, in the file's order (the longest that matches wins)
line_styles[LANGUAGE_LIST_MAX] uint8_t the LanguageStyle of each line_prefixes rule
lines_only int a line no [lines] rule takes is text: no tokens (diff)
markup int tags and text between them (HTML, XML)
markdown int a Markdown document: headings, emphasis, code fences
jsx int <tag> where an operand may stand is a JSX element (JavaScript, TSX)
code_regions LanguageTriples open, close: <?php ?> holds the language's own code
embeds LanguageTriples tag (or fence word), language id: <script> holds javascript
fence_languages int a fence's word is looked for among the set's languages
block_fences LanguageTriples marker, style: a line of the marker opens a block to the same line (AsciiDoc's -—)
inlines LanguageTriples marker, style: text between two markers in a line (strong); none: Markdown's own
setext_headings[16] char characters of a line under a heading (= -); "" for none
indented_code int an indented line after a blank one is code (Markdown's four spaces)
literal_after LanguageMarker a line that ends with it opens an indented code block (reStructuredText's ::)
words LanguageWord * sorted by word
word_count size_t how many
word_text char * the words, each NUL-ended
patterns[LANGUAGE_SYMBOLS_MAX] LanguagePattern the [symbols] patterns, in the file's order
pattern_count int how many
symbol_not_after LanguageMarkers a pattern after one of these declares nothing (end subroutine foo)
comment_toggle[16] char what Toggle Comment puts at a line's start
indent_after[16] char characters that, ending a line, indent the next one
semicolon_after[64] char space-separated: words whose { } block ends with a ; (struct union enum): an editor's auto-close types }; for them
import_words[128] char space-separated: words that bring in another file (import from require use mod include source)
import_separator char the character between a module's parts (Python's ., Rust's ::'s :), or 0
import_files[256] char space-separated: the files a module can be, {} its path ({}.py {}/init.py)
unicode_escape[32] char how a character is escaped in a string: a form for U+0080 to U+FFFF and one past it (\u%4 \U%8); %4 %8 hex digits, % as few as it takes; utf16 for two \u%4 (Java)
spelling uint8_t LanguageSpelling: which of its text a spelling checker reads (spelling = code, prose, markup or none)

LanguageSet

Several languages: a file and those it embeds.

Field Type Description
languages Language * the languages read
count int how many

LanguagePairKind

A kind of pair besides ( ) [ ] { }: the words of the pairs key.

Name Value Description
LANGUAGE_PAIR_QUOTE quotes: a string's or a character's opening and closing quotes
LANGUAGE_PAIR_COMMENT comments: a block comment's opener and closer
LANGUAGE_PAIR_INTERPOLATION interpolation: ${ } in a string
LANGUAGE_PAIR_TAG tags: a start tag and its end tag
LANGUAGE_PAIR_ANGLE angles: < > of generics and templates
LANGUAGE_PAIR_KEYWORD keywords: a block's words (do ... end, if ... fi, \begin{x} ... \end{x})
LANGUAGE_PAIR_CONDITIONAL conditionals: if, elif, else, endif
LANGUAGE_PAIR_TEXT text: the text-pairs, in prose
LANGUAGE_PAIR_KIND_COUNT

LanguagePair

One end of a pair.

Field Type Description
offset uint32_t its first byte
length uint32_t its length in bytes: the quote, the whole tag (< to >), the keyword, the # and its word
partner int32_t the other end's index in the array; -1 when it has none (see ghost_offset). In a if chain, the next of the chain, the endif's being the if
ghost_offset uint32_t when partner is -1: where the missing end most likely goes, or LANGUAGE_PAIR_NOWHERE
kind uint8_t LanguagePairKind.
confidence uint8_t as a brace answer's: 0 certain, 1 likely, 2 ambiguous (BraceConfidence)
role uint8_t 1 it opens, 0 it closes, 2 it is in the middle (else)
missing[24] char when partner is -1: the text of the missing end (, fi, "), "" when it needs none

LanguageSpelling

Which text of a language a spelling checker reads (the spelling key): language_spelling_ranges().

Name Value Description
LANGUAGE_SPELLING_CODE comments, and strings with a space in them (words, not keys)
LANGUAGE_SPELLING_PROSE all of it but code, links, tags, entities, commands and the like (Markdown, LaTeX, plain text)
LANGUAGE_SPELLING_MARKUP comments, strings with a space, and the text between the tags (HTML, XML)
LANGUAGE_SPELLING_NONE none of it (a diff, data)

LanguageRange

A run of a text: [start, end).

Field Type Description
start uint32_t
end uint32_t

LanguagePoMessage

One message of a gettext catalog (.po, .pot): where its parts are. Strings run from their first " to after their last (over continuation lines); a msgid_plural's is 0 when there is none.

Field Type Description
start uint32_t
end uint32_t its first line's start (a comment or msgctxt or msgid) and its last line's end
flags uint32_t its "#," line, or UINT32_MAX for none
keyword uint32_t its msgid keyword
msgid_start uint32_t
msgid_end uint32_t msgid's strings
plural_start uint32_t
plural_end uint32_t msgid_plural's, 0 for none
msgstr_start[LANGUAGE_PO_FORMS] uint32_t
msgstr_end[LANGUAGE_PO_FORMS] uint32_t msgstr's, or each msgstr[n]'s
msgstr_empty[LANGUAGE_PO_FORMS] uint8_t 1 when that one is ""
msgstr_count int how many msgstr
fuzzy uint8_t #, fuzzy
untranslated uint8_t every msgstr is ""
header uint8_t msgid "": the catalog's header
obsolete uint8_t #~: kept, no longer used
format int8_t 1 for c-format or python-format, -1 for no-c-format and the like, 0 unsaid

LanguageUnusualKind

What is unusual about a character that language_unusual_characters() flags.

Name Value Description
LANGUAGE_UNUSUAL_BIDI a bidirectional control (U+202E and the others): code can read otherwise than it runs ("Trojan Source")
LANGUAGE_UNUSUAL_INVISIBLE a character that shows nothing: a zero-width space, a joiner, a soft hyphen, a BOM inside the text
LANGUAGE_UNUSUAL_SPACE a space that looks like a plain one and is not: a no-break space, an em space ...

LanguageUnusual

One flagged character.

Field Type Description
offset uint32_t its first byte
length uint32_t its bytes
code uint32_t its code point
kind uint8_t LanguageUnusualKind.
label[8] char a short name to show it by: RLO, ZWSP, NBSP, BOM

LanguageFold

One block that can fold: its lines, counted from 0.

Field Type Description
first_line uint32_t the block's first line, from 0: the line that stays in sight when it is folded
last_line uint32_t its last line

Constants

LANGUAGE_LIST_MAX

#define LANGUAGE_LIST_MAX 24

markers of one kind (line comments, strings ...)

LANGUAGE_MARKER_MAX

#define LANGUAGE_MARKER_MAX 16

bytes in one marker, with its NUL

LANGUAGE_OPERATORS_MAX

#define LANGUAGE_OPERATORS_MAX 320

operators a language can list

LANGUAGE_SYMBOLS_MAX

#define LANGUAGE_SYMBOLS_MAX 32

[symbols] patterns

LANGUAGE_PATTERN_MAX

#define LANGUAGE_PATTERN_MAX 6

words in a pattern beside NAME

LANGUAGE_BLOCK_RULES_MAX

#define LANGUAGE_BLOCK_RULES_MAX 32

block-rule entries

LANGUAGE_BLOCK_WORDS_MAX

#define LANGUAGE_BLOCK_WORDS_MAX 10

words of one block-rule

LANGUAGE_PAIR_NOWHERE

#define LANGUAGE_PAIR_NOWHERE UINT32_MAX

Where a pair's partner is not: LanguagePair.ghost_offset when no place is known.

LANGUAGE_PO_FORMS

#define LANGUAGE_PO_FORMS 8

the msgstr[n] of one message kept: more plural forms are left out

Functions

language_parse

int language_parse(Language *language, const char *text, const char *id);

Parse a language file.

A key or section that Kami does not know is ignored, and a value that is not valid leaves the default. A [grammar] that cannot be compiled leaves grammar NULL and says why in grammar_problem.

Parameter Description
language Filled in; free it with language_free()
text The file's text, or NULL for a language with only the defaults
id The language's id: the file's name without .lang ("python"); it is also the name until a name key sets one

Returns: 0, or -1 when out of memory or language is NULL (the language is then cleared)

language_free

void language_free(Language *language);

Free what language_parse() made, and clear the language.

Parameter Description
language The language, or NULL (which does nothing)

language_set_parse

int language_set_parse(LanguageSet *set, const char *text);

Parse several language files in one text.

Each file starts with a line "%%language ID". A text whose first line is not such a line starts with a file whose id is "main". The first file is the language of the text being read; the others are the languages it embeds.

Parameter Description
set Filled in; free it with language_set_free()
text The files, one after the other, or NULL for an empty set

Returns: 0, or -1 when out of memory or set is NULL

language_set_free

void language_set_free(LanguageSet *set);

Free a set and its languages, and clear it.

Parameter Description
set The set, or NULL (which does nothing)

language_set_find

const Language *language_set_find(
    const LanguageSet *set,
    const char *word,
    size_t length);

Find a language of a set by a word, such as a code fence's word.

The word is compared, in any case, first with each language's id, then with each name, then with each language's extensions.

Parameter Description
set The set
word The word (it need not end with a NUL)
length The word's length in bytes

Returns: The language, or NULL when none matches

language_builtin

const Language *language_builtin(const char *id);

Kami's built-in C or C++ language, parsed once (safely from any thread) from the file built into Kami.

Parameter Description
id "c" or "cpp"

Returns: The language, or NULL for another id

language_builtin_text

const char *language_builtin_text(const char *id);

The text of a built-in language file: the c.lang or cpp.lang that Unryu ships.

Parameter Description
id "c" or "cpp"

Returns: The text, or NULL for another id

language_match

int language_match(const Language *language, const char *path, const char *first_line);

How well a language fits a file.

Parameter Description
language The language
path The file's path or name
first_line Its first line, for a #! program; NULL when it is not known

Returns: 3 for a whole file name in filenames, 2 for an extension or a filenames pattern, 1 for a #! program in shebangs, 0 for no match

language_detect_score

int language_detect_score(const Language *language, const char *text, size_t length);

How many of a language's detect words the start of a text holds.

For two languages that claim the same extension (.m), the one with more wins. Each word counts once, wherever it is, even inside another word; case matters.

Parameter Description
language The language
text The file's text (its first 4 KB are looked at)
length Its length in bytes

Returns: How many of the words were found

language_lex

int language_lex(const Language *language, Lexer *lexer, const char *text, size_t size);

Lex a text in a language; each token gets its style (Token.style)

Parameter Description
language The language
lexer Filled in; free it with lexer_free()
text The text; it must stay alive while the tokens are used
size Its length in bytes

Returns: 0, or -1 when out of memory or an argument is missing

language_lex_set

int language_lex_set(
    const LanguageSet *set,
    Lexer *lexer,
    const char *text,
    size_t size,
    unsigned options);

Lex a text in the first language of a set, with the others for the languages it embeds.

Parameter Description
set The set
lexer Filled in; free it with lexer_free()
text The text
size Its length in bytes
options LEXER_TRIGRAPHS also reads trigraphs; LEXER_CPLUSPLUS is only recorded in lexer->options (it is set anyway for a language whose engine is c++)

Returns: 0, or -1 when out of memory, when the set is empty or an argument is missing

language_lex_options

int language_lex_options(
    const Language *language,
    Lexer *lexer,
    const char *text,
    size_t size,
    unsigned options);

language_lex() with lexer options, as lexer_run() calls it

Parameter Description
language The language
lexer Filled in; free it with lexer_free()
text The text
size Its length in bytes
options As for language_lex_set()

Returns: 0, or -1 when out of memory or an argument is missing

language_word_style

LanguageStyle language_word_style(
    const Language *language,
    const char *word,
    size_t length);

The class of a word, as the [words] section gives it.

Parameter Description
language The language
word The word (it need not end with a NUL)
length Its length in bytes

Returns: The word's style, or LANGUAGE_STYLE_IDENTIFIER for a word that no class lists

language_conditional_word

int language_conditional_word(const Language *language, const char *word, size_t length);

Which of the conditional-words a word is, in any case.

Parameter Description
language The language
word The word, after the directive character
length Its length in bytes

Returns: 0 for if, 1 for else-if, 2 for else, 3 for end; -1 for none

language_token_style

LanguageStyle language_token_style(
    const Language *language,
    const Lexer *lexer,
    size_t index);

What a token is, for highlighting: the style the lexer set, else what its kind says.

Parameter Description
language The language whose lexer made the tokens
lexer The tokens
index A token's index

Returns: Its style; LANGUAGE_STYLE_DEFAULT for an index past the end

language_style_name

const char *language_style_name(LanguageStyle style);

A style's name, as [words] and an editor's theme use it.

Parameter Description
style The style

Returns: The name ("keyword", "comment-line" ...), or "" for a value that is no style

language_style_from_name

LanguageStyle language_style_from_name(const char *name);

The style that a name names.

Parameter Description
name The name ("keyword"), or NULL

Returns: The style, or LANGUAGE_STYLE_COUNT when the name is no style's

language_symbols_build

int language_symbols_build(
    const Language *language,
    const Lexer *lexer,
    SemanticModel *model);

The semantic model of a file in a language other than C.

A language with a [grammar] is read by it (language_grammar_build()). When it has none, or the grammar gives up on this file, the [symbols] patterns make the model: a symbol for each place a pattern matches (and for each heading of a document), nested in the block it is in (a method in its class), with the comment lines right above it as its documentation; every name spelled as a declared name is a use of its first declaration; and the model is always valid.

Parameter Description
language The language
lexer Its tokens (language_lex())
model Filled in; free it with semantic_free()

Returns: 0, or -1 when out of memory or an argument is missing

language_grammar_compile

int language_grammar_compile(Language *language, const char *text);

Compile a [grammar] into language->grammar (language_parse() does this)

Parameter Description
language The language; on a fault its grammar_problem says what is wrong
text The rules, one "name = expression" each; a line that starts with a space or tab continues the rule above

Returns: 0, or -1 when the grammar cannot be used (language->grammar is then NULL)

language_grammar_free

void language_grammar_free(LanguageGrammar *grammar);

Free a compiled grammar (language_free() does this)

Parameter Description
grammar The grammar, or NULL

language_grammar_build

int language_grammar_build(
    const Language *language,
    const Lexer *lexer,
    SemanticModel *model);

The semantic model of a file, by its language's grammar.

The model says whether the file parses and, if not, where and why it stops (problem); it holds each declaration with its scope and body, and each use resolved through the scopes around it (a member through the type of what it is taken from). language_symbols_build() calls it for a language with a grammar.

Parameter Description
language The language, which must have a grammar
lexer Its tokens (language_lex())
model Filled in; free it with semantic_free()

Returns: 0, or -1 when out of memory, when the language has no grammar, or when the grammar gave up because the file would take too many steps

language_pairs

LanguagePair *language_pairs(const Language *language, const Lexer *lexer, size_t *count);

The pairs of a text besides ( ) [ ] { }, as its language declares them (pairs)

Read from the tokens alone: strings, comments and interpolation as the lexer cut them, tags by name (void-tags have no end tag; an optional-end-tags element closes when its parent does), keyword blocks as folding pairs them (and keyword-pair, named-pair), if chains, < > that hold types, and text-pairs in prose. An end with no partner has a ghost: where the other most likely goes, and its text.

Parameter Description
language The language
lexer Its tokens (language_lex())
count Set to how many pairs ends there are

Returns: The ends in the order of the text (malloc'd: free() it), or NULL when there are none or out of memory

language_keyword_pairs

long *language_keyword_pairs(const Language *language, const Lexer *lexer);

The keyword blocks of a blocks = keywords language, as folding pairs them.

Parameter Description
language The language
lexer The tokens

Returns: Per token: its closer's index for an opener, -2 for an opener with no closer, -3 for a closer with no opener, -1 for any other token (malloc'd: free() it); NULL when out of memory

language_spelling_ranges

LanguageRange *language_spelling_ranges(
    const Language *language,
    const Lexer *lexer,
    size_t *count);

The runs of a text that hold words to check for spelling, by the language's spelling key.

Reads only the tokens, as language_pairs() does. In code, a comment's run and a string's (not a string of code inside another, nor one with no space: a key, a path, a format); in prose, all but the tokens that are code, links, tags, entities, commands, numbers and operators; in markup, comments, strings with a space, and the text no token covers.

Parameter Description
language The language
lexer The text's tokens (lexer->source is the text)
count Set to how many runs there are

Returns: The runs in the order of the text (malloc'd: free() it), or NULL when there are none

language_po_messages

LanguagePoMessage *language_po_messages(const char *text, size_t size, size_t *count);

The messages of a gettext catalog, line by line as gettext writes it.

Parameter Description
text The catalog
size Its length
count Set to how many

Returns: The messages in the order of the text (malloc'd: free() it), or NULL when there are none

language_po_directives_match

int language_po_directives_match(
    const char *text,
    size_t size,
    const LanguagePoMessage *message,
    int form);

Whether a translation's format directives are the original's.

The directives of msgstr (or msgstr[form]) against those of msgid (or, for a plural form past the first, msgid_plural): s, %5.2f, ld and %1$s; numbered ones by their numbers, the others in order; a plural form may leave out the first (the number: "one file"). Checked for a c-format or python-format message, and for one that says nothing when either text has a directive; a no-c-format one, an empty translation and a % that starts no directive we know (a python %(name)s) count as matching.

Returns: 1 when they match (or there is nothing to check), 0 when they do not

language_unusual_characters

LanguageUnusual *language_unusual_characters(
    const char *text,
    size_t size,
    int spaces,
    size_t *count);

The characters of a text that are not what they look like.

Bidirectional controls, characters that show nothing (a BOM counts only past the text's first byte), and spaces that look like a plain one. Spaces are left out when spaces is 0, for prose, where a no-break space is meant.

Parameter Description
text The text (UTF-8)
size Its length in bytes
spaces 1 to flag the spaces that look plain too
count Set to how many there are

Returns: The flagged characters in the order of the text (malloc'd: free() it), or NULL when there are none

language_indent_folds

size_t language_indent_folds(const Lexer *lexer, LanguageFold *folds, size_t capacity);

The blocks of an indented language.

A statement (a line's first token outside every bracket) followed by deeper statements opens a block that runs to the last line of the last of them; blank lines inside belong to it. Lines inside an open bracket continue the line above, and open no block of their own.

Parameter Description
lexer The tokens (their lines and columns)
folds Filled in, or NULL to only count
capacity How many folds the array holds

Returns: How many blocks there are (which may be more than capacity)

language_keyword_folds

size_t language_keyword_folds(
    const Language *language,
    const Lexer *lexer,
    LanguageFold *folds,
    size_t capacity);

The blocks of a language whose words open and close them (blocks = keywords)

Each opener (a block-start word at a statement's start, or a block-open word anywhere) runs to the block-end word that matches it, when the two are on different lines.

Parameter Description
language The language (its block words and block rules)
lexer The tokens
folds Filled in, or NULL to only count
capacity How many folds the array holds

Returns: How many blocks there are (which may be more than capacity)