2
* Copyright 2006-2008 Ondrej Jirman <ondrej.jirman@zonio.net>
4
* This file is part of libxr.
6
* Libxr is free software: you can redistribute it and/or modify it under the
7
* terms of the GNU Lesser General Public License as published by the Free
8
* Software Foundation, either version 2 of the License, or (at your option) any
11
* Libxr is distributed in the hope that it will be useful, but WITHOUT ANY
12
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with libxr. If not, see <http://www.gnu.org/licenses/>.
20
#ifndef __PARSER_LIB_H
21
#define __PARSER_LIB_H
28
#define TK_UNKNOWN (-1)
30
/* lexer helper macros */
32
return token_new(s, tok, c-b)
34
#define EAT(next_state) \
35
stream_advance(s, c-b); \
38
#define DECLARE_LEXER() \
39
static token *stream_peek_token(stream* s) \
42
if (s->length - s->index == 0) \
43
return token_new(s, TK_EOF, 0); \
44
c = b = s->buffer + s->index;
46
#define DECLARE_LEXER_END() \
50
/* parser helper macros */
51
#define DECLARE_PARSER(name, fname, rtype) \
52
extern void *name##Alloc(void *(*)(size_t)); \
53
extern void name##Free(void*, void(*)(void *)); \
54
extern void name(void*, int, token*, parser_context* ctx); \
55
extern void name##Trace(FILE *TraceFILE, char *zTracePrompt); \
56
extern const char* name##TokenName(int code); \
57
rtype fname##_string(const char* str, GError** err) \
59
return (rtype)__parse_string(str, err, name, name##Alloc, name##Free, stream_peek_token); \
61
rtype fname##_file(const char* path, GError** err) \
63
return (rtype)__parse_file(path, err, name, name##Alloc, name##Free, stream_peek_token); \
66
#define HANDLE_SYNTAX_ERROR(TOKEN) \
68
if (TOKEN == NULL || TOKEN->type == TK_EOF) \
69
ctx->error = g_strdup_printf("Unexpected EOF!\n"); \
71
ctx->error = g_strdup_printf("Syntax error on line %d column %d: unexpected token %s\n", \
72
TOKEN->sline+1, TOKEN->scol+1, TOKEN->type == TK_UNKNOWN ? "TK_UNKNOWN" : yyTokenName[TOKEN->type])
74
#define HANDLE_STACK_OVERFLOW() \
76
ctx->error = g_strdup_printf("Parser stack overflow!\n")
78
typedef struct stream stream;
88
typedef struct token token;
92
char* text; /* token text */
94
int sline; /* token location (starts from 0) */
100
typedef struct parser_context parser_context;
101
struct parser_context
108
/* parser functions types */
109
typedef void* (*parser_alloc)(void *(*)(size_t));
110
typedef void (*parser_free)(void*, void(*)(void *));
111
typedef void (*parser)(void*, int, token*, parser_context* ctx);
112
typedef token* (*lexer)(stream*);
114
stream* stream_new_from_string(const char* str);
115
void stream_advance(stream* s, int length);
117
token *token_new(stream* s, int type, int length);
118
void token_free(token * t);
120
void* __parse_string(const char* str,
123
parser_alloc parser_alloc_cb,
124
parser_free parser_free_cb,
126
void* __parse_file(const char* path,
129
parser_alloc parser_alloc_cb,
130
parser_free parser_free_cb,