-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
example.c
50 lines (40 loc) · 1.38 KB
/
example.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
#include <stdio.h>
#include <peppa.h>
int main() {
P4_Grammar* grammar;
P4_Source* source;
P4_Node* root;
char* text;
grammar = P4_LoadGrammar("entry = i\"hello\\nworld\";");
if (grammar == NULL) {
printf("Error: CreateGrammar: Error.\n");
return 1;
}
source = P4_CreateSource("Hello\nWORLD", "entry");
if (source == NULL) {
printf("Error: CreateSource: MemoryError.\n");
return 1;
}
if (P4_Parse(grammar, source) != P4_Ok) {
printf("Error: Parse: ErrCode[%u] Err[%s] Message[%s]\n",
P4_GetError(source),
P4_GetErrorString(P4_GetError(source)),
P4_GetErrorMessage(source)
);
return 1;
}
root = P4_GetSourceAst(source);
text = P4_CopyNodeString(root);
printf("root span: [%lu %lu]\n", root->slice.start.pos, root->slice.stop.pos);
printf("root start: line=%lu offset=%lu\n", root->slice.start.lineno, root->slice.start.offset);
printf("root stop: line=%lu offset=%lu\n", root->slice.stop.lineno, root->slice.stop.offset);
printf("root next: %p\n", (void *)root->next);
printf("root head: %p\n", (void *)root->head);
printf("root tail: %p\n", (void *)root->tail);
printf("root text: %s\n", text);
free(text);
P4_JsonifySourceAst(stdout, root, NULL);
P4_DeleteSource(source);
P4_DeleteGrammar(grammar);
return 1;
}