expr: add constructors for more expression types
authorRan Benita <ran234@gmail.com>
Sat, 30 Nov 2013 21:12:45 +0000 (23:12 +0200)
committerRan Benita <ran234@gmail.com>
Sun, 1 Dec 2013 08:47:56 +0000 (10:47 +0200)
This makes the parser a bit more declarative. But really it might make
error handling easier.

Signed-off-by: Ran Benita <ran234@gmail.com>
src/xkbcomp/ast-build.c
src/xkbcomp/ast-build.h
src/xkbcomp/parser.y

index 29980aaa26d356bf200291a21a80376a89917ab9..2130e267bd7f63b7b005a9dc7a7bc41fd6588169 100644 (file)
@@ -70,7 +70,7 @@ AppendStmt(ParseCommon *to, ParseCommon *append)
     return to;
 }
 
-ExprDef *
+static ExprDef *
 ExprCreate(enum expr_op_type op, enum expr_value_type type)
 {
     ExprDef *expr = malloc(sizeof(*expr));
@@ -85,6 +85,54 @@ ExprCreate(enum expr_op_type op, enum expr_value_type type)
     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 *
 ExprCreateUnary(enum expr_op_type op, enum expr_value_type type,
                 ExprDef *child)
@@ -125,6 +173,33 @@ ExprCreateBinary(enum expr_op_type op, ExprDef *left, ExprDef *right)
     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 *
 KeycodeCreate(xkb_atom_t name, int64_t value)
 {
index 4413f88986ff3a5c22e82e7bbd24ca1a71a3f6ca..b92b88b1e8787245022f5032f79cc3a1f9302a5d 100644 (file)
@@ -31,7 +31,16 @@ ParseCommon *
 AppendStmt(ParseCommon *to, ParseCommon *append);
 
 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 *
 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 *
 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 *
 KeycodeCreate(xkb_atom_t name, int64_t value);
 
index b48966dfe018ff70c88b80769a43323c565e8e50..34bcdc0777096d7689e73089655e0dce80e8ef63 100644 (file)
@@ -666,65 +666,23 @@ Action          :       FieldSpec OPAREN OptExprList CPAREN
                 ;
 
 Lhs             :       FieldSpec
-                        {
-                            ExprDef *expr;
-                            expr = ExprCreate(EXPR_IDENT, EXPR_TYPE_UNKNOWN);
-                            expr->value.ident = $1;
-                            $$ = expr;
-                        }
+                        { $$ = ExprCreateIdent($1); }
                 |       FieldSpec DOT FieldSpec
-                        {
-                            ExprDef *expr;
-                            expr = ExprCreate(EXPR_FIELD_REF, EXPR_TYPE_UNKNOWN);
-                            expr->value.field.element = $1;
-                            expr->value.field.field = $3;
-                            $$ = expr;
-                        }
+                        { $$ = ExprCreateFieldRef($1, $3); }
                 |       FieldSpec OBRACKET Expr CBRACKET
-                        {
-                            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;
-                        }
+                        { $$ = ExprCreateArrayRef(XKB_ATOM_NONE, $1, $3); }
                 |       FieldSpec DOT FieldSpec OBRACKET Expr CBRACKET
-                        {
-                            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;
-                        }
+                        { $$ = ExprCreateArrayRef($1, $3, $5); }
                 ;
 
 Terminal        :       String
-                        {
-                            ExprDef *expr;
-                            expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_STRING);
-                            expr->value.str = $1;
-                            $$ = expr;
-                        }
+                        { $$ = ExprCreateString($1); }
                 |       Integer
-                        {
-                            ExprDef *expr;
-                            expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_INT);
-                            expr->value.ival = $1;
-                            $$ = expr;
-                        }
+                        { $$ = ExprCreateInteger($1); }
                 |       Float
-                        {
-                            $$ = NULL;
-                        }
+                        { $$ = NULL; }
                 |       KEYNAME
-                        {
-                            ExprDef *expr;
-                            expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_KEYNAME);
-                            expr->value.keyName = $1;
-                            $$ = expr;
-                        }
+                        { $$ = ExprCreateKeyName($1); }
                 ;
 
 OptKeySymList   :       KeySymList      { $$ = $1; }