This repository has been archived by the owner on Jan 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.c
124 lines (110 loc) · 1.64 KB
/
tokenizer.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
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
#include <string.h>
#include "tokenizer.h"
static const char *
get_props(token *tok)
{
const char *p = tok->start;
int depth = 0;
while (p != tok->eof)
{
if (depth == 1 && *p == '}')
return p;
else if (*p == '}')
depth--;
else if (*p == '{')
{
depth++;
}
p++;
}
return NULL;
}
static const char *
tok_strchr(token *tok, char c)
{
const char *p = tok->start;
while (p != tok->eof)
{
if (*p == c)
return p;
p++;
}
return NULL;
}
/*
* 3 token types:
* 1. id -> label
* 2. [] -> gid
* 3. {} -> json string
*/
int
get_token(token *data)
{
int depth = 0;
if (token_is_end (data))
return 0;
if (data->end != NULL)
data->start = data->end;
switch (*data->start)
{
case '[':
data->end = tok_strchr(data, ']') + 1;
break;
case '{':
data->end = get_props(data) + 1;
break;
default:
data->end = tok_strchr(data, '[');
break;
}
return 1;
}
void
copy_token_str(char *dest, token *src)
{
strncpy (dest, src->start, token_length(src));
dest[token_length(src)] = '\0';
}
/*
* vertex0,edge0,vertex1,edge1,vertex2
*/
int
get_path_token(token *data)
{
int depth = 0;
int in_gid = 0;
const char *p;
if (token_is_end (data))
return 0;
if (data->end != NULL)
data->start = data->end + 1; /* skip "," */
p = data->start;
while (p != data->eof)
{
char c = *p;
if ('{' == c)
{
depth++;
}
else if ('}' == c)
{
depth--;
}
else if (0 == depth && '[' == c)
{
in_gid = 1;
}
else if (in_gid && ']' == c)
{
in_gid = 0;
}
else if (0 == depth && !in_gid && ',' == c)
{
data->end = p;
return 1;
}
p++;
}
data->end = p;
return 1;
}