From 64aa5c95ed4507f88aa00d397e793af73dd812f4 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Wed, 9 May 2012 11:12:30 +0300 Subject: [PATCH] Make the context available to the parser 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 --- src/xkbcomp/misc.c | 5 ++-- src/xkbcomp/parseutils.h | 22 ++++++++++------- src/xkbcomp/scanner.l | 62 ++++++++++++++++++++++++++++-------------------- src/xkbcomp/xkbcomp.c | 6 ++--- 4 files changed, 55 insertions(+), 40 deletions(-) diff --git a/src/xkbcomp/misc.c b/src/xkbcomp/misc.c index 7e163fa..7072adc 100644 --- a/src/xkbcomp/misc.c +++ b/src/xkbcomp/misc.c @@ -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); diff --git a/src/xkbcomp/parseutils.h b/src/xkbcomp/parseutils.h index dc9d432..9bcc9fb 100644 --- a/src/xkbcomp/parseutils.h +++ b/src/xkbcomp/parseutils.h @@ -24,8 +24,8 @@ ********************************************************/ -#ifndef XKBPARSE_H -#define XKBPARSE_H 1 +#ifndef PARSEUTILS_H +#define PARSEUTILS_H #include @@ -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 */ diff --git a/src/xkbcomp/scanner.l b/src/xkbcomp/scanner.l index b8f9be2..7711d82 100644 --- a/src/xkbcomp/scanner.l +++ b/src/xkbcomp/scanner.l @@ -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, ¶m.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(¶m); 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, ¶m.scanner) != 0) - return 0; - extra.scanFile = strdup(fileName); + ret = yylex_init_extra(&extra, ¶m.scanner); + if (ret != 0) + return false; + + extra.scanFile = strdup(file_name); + if (!extra.scanFile) + return false; yyset_in(file, param.scanner); ret = yyparse(¶m); 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; } diff --git a/src/xkbcomp/xkbcomp.c b/src/xkbcomp/xkbcomp.c index 1912dd5..c1b7bd0 100644 --- a/src/xkbcomp/xkbcomp.c +++ b/src/xkbcomp/xkbcomp.c @@ -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); -- 2.7.4