aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/sxpr_parser.h
blob: 7296312e446f52065b77e08efc8b121b9c9336e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or  (at your option) any later version. This library is 
 * distributed in the  hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#ifndef _XEN_LIB_SXPR_PARSER_H_
#define _XEN_LIB_SXPR_PARSER_H_

#include "sxpr.h"
#include "iostream.h"

/** @file
 * Sxpr parsing definitions.
 */

/** Size of a parser input buffer.
 * Tokens read must fit into this size (including trailing null).
 */
#define PARSER_BUF_SIZE 1024

struct Parser;
typedef int ParserStateFn(struct Parser *, char c);

typedef struct ParserState {
    struct ParserState *parent;
    Sxpr val;
    int ival;
    int count;
    char delim;
    ParserStateFn *fn;
} ParserState;

/** Structure representing an input source for the parser.
 * Can read from any IOStream implementation.
 */
typedef struct Parser {
    Sxpr val;
    /** Error reporting stream (null for no reports). */
    IOStream *error_out;
    int eof;
    /** Error flag. Non-zero if there has been a read error. */
    int err;
    /** Line number on input (from 1). */
    int line_no;
    /** Column number of input (reset on new line). */
    int char_no;
    /** Lookahead character. */
    char c;
    /** Buffer for reading tokens. */
    char buf[PARSER_BUF_SIZE];
    /** Size of token buffer. */
    int buf_n;
    int buf_i;
    /** Line the last token started on. */
    int tok_begin_line;
    /** Character number the last token started on. */
    int tok_begin_char;
    /** Parsing flags. */
    int flags;
    ParserState *state;
} Parser;

/** Parser error codes. */
typedef enum {
    PARSE_ERR_NONE=0,
    PARSE_ERR_UNSPECIFIED,
    PARSE_ERR_NOMEM,
    PARSE_ERR_UNEXPECTED_EOF,
    PARSE_ERR_TOKEN_TOO_LONG,
    PARSE_ERR_INVALID_SYNTAX,
    PARSE_ERR_INVALID_ESCAPE,
} ParseErrorId;


/** Parser flags. */
//enum {
//};

/** Raise some parser flags.
 *
 * @param in parser
 * @param flags flags mask
 */
inline static void parser_flags_raise(Parser *in, int flags){
    in->flags |= flags;
}

/** Lower some parser flags.
 *
 * @param in parser
 * @param flags flags mask
 */
inline static void parser_flags_lower(Parser *in, int flags){
    in->flags &= ~flags;
}

/** Clear all parser flags.
 *
 * @param in parser
 */
inline static void parser_flags_clear(Parser *in){
    in->flags = 0;
}

extern void Parser_free(Parser *z);
extern Parser * Parser_new(void);
extern int Parser_input(Parser *p, char *buf, int buf_n);
extern int Parser_input_eof(Parser *p);

extern int parse_error_message(Parser *in, char *buf, int n);
extern int has_error(Parser *in);
extern int at_eof(Parser *in);

#endif /* ! _XEN_LIB_SXPR_PARSER_H_ */