aboutsummaryrefslogtreecommitdiffstats
path: root/tokenizer.c
blob: a7b3afb7ca7177e5a34a586d88e6e899ffb59aa9 (plain) (blame)
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
153
154
155
156
#include "tokenizer.h"

/* options avaliable */
#define ASSERT_ON
#define __EXIT_ABBORT
#include "assert.h"

/*****************************************************************************/
token* token_create()
{
	token *t = NULL;
	t = malloc(sizeof(token));
	memset(t,0,sizeof(token));
	return t;
}


/*****************************************************************************/
int  token_set( token **tok, int val, char *s, char *e )
{
	AS_NULL(s)
	AS_NULL(e)
	AS_NULL(tok)
	AS_NULL(*tok)
	AS_EXPR(s>e)
	AS_EXPR(val<0)

	if ( tok == NULL )
	{
		printf("token is NULL\n");
		return -1;
	}

	if ( !(TOK_IN_RANGE(val)) )
	{
		printf("Token value not in range\n");
		return -1;
	}

	(*tok)->token = val;
	(*tok)->s = s;
	(*tok)->e = e;

	return 0;
}


/*****************************************************************************/
token_list* tl_create()
{
	token_list *tl = NULL;

	tl = malloc( sizeof(token_list) );
	if (tl == NULL)
	{
		printf("Cannot alloc mem\n");
		goto error;
	}
	memset(tl, 0, sizeof(token_list));

	tl->list = darr_create( sizeof(token), 100 );
	if (tl->list == NULL)
	{
		printf("cannot create ->list\n");
		goto error;
	}

	return tl;

error:
	return NULL;
}


/*****************************************************************************/
int tl_add_tok( token_list *tl, int t, char *s, char *e )
{
	AS_NULL(tl)
	AS_NULL(s)
	AS_NULL(e)
	AS_EXPR(t<1)

	token *tok = NULL;
	tok = token_create();
	if (tok == NULL)
	{
		printf("Cannot add token\n");
		return -1;
	}
	token_set( &tok, t, s, e );
	darr_push( tl->list, (void *)tok );
	return 0;
}


/*****************************************************************************/
char* tl_str( token_list *tl )
{
	AS_NULL(tl)
	int i   = 0;

	for (i=0; i<darr_end(tl->list); i++)
	{
		token *t = (token *)darr_get(tl->list, i);
		//printf("t=0x%x\n",t);
		if ( t != NULL )
		{
			printf("%02d:TOK:0x%x s:0x%08x e:0x%08x\n",i,t->token,t->s,t->e);
		}
	}

	return NULL;
}


/*****************************************************************************/
void tl_destroy( token_list *tl )
{
	if ( tl == NULL )
		return;
	if ( tl->list != NULL )
	{
		darr_clear_destroy( tl->list );
	}

	free(tl);
}


/*****************************************************************************/
int tl_size( token_list *tl )
{
	AS_NULL(tl)

	return darr_end( tl->list );
}


int tok2int( token *tok )
{
	AS_NULL(tok)

	char str[128];
	int sz = tok->e - tok->s;
	memcpy(str,tok->s,sz);
	str[sz] = 0;
	//printf("!%s %d\n",str,strtol(str,NULL,16));
	return strtol(str,NULL,16);
}


char *tok2str( token *tok )
{
	return NULL;
}