4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
24 __ALLOCATOR(struct token
, "token store", perm_token
);
25 ALLOCATOR(line
, "line of tokens");
27 static struct token
*copy_token(struct token
*token
)
31 new = __alloc_perm_token(0);
32 memcpy(new, token
, sizeof(*token
));
37 static struct line
*cursor
;
39 static void find_line(struct position pos
)
43 if (pos
.line
== cursor
->pos
.line
)
45 if (pos
.line
< cursor
->pos
.line
) {
48 cursor
= cursor
->prev
;
54 if (pos
.line
< cursor
->next
->pos
.line
)
56 cursor
= cursor
->next
;
60 static void insert_into_line(struct token
**current
, struct token
*new)
67 if (new->pos
.pos
< (*current
)->pos
.pos
) {
73 if (new->pos
.pos
== (*current
)->pos
.pos
)
76 insert_into_line(&(*current
)->next
, new);
79 static void store_token(struct token
*token
)
81 token
= copy_token(token
);
83 find_line(token
->pos
);
86 cursor
= __alloc_line(0);
87 cursor
->pos
= token
->pos
;
88 cursor
->token
= token
;
92 if (token
->pos
.line
< cursor
->pos
.line
) {
93 cursor
->prev
= __alloc_line(0);
94 cursor
->prev
->next
= cursor
;
95 cursor
= cursor
->prev
;
96 cursor
->pos
= token
->pos
;
97 cursor
->token
= token
;
101 if (token
->pos
.line
== cursor
->pos
.line
) {
102 insert_into_line(&cursor
->token
, token
);
106 cursor
->next
= __alloc_line(0);
107 cursor
->next
->prev
= cursor
;
108 cursor
= cursor
->next
;
109 cursor
->pos
= token
->pos
;
110 cursor
->token
= token
;
113 void store_all_tokens(struct token
*token
)
115 while (token_type(token
) != TOKEN_STREAMEND
) {
121 struct token
*pos_get_token(struct position pos
)
129 if (cursor
->pos
.stream
!= pos
.stream
)
132 if (cursor
->pos
.line
!= pos
.line
)
135 token
= cursor
->token
;
137 if (pos
.pos
== token
->pos
.pos
)
139 if (pos
.pos
< token
->pos
.pos
)
146 char *pos_ident(struct position pos
)
150 token
= pos_get_token(pos
);
153 if (token_type(token
) != TOKEN_IDENT
)
155 return token
->ident
->name
;