Renamed language option constants

This commit is contained in:
Daniel Holden
2014-04-15 16:06:59 +01:00
parent 0164931350
commit 8d0ee197e1
8 changed files with 18 additions and 18 deletions

View File

@@ -703,7 +703,7 @@ String literals are surrounded in double quotes `"`. Character literals in singl
Parts specified one after another are parsed in order (like `mpc_and`), while parts separated by a pipe `|` are alternatives (like `mpc_or`). Parenthesis `()` are used to specify precedence. `*` can be used to mean zero or more of. `+` for one or more of. `?` for zero or one of. `!` for negation. And a number inside braces `{5}` to means so many counts of.
Rules are specified by rule name, optionally followed by an _expected_ string, followed by a colon `:`, followed by the definition, and ending in a semicolon `;`. The flags variable is a set of flags `MPC_LANG_DEFAULT`, `MPC_LANG_PREDICTIVE`, or `MPC_LANG_WHITESPACE_SENSITIVE`. For specifying if the language is predictive or whitespace sensitive.
Rules are specified by rule name, optionally followed by an _expected_ string, followed by a colon `:`, followed by the definition, and ending in a semicolon `;`. The flags variable is a set of flags `MPCA_LANG_DEFAULT`, `MPCA_LANG_PREDICTIVE`, or `MPCA_LANG_WHITESPACE_SENSITIVE`. For specifying if the language is predictive or whitespace sensitive.
Like with the regular expressions, this user input is parsed by existing parts of the _mpc_ library. It provides one of the more powerful features of the library.

View File

@@ -7,7 +7,7 @@ int main(int argc, char **argv) {
mpc_parser_t* Phrase = mpc_new("phrase");
mpc_parser_t* Doge = mpc_new("doge");
mpca_lang(MPC_LANG_DEFAULT,
mpca_lang(MPCA_LANG_DEFAULT,
" \
adjective : \"wow\" | \"many\" | \"so\" | \"such\"; \
noun : \"lisp\" | \"language\" | \"c\" | \"book\" | \"build\"; \

View File

@@ -11,7 +11,7 @@ int main(int argc, char **argv) {
mpc_parser_t* Expr = mpc_new("expr");
mpc_parser_t* Lispy = mpc_new("lispy");
mpca_lang(MPC_LANG_PREDICTIVE,
mpca_lang(MPCA_LANG_PREDICTIVE,
" \
number \"number\" : /[0-9]+/ ; \
symbol \"symbol\" : /[a-zA-Z0-9_+\\-*\\/\\\\=<>!&]+/ ; \

View File

@@ -7,7 +7,7 @@ int main(int argc, char **argv) {
mpc_parser_t *Value = mpc_new("value");
mpc_parser_t *Maths = mpc_new("maths");
mpca_lang(MPC_LANG_PREDICTIVE,
mpca_lang(MPCA_LANG_PREDICTIVE,
" \
expression : <product> (('+' | '-') <product>)*; \
product : <value> (('*' | '/') <value>)*; \

View File

@@ -20,7 +20,7 @@ int main(int argc, char **argv) {
mpc_parser_t* Includes = mpc_new("includes");
mpc_parser_t* Smallc = mpc_new("smallc");
mpc_err_t* err = mpca_lang(MPC_LANG_DEFAULT,
mpc_err_t* err = mpca_lang(MPCA_LANG_DEFAULT,
" \n\
ident : /[a-zA-Z_][a-zA-Z0-9_]*/ ; \n\
number : /[0-9]+/ ; \n\

10
mpc.c
View File

@@ -2781,7 +2781,7 @@ static mpc_val_t *mpcaf_grammar_repeat(int n, mpc_val_t **xs) {
static mpc_val_t *mpcaf_grammar_string(mpc_val_t *x, void *s) {
mpca_grammar_st_t *st = s;
char *y = mpcf_unescape(x);
mpc_parser_t *p = (st->flags & MPC_LANG_WHITESPACE_SENSITIVE) ? mpc_string(y) : mpc_tok(mpc_string(y));
mpc_parser_t *p = (st->flags & MPCA_LANG_WHITESPACE_SENSITIVE) ? mpc_string(y) : mpc_tok(mpc_string(y));
free(y);
return mpca_state(mpca_tag(mpc_apply(p, mpcf_str_ast), "string"));
}
@@ -2789,7 +2789,7 @@ static mpc_val_t *mpcaf_grammar_string(mpc_val_t *x, void *s) {
static mpc_val_t *mpcaf_grammar_char(mpc_val_t *x, void *s) {
mpca_grammar_st_t *st = s;
char *y = mpcf_unescape(x);
mpc_parser_t *p = (st->flags & MPC_LANG_WHITESPACE_SENSITIVE) ? mpc_char(y[0]) : mpc_tok(mpc_char(y[0]));
mpc_parser_t *p = (st->flags & MPCA_LANG_WHITESPACE_SENSITIVE) ? mpc_char(y[0]) : mpc_tok(mpc_char(y[0]));
free(y);
return mpca_state(mpca_tag(mpc_apply(p, mpcf_str_ast), "char"));
}
@@ -2797,7 +2797,7 @@ static mpc_val_t *mpcaf_grammar_char(mpc_val_t *x, void *s) {
static mpc_val_t *mpcaf_grammar_regex(mpc_val_t *x, void *s) {
mpca_grammar_st_t *st = s;
char *y = mpcf_unescape_regex(x);
mpc_parser_t *p = (st->flags & MPC_LANG_WHITESPACE_SENSITIVE) ? mpc_re(y) : mpc_tok(mpc_re(y));
mpc_parser_t *p = (st->flags & MPCA_LANG_WHITESPACE_SENSITIVE) ? mpc_re(y) : mpc_tok(mpc_re(y));
free(y);
return mpca_state(mpca_tag(mpc_apply(p, mpcf_str_ast), "regex"));
}
@@ -2927,7 +2927,7 @@ mpc_parser_t *mpca_grammar_st(const char *grammar, mpca_grammar_st_t *st) {
mpc_cleanup(5, GrammarTotal, Grammar, Term, Factor, Base);
return (st->flags & MPC_LANG_PREDICTIVE) ? mpc_predictive(r.output) : r.output;
return (st->flags & MPCA_LANG_PREDICTIVE) ? mpc_predictive(r.output) : r.output;
}
@@ -3006,7 +3006,7 @@ static mpc_val_t *mpca_stmt_list_apply_to(mpc_val_t *x, void *s) {
while(*stmts) {
stmt = *stmts;
left = mpca_grammar_find_parser(stmt->ident, st);
if (st->flags & MPC_LANG_PREDICTIVE) { stmt->grammar = mpc_predictive(stmt->grammar); }
if (st->flags & MPCA_LANG_PREDICTIVE) { stmt->grammar = mpc_predictive(stmt->grammar); }
if (stmt->name) { stmt->grammar = mpc_expect(stmt->grammar, stmt->name); }
mpc_define(left, stmt->grammar);
free(stmt->ident);

6
mpc.h
View File

@@ -285,9 +285,9 @@ mpc_parser_t *mpca_or(int n, ...);
mpc_parser_t *mpca_and(int n, ...);
enum {
MPC_LANG_DEFAULT = 0,
MPC_LANG_PREDICTIVE = 1,
MPC_LANG_WHITESPACE_SENSITIVE = 2
MPCA_LANG_DEFAULT = 0,
MPCA_LANG_PREDICTIVE = 1,
MPCA_LANG_WHITESPACE_SENSITIVE = 2
};
mpc_parser_t *mpca_grammar(int flags, const char *grammar, ...);

View File

@@ -11,9 +11,9 @@ void test_grammar(void) {
Value = mpc_new("value");
Maths = mpc_new("maths");
mpc_define(Expr, mpca_grammar(MPC_LANG_DEFAULT, " <product> (('+' | '-') <product>)* ", Prod));
mpc_define(Prod, mpca_grammar(MPC_LANG_DEFAULT, " <value> (('*' | '/') <value>)* ", Value));
mpc_define(Value, mpca_grammar(MPC_LANG_DEFAULT, " /[0-9]+/ | '(' <expression> ')' ", Expr));
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));
mpc_define(Maths, mpca_total(Expr));
t0 = mpc_ast_new("product|value|regex", "24");
@@ -66,7 +66,7 @@ void test_language(void) {
Value = mpc_new("value");
Maths = mpc_new("maths");
mpca_lang(MPC_LANG_DEFAULT,
mpca_lang(MPCA_LANG_DEFAULT,
" \
expression : <product> (('+' | '-') <product>)*; \
product : <value> (('*' | '/') <value>)*; \
@@ -87,7 +87,7 @@ void test_language_file(void) {
Value = mpc_new("value");
Maths = mpc_new("maths");
mpca_lang_contents(MPC_LANG_DEFAULT,"./tests/maths.grammar", Expr, Prod, Value, Maths);
mpca_lang_contents(MPCA_LANG_DEFAULT,"./tests/maths.grammar", Expr, Prod, Value, Maths);
mpc_cleanup(4, Expr, Prod, Value, Maths);