- Version: 1.0.4 (02.05.2026)
- Home: https://github.com/1Hyena/nt4c
- Issue tracker: https://github.com/1Hyena/nt4c/issues
NT4C stands for "NestedText for C" and that is exactly what this project is about.
In short, NestedText is a file format for holding structured data.
The following resources can explain more if you are unfamiliar with it:
NT4C is a NestedText parser implementation written in accordance with the C23 standard of the C programming language. It includes the following features:
-
Compliance: NT4C aims to comply with the latest version of the NestedText specification. However, it is currently only compliant with the Minimal NestedText specification.
-
Performance: NT4C is fast as it does not involve any heap memory allocations. It also avoids unnecessary memory copying by directly referencing the input text in the resulting graph.
-
Compactness: The NT4C parser is implemented in a single header file with no dependencies other than the standard C library.
-
Embedding: The NT4C parser is easily reusable in other projects with a simple API that includes a few key functions, primarily
nt_parse(). -
Callbacks: NT4C parses the entire document and calls a callback function provided by the application to inform it about each NestedText unit.
-
Tree model: If sufficient memory is provided to the NT4C parser, it constructs a graph where each node directly references a segment from the input text.
-
Portability: NT4C builds and functions on Linux. It should be relatively simple to make it run on most other platforms as long as the platform provides the C standard library.
-
Encoding: NT4C expects UTF-8 encoding of the input text and does not attempt to detect Unicode encoding errors.
-
Permissive license: NT4C is available under the MIT license.
To parse a NestedText document, you can include the nt4c.h header file directly in your codebase. The parser is implemented in a single C header file for easy integration.
The main functions to use are nt_parse() and nt_parser_parse(). The former
is a convenience function for simple callback based parsing whereas the latter
takes a pointer to the NT_PARSER structure as its first argument and is to be
used for customized parsing.
The NT_PARSER structure stores parsing configuration and the parsing process
state. By default, it can handle up to NT_PARSER_NCOUNT nodes in its internal
memory. However, you can use the nt_parser_set_memory function to work with a
custom array of NT_NODE structures.
When you call nt_parser_parse(), the parser populates the document graph with
nodes. It continues processing even if the output buffer reaches its capacity.
After a successful parsing operation, both nt_parse() and nt_parser_parse()
return the number of nodes in the input text. This information can help you to
determine the memory required for storing the full graph of the document. If
parsing fails, the function returns a negative value.
The graph of the document is considered fully stored when the value returned by
nt_parser_parse() is non-negative and does not exceed the output buffer's
capacity.
The exhello example demonstrates how to use the NT4C parser to generate the text "hello world" and display it on the screen.
Lines 5 to 14 in 5e5b8fc
The excallback example demonstrates how to make the NT4C parser call a user-specified function each time it parses the next logical portion of the input document.
nt4c/examples/src/excallback.c
Lines 6 to 37 in 5e5b8fc
This example demonstrates how to utilize the NT4C parser to parse and display a NestedText document on the screen. The input document undergoes parsing twice. Initially, the length of the document is calculated. Subsequently, a variable-length array is set up to store the Document Object Model (DOM).
Lines 33 to 35 in 5e5b8fc
This example shows how to use the NT4C parser to pretty-print a NestedText document. It reformats the input text and adds syntax highlighting.
Lines 70 to 78 in 5e5b8fc
Here is a NestedText document before and after pretty-printing, as shown in the screenshot below:
Lines 1 to 29 in 5e5b8fc
This example shows how to use the NT4C parser to print the structure of a NestedText document on the screen.
Lines 84 to 98 in 5e5b8fc
Here is a screenshot showing the structure of the parsed NestedText document:
The exquery example demonstrates how to query an arbitrary NestedText document for a specific key and print the contents of the matching node on screen.
Lines 1 to 21 in 5e5b8fc
-
- nt_make_parser () →
NT_PARSER - nt_parser_reset (&parser)
- nt_make_parser () →
-
- nt_parse (text, text size, callback, userdata) →
int - nt_parser_parse (&parser, text, text size) →
int
- nt_parse (text, text size, callback, userdata) →
-
- nt_parser_set_memory (&parser, &nodes, node count)
- nt_parser_set_recursion (&parser, depth)
- nt_parser_set_blacklist (&parser, banned types)
- nt_parser_set_whitelist (&parser, allowed types)
- nt_parser_set_userdata (&parser, &userdata)
- nt_parser_set_callback (&parser, &callback)
-
- nt_type_code (type) →
const char * - nt_type_type (type) →
NT_TYPE
- nt_type_code (type) →
Lines 48 to 87 in 5e5b8fc
Specify the size of the integrated memory buffer of the NT_PARSER structure by
defining the NT_PARSER_NCOUNT macro before including the nt4c.h header. The
integrated memory was added to increase the API usage convenience in cases where
the size of the input document is always known to be small (see
expretty).
Lines 41 to 43 in 5e5b8fc
Lines 113 to 115 in 5e5b8fc
Examples: execho, expretty, exquery, extree
Lines 117 to 121 in 5e5b8fc
Lines 98 to 111 in 5e5b8fc
Examples: excallback, execho, exhello
Lines 123 to 133 in 5e5b8fc
Examples: execho, expretty, exquery, extree
Lines 135 to 142 in 5e5b8fc
Lines 144 to 149 in 5e5b8fc
Lines 151 to 157 in 5e5b8fc
Lines 159 to 165 in 5e5b8fc
Lines 167 to 173 in 5e5b8fc
Examples: exquery
Lines 175 to 181 in 5e5b8fc
Examples: exquery
Lines 183 to 188 in 5e5b8fc
Examples: excallback, extree
Lines 190 to 194 in 5e5b8fc
Examples: excallback, exquery, extree
NT4C has been authored by Erich Erstu and is released under the MIT license.





