82 lines
2.0 KiB
Markdown
82 lines
2.0 KiB
Markdown
# spp
|
|
|
|
## Description
|
|
SPP (Single Pass Parser) is a minimal, zero-allocation parsing utility for C
|
|
that operates directly on a character stream. It provides a small set of
|
|
composable primitives for building fast, simple parsers without backtracking.
|
|
|
|
The library is designed around the idea of consuming input in a single forward
|
|
pass, making it ideal for tokenization, lightweight parsing, and embedded
|
|
systems.
|
|
|
|
## Table of Contents
|
|
|
|
* [Features](#features)
|
|
* [Todos](#todos)
|
|
* [Usage](#usage)
|
|
* [Acknowledgments](#acknowledgments)
|
|
* [License](#license)
|
|
|
|
## Features
|
|
|
|
* Single-pass, forward-only parsing
|
|
* Zero allocations
|
|
* Tiny API surface
|
|
* Composable primitives
|
|
* Works directly on const char* streams
|
|
|
|
## Todos
|
|
|
|
* [ ] Add new feature X
|
|
* [ ] Improve documentation
|
|
* [ ] Write tests
|
|
|
|
## Usage
|
|
```
|
|
// Returns true if the current character is in the provided character set.
|
|
bool spp_is(struct Stream s, const char *list);
|
|
|
|
// Returns true if the stream has reached the end '\0'
|
|
bool spp_eof(struct Stream s);
|
|
|
|
// Consumes and returns the current stream character if in a list.
|
|
char spp_take(struct Stream s, const char *list);
|
|
|
|
// Consumes characters while they belong to the set. I.E. skip white-space.
|
|
uintptr_t spp_skip(struct Stream s, const char *list);
|
|
|
|
// Consumes characters until a character from the set is encountered.
|
|
uintptr_t spp_until(struct Stream s, const char *list);
|
|
|
|
// Returns current cursor position of the stream.
|
|
const char *spp_cursor(struct Stream s);
|
|
|
|
```
|
|
|
|
## Tokenizing Words
|
|
|
|
```
|
|
while (!spp_eof(s)) {
|
|
spp_skip(s, WS);
|
|
|
|
const char *start = spp_cursor(s);
|
|
uintptr_t len = spp_until(s, WS);
|
|
|
|
if (len > 0) {
|
|
printf("Token: %.*s\n", (int)len, start);
|
|
}
|
|
}
|
|
```
|
|
|
|
## Acknowledgments
|
|
|
|
[Tom Preston-Werner README Driven Development](https://tom.preston-werner.com/2010/08/23/readme-driven-development)<br>
|
|
[Make a README](https://www.makeareadme.com/)<br>
|
|
[Choose a LICENSE](https://choosealicense.com/)<br>
|
|
|
|
|
|
## License
|
|
|
|
This project is licensed under the MIT License - see the [LICENSE] file for details.
|
|
|