orangeduck / mpc

A Parser Combinator library for C

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

regex

mgood7123 opened this issue · comments

is [a-z] or [A-Z] meant to prevent characters such as '\n' from being capturable?

#include "../mpc.h"

void eval(char * string, mpc_parser_t *p, mpc_result_t * r) {
	if (mpc_parse("test", string, p, r)) {
		mpc_ast_print((*r).output);
			mpc_ast_delete((*r).output);
		} else {
			mpc_err_print((*r).error);
			mpc_err_delete((*r).error);
		}
}

#define mpcnew(x)	 mpc_parser_t * x = mpc_new(#x)

int main(int argc, char **argv) {
	mpc_result_t r;
	mpcnew(Line);
	mpcnew(ValidLine);
	mpcnew(Eol);
	mpcnew(Eof);
	mpcnew(NewLine);
	mpcnew(Any);
	mpca_lang(MPCA_LANG_DEFAULT, 
						"Line      : <ValidLine>;"
						"ValidLine : <Any>* <Eol>  ;"
						"Eol       : (<NewLine> | <Eof>);"
						"Eof       : /$/;"
						"NewLine   : '\n';"
						"Any       : /[a-z]/;", Line, ValidLine, Eol, Eof, NewLine, Any, NULL);
	eval("hi\nk", Line, &r);
	mpc_cleanup(6,Line, ValidLine, Eol, Eof, NewLine, Any, NULL);

	return 0;
	
}


> 
  ValidLine|> 
    Any|regex:1:1 'h'
    Any|regex:1:2 'i'
    Any|regex:2:1 'k'
    Eol|Eof|regex

id expect it to look like this

> 
  ValidLine|> 
    Any|regex:1:1 'h'
    Any|regex:1:2 'i'
    Eol|NewLine|char '
'
  ValidLine|> 
    Any|regex:2:1 'k'
    Eol|Eof|regex

Your problem here is that your regex is not defined to match multiple characters: /[a-z]/ will match one character in the range a to z. So each time the parser matches Any it will just parse one character and then strip the trailing whitespace following this (all rules by default strip whitespace after parsing unless the MPCA_LANG_WHITESPACE_SENSITIVE flag is used). So when it parses the i character it strips the trailing white-space and goes straight on to parse the k.

I suggest you read through the README again because really github issues are for bugs (not for me to help you debug whatever application you are building) and this library is fairly well tested in practice so it is unlikely there are bugs unless it is really a kind of obscure edge cases so next time you can make sure you are really certain it is a bug and not the expected behavior of the library before opening an issue.