[go: up one dir, main page]

Menu

[897c37]: / lpc / lexer.c  Maximize  Restore  History

Download this file

155 lines (134 with data), 3.4 kB

  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
#include <stdlib.h>
#include <memory.h>
#include "lpc.h"
#include "lexer.h"
/*
* Lexical string space (designed for fast addition of strings!)
* ok - limited to no more than 1 Meg of strings while compiling..
* that should be plenty!
*/
#define MAX_BLOCKS 80
#define BLOCK_SIZE 30000
#define DONT_FREE 1
char *lex_string_space[MAX_BLOCKS];
int LSP = 0, LSP_where = 0, LSP_max = 1;
int lex_boundary = DONT_FREE;
void init_lex_space()
{
int i;
for (i = 0; i < MAX_BLOCKS; i++)
lex_string_space[i] = 0;
for (i = 0; i < lex_boundary; i++)
lex_string_space[i] = (char *) malloc(BLOCK_SIZE);
}
void free_lex_space()
{
for (; LSP >= lex_boundary; LSP--)
{
free(lex_string_space[LSP]);
lex_string_space[LSP] = 0;
}
LSP_where = 0;
}
lexar * lex_str_copy(char *s)
{
int l;
lexar *ret;
l = strlen(s);
if (l > BLOCK_SIZE)
fatal("HUGE string found in lex_str_copy.\n");
if (BLOCK_SIZE - LSP_where < l)
{
LSP++;
if (LSP > LSP_max)
LSP_max = LSP;
if (LSP == MAX_BLOCKS)
fatal("No string space in lexer.\n");
if (!lex_string_space[LSP])
{
lex_string_space[LSP] = (char *) malloc(BLOCK_SIZE);
}
LSP_where = 0;
}
strncpy(&(lex_string_space[LSP][LSP_where]), s, l);
ret = (lexar *) malloc(sizeof(lexar));
ret->length = l;
ret->s = &(lex_string_space[LSP][LSP_where]);
LSP_where += l;
return ret;
}
/* concatenates onto a */
void lex_strcat(lexar * a, lexar * b)
{
lexar *y, *z;
Shared *t1, *t2;
if ((a->length + (int) a->s) == (int) b->s)
{
/* already lined up */
/*printf("lexstr cat lined up\n"); */
a->length += b->length;
free(b);
return;
}
/* this occurs if we crossed a block boundary */
/* this COULD be a source of error for huge strings */
/* 2 lex_str_copies are done so the strings are sequential */
/* in lex string space */
y = lex_str_copy((t1 = to_Cstring(a))->str);
z = lex_str_copy((t2 = to_Cstring(b))->str);
a->s = y->s;
a->length += z->length;
free(y); /* free the lexars */
free(z);
free(b); /* free the joined one? */
free_string(t1); /* free the temporary shared strings */
free_string(t2);
}
char * lex_Cstr_copy(char *s)
{
int l;
char *ret;
l = strlen(s);
if (l > BLOCK_SIZE)
fatal("HUGE string found in lex_Cstr_copy.\n");
if (BLOCK_SIZE - LSP_where <= l)
{
LSP++;
if (LSP > LSP_max)
LSP_max = LSP;
if (LSP == MAX_BLOCKS)
fatal("No string space in lexer.\n");
if (!lex_string_space[LSP])
{
lex_string_space[LSP] = (char *) malloc(BLOCK_SIZE);
}
LSP_where = 0;
}
strcpy(&(lex_string_space[LSP][LSP_where]), s);
ret = &(lex_string_space[LSP][LSP_where]);
LSP_where += l + 1;
return ret;
}
Shared * to_Cstring(lexar * s)
{
char *t;
Shared *n;
t = (char *) malloc(s->length + 1);
strncpy(t, s->s, s->length);
t[s->length] = '\0';
n = string_copy(t);
free(t);
// free(s);
return n;
}
char * to_str(lexar * s)
{
char * t;
char * n;
t = (char *) malloc(s->length + 1);
strncpy(t, s->s, s->length);
t[s->length] = '\0';
n = str_copy(t);
free(t);
return n;
}