scanner: share code in XkbParse{File,String}

Some refactoring to prepare for changes in the parse() function.

Signed-off-by: Ran Benita <ran234@gmail.com>
master
Ran Benita 2012-09-28 10:15:27 +02:00
parent 471ebad623
commit ee4ab30ea2
4 changed files with 75 additions and 67 deletions

View File

@ -291,7 +291,8 @@ ProcessIncludeFile(struct xkb_context *ctx,
if (!file) if (!file)
return false; return false;
if (!XkbParseFile(ctx, file, stmt->file, &rtrn)) { rtrn = XkbParseFile(ctx, file, stmt->file);
if (!rtrn) {
log_err(ctx, "Error interpreting include file \"%s\"\n", stmt->file); log_err(ctx, "Error interpreting include file \"%s\"\n", stmt->file);
fclose(file); fclose(file);
return false; return false;

View File

@ -245,76 +245,86 @@ CheckDefaultMap(struct xkb_context *ctx, XkbFile *maps, const char *fileName)
} }
} }
bool static XkbFile *
XkbParseString(struct xkb_context *ctx, const char *string, parse(struct xkb_context *ctx, yyscan_t scanner)
const char *file_name, XkbFile **out)
{ {
int ret;
struct parser_param param; struct parser_param param;
param.scanner = scanner;
param.ctx = ctx;
if (_xkbcommon_parse(&param) != 0)
return NULL;
CheckDefaultMap(param.ctx, param.rtrn, yyget_extra(scanner)->scanFile);
return param.rtrn;
}
static bool
init_scanner(yyscan_t *scanner, struct scanner_extra *extra,
struct xkb_context *ctx, const char *file_name)
{
memset(extra, 0, sizeof(*extra));
if (yylex_init_extra(extra, scanner) != 0)
return false;
extra->ctx = ctx;
extra->scanFile = strdup(file_name);
if (!extra->scanFile)
return false;
return true;
}
static void
clear_scanner(yyscan_t scanner, struct scanner_extra *extra)
{
free(extra->scanFile);
yylex_destroy(scanner);
}
XkbFile *
XkbParseString(struct xkb_context *ctx, const char *string,
const char *file_name)
{
yyscan_t scanner;
struct scanner_extra extra; struct scanner_extra extra;
YY_BUFFER_STATE state; YY_BUFFER_STATE state;
XkbFile *xkb_file;
if (string == NULL) if (!init_scanner(&scanner, &extra, ctx, file_name))
return false; return NULL;
param.ctx = ctx; state = yy_scan_string(string, scanner);
memset(&extra, 0, sizeof(extra)); xkb_file = parse(ctx, scanner);
ret = yylex_init_extra(&extra, &param.scanner);
if (ret != 0)
return false;
extra.ctx = ctx; yy_delete_buffer(state, scanner);
clear_scanner(scanner, &extra);
extra.scanFile = strdup(file_name); return xkb_file;
if (!extra.scanFile)
return false;
state = yy_scan_string(string, param.scanner);
ret = _xkbcommon_parse(&param);
yy_delete_buffer(state, param.scanner);
_xkbcommon_lex_destroy(param.scanner);
free(extra.scanFile);
if (ret != 0)
return false;
CheckDefaultMap(param.ctx, param.rtrn, file_name);
*out = param.rtrn;
return true;
} }
bool XkbFile *
XkbParseFile(struct xkb_context *ctx, FILE *file, XkbParseFile(struct xkb_context *ctx, FILE *file, const char *file_name)
const char *file_name, XkbFile **out)
{ {
int ret; yyscan_t scanner;
struct parser_param param;
struct scanner_extra extra; struct scanner_extra extra;
YY_BUFFER_STATE state;
XkbFile *xkb_file;
if (!file) if (!init_scanner(&scanner, &extra, ctx, file_name))
return false; return NULL;
param.ctx = ctx; state = yy_create_buffer(file, YY_BUF_SIZE, scanner);
yy_switch_to_buffer(state, scanner);
memset(&extra, 0, sizeof(extra)); xkb_file = parse(ctx, scanner);
ret = yylex_init_extra(&extra, &param.scanner);
if (ret != 0)
return false;
extra.ctx = ctx; yy_delete_buffer(state, scanner);
clear_scanner(scanner, &extra);
extra.scanFile = strdup(file_name); return xkb_file;
if (!extra.scanFile)
return false;
_xkbcommon_set_in(file, param.scanner);
ret = _xkbcommon_parse(&param);
_xkbcommon_lex_destroy(param.scanner);
free(extra.scanFile);
if (ret != 0)
return false;
CheckDefaultMap(param.ctx, param.rtrn, file_name);
*out = param.rtrn;
return true;
} }

View File

@ -37,13 +37,12 @@ struct xkb_component_names {
char *symbols; char *symbols;
}; };
bool XkbFile *
XkbParseFile(struct xkb_context *ctx, FILE *file, const char *file_name, XkbParseFile(struct xkb_context *ctx, FILE *file, const char *file_name);
XkbFile **out);
bool XkbFile *
XkbParseString(struct xkb_context *context, const char *string, XkbParseString(struct xkb_context *ctx, const char *string,
const char *file_name, XkbFile **out); const char *file_name);
void void
FreeXkbFile(XkbFile *file); FreeXkbFile(XkbFile *file);

View File

@ -124,7 +124,6 @@ xkb_keymap_new_from_string(struct xkb_context *ctx,
enum xkb_keymap_format format, enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags) enum xkb_keymap_compile_flags flags)
{ {
bool ok;
XkbFile *file; XkbFile *file;
struct xkb_keymap *keymap; struct xkb_keymap *keymap;
@ -138,8 +137,8 @@ xkb_keymap_new_from_string(struct xkb_context *ctx,
return NULL; return NULL;
} }
ok = XkbParseString(ctx, string, "input", &file); file = XkbParseString(ctx, string, "input");
if (!ok) { if (!file) {
log_err(ctx, "Failed to parse input xkb file\n"); log_err(ctx, "Failed to parse input xkb file\n");
return NULL; return NULL;
} }
@ -155,7 +154,6 @@ xkb_keymap_new_from_file(struct xkb_context *ctx,
enum xkb_keymap_format format, enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags) enum xkb_keymap_compile_flags flags)
{ {
bool ok;
XkbFile *xkb_file; XkbFile *xkb_file;
struct xkb_keymap *keymap; struct xkb_keymap *keymap;
@ -169,8 +167,8 @@ xkb_keymap_new_from_file(struct xkb_context *ctx,
return NULL; return NULL;
} }
ok = XkbParseFile(ctx, file, "(unknown file)", &xkb_file); xkb_file = XkbParseFile(ctx, file, "(unknown file)");
if (!ok) { if (!xkb_file) {
log_err(ctx, "Failed to parse input xkb file\n"); log_err(ctx, "Failed to parse input xkb file\n");
return NULL; return NULL;
} }