-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathps_tokstrm.c
76 lines (63 loc) · 1.53 KB
/
ps_tokstrm.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
/*
* Parser token stream.
* Pulls lines from preprocessor if needed, maintain a logical stream of tokens.
* State only applies for a single translation unit.
*/
int TKUCC_TokStrmGetMark(TKUCC_MainContext *ctx)
{
return(ctx->tokstrm_idx);
}
int TKUCC_TokStrmSetMark(TKUCC_MainContext *ctx, int idx)
{
ctx->tokstrm_idx=idx;
return(0);
}
int TKUCC_TokStrmAddTokenEnd(TKUCC_MainContext *ctx, char *tok)
{
int strix, tkidx;
strix=TKUCC_InternTokenToIndex(ctx, tok, TKUCC_ZID_TOKSTRM);
tkidx=ctx->tokstrm_max++;
if(!ctx->tokstrm_span[tkidx>>12])
{
ctx->tokstrm_span[tkidx>>12]=tkucc_malloc(4096*sizeof(int));
}
ctx->tokstrm_span[tkidx>>12][tkidx&4095]=strix;
return(tkidx);
}
char *TKUCC_TokStrmGetToken(TKUCC_MainContext *ctx, int idx)
{
TKUCC_LineBuf *line;
char **a;
int i;
while(idx>=ctx->tokstrm_max)
{
line=TKUCC_PpGetProcessedLines(ctx);
if(!line)
break;
a=TKUCC_TokSplitLine(line->text);
for(i=0; a[i]; i++)
TKUCC_TokStrmAddTokenEnd(ctx, a[i]);
TKUCC_FreeLine(ctx, line);
}
if(idx>=ctx->tokstrm_max)
return(NULL);
i=ctx->tokstrm_span[idx>>12][idx&4095];
return(TKUCC_InternIndexToString(ctx, i));
}
char *TKUCC_TokStrmPeekToken(TKUCC_MainContext *ctx, int relidx)
{
int idx;
idx=ctx->tokstrm_idx+relidx;
return(TKUCC_TokStrmGetToken(ctx, idx));
}
void TKUCC_TokStrmStepToken(TKUCC_MainContext *ctx, int relidx)
{
ctx->tokstrm_idx+=relidx;
}
char *TKUCC_TokStrmNextToken(TKUCC_MainContext *ctx)
{
char *str;
str=TKUCC_TokStrmPeekToken(ctx, 0);
TKUCC_TokStrmStepToken(ctx, 1);
return(str);
}