rules: use goto instead of state variable There's no noticeable speed difference, but I think it's nicer and more explicit than the previous code. Some people just don't like goto, though.. 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
diff --git a/src/xkbcomp/rules.c b/src/xkbcomp/rules.c
index c048605..ee936c9 100644
--- a/src/xkbcomp/rules.c
+++ b/src/xkbcomp/rules.c
@@ -412,6 +412,7 @@ struct matcher {
/* Input.*/
struct rule_names rmlvo;
struct location loc;
+ union lvalue val;
struct scanner scanner;
darray(struct group) groups;
/* Current mapping. */
@@ -999,179 +1000,153 @@ matcher_rule_apply_if_matches(struct matcher *m)
m->mapping.skip = true;
}
-enum rules_state {
- STATE_INITIAL = 0,
- STATE_BANG,
- STATE_GROUP_NAME,
- STATE_GROUP_ELEMENT,
- STATE_MAPPING_MLVO,
- STATE_MAPPING_KCCGST,
- STATE_RULE_MLVO_FIRST,
- STATE_RULE_MLVO,
- STATE_RULE_KCCGST,
-};
+static enum rules_token
+gettok(struct matcher *m)
+{
+ return lex(&m->scanner, &m->val, &m->loc);
+}
static bool
matcher_match(struct matcher *m, const char *string, size_t len,
const char *file_name, struct xkb_component_names *out)
{
enum rules_token tok;
- enum rules_state state = STATE_INITIAL;
- union lvalue val;
if (!m)
return false;
scanner_init(&m->scanner, m->ctx, string, len, file_name);
- memset(&val, 0, sizeof(val));
-
- while ((tok = lex(&m->scanner, &val, &m->loc)) != TOK_END_OF_FILE)
- {
- if (tok == TOK_ERROR)
- goto error;
-
- switch (state) {
- case STATE_INITIAL:
- switch (tok) {
- case TOK_BANG:
- state = STATE_BANG;
- break;
- case TOK_END_OF_LINE:
- state = STATE_INITIAL;
- break;
- default:
- goto state_error;
- }
- break;
- case STATE_BANG:
- switch (tok) {
- case TOK_GROUP_NAME:
- matcher_group_start_new(m, val.string);
- state = STATE_GROUP_NAME;
- break;
- case TOK_IDENTIFIER:
- matcher_mapping_start_new(m);
- matcher_mapping_set_mlvo(m, val.string);
- state = STATE_MAPPING_MLVO;
- break;
- default:
- goto state_error;
- }
- break;
+initial:
+ switch (tok = gettok(m)) {
+ case TOK_BANG:
+ goto bang;
+ case TOK_END_OF_LINE:
+ goto initial;
+ case TOK_END_OF_FILE:
+ goto finish;
+ default:
+ goto unexpected;
+ }
- case STATE_GROUP_NAME:
- switch (tok) {
- case TOK_EQUALS:
- state = STATE_GROUP_ELEMENT;
- break;
- default:
- goto state_error;
- }
- break;
+bang:
+ switch (tok = gettok(m)) {
+ case TOK_GROUP_NAME:
+ matcher_group_start_new(m, m->val.string);
+ goto group_name;
+ case TOK_IDENTIFIER:
+ matcher_mapping_start_new(m);
+ matcher_mapping_set_mlvo(m, m->val.string);
+ goto mapping_mlvo;
+ default:
+ goto unexpected;
+ }
- case STATE_GROUP_ELEMENT:
- switch (tok) {
- case TOK_IDENTIFIER:
- matcher_group_add_element(m, val.string);
- state = STATE_GROUP_ELEMENT;
- break;
- case TOK_END_OF_LINE:
- state = STATE_INITIAL;
- break;
- default:
- goto state_error;
- }
- break;
+group_name:
+ switch (tok = gettok(m)) {
+ case TOK_EQUALS:
+ goto group_element;
+ default:
+ goto unexpected;
+ }
- case STATE_MAPPING_MLVO:
- switch (tok) {
- case TOK_IDENTIFIER:
- if (!m->mapping.skip)
- matcher_mapping_set_mlvo(m, val.string);
- state = STATE_MAPPING_MLVO;
- break;
- case TOK_EQUALS:
- state = STATE_MAPPING_KCCGST;
- break;
- default:
- goto state_error;
- }
- break;
+group_element:
+ switch (tok = gettok(m)) {
+ case TOK_IDENTIFIER:
+ matcher_group_add_element(m, m->val.string);
+ goto group_element;
+ case TOK_END_OF_LINE:
+ goto initial;
+ default:
+ goto unexpected;
+ }
- case STATE_MAPPING_KCCGST:
- switch (tok) {
- case TOK_IDENTIFIER:
- if (!m->mapping.skip)
- matcher_mapping_set_kccgst(m, val.string);
- state = STATE_MAPPING_KCCGST;
- break;
- case TOK_END_OF_LINE:
- if (!m->mapping.skip)
- matcher_mapping_verify(m);
- state = STATE_RULE_MLVO_FIRST;
- break;
- default:
- goto state_error;
- }
- break;
+mapping_mlvo:
+ switch (tok = gettok(m)) {
+ case TOK_IDENTIFIER:
+ if (!m->mapping.skip)
+ matcher_mapping_set_mlvo(m, m->val.string);
+ goto mapping_mlvo;
+ case TOK_EQUALS:
+ goto mapping_kccgst;
+ default:
+ goto unexpected;
+ }
- case STATE_RULE_MLVO_FIRST:
- if (tok == TOK_BANG) {
- state = STATE_BANG;
- break;
- } else if (tok == TOK_END_OF_LINE) {
- state = STATE_INITIAL;
- break;
- }
- matcher_rule_start_new(m);
- /* fallthrough */
- case STATE_RULE_MLVO:
- switch (tok) {
- case TOK_IDENTIFIER:
- if (!m->rule.skip)
- matcher_rule_set_mlvo(m, val.string);
- state = STATE_RULE_MLVO;
- break;
- case TOK_STAR:
- if (!m->rule.skip)
- matcher_rule_set_mlvo_wildcard(m);
- state = STATE_RULE_MLVO;
- break;
- case TOK_GROUP_NAME:
- if (!m->rule.skip)
- matcher_rule_set_mlvo_group(m, val.string);
- state = STATE_RULE_MLVO;
- break;
- case TOK_EQUALS:
- state = STATE_RULE_KCCGST;
- break;
- default:
- goto state_error;
- }
- break;
+mapping_kccgst:
+ switch (tok = gettok(m)) {
+ case TOK_IDENTIFIER:
+ if (!m->mapping.skip)
+ matcher_mapping_set_kccgst(m, m->val.string);
+ goto mapping_kccgst;
+ case TOK_END_OF_LINE:
+ if (!m->mapping.skip)
+ matcher_mapping_verify(m);
+ goto rule_mlvo_first;
+ default:
+ goto unexpected;
+ }
- case STATE_RULE_KCCGST:
- switch (tok) {
- case TOK_IDENTIFIER:
- if (!m->rule.skip)
- matcher_rule_set_kccgst(m, val.string);
- state = STATE_RULE_KCCGST;
- break;
- case TOK_END_OF_LINE:
- if (!m->rule.skip)
- matcher_rule_verify(m);
- if (!m->rule.skip)
- matcher_rule_apply_if_matches(m);
- state = STATE_RULE_MLVO_FIRST;
- break;
- default:
- goto state_error;
- }
- break;
- }
+rule_mlvo_first:
+ switch (tok = gettok(m)) {
+ case TOK_BANG:
+ goto bang;
+ case TOK_END_OF_LINE:
+ goto rule_mlvo_first;
+ case TOK_END_OF_FILE:
+ goto finish;
+ default:
+ matcher_rule_start_new(m);
+ goto rule_mlvo_no_tok;
+ }
+
+rule_mlvo:
+ tok = gettok(m);
+rule_mlvo_no_tok:
+ switch (tok) {
+ case TOK_IDENTIFIER:
+ if (!m->rule.skip)
+ matcher_rule_set_mlvo(m, m->val.string);
+ goto rule_mlvo;
+ case TOK_STAR:
+ if (!m->rule.skip)
+ matcher_rule_set_mlvo_wildcard(m);
+ goto rule_mlvo;
+ case TOK_GROUP_NAME:
+ if (!m->rule.skip)
+ matcher_rule_set_mlvo_group(m, m->val.string);
+ goto rule_mlvo;
+ case TOK_EQUALS:
+ goto rule_kccgst;
+ default:
+ goto unexpected;
+ }
+
+rule_kccgst:
+ switch (tok = gettok(m)) {
+ case TOK_IDENTIFIER:
+ if (!m->rule.skip)
+ matcher_rule_set_kccgst(m, m->val.string);
+ goto rule_kccgst;
+ case TOK_END_OF_LINE:
+ if (!m->rule.skip)
+ matcher_rule_verify(m);
+ if (!m->rule.skip)
+ matcher_rule_apply_if_matches(m);
+ goto rule_mlvo_first;
+ default:
+ goto unexpected;
+ }
+
+unexpected:
+ switch (tok) {
+ case TOK_ERROR:
+ goto error;
+ default:
+ goto state_error;
}
+finish:
if (darray_empty(m->kccgst[KCCGST_KEYCODES]) ||
darray_empty(m->kccgst[KCCGST_TYPES]) ||
darray_empty(m->kccgst[KCCGST_COMPAT]) ||