First commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*~
|
||||
*.exe
|
13
Makefile
Normal file
13
Makefile
Normal file
@@ -0,0 +1,13 @@
|
||||
CC = gcc
|
||||
CFLAGS = -ansi -Wall -Werror -Wno-unused -g
|
||||
|
||||
TESTS = $(wildcard tests/*.c)
|
||||
|
||||
all: check
|
||||
|
||||
check: $(TESTS) mpc.c
|
||||
$(CC) $(CFLAGS) $^ -o test
|
||||
./test
|
||||
|
||||
clean:
|
||||
rm test.exe
|
101
README.md
Normal file
101
README.md
Normal file
@@ -0,0 +1,101 @@
|
||||
Micro Parser Combinators
|
||||
========================
|
||||
|
||||
_mpc_ is a lightweight Parser Combinator library for C.
|
||||
|
||||
The current main alternative is a branch of (https://github.com/wbhart/Cesium3)[Cesium3].
|
||||
|
||||
Features & Advantages
|
||||
---------------------
|
||||
|
||||
* Error Message Support
|
||||
* Regular Expression Support
|
||||
* Parser Grammar Support
|
||||
* Works for Generic Types
|
||||
* AST Extension
|
||||
* Single source & header files
|
||||
* Written in clean ANSI C
|
||||
* Doesn't rely on Boehm-Demers-Weiser Garbage Collection
|
||||
* Doesn't use `setjmp` and `longjmp` for errors
|
||||
* Doesn't pollute namespace
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
```c
|
||||
|
||||
mpc_val_t* combine_maths(int n, mpc_val_t** xs) {
|
||||
|
||||
int** vs = (int**)xs;
|
||||
|
||||
if (*vs[1] == '*') { *vs[0] *= *vs[2]; }
|
||||
if (*vs[1] == '/') { *vs[0] /= *vs[2]; }
|
||||
if (*vs[1] == '+') { *vs[0] += *vs[2]; }
|
||||
if (*vs[1] == '-') { *vs[0] -= *vs[2]; }
|
||||
|
||||
free(vs[1]);
|
||||
free(vs[2]);
|
||||
|
||||
return vs[0];
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
mpc_parser_t* Expr = mpc_new();
|
||||
mpc_parser_t* Factor = mpc_new();
|
||||
mpc_parser_t* Term = mpc_new();
|
||||
mpc_parser_t* Maths = mpc_new();
|
||||
|
||||
mpc_define(Expr,
|
||||
mpc_pc("cmaths ( fact ['*' | '/'] fact ) | fact",
|
||||
combine_maths, Factor, free, Factor, free, Factor),
|
||||
);
|
||||
|
||||
mpc_define(Factor,
|
||||
mpc_pc("cmaths ( term ['+' | '-'] term ) | term",
|
||||
combine_maths, Term, free, Term, free, Term),
|
||||
);
|
||||
|
||||
mpc_define(Term,
|
||||
mpc_pc("num | snd ('(' expr ')')",
|
||||
mpc_int(), mpcf_asnd_free, Expr, free)
|
||||
);
|
||||
|
||||
mpc_define(Maths, mpc_ends(Expr, free));
|
||||
|
||||
mpc_delete(Expr);
|
||||
mpc_delete(Factor);
|
||||
mpc_delete(Term);
|
||||
mpc_delete(Maths);
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Parsers
|
||||
-------
|
||||
|
||||
|
||||
Combinators
|
||||
-----------
|
||||
|
||||
|
||||
Regular Expressions
|
||||
-------------------
|
||||
|
||||
|
||||
Combinator Grammars
|
||||
-------------------
|
||||
|
||||
|
||||
Abstract Syntax Tree
|
||||
--------------------
|
||||
|
||||
|
||||
Reference
|
||||
---------
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
204
mpc.h
Normal file
204
mpc.h
Normal file
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
** mpc - Micro Parser Combinator library for C
|
||||
** https://github.com/orangeduck/mpc
|
||||
** Daniel Holden - contact@daniel-holden.com
|
||||
** Licensed under BSD3
|
||||
*/
|
||||
#ifndef mpc_h
|
||||
#define mpc_h
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
** Error Type
|
||||
*/
|
||||
|
||||
struct mpc_err_t;
|
||||
typedef struct mpc_err_t mpc_err_t;
|
||||
|
||||
int mpc_err_line(mpc_err_t* x);
|
||||
int mpc_err_column(mpc_err_t* x);
|
||||
char mpc_err_unexpected(mpc_err_t* x);
|
||||
char** mpc_err_expected(mpc_err_t* x, int* num);
|
||||
|
||||
void mpc_err_delete(mpc_err_t* x);
|
||||
void mpc_err_print(mpc_err_t* x);
|
||||
void mpc_err_print_to(mpc_err_t* x, FILE* f);
|
||||
void mpc_err_msg(mpc_err_t* x, char* out, int* outn, int outmax);
|
||||
|
||||
/*
|
||||
** Parsing
|
||||
*/
|
||||
|
||||
typedef void mpc_val_t;
|
||||
|
||||
typedef union {
|
||||
mpc_err_t* error;
|
||||
mpc_val_t* output;
|
||||
} mpc_result_t;
|
||||
|
||||
struct mpc_parser_t;
|
||||
typedef struct mpc_parser_t mpc_parser_t;
|
||||
|
||||
bool mpc_parse(const char* s, mpc_parser_t* p, mpc_result_t* r);
|
||||
bool mpc_parse_file(FILE* f, mpc_parser_t* p, mpc_result_t* r);
|
||||
bool mpc_parse_filename(const char* filename, mpc_parser_t* p, mpc_result_t* r);
|
||||
|
||||
/*
|
||||
** Building a Parser
|
||||
*/
|
||||
|
||||
void mpc_delete(mpc_parser_t* p);
|
||||
mpc_parser_t* mpc_new(void);
|
||||
mpc_parser_t* mpc_assign(mpc_parser_t* p, mpc_parser_t* a);
|
||||
mpc_parser_t* mpc_define(mpc_parser_t* p, mpc_parser_t* a);
|
||||
mpc_parser_t* mpc_retain(mpc_parser_t* p);
|
||||
mpc_parser_t* mpc_expect(mpc_parser_t* a, const char* expected);
|
||||
|
||||
mpc_parser_t* mpc_pass(void);
|
||||
mpc_parser_t* mpc_fail(void);
|
||||
mpc_parser_t* mpc_lift(mpc_val_t* x);
|
||||
|
||||
/*
|
||||
** Basic Parsers
|
||||
*/
|
||||
|
||||
mpc_parser_t* mpc_any(void);
|
||||
mpc_parser_t* mpc_char(char c);
|
||||
mpc_parser_t* mpc_range(char s, char e);
|
||||
mpc_parser_t* mpc_oneof(const char* s);
|
||||
mpc_parser_t* mpc_noneof(const char* s);
|
||||
mpc_parser_t* mpc_satisfy(bool(*f)(char));
|
||||
mpc_parser_t* mpc_string(const char* s);
|
||||
|
||||
/*
|
||||
** Function Types
|
||||
*/
|
||||
|
||||
typedef void (*mpc_dtor_t)(mpc_val_t*);
|
||||
typedef mpc_val_t*(*mpc_apply_t)(mpc_val_t*);
|
||||
typedef mpc_val_t*(*mpc_fold_t)(mpc_val_t*,mpc_val_t*);
|
||||
typedef mpc_val_t*(*mpc_afold_t)(int,mpc_val_t**);
|
||||
|
||||
void mpc_dtor_null(mpc_val_t* x);
|
||||
|
||||
/*
|
||||
** Core Parsers
|
||||
*/
|
||||
|
||||
mpc_parser_t* mpc_apply(mpc_parser_t* a, mpc_apply_t f);
|
||||
mpc_parser_t* mpc_maybe(mpc_parser_t* a);
|
||||
mpc_parser_t* mpc_many(mpc_parser_t* a, mpc_fold_t f);
|
||||
mpc_parser_t* mpc_many1(mpc_parser_t* a, mpc_fold_t f);
|
||||
mpc_parser_t* mpc_count(mpc_parser_t* a, mpc_dtor_t da, mpc_fold_t f, int n);
|
||||
mpc_parser_t* mpc_either(mpc_parser_t* a, mpc_parser_t* b);
|
||||
mpc_parser_t* mpc_also(mpc_parser_t* a, mpc_parser_t* b, mpc_dtor_t da, mpc_fold_t f);
|
||||
mpc_parser_t* mpc_bind(mpc_parser_t* a, mpc_parser_t* b, mpc_dtor_t da, mpc_fold_t f);
|
||||
mpc_parser_t* mpc_or(int n, ...);
|
||||
mpc_parser_t* mpc_and(int n, mpc_afold_t f, ...);
|
||||
|
||||
/*
|
||||
** Common Parsers
|
||||
*/
|
||||
|
||||
mpc_parser_t* mpc_space(void);
|
||||
mpc_parser_t* mpc_spaces(void);
|
||||
mpc_parser_t* mpc_whitespace(void);
|
||||
|
||||
mpc_parser_t* mpc_newline(void);
|
||||
mpc_parser_t* mpc_tab(void);
|
||||
mpc_parser_t* mpc_eoi(void);
|
||||
mpc_parser_t* mpc_escape(void);
|
||||
|
||||
mpc_parser_t* mpc_digit(void);
|
||||
mpc_parser_t* mpc_hexdigit(void);
|
||||
mpc_parser_t* mpc_octdigit(void);
|
||||
mpc_parser_t* mpc_digits(void);
|
||||
mpc_parser_t* mpc_hexdigits(void);
|
||||
mpc_parser_t* mpc_octdigits(void);
|
||||
|
||||
mpc_parser_t* mpc_lower(void);
|
||||
mpc_parser_t* mpc_upper(void);
|
||||
mpc_parser_t* mpc_alpha(void);
|
||||
mpc_parser_t* mpc_underscore(void);
|
||||
|
||||
mpc_parser_t* mpc_int(void);
|
||||
mpc_parser_t* mpc_hex(void);
|
||||
mpc_parser_t* mpc_oct(void);
|
||||
mpc_parser_t* mpc_number(void);
|
||||
|
||||
mpc_parser_t* mpc_float(void);
|
||||
|
||||
mpc_parser_t* mpc_semi(void);
|
||||
mpc_parser_t* mpc_comma(void);
|
||||
mpc_parser_t* mpc_colon(void);
|
||||
mpc_parser_t* mpc_dot(void);
|
||||
|
||||
mpc_parser_t* mpc_char_lit(void);
|
||||
mpc_parser_t* mpc_string_lit(void);
|
||||
|
||||
mpc_parser_t* mpc_ident(void);
|
||||
|
||||
/*
|
||||
** Useful Parsers
|
||||
*/
|
||||
|
||||
mpc_parser_t* mpc_ends(mpc_parser_t* a, mpc_dtor_t da);
|
||||
mpc_parser_t* mpc_skip_many(mpc_parser_t* a, mpc_fold_t f);
|
||||
mpc_parser_t* mpc_skip_many1(mpc_parser_t* a, mpc_fold_t f);
|
||||
mpc_parser_t* mpc_tok(mpc_parser_t* a);
|
||||
mpc_parser_t* mpc_sym(const char* s);
|
||||
mpc_parser_t* mpc_between(mpc_parser_t* a, mpc_dtor_t ad, const char* o, const char* c);
|
||||
mpc_parser_t* mpc_parens(mpc_parser_t* a, mpc_dtor_t ad);
|
||||
mpc_parser_t* mpc_braces(mpc_parser_t* a, mpc_dtor_t ad);
|
||||
mpc_parser_t* mpc_brackets(mpc_parser_t* a, mpc_dtor_t ad);
|
||||
mpc_parser_t* mpc_squares(mpc_parser_t* a, mpc_dtor_t ad);
|
||||
|
||||
|
||||
/*
|
||||
** Regular Expression Parsers
|
||||
*/
|
||||
|
||||
mpc_parser_t* mpc_re(const char* re);
|
||||
|
||||
/*
|
||||
** Common Fold Functions
|
||||
*/
|
||||
|
||||
mpc_val_t* mpcf_free(mpc_val_t* x);
|
||||
mpc_val_t* mpcf_int(mpc_val_t* x);
|
||||
mpc_val_t* mpcf_hex(mpc_val_t* x);
|
||||
mpc_val_t* mpcf_oct(mpc_val_t* x);
|
||||
mpc_val_t* mpcf_escape(mpc_val_t* x);
|
||||
|
||||
mpc_val_t* mpcf_fst(mpc_val_t* x, mpc_val_t* y);
|
||||
mpc_val_t* mpcf_snd(mpc_val_t* x, mpc_val_t* y);
|
||||
|
||||
mpc_val_t* mpcf_fst_free(mpc_val_t* x, mpc_val_t* y);
|
||||
mpc_val_t* mpcf_snd_free(mpc_val_t* x, mpc_val_t* y);
|
||||
|
||||
mpc_val_t* mpcf_freefold(mpc_val_t* t, mpc_val_t* x);
|
||||
mpc_val_t* mpcf_strfold(mpc_val_t* t, mpc_val_t* x);
|
||||
|
||||
mpc_val_t* mpcf_between_free(int n, mpc_val_t** xs);
|
||||
mpc_val_t* mpcf_maths(int n, mpc_val_t** xs);
|
||||
|
||||
|
||||
/*
|
||||
** Printing
|
||||
*/
|
||||
|
||||
void mpc_print(mpc_parser_t* p);
|
||||
|
||||
/*
|
||||
** Testing
|
||||
*/
|
||||
|
||||
bool mpc_test(mpc_parser_t* p, const char* input, void* data,
|
||||
bool(*tester)(void*, void*),
|
||||
void(*destructor)(void*),
|
||||
void(*printer)(void*));
|
||||
|
||||
#endif
|
37
tests/ident.c
Normal file
37
tests/ident.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "../mpc.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static bool string_eq(void* x, void* y) { return (strcmp(x, y) == 0); }
|
||||
static void string_print(void* x) { printf("'%s'", (char*)x); }
|
||||
|
||||
bool suite_ident(void) {
|
||||
|
||||
/* ^[a-zA-Z_][a-zA-Z0-9_]*$ */
|
||||
|
||||
mpc_parser_t* Ident = mpc_new();
|
||||
|
||||
mpc_define(Ident, mpc_ends(
|
||||
mpc_also(
|
||||
mpc_either(mpc_alpha(), mpc_underscore()),
|
||||
mpc_many1(mpc_or(3, mpc_alpha(), mpc_underscore(), mpc_digit()), mpcf_strfold),
|
||||
free, mpcf_strfold
|
||||
),
|
||||
free)
|
||||
);
|
||||
|
||||
mpc_print(Ident);
|
||||
|
||||
mpc_test(Ident, "test", "test", string_eq, free, string_print);
|
||||
mpc_test(Ident, " blah", "", string_eq, free, string_print);
|
||||
mpc_test(Ident, "anoth21er", "anoth21er", string_eq, free, string_print);
|
||||
mpc_test(Ident, "du__de", "du__de", string_eq, free, string_print);
|
||||
mpc_test(Ident, "some spaces", "", string_eq, free, string_print);
|
||||
mpc_test(Ident, "", "", string_eq, free, string_print);
|
||||
mpc_test(Ident, "18nums", "", string_eq, free, string_print);
|
||||
|
||||
mpc_delete(Ident);
|
||||
|
||||
return true;
|
||||
}
|
56
tests/math.c
Normal file
56
tests/math.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "../mpc.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static bool int_eq(void* x, void* y) {
|
||||
return (*(int*)x == *(int*)y);
|
||||
}
|
||||
|
||||
static void int_print(void* x) {
|
||||
printf("'%i'", *((int*)x));
|
||||
}
|
||||
|
||||
bool suite_math(void) {
|
||||
|
||||
mpc_parser_t* Expr = mpc_new();
|
||||
mpc_parser_t* Factor = mpc_new();
|
||||
mpc_parser_t* Term = mpc_new();
|
||||
mpc_parser_t* Maths = mpc_new();
|
||||
|
||||
mpc_define(Expr, mpc_either(
|
||||
mpc_and(3, mpcf_maths, Factor, mpc_oneof("*/"), Factor, free, free),
|
||||
Factor
|
||||
));
|
||||
|
||||
mpc_define(Factor, mpc_either(
|
||||
mpc_and(3, mpcf_maths, Term, mpc_oneof("+-"), Term, free, free),
|
||||
Term
|
||||
));
|
||||
|
||||
mpc_define(Term, mpc_either(
|
||||
mpc_int(),
|
||||
mpc_parens(Expr, free)
|
||||
));
|
||||
|
||||
mpc_define(Maths, mpc_ends(Expr, free));
|
||||
|
||||
mpc_print(Expr);
|
||||
mpc_print(Factor);
|
||||
mpc_print(Term);
|
||||
mpc_print(Maths);
|
||||
|
||||
mpc_test(Maths, "1", (int[]){ 1 }, int_eq, free, int_print);
|
||||
mpc_test(Maths, "(5)", (int[]){ 5 }, int_eq, free, int_print);
|
||||
mpc_test(Maths, "(4*2)+5", (int[]){ 13 }, int_eq, free, int_print);
|
||||
mpc_test(Maths, "a", (int[]){ 0 }, int_eq, free, int_print);
|
||||
mpc_test(Maths, "2b+4", (int[]){ 2 }, int_eq, free, int_print);
|
||||
|
||||
mpc_delete(Expr);
|
||||
mpc_delete(Factor);
|
||||
mpc_delete(Term);
|
||||
mpc_delete(Maths);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
34
tests/regex.c
Normal file
34
tests/regex.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "../mpc.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
static bool string_eq(void* x, void* y) { return (strcmp(x, y) == 0); }
|
||||
static void string_print(void* x) { printf("'%s'", (char*)x); }
|
||||
*/
|
||||
|
||||
bool suite_regex(void) {
|
||||
|
||||
mpc_parser_t* re0 = mpc_re("abc|bcd");
|
||||
mpc_parser_t* re1 = mpc_re("abc|bcd|e");
|
||||
mpc_parser_t* re2 = mpc_re("abc(ab)*");
|
||||
mpc_parser_t* re3 = mpc_re("abc(abdd)?");
|
||||
mpc_parser_t* re4 = mpc_re("ab|c(abdd)?");
|
||||
mpc_parser_t* re5 = mpc_re("abc(ab|dd)+g$");
|
||||
|
||||
mpc_print(re0);
|
||||
mpc_print(re1);
|
||||
mpc_print(re2);
|
||||
mpc_print(re3);
|
||||
mpc_print(re4);
|
||||
mpc_print(re5);
|
||||
|
||||
mpc_delete(re0);
|
||||
mpc_delete(re1);
|
||||
mpc_delete(re2);
|
||||
mpc_delete(re3);
|
||||
mpc_delete(re4);
|
||||
mpc_delete(re5);
|
||||
|
||||
return true;
|
||||
}
|
15
tests/test.c
Normal file
15
tests/test.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
bool suite_ident(void);
|
||||
bool suite_math(void);
|
||||
bool suite_regex(void);
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
suite_ident();
|
||||
suite_math();
|
||||
suite_regex();
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
Reference in New Issue
Block a user