Make the context available to the parser
authorRan Benita <ran234@gmail.com>
Wed, 9 May 2012 08:12:30 +0000 (11:12 +0300)
committerDaniel Stone <daniel@fooishbar.org>
Wed, 9 May 2012 14:30:48 +0000 (15:30 +0100)
We will need the context to remove some global state.
Also make the Parse* function just return bool while wer'e at it.

Signed-off-by: Ran Benita <ran234@gmail.com>
src/xkbcomp/misc.c
src/xkbcomp/parseutils.h
src/xkbcomp/scanner.l
src/xkbcomp/xkbcomp.c

index 7e163fa..7072adc 100644 (file)
@@ -59,8 +59,9 @@ ProcessIncludeFile(struct xkb_context *context,
                 XkbDirectoryForInclude(file_type));
         return false;
     }
-    /* parse the file */
-    if ((XKBParseFile(file, stmt->file, &rtrn) == 0) || (rtrn == NULL))
+
+
+    if (!XKBParseFile(context, file, stmt->file, &rtrn))
     {
         ERROR("Error interpreting include file \"%s\"\n", stmt->file);
         fclose(file);
index dc9d432..9bcc9fb 100644 (file)
@@ -24,8 +24,8 @@
 
  ********************************************************/
 
-#ifndef XKBPARSE_H
-#define        XKBPARSE_H 1
+#ifndef PARSEUTILS_H
+#define PARSEUTILS_H
 
 #include <stdio.h>
 
@@ -33,6 +33,7 @@
 #include "parser.h"
 
 struct parser_param {
+    struct xkb_context *context;
     void *scanner;
     XkbFile *rtrn;
 };
@@ -118,14 +119,17 @@ StmtSetMerge(ParseCommon *stmt, unsigned merge, struct YYLTYPE *loc, void *scann
 extern void
 CheckDefaultMap(XkbFile *maps, const char *fileName);
 
-extern int
-XKBParseFile(FILE *file, const char *fileName, XkbFile **pRtrn);
+extern XkbFile *
+CreateXKBFile(int type, char *name,
+              ParseCommon *defs, unsigned flags);
 
-extern int
-XKBParseString(const char *string, const char *fileName, XkbFile **pRtrn);
+extern bool
+XKBParseFile(struct xkb_context *context, FILE *file,
+               const char *file_name, XkbFile **out);
 
-extern XkbFile *
-CreateXKBFile(int type, char *name, ParseCommon *defs, unsigned flags);
+extern bool
+XKBParseString(struct xkb_context *contex, const char *string,
+                 const char *file_name, XkbFile **out);
 
 extern void
 FreeXKBFile(XkbFile *file);
@@ -136,4 +140,4 @@ FreeStmt(ParseCommon *stmt);
 extern void
 yyerror(struct YYLTYPE *loc, void *scanner, const char *msg);
 
-#endif /* XKBPARSE_H */
+#endif /* PARSEUTILS_H */
index b8f9be2..7711d82 100644 (file)
@@ -204,62 +204,72 @@ yyerror(struct YYLTYPE *loc, void *scanner, const char *msg)
     }
 }
 
-int
-XKBParseString(const char *string, const char *fileName, XkbFile **pRtrn)
+bool
+XKBParseString(struct xkb_context *context, const char *string,
+               const char *file_name, XkbFile **out)
 {
-    YY_BUFFER_STATE state;
+    int ret;
     struct parser_param param;
     struct scanner_extra extra;
-    int ret;
+    YY_BUFFER_STATE state;
 
-    *pRtrn = NULL;
     if (string == NULL)
-        return 1;
+        return false;
+
+    param.context = context;
 
     memset(&extra, 0, sizeof(extra));
     ret = yylex_init_extra(&extra, &param.scanner);
     if (ret != 0)
-        return 0;
-    extra.scanFile = strdup(fileName);
+        return false;
+
+    extra.scanFile = strdup(file_name);
+    if (!extra.scanFile)
+        return false;
 
     state = yy_scan_string(string, param.scanner);
     ret = yyparse(&param);
     yy_delete_buffer(state, param.scanner);
     yylex_destroy(param.scanner);
     free(extra.scanFile);
-    if (ret)
-        return 0;
-
-    CheckDefaultMap(param.rtrn, fileName);
-    *pRtrn = param.rtrn;
+    if (ret != 0)
+        return false;
 
-    return 1;
+    CheckDefaultMap(param.rtrn, file_name);
+    *out = param.rtrn;
+    return true;
 }
 
-int
-XKBParseFile(FILE * file, const char *fileName, XkbFile ** pRtrn)
+bool
+XKBParseFile(struct xkb_context *context, FILE *file,
+             const char *file_name, XkbFile **out)
 {
     int ret;
     struct parser_param param;
     struct scanner_extra extra;
 
-    *pRtrn = NULL;
     if (!file)
-        return 1;
+        return false;
+
+    param.context = context;
 
     memset(&extra, 0, sizeof(extra));
-    if (yylex_init_extra(&extra, &param.scanner) != 0)
-        return 0;
-    extra.scanFile = strdup(fileName);
+    ret = yylex_init_extra(&extra, &param.scanner);
+    if (ret != 0)
+        return false;
+
+    extra.scanFile = strdup(file_name);
+    if (!extra.scanFile)
+        return false;
 
     yyset_in(file, param.scanner);
     ret = yyparse(&param);
     yylex_destroy(param.scanner);
     free(extra.scanFile);
-    if (ret)
-        return 0;
+    if (ret != 0)
+        return false;
 
-    CheckDefaultMap(param.rtrn, fileName);
-    *pRtrn = param.rtrn;
-    return 1;
+    CheckDefaultMap(param.rtrn, file_name);
+    *out = param.rtrn;
+    return true;
 }
index 1912dd5..c1b7bd0 100644 (file)
@@ -275,7 +275,7 @@ xkb_map_new_from_string(struct xkb_context *context,
         return NULL;
     }
 
-    if (!XKBParseString(string, "input", &file) || !file) {
+    if (!XKBParseString(context, string, "input", &file)) {
         ERROR("failed to parse input xkb file\n");
         return NULL;
     }
@@ -308,9 +308,9 @@ xkb_map_new_from_fd(struct xkb_context *context,
         return NULL;
     }
 
-    if (!XKBParseFile(fptr, "(unknown file)", &file) || !file) {
+    if (!XKBParseFile(context, fptr, "(unknown file)", &file)) {
         ERROR("failed to parse input xkb file\n");
-       return NULL;
+        return NULL;
     }
 
     return compile_keymap(context, file);