~ubuntu-branches/debian/stretch/libxr/stretch

« back to all changes in this revision

Viewing changes to xdl-compiler/parser-lib.h

  • Committer: Bazaar Package Importer
  • Author(s): Fabien Boucher
  • Date: 2009-10-14 00:52:51 UTC
  • Revision ID: james.westby@ubuntu.com-20091014005251-epx05xv5ef9188q0
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright 2006-2008 Ondrej Jirman <ondrej.jirman@zonio.net>
 
3
 * 
 
4
 * This file is part of libxr.
 
5
 *
 
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
 
9
 * later version.
 
10
 *
 
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
 
14
 * details.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#ifndef __PARSER_LIB_H
 
21
#define __PARSER_LIB_H
 
22
 
 
23
#include <glib.h>
 
24
#include <string.h>
 
25
 
 
26
/* special tokens */
 
27
#define TK_EOF 0
 
28
#define TK_UNKNOWN (-1)
 
29
 
 
30
/* lexer helper macros */
 
31
#define RET(tok) \
 
32
  return token_new(s, tok, c-b)
 
33
 
 
34
#define EAT(next_state) \
 
35
  stream_advance(s, c-b); \
 
36
  b = c
 
37
 
 
38
#define DECLARE_LEXER() \
 
39
  static token *stream_peek_token(stream* s) \
 
40
  { \
 
41
    char *c, *b, *q; \
 
42
    if (s->length - s->index == 0) \
 
43
      return token_new(s, TK_EOF, 0); \
 
44
    c = b = s->buffer + s->index;
 
45
 
 
46
#define DECLARE_LEXER_END() \
 
47
    return NULL; \
 
48
  }
 
49
 
 
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) \
 
58
  { \
 
59
    return (rtype)__parse_string(str, err, name, name##Alloc, name##Free, stream_peek_token); \
 
60
  } \
 
61
  rtype fname##_file(const char* path, GError** err) \
 
62
  { \
 
63
    return (rtype)__parse_file(path, err, name, name##Alloc, name##Free, stream_peek_token); \
 
64
  }
 
65
 
 
66
#define HANDLE_SYNTAX_ERROR(TOKEN) \
 
67
  g_free(ctx->error); \
 
68
  if (TOKEN == NULL || TOKEN->type == TK_EOF) \
 
69
    ctx->error = g_strdup_printf("Unexpected EOF!\n"); \
 
70
  else \
 
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])
 
73
 
 
74
#define HANDLE_STACK_OVERFLOW() \
 
75
  g_free(ctx->error); \
 
76
  ctx->error = g_strdup_printf("Parser stack overflow!\n")
 
77
 
 
78
typedef struct stream stream;
 
79
struct stream {
 
80
  char* path;
 
81
  char* buffer;
 
82
  int index;
 
83
  int line;
 
84
  int col;
 
85
  int length;
 
86
};
 
87
 
 
88
typedef struct token token;
 
89
struct token 
 
90
{
 
91
  int type;
 
92
  char* text;       /* token text */
 
93
  int length;
 
94
  int sline;        /* token location (starts from 0) */
 
95
  int scol;
 
96
  int eline;
 
97
  int ecol;
 
98
};
 
99
 
 
100
typedef struct parser_context parser_context;
 
101
struct parser_context
 
102
{
 
103
  void* data;
 
104
  stream* stream;
 
105
  char* error;
 
106
};
 
107
 
 
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*);
 
113
 
 
114
stream* stream_new_from_string(const char* str);
 
115
void stream_advance(stream* s, int length);
 
116
 
 
117
token *token_new(stream* s, int type, int length);
 
118
void token_free(token * t);
 
119
 
 
120
void* __parse_string(const char* str, 
 
121
                     GError** err,
 
122
                     parser parser_cb, 
 
123
                     parser_alloc parser_alloc_cb, 
 
124
                     parser_free parser_free_cb,
 
125
                     lexer lexer_cb);
 
126
void* __parse_file(const char* path, 
 
127
                   GError** err,
 
128
                   parser parser_cb, 
 
129
                   parser_alloc parser_alloc_cb, 
 
130
                   parser_free parser_free_cb,
 
131
                   lexer lexer_cb);
 
132
 
 
133
#endif