-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoken.c
47 lines (42 loc) · 790 Bytes
/
token.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
/*
* arbre
*
* (c) 2011, Alexis Sellier
*
* token.c
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "source.h"
#include "tokens.h"
#include "token.h"
#include "util.h"
/*
* Print the string representation of a token
* `t` into `str`, and return it.
*/
char *tokentos(Token *t, char *str, bool showsrc)
{
const char *token = TOKEN_STRINGS[t->tok];
if (t->src && showsrc) {
sprintf(str, "<%s %s>", token, escape(t->src));
} else {
sprintf(str, "<%s>", token);
}
return str;
}
/*
* Allocate and return a new Token.
*/
Token *token(TOKEN tok, struct source *source, size_t pos, char *src)
{
Token *t = malloc(sizeof(*t));
t->tok = tok;
t->source = source;
t->pos = pos;
t->src = src;
return t;
}