-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
153 lines (119 loc) · 2.4 KB
/
parser.h
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef _PARSER_H_
#define _PARSER_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
#define bool int
#define true 1
#define false 0
#define PUNCT '.': \
case '(': case ')': \
case '[': case ']': \
case '{': case '}'
#define SPACE ' ': \
case ',': case '\n': \
case '\r': case '\t'
enum Type {
UNDEFINED,
SYNTAX,
// SYNTAX_OPEN_PAREN,
// SYNTAX_CLOSE_PAREN,
// SYNTAX_OPEN_SQUARE,
// SYNTAX_CLOSE_SQUARE,
// SYNTAX_OPEN_CURLY,
// SYNTAX_CLOSE_CURLY,
SYMBOL,
CHAR,
STRING,
NUMBER,
FLOAT
};
extern const char *types[];
const char *
type_to_string(enum Type type);
typedef struct Token {
const char *token;
int toklen;
bool managed;
enum Type type;
struct Token *next;
struct Token *prev;
struct Arena *arena;
} Token;
typedef struct ArenaOptions {
const size_t BUFFSIZE;
const size_t MAXALLOCS;
const double EXPONENT;
int (*inc_factor)(const struct Arena *a);
} ArenaOptions;
typedef struct Arena {
struct Token *first;
struct Token *tptr;
int multiplier;
size_t numAllocs;
struct Token **allocs;
size_t numTokens;
size_t totalNumTokens;
const struct ArenaOptions *opts;
} Arena;
typedef struct Expression {
const Token *function;
int arity;
struct Expression *child;
struct Expression *sibling;
} Expression;
int
abs(int n);
const char *
tokens_to_string(const Token *token);
const char *
tokens_to_string2(const Token *token, const char sep);
void
print_tokens(const Token *tokens);
void *
die_parser_error(const char *msg, Arena *arena);
Token *
prior_expression(Token *token);
void
unlink_token(Token *token);
Token *
new_token();
void
free_tokens(Arena *arena);
Token *
inc_token(Token *token);
Token *
token_from_string(const char *str, Arena *arena);
Token *
token_from_char(char c, Arena *arena);
int
insert_tokens_after(Token *token, Token *after);
bool
is_numeric(char c);
bool
is_decimal_point(const char *dot, bool check_behind);
/**
** Replace '.' notation with functional equivalent
**
***/
Arena *
normalize(Arena *arena);
ArenaOptions *
new_arena_options(
size_t buffsize,
size_t maxallocs,
double exponent,
int (*inc_factor)(const Arena *)
);
Arena *
tokenize2(const char *query, struct ArenaOptions *opts);
Arena *
tokenize3(const char *query, struct ArenaOptions *opts, bool verbose);
Arena *
tokenize(const char *query);
const Expression *
analyze(const Arena *arena);
#endif // _PARSER_H_