expr: add constructors for more expression types

This makes the parser a bit more declarative. But really it might make
error handling easier.

Signed-off-by: Ran Benita <ran234@gmail.com>
master
Ran Benita 2013-11-30 23:12:45 +02:00
parent dbd8b1ef8d
commit c5d859385f
3 changed files with 100 additions and 52 deletions

View File

@ -70,7 +70,7 @@ AppendStmt(ParseCommon *to, ParseCommon *append)
return to; return to;
} }
ExprDef * static ExprDef *
ExprCreate(enum expr_op_type op, enum expr_value_type type) ExprCreate(enum expr_op_type op, enum expr_value_type type)
{ {
ExprDef *expr = malloc(sizeof(*expr)); ExprDef *expr = malloc(sizeof(*expr));
@ -85,6 +85,54 @@ ExprCreate(enum expr_op_type op, enum expr_value_type type)
return expr; return expr;
} }
ExprDef *
ExprCreateString(xkb_atom_t str)
{
ExprDef *expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_STRING);
if (!expr)
return NULL;
expr->value.str = str;
return expr;
}
ExprDef *
ExprCreateInteger(int ival)
{
ExprDef *expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_INT);
if (!expr)
return NULL;
expr->value.ival = ival;
return expr;
}
ExprDef *
ExprCreateKeyName(xkb_atom_t key_name)
{
ExprDef *expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_KEYNAME);
if (!expr)
return NULL;
expr->value.keyName = key_name;
return expr;
}
ExprDef *
ExprCreateIdent(xkb_atom_t ident)
{
ExprDef *expr = ExprCreate(EXPR_IDENT, EXPR_TYPE_UNKNOWN);
if (!expr)
return NULL;
expr->value.ident = ident;
return expr;
}
ExprDef * ExprDef *
ExprCreateUnary(enum expr_op_type op, enum expr_value_type type, ExprCreateUnary(enum expr_op_type op, enum expr_value_type type,
ExprDef *child) ExprDef *child)
@ -125,6 +173,33 @@ ExprCreateBinary(enum expr_op_type op, ExprDef *left, ExprDef *right)
return expr; return expr;
} }
ExprDef *
ExprCreateFieldRef(xkb_atom_t element, xkb_atom_t field)
{
ExprDef *expr = ExprCreate(EXPR_FIELD_REF, EXPR_TYPE_UNKNOWN);
if (!expr)
return NULL;
expr->value.field.element = element;
expr->value.field.field = field;
return expr;
}
ExprDef *
ExprCreateArrayRef(xkb_atom_t element, xkb_atom_t field, ExprDef *entry)
{
ExprDef *expr = ExprCreate(EXPR_ARRAY_REF, EXPR_TYPE_UNKNOWN);
if (!expr)
return NULL;
expr->value.array.element = element;
expr->value.array.field = field;
expr->value.array.entry = entry;
return expr;
}
KeycodeDef * KeycodeDef *
KeycodeCreate(xkb_atom_t name, int64_t value) KeycodeCreate(xkb_atom_t name, int64_t value)
{ {

View File

@ -31,7 +31,16 @@ ParseCommon *
AppendStmt(ParseCommon *to, ParseCommon *append); AppendStmt(ParseCommon *to, ParseCommon *append);
ExprDef * ExprDef *
ExprCreate(enum expr_op_type op, enum expr_value_type type); ExprCreateString(xkb_atom_t str);
ExprDef *
ExprCreateInteger(int ival);
ExprDef *
ExprCreateKeyName(xkb_atom_t key_name);
ExprDef *
ExprCreateIdent(xkb_atom_t ident);
ExprDef * ExprDef *
ExprCreateUnary(enum expr_op_type op, enum expr_value_type type, ExprCreateUnary(enum expr_op_type op, enum expr_value_type type,
@ -40,6 +49,12 @@ ExprCreateUnary(enum expr_op_type op, enum expr_value_type type,
ExprDef * ExprDef *
ExprCreateBinary(enum expr_op_type op, ExprDef *left, ExprDef *right); ExprCreateBinary(enum expr_op_type op, ExprDef *left, ExprDef *right);
ExprDef *
ExprCreateFieldRef(xkb_atom_t element, xkb_atom_t field);
ExprDef *
ExprCreateArrayRef(xkb_atom_t element, xkb_atom_t field, ExprDef *entry);
KeycodeDef * KeycodeDef *
KeycodeCreate(xkb_atom_t name, int64_t value); KeycodeCreate(xkb_atom_t name, int64_t value);

View File

@ -666,65 +666,23 @@ Action : FieldSpec OPAREN OptExprList CPAREN
; ;
Lhs : FieldSpec Lhs : FieldSpec
{ { $$ = ExprCreateIdent($1); }
ExprDef *expr;
expr = ExprCreate(EXPR_IDENT, EXPR_TYPE_UNKNOWN);
expr->value.ident = $1;
$$ = expr;
}
| FieldSpec DOT FieldSpec | FieldSpec DOT FieldSpec
{ { $$ = ExprCreateFieldRef($1, $3); }
ExprDef *expr;
expr = ExprCreate(EXPR_FIELD_REF, EXPR_TYPE_UNKNOWN);
expr->value.field.element = $1;
expr->value.field.field = $3;
$$ = expr;
}
| FieldSpec OBRACKET Expr CBRACKET | FieldSpec OBRACKET Expr CBRACKET
{ { $$ = ExprCreateArrayRef(XKB_ATOM_NONE, $1, $3); }
ExprDef *expr;
expr = ExprCreate(EXPR_ARRAY_REF, EXPR_TYPE_UNKNOWN);
expr->value.array.element = XKB_ATOM_NONE;
expr->value.array.field = $1;
expr->value.array.entry = $3;
$$ = expr;
}
| FieldSpec DOT FieldSpec OBRACKET Expr CBRACKET | FieldSpec DOT FieldSpec OBRACKET Expr CBRACKET
{ { $$ = ExprCreateArrayRef($1, $3, $5); }
ExprDef *expr;
expr = ExprCreate(EXPR_ARRAY_REF, EXPR_TYPE_UNKNOWN);
expr->value.array.element = $1;
expr->value.array.field = $3;
expr->value.array.entry = $5;
$$ = expr;
}
; ;
Terminal : String Terminal : String
{ { $$ = ExprCreateString($1); }
ExprDef *expr;
expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_STRING);
expr->value.str = $1;
$$ = expr;
}
| Integer | Integer
{ { $$ = ExprCreateInteger($1); }
ExprDef *expr;
expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_INT);
expr->value.ival = $1;
$$ = expr;
}
| Float | Float
{ { $$ = NULL; }
$$ = NULL;
}
| KEYNAME | KEYNAME
{ { $$ = ExprCreateKeyName($1); }
ExprDef *expr;
expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_KEYNAME);
expr->value.keyName = $1;
$$ = expr;
}
; ;
OptKeySymList : KeySymList { $$ = $1; } OptKeySymList : KeySymList { $$ = $1; }