Files
mpc/tests/grammar.c

99 lines
3.5 KiB
C
Raw Normal View History

2013-09-23 22:41:58 +01:00
#include "ptest.h"
#include "../mpc.h"
void test_grammar(void) {
2013-09-30 20:55:57 +01:00
mpc_parser_t *Expr, *Prod, *Value, *Maths;
mpc_ast_t *t0, *t1, *t2;
Expr = mpc_new("expression");
Prod = mpc_new("product");
Value = mpc_new("value");
Maths = mpc_new("maths");
2013-09-26 13:15:00 +01:00
2014-04-15 16:06:59 +01:00
mpc_define(Expr, mpca_grammar(MPCA_LANG_DEFAULT, " <product> (('+' | '-') <product>)* ", Prod));
mpc_define(Prod, mpca_grammar(MPCA_LANG_DEFAULT, " <value> (('*' | '/') <value>)* ", Value));
mpc_define(Value, mpca_grammar(MPCA_LANG_DEFAULT, " /[0-9]+/ | '(' <expression> ')' ", Expr));
2013-09-26 13:15:00 +01:00
mpc_define(Maths, mpca_total(Expr));
2013-11-11 16:56:20 +00:00
t0 = mpc_ast_new("product|value|regex", "24");
t1 = mpc_ast_build(1, "product|>",
2013-10-17 11:40:30 +01:00
mpc_ast_build(3, "value|>",
2013-09-26 13:15:00 +01:00
mpc_ast_new("char", "("),
2013-11-11 16:56:20 +00:00
mpc_ast_new("expression|product|value|regex", "5"),
2013-09-26 13:15:00 +01:00
mpc_ast_new("char", ")")));
2013-10-17 11:40:30 +01:00
t2 = mpc_ast_build(3, ">",
2013-09-26 13:15:00 +01:00
2013-10-17 11:40:30 +01:00
mpc_ast_build(3, "value|>",
2013-09-26 13:15:00 +01:00
mpc_ast_new("char", "("),
2013-10-17 11:40:30 +01:00
mpc_ast_build(3, "expression|>",
2013-09-26 13:15:00 +01:00
2013-10-17 11:40:30 +01:00
mpc_ast_build(5, "product|>",
mpc_ast_new("value|regex", "4"),
2013-09-26 13:15:00 +01:00
mpc_ast_new("char", "*"),
2013-10-17 11:40:30 +01:00
mpc_ast_new("value|regex", "2"),
2013-09-26 13:15:00 +01:00
mpc_ast_new("char", "*"),
2013-10-17 11:40:30 +01:00
mpc_ast_new("value|regex", "11")),
2013-09-26 13:15:00 +01:00
mpc_ast_new("char", "+"),
2013-11-11 16:56:20 +00:00
mpc_ast_new("product|value|regex", "2")),
2013-09-26 13:15:00 +01:00
mpc_ast_new("char", ")")),
mpc_ast_new("char", "+"),
2013-11-11 16:56:20 +00:00
mpc_ast_new("product|value|regex", "5"));
2013-09-30 20:55:57 +01:00
PT_ASSERT(mpc_test_pass(Maths, " 24 ", t0, (int(*)(const void*,const void*))mpc_ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(const void*))mpc_ast_print));
PT_ASSERT(mpc_test_pass(Maths, "(5)", t1, (int(*)(const void*,const void*))mpc_ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(const void*))mpc_ast_print));
PT_ASSERT(mpc_test_pass(Maths, "(4 * 2 * 11 + 2) + 5", t2, (int(*)(const void*,const void*))mpc_ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(const void*))mpc_ast_print));
PT_ASSERT(mpc_test_fail(Maths, "a", t0, (int(*)(const void*,const void*))mpc_ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(const void*))mpc_ast_print));
PT_ASSERT(mpc_test_fail(Maths, "2b+4", t0, (int(*)(const void*,const void*))mpc_ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(const void*))mpc_ast_print));
2013-09-27 00:35:34 +01:00
2013-09-26 13:15:00 +01:00
mpc_ast_delete(t0);
mpc_ast_delete(t1);
mpc_ast_delete(t2);
2013-09-26 13:15:00 +01:00
mpc_cleanup(4, Expr, Prod, Value, Maths);
2013-09-27 00:35:34 +01:00
2013-09-23 22:41:58 +01:00
}
2013-09-26 17:58:14 +01:00
void test_language(void) {
2013-09-30 20:55:57 +01:00
mpc_parser_t *Expr, *Prod, *Value, *Maths;
Expr = mpc_new("expression");
Prod = mpc_new("product");
Value = mpc_new("value");
Maths = mpc_new("maths");
2013-09-26 17:58:14 +01:00
2014-04-15 16:06:59 +01:00
mpca_lang(MPCA_LANG_DEFAULT,
" expression : <product> (('+' | '-') <product>)*; "
" product : <value> (('*' | '/') <value>)*; "
" value : /[0-9]+/ | '(' <expression> ')'; "
" maths : /^/ <expression> /$/; ",
2013-09-26 17:58:14 +01:00
Expr, Prod, Value, Maths);
mpc_cleanup(4, Expr, Prod, Value, Maths);
}
void test_language_file(void) {
2013-09-30 20:55:57 +01:00
mpc_parser_t *Expr, *Prod, *Value, *Maths;
Expr = mpc_new("expression");
Prod = mpc_new("product");
Value = mpc_new("value");
Maths = mpc_new("maths");
2013-09-26 17:58:14 +01:00
mpca_lang_contents(MPCA_LANG_DEFAULT, "./tests/maths.grammar", Expr, Prod, Value, Maths);
2013-09-26 17:58:14 +01:00
mpc_cleanup(4, Expr, Prod, Value, Maths);
}
2013-09-23 22:41:58 +01:00
void suite_grammar(void) {
pt_add_test(test_grammar, "Test Grammar", "Suite Grammar");
2013-09-26 17:58:14 +01:00
pt_add_test(test_language, "Test Language", "Suite Grammar");
pt_add_test(test_language_file, "Test Language File", "Suite Grammar");
2013-11-10 16:56:59 +00:00
}