浏览代码

Add the "& rule"

main
benoit.thomas 3 年前
父节点
当前提交
3fc1196425
共有 1 个文件被更改,包括 23 次插入1 次删除
  1. +23
    -1
      parse.leg

+ 23
- 1
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;

正在加载...
取消
保存