-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
81 lines (70 loc) · 1.7 KB
/
parser.c
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
#include <stdlib.h>
#include <string.h>
typedef struct parser_
{
const char* buffer;
int pos;
int line;
} parser;
/** Create a new parser with the given string to parse. **/
parser* create_parser(const char* buffer)
{
parser* rval = malloc(sizeof(parser));
rval->buffer = buffer;
rval->pos = 0;
rval->line = 0;
};
/** Returns 1 if we're at the end of the buffer. **/
int parser_at_end(parser* p)
{
return p->pos >= strlen(p->buffer);
};
/** Returns a new string with the next amount chars in the buffer. **/
const char* parser_get_next(parser* p, int amount)
{
if(p->pos + amount > strlen(p->buffer))
{
return NULL;
}
char* rval = malloc(amount+1);
strncpy(rval, p->buffer+p->pos, amount);
return rval;
};
/** Returns the next character in the buffer. **/
char parser_get_char(parser* p)
{
if(p->pos < strlen(p->buffer))
{
return p->buffer[p->pos];
}
return 0;
};
/** Checks if the given word is ahead in the buffer.
Return 1 if it is, 0 otherwise.
**/
int parser_is_next(parser* p, const char* word)
{
const char* compare = parser_get_next(p, strlen(word));
return strcmp(word, compare) == 0;
};
/** Skips the next amount chars in the buffer. Returns the skipped string. **/
const char* parser_skip_amount(parser* p, int amount)
{
int start = p->pos;
p->pos += amount;
if(p->pos > strlen(p->buffer)) p->pos = strlen(p->buffer);
int length = p->pos - start + 1;
char* rval = malloc(length);
strncpy(rval, p->buffer+start, length);
return rval;
};
/** Checks if the word is in the buffer, and if so, skips it. Returns 1 if word matched. **/
int parser_skip(parser* p, const char* word)
{
if(parser_is_next(p, word))
{
parser_skip_amount(p, strlen(word));
return 1;
}
else return 0;
};