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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
diff --git a/src/xkbcomp/ast-build.c b/src/xkbcomp/ast-build.c
index 29980aa..2130e26 100644
--- a/src/xkbcomp/ast-build.c
+++ b/src/xkbcomp/ast-build.c
@@ -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));
@@ -86,6 +86,54 @@ ExprCreate(enum expr_op_type op, enum expr_value_type type)
}
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)
{
diff --git a/src/xkbcomp/ast-build.h b/src/xkbcomp/ast-build.h
index 4413f88..b92b88b 100644
--- a/src/xkbcomp/ast-build.h
+++ b/src/xkbcomp/ast-build.h
@@ -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);
diff --git a/src/xkbcomp/parser.y b/src/xkbcomp/parser.y
index b48966d..34bcdc0 100644
--- a/src/xkbcomp/parser.y
+++ b/src/xkbcomp/parser.y
@@ -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; }