From 3fc11964254e0add22c0394556c26cc6ee0697c1 Mon Sep 17 00:00:00 2001 From: "benoit.thomas" Date: Fri, 16 Jul 2021 18:42:12 +0900 Subject: [PATCH] Add the "& rule" --- parse.leg | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/parse.leg b/parse.leg index d836cc4..2ba6838 100644 --- a/parse.leg +++ b/parse.leg @@ -20,7 +20,7 @@ void fatal(char *fmt, ...) exit(1); } -enum op { String, Query, Star, Plus, Or, And, Class, Dot, Exc, Id } ; +enum op { String, Query, Star, Plus, Or, And, Class, Dot, Exc, Et, Id} ; typedef union Node Node; @@ -41,6 +41,7 @@ struct And { enum op type; Node *children[2]; }; struct Class { enum op type; char *stringValue; int len; }; struct Dot { enum op type; }; struct Exc { enum op type; Node *children[1]; }; +struct Et { enum op type; Node *children[1]; }; struct Id { enum op type; Symbol *symbol; }; union Node { @@ -54,6 +55,7 @@ union Node { struct Class Class; struct Dot Dot; struct Exc Exc; + struct Et Et; struct Id Id; }; @@ -163,6 +165,13 @@ Node *mkExc(Node *n) return node; } +Node *mkEt(Node *n) +{ + Node *node= new(Et); + node->Et.children[0]= n; + return node; +} + Node *_checktype(Node *object, enum op type) { if (object->type == type) return object; @@ -211,6 +220,10 @@ void print(Node *node) printf("!"); print(node->Exc.children[0]); return; + case Et: + printf("&"); + print(node->Et.children[0]); + return; case Id: printf("%s\n",get(node,Id,symbol)->name); return; @@ -288,6 +301,7 @@ and = p:prefix a:and { $$ = mkAnd(p, a) } | p:prefix { $$ = p } prefix = "!" - p : postfix { $$ = mkExc(p) } + | "&" - p : postfix { $$ = mkEt(p) } | p: postfix { $$ = p} postfix = s:atom ( "?" - { s = mkQuery(s) } @@ -374,6 +388,14 @@ int execute(Node *node, InputBuffer *in) setPosition(in, pos); return 0; } + case Et: { + int pos= getPosition(in); + if (!execute(get(node,Et,children[0]), in)) { + return 0; + } + setPosition(in, pos); + return 1; + } case Dot: { if (atEnd(in)) { return 0;