Supports (, [, {, and < as brace characters.
This utility is intended as a simple, lightweight config loader for development utilities, not a full-fledged user-facing parser
with meaningful error messages. It was originally built to use with gode generators.
typedef struct sexp {
char type; // 0 = list, 1 = literal
char brace; // '(', '[', '{', or '<'
char* str; // scalar value; NULL when VEC_len(args) > 0
VEC(struct sexp*) args;
} sexp;
sexp* sexp_parse(char* source)
Parses the NULL-terminated source string. Syntax must be valid. The resulting tree does not depend on the memory inside source.
sexp* sexp_parse_file(char* path)
Parses the file at path. Syntax must be valid. The resulting tree does not depend on the memory inside source.
Frees the entire contents of x recursively.
void sexp_print(int fd, sexp* x)
Recursively prints the contents of x to the file descriptor fd.
sexp* sexp_arg(sexp* x, size_t argn)
Returns the nth argment from the list inside x. Returns NULL if called on a scalar
value or if x is NULL.
int64_t sexp_int(sexp* x, size_t argn)
Returns the nth argment from the list inside x converted to a signed integer using
strtol(y->str, NULL, 0). If called on a scalar value, the value is converted and
argn is ignored. Returns 0 if x is NULL.
uint64_t sexp_uint(sexp* x, size_t argn)
Returns the nth argment from the list inside x converted to an unsigned integer using
strtoul(y->str, NULL, 0). If called on a scalar value, the value is converted and
argn is ignored. Returns 0 if x is NULL.
float sexp_float(sexp* x, size_t argn)
Returns the nth argment from the list inside x converted to a float using strtof(). If
called on a scalar value, the value is converted and argn is ignored. Returns 0 if x
is NULL.
double sexp_double(sexp* x, size_t argn)
Returns the nth argment from the list inside x converted to a double using strtod(). If
called on a scalar value, the value is converted and argn is ignored. Returns 0 if x
is NULL.
char* sexp_str(sexp* x, size_t argn)
Returns the internal string pointer for the nth argment from the list inside x. If
called on a scalar value, the value's pointer is returned. Returns NULL if x
is NULL.
char* sexp_str(sexp* x, size_t argn)
Returns a newly-allocated copy of the string for the nth argment from the list inside x. If
called on a scalar value, the a copy of the value's string is returned. Returns NULL if x
is NULL.