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)
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);
fclose(file);
return false;

View File

@ -245,76 +245,86 @@ CheckDefaultMap(struct xkb_context *ctx, XkbFile *maps, const char *fileName)
}
}
bool
XkbParseString(struct xkb_context *ctx, const char *string,
const char *file_name, XkbFile **out)
static XkbFile *
parse(struct xkb_context *ctx, yyscan_t scanner)
{
int ret;
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;
YY_BUFFER_STATE state;
XkbFile *xkb_file;
if (string == NULL)
return false;
if (!init_scanner(&scanner, &extra, ctx, file_name))
return NULL;
param.ctx = ctx;
state = yy_scan_string(string, scanner);
memset(&extra, 0, sizeof(extra));
ret = yylex_init_extra(&extra, &param.scanner);
if (ret != 0)
return false;
xkb_file = parse(ctx, scanner);
extra.ctx = ctx;
yy_delete_buffer(state, scanner);
clear_scanner(scanner, &extra);
extra.scanFile = strdup(file_name);
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;
return xkb_file;
}
bool
XkbParseFile(struct xkb_context *ctx, FILE *file,
const char *file_name, XkbFile **out)
XkbFile *
XkbParseFile(struct xkb_context *ctx, FILE *file, const char *file_name)
{
int ret;
struct parser_param param;
yyscan_t scanner;
struct scanner_extra extra;
YY_BUFFER_STATE state;
XkbFile *xkb_file;
if (!file)
return false;
if (!init_scanner(&scanner, &extra, ctx, file_name))
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));
ret = yylex_init_extra(&extra, &param.scanner);
if (ret != 0)
return false;
xkb_file = parse(ctx, scanner);
extra.ctx = ctx;
yy_delete_buffer(state, scanner);
clear_scanner(scanner, &extra);
extra.scanFile = strdup(file_name);
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;
return xkb_file;
}

View File

@ -37,13 +37,12 @@ struct xkb_component_names {
char *symbols;
};
bool
XkbParseFile(struct xkb_context *ctx, FILE *file, const char *file_name,
XkbFile **out);
XkbFile *
XkbParseFile(struct xkb_context *ctx, FILE *file, const char *file_name);
bool
XkbParseString(struct xkb_context *context, const char *string,
const char *file_name, XkbFile **out);
XkbFile *
XkbParseString(struct xkb_context *ctx, const char *string,
const char *file_name);
void
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_compile_flags flags)
{
bool ok;
XkbFile *file;
struct xkb_keymap *keymap;
@ -138,8 +137,8 @@ xkb_keymap_new_from_string(struct xkb_context *ctx,
return NULL;
}
ok = XkbParseString(ctx, string, "input", &file);
if (!ok) {
file = XkbParseString(ctx, string, "input");
if (!file) {
log_err(ctx, "Failed to parse input xkb file\n");
return NULL;
}
@ -155,7 +154,6 @@ xkb_keymap_new_from_file(struct xkb_context *ctx,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags)
{
bool ok;
XkbFile *xkb_file;
struct xkb_keymap *keymap;
@ -169,8 +167,8 @@ xkb_keymap_new_from_file(struct xkb_context *ctx,
return NULL;
}
ok = XkbParseFile(ctx, file, "(unknown file)", &xkb_file);
if (!ok) {
xkb_file = XkbParseFile(ctx, file, "(unknown file)");
if (!xkb_file) {
log_err(ctx, "Failed to parse input xkb file\n");
return NULL;
}