| 
									
										
										
										
											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
										 |  |  |        | 
					
						
							| 
									
										
										
										
											2016-06-11 14:11:19 +01:00
										 |  |  |       mpc_ast_build(3, "product|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
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2014-10-16 15:38:27 +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); | 
					
						
							| 
									
										
										
										
											2014-04-15 16:04:07 +01:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2014-04-16 17:06:16 +01:00
										 |  |  |     " 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
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2014-04-16 19:19:25 +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); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-09 12:06:13 +00:00
										 |  |  | void test_doge(void) { | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_ast_t *t0;   | 
					
						
							|  |  |  |   mpc_parser_t* Adjective = mpc_new("adjective"); | 
					
						
							|  |  |  |   mpc_parser_t* Noun      = mpc_new("noun"); | 
					
						
							|  |  |  |   mpc_parser_t* Phrase    = mpc_new("phrase"); | 
					
						
							|  |  |  |   mpc_parser_t* Doge      = mpc_new("doge"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mpca_lang(MPCA_LANG_DEFAULT, | 
					
						
							|  |  |  |     " adjective : \"wow\" | \"many\" | \"so\" | \"such\";                 " | 
					
						
							|  |  |  |     " noun      : \"lisp\" | \"language\" | \"c\" | \"book\" | \"build\"; " | 
					
						
							|  |  |  |     " phrase    : <adjective> <noun>;                                     " | 
					
						
							|  |  |  |     " doge      : /^/ <phrase>* /$/;                                      ", | 
					
						
							|  |  |  |     Adjective, Noun, Phrase, Doge, NULL); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   t0 =  | 
					
						
							|  |  |  |       mpc_ast_build(4, ">",  | 
					
						
							|  |  |  |           mpc_ast_new("regex", ""), | 
					
						
							|  |  |  |           mpc_ast_build(2, "phrase|>",  | 
					
						
							|  |  |  |             mpc_ast_new("adjective|string", "so"), | 
					
						
							|  |  |  |             mpc_ast_new("noun|string", "c")), | 
					
						
							|  |  |  |           mpc_ast_build(2, "phrase|>",  | 
					
						
							|  |  |  |             mpc_ast_new("adjective|string", "so"), | 
					
						
							|  |  |  |             mpc_ast_new("noun|string", "c")), | 
					
						
							|  |  |  |           mpc_ast_new("regex", "") | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |   PT_ASSERT(mpc_test_pass(Doge, "so c so c", 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(Doge, "so a so c", t0, (int(*)(const void*,const void*))mpc_ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(const void*))mpc_ast_print)); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_ast_delete(t0); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_cleanup(4, Adjective, Noun, Phrase, Doge); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 12:59:55 +01:00
										 |  |  | void test_partial(void) { | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_ast_t *t0; | 
					
						
							|  |  |  |   mpc_err_t *err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mpc_parser_t *Line = mpc_new("line"); | 
					
						
							|  |  |  |   mpc_parser_t *Number = mpc_new("number"); | 
					
						
							|  |  |  |   mpc_parser_t *QuotedString = mpc_new("quoted_string"); | 
					
						
							|  |  |  |   mpc_parser_t *LinePragma = mpc_new("linepragma"); | 
					
						
							|  |  |  |   mpc_parser_t *Parser = mpc_new("parser"); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_define(Line, mpca_tag(mpc_apply(mpc_sym("#line"), mpcf_str_ast), "string")); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   err = mpca_lang(MPCA_LANG_PREDICTIVE, | 
					
						
							|  |  |  |     "number        : /[0-9]+/ ;\n" | 
					
						
							|  |  |  |     "quoted_string : /\"(\\.|[^\"])*\"/ ;\n" | 
					
						
							|  |  |  |     "linepragma    : <line> <number> <quoted_string>;\n" | 
					
						
							|  |  |  |     "parser        : /^/ (<linepragma>)* /$/ ;\n", | 
					
						
							|  |  |  |     Line, Number, QuotedString, LinePragma, Parser, NULL); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   PT_ASSERT(err == NULL); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   t0 = mpc_ast_build(3, ">",  | 
					
						
							|  |  |  |           mpc_ast_new("regex", ""), | 
					
						
							|  |  |  |           mpc_ast_build(3, "linepragma|>",  | 
					
						
							|  |  |  |             mpc_ast_new("line|string", "#line"), | 
					
						
							|  |  |  |             mpc_ast_new("number|regex", "10"), | 
					
						
							|  |  |  |             mpc_ast_new("quoted_string|regex", "\"test\"")), | 
					
						
							|  |  |  |           mpc_ast_new("regex", "")); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   PT_ASSERT(mpc_test_pass(Parser, "#line 10 \"test\"", t0,  | 
					
						
							|  |  |  |     (int(*)(const void*,const void*))mpc_ast_eq,  | 
					
						
							|  |  |  |     (mpc_dtor_t)mpc_ast_delete,  | 
					
						
							|  |  |  |     (void(*)(const void*))mpc_ast_print)); | 
					
						
							| 
									
										
										
										
											2018-10-14 17:20:11 -04:00
										 |  |  |      | 
					
						
							|  |  |  |   mpc_ast_delete(t0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 12:59:55 +01:00
										 |  |  |   mpc_cleanup(5, Line, Number, QuotedString, LinePragma, Parser); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 14:11:19 +01:00
										 |  |  | void test_qscript(void) { | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_ast_t *t0; | 
					
						
							|  |  |  |   mpc_parser_t *Qscript = mpc_new("qscript"); | 
					
						
							|  |  |  |   mpc_parser_t *Comment = mpc_new("comment"); | 
					
						
							|  |  |  |   mpc_parser_t *Resource = mpc_new("resource"); | 
					
						
							|  |  |  |   mpc_parser_t *Rtype = mpc_new("rtype"); | 
					
						
							|  |  |  |   mpc_parser_t *Rname = mpc_new("rname"); | 
					
						
							|  |  |  |   mpc_parser_t *InnerBlock = mpc_new("inner_block"); | 
					
						
							|  |  |  |   mpc_parser_t *Statement = mpc_new("statement"); | 
					
						
							|  |  |  |   mpc_parser_t *Function = mpc_new("function"); | 
					
						
							|  |  |  |   mpc_parser_t *Parameter = mpc_new("parameter"); | 
					
						
							|  |  |  |   mpc_parser_t *Literal = mpc_new("literal"); | 
					
						
							|  |  |  |   mpc_parser_t *Block = mpc_new("block"); | 
					
						
							|  |  |  |   mpc_parser_t *Seperator = mpc_new("seperator"); | 
					
						
							|  |  |  |   mpc_parser_t *Qstring = mpc_new("qstring"); | 
					
						
							|  |  |  |   mpc_parser_t *SimpleStr = mpc_new("simplestr"); | 
					
						
							|  |  |  |   mpc_parser_t *ComplexStr = mpc_new("complexstr"); | 
					
						
							|  |  |  |   mpc_parser_t *Number = mpc_new("number"); | 
					
						
							|  |  |  |   mpc_parser_t *Float = mpc_new("float"); | 
					
						
							|  |  |  |   mpc_parser_t *Int = mpc_new("int"); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_err_t *err = mpca_lang(0, | 
					
						
							|  |  |  |     "  qscript        : /^/ (<comment> | <resource>)* /$/ ;\n" | 
					
						
							|  |  |  |     "   comment     : '#' /[^\\n]*/ ;\n" | 
					
						
							|  |  |  |     "resource       : '[' (<rtype> <rname>) ']' <inner_block> ;\n" | 
					
						
							|  |  |  |     "   rtype       : /[*]*/ ;\n" | 
					
						
							|  |  |  |     "   rname       : <qstring> ;\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "inner_block    : (<comment> | <statement>)* ;\n" | 
					
						
							|  |  |  |     "   statement   : <function> '(' (<comment> | <parameter> | <block>)* ')'  <seperator> ;\n" | 
					
						
							|  |  |  |     "   function    : <qstring> ;\n" | 
					
						
							|  |  |  |     "   parameter   : (<statement> | <literal>) ;\n" | 
					
						
							|  |  |  |     "      literal  : (<number> | <qstring>) <seperator> ;\n" | 
					
						
							|  |  |  |     "   block       : '{' <inner_block> '}' ;\n" | 
					
						
							|  |  |  |     "   seperator   : ',' | \"\" ;\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "qstring        : (<complexstr> | <simplestr>) <qstring>* ;\n" | 
					
						
							|  |  |  |     "   simplestr   : /[a-zA-Z0-9_!@#$%^&\\*_+\\-\\.=\\/<>]+/ ;\n" | 
					
						
							|  |  |  |     "   complexstr  : (/\"[^\"]*\"/ | /'[^']*'/) ;\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "number         : (<float> | <int>) ;\n" | 
					
						
							|  |  |  |     "   float       : /[-+]?[0-9]+\\.[0-9]+/ ;\n" | 
					
						
							|  |  |  |     "   int         : /[-+]?[0-9]+/ ;\n", | 
					
						
							|  |  |  |   Qscript, Comment, Resource, Rtype, Rname, InnerBlock, Statement, Function, | 
					
						
							|  |  |  |   Parameter, Literal, Block, Seperator, Qstring, SimpleStr, ComplexStr, Number, | 
					
						
							|  |  |  |   Float, Int, NULL); | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |   PT_ASSERT(err == NULL); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   t0 = mpc_ast_build(3, ">", | 
					
						
							|  |  |  |           mpc_ast_new("regex", ""), | 
					
						
							|  |  |  |           mpc_ast_build(5, "resource|>", | 
					
						
							|  |  |  |             mpc_ast_new("char", "["), | 
					
						
							|  |  |  |             mpc_ast_new("rtype|regex", ""), | 
					
						
							|  |  |  |             mpc_ast_new("rname|qstring|simplestr|regex", "my_func"), | 
					
						
							|  |  |  |             mpc_ast_new("char", "]"), | 
					
						
							|  |  |  |             mpc_ast_build(5, "inner_block|statement|>", | 
					
						
							|  |  |  |               mpc_ast_new("function|qstring|simplestr|regex", "echo"), | 
					
						
							|  |  |  |               mpc_ast_new("char", "("), | 
					
						
							|  |  |  |               mpc_ast_build(2, "parameter|literal|>", | 
					
						
							|  |  |  |                 mpc_ast_build(2, "qstring|>", | 
					
						
							|  |  |  |                   mpc_ast_new("simplestr|regex", "a"), | 
					
						
							|  |  |  |                   mpc_ast_build(2, "qstring|>", | 
					
						
							|  |  |  |                     mpc_ast_new("simplestr|regex", "b"), | 
					
						
							|  |  |  |                     mpc_ast_new("qstring|simplestr|regex", "c") | 
					
						
							|  |  |  |                   ) | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |                 mpc_ast_new("seperator|string", "") | 
					
						
							|  |  |  |               ), | 
					
						
							|  |  |  |               mpc_ast_new("char", ")"), | 
					
						
							|  |  |  |               mpc_ast_new("seperator|string", "") | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |           ), | 
					
						
							|  |  |  |           mpc_ast_new("regex", "")); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   PT_ASSERT(mpc_test_pass(Qscript, "[my_func]\n  echo (a b c)\n", t0, | 
					
						
							|  |  |  |     (int(*)(const void*,const void*))mpc_ast_eq, | 
					
						
							|  |  |  |     (mpc_dtor_t)mpc_ast_delete, | 
					
						
							|  |  |  |     (void(*)(const void*))mpc_ast_print)); | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2018-10-14 17:20:11 -04:00
										 |  |  |   mpc_ast_delete(t0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 14:11:19 +01:00
										 |  |  |   mpc_cleanup(18, Qscript, Comment, Resource, Rtype, Rname, InnerBlock, | 
					
						
							|  |  |  |   Statement, Function, Parameter, Literal, Block, Seperator, Qstring, | 
					
						
							|  |  |  |   SimpleStr, ComplexStr, Number, Float, Int); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 20:30:14 -08:00
										 |  |  | void test_missingrule(void) { | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-11-08 09:59:50 +00:00
										 |  |  |   int result; | 
					
						
							| 
									
										
										
										
											2016-11-07 20:30:14 -08:00
										 |  |  |   mpc_err_t *err; | 
					
						
							| 
									
										
										
										
											2016-11-08 09:59:50 +00:00
										 |  |  |   mpc_result_t r; | 
					
						
							| 
									
										
										
										
											2016-11-07 20:30:14 -08:00
										 |  |  |   mpc_parser_t *Parser = mpc_new("parser"); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   err = mpca_lang(MPCA_LANG_DEFAULT, | 
					
						
							|  |  |  |     "parser        : /^/ (<missing>)* /$/ ;\n", | 
					
						
							|  |  |  |     Parser, NULL); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   PT_ASSERT(err == NULL); | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-11-08 09:59:50 +00:00
										 |  |  |   result = mpc_parse("<stdin>", "test", Parser, &r); | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-11-07 20:30:14 -08:00
										 |  |  |   PT_ASSERT(result == 0); | 
					
						
							|  |  |  |   PT_ASSERT(r.error != NULL); | 
					
						
							|  |  |  |   PT_ASSERT(strcmp(r.error->failure, "Unknown Parser 'missing'!") == 0); | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-11-08 09:59:50 +00:00
										 |  |  |   mpc_err_delete(r.error); | 
					
						
							| 
									
										
										
										
											2016-11-07 20:30:14 -08:00
										 |  |  |   mpc_cleanup(1, Parser); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-14 17:20:11 -04:00
										 |  |  | void test_regex_mode(void) { | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_parser_t *Line0, *Line1, *Line2, *Line3; | 
					
						
							|  |  |  |   mpc_ast_t *t0, *t1, *t2, *t3, *t4; | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Line0 = mpc_new("line0"); | 
					
						
							|  |  |  |   Line1 = mpc_new("line1"); | 
					
						
							|  |  |  |   Line2 = mpc_new("line2"); | 
					
						
							|  |  |  |   Line3 = mpc_new("line3"); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpca_lang(MPCA_LANG_DEFAULT, " line0 : /.*/; ", Line0); | 
					
						
							|  |  |  |   mpca_lang(MPCA_LANG_DEFAULT, " line1 : /.*/s; ", Line1); | 
					
						
							|  |  |  |   mpca_lang(MPCA_LANG_DEFAULT, " line2 : /(^[a-z]*$)*/; ", Line2); | 
					
						
							|  |  |  |   mpca_lang(MPCA_LANG_DEFAULT, " line3 : /(^[a-z]*$)*/m; ", Line3); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   t0 = mpc_ast_new("regex", "blah"); | 
					
						
							|  |  |  |   t1 = mpc_ast_new("regex", "blah\nblah"); | 
					
						
							|  |  |  |   t2 = mpc_ast_new("regex", ""); | 
					
						
							|  |  |  |   t3 = mpc_ast_new("regex", "blah"); | 
					
						
							|  |  |  |   t4 = mpc_ast_new("regex", "blah\nblah"); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   PT_ASSERT(mpc_test_pass(Line0, "blah\nblah", 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(Line1, "blah\nblah", 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(Line2, "blah\nblah", 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_pass(Line2, "blah", t3, | 
					
						
							|  |  |  |     (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(Line3, "blah\nblah", t4, | 
					
						
							|  |  |  |     (int(*)(const void*,const void*))mpc_ast_eq, | 
					
						
							|  |  |  |     (mpc_dtor_t)mpc_ast_delete, | 
					
						
							|  |  |  |     (void(*)(const void*))mpc_ast_print)); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_ast_delete(t0); | 
					
						
							|  |  |  |   mpc_ast_delete(t1); | 
					
						
							|  |  |  |   mpc_ast_delete(t2); | 
					
						
							|  |  |  |   mpc_ast_delete(t3); | 
					
						
							|  |  |  |   mpc_ast_delete(t4); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_cleanup(4, Line0, Line1, Line2, Line3); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-15 16:11:39 -05:00
										 |  |  | void test_digits_file(void) { | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   FILE *f; | 
					
						
							|  |  |  |   mpc_result_t r; | 
					
						
							|  |  |  |   mpc_parser_t *Digit = mpc_new("digit"); | 
					
						
							|  |  |  |   mpc_parser_t *Program = mpc_new("program"); | 
					
						
							|  |  |  |   mpc_ast_t* t0; | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_err_t* err = mpca_lang(MPCA_LANG_DEFAULT, | 
					
						
							|  |  |  |     " digit   : /[0-9]/ ;" | 
					
						
							|  |  |  |     " program : /^/ <digit>+ /$/ ;" | 
					
						
							|  |  |  |     , Digit, Program, NULL); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   PT_ASSERT(err == NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   t0 = mpc_ast_build(5, ">",  | 
					
						
							|  |  |  |     mpc_ast_new("regex", ""), | 
					
						
							|  |  |  |     mpc_ast_new("digit|regex", "1"), | 
					
						
							|  |  |  |     mpc_ast_new("digit|regex", "2"), | 
					
						
							|  |  |  |     mpc_ast_new("digit|regex", "3"), | 
					
						
							|  |  |  |     mpc_ast_new("regex", "")); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   if (mpc_parse_contents("tests/digits.txt", Program, &r)) { | 
					
						
							|  |  |  |     PT_ASSERT(1); | 
					
						
							|  |  |  |     PT_ASSERT(mpc_ast_eq(t0, r.output)); | 
					
						
							|  |  |  |     mpc_ast_delete(r.output); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     PT_ASSERT(0); | 
					
						
							|  |  |  |     mpc_err_print(r.error); | 
					
						
							|  |  |  |     mpc_err_delete(r.error); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   f = fopen("tests/digits.txt", "r"); | 
					
						
							|  |  |  |   PT_ASSERT(f != NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (mpc_parse_file("tests/digits.txt", f, Program, &r)) { | 
					
						
							|  |  |  |     PT_ASSERT(1); | 
					
						
							|  |  |  |     PT_ASSERT(mpc_ast_eq(t0, r.output)); | 
					
						
							|  |  |  |     mpc_ast_delete(r.output); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     PT_ASSERT(0); | 
					
						
							|  |  |  |     mpc_err_print(r.error); | 
					
						
							|  |  |  |     mpc_err_delete(r.error); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |   fclose(f); | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |   if (mpc_parse("tests/digits.txt", "123", Program, &r)) { | 
					
						
							|  |  |  |     PT_ASSERT(1); | 
					
						
							|  |  |  |     PT_ASSERT(mpc_ast_eq(t0, r.output)); | 
					
						
							|  |  |  |     mpc_ast_delete(r.output); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     PT_ASSERT(0); | 
					
						
							|  |  |  |     mpc_err_print(r.error); | 
					
						
							|  |  |  |     mpc_err_delete(r.error); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_ast_delete(t0); | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |   mpc_cleanup(2, Digit, Program); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2015-11-09 12:06:13 +00:00
										 |  |  |   pt_add_test(test_doge, "Test Doge", "Suite Grammar"); | 
					
						
							| 
									
										
										
										
											2016-06-11 12:59:55 +01:00
										 |  |  |   pt_add_test(test_partial, "Test Partial", "Suite Grammar"); | 
					
						
							| 
									
										
										
										
											2016-06-11 14:11:19 +01:00
										 |  |  |   pt_add_test(test_qscript, "Test QScript", "Suite Grammar"); | 
					
						
							| 
									
										
										
										
											2016-11-07 20:30:14 -08:00
										 |  |  |   pt_add_test(test_missingrule, "Test Missing Rule", "Suite Grammar"); | 
					
						
							| 
									
										
										
										
											2018-10-14 17:20:11 -04:00
										 |  |  |   pt_add_test(test_regex_mode, "Test Regex Mode", "Suite Grammar"); | 
					
						
							| 
									
										
										
										
											2018-12-15 16:11:39 -05:00
										 |  |  |   pt_add_test(test_digits_file, "Test Digits File", "Suite Grammar"); | 
					
						
							| 
									
										
										
										
											2013-11-10 16:56:59 +00:00
										 |  |  | } |