4 * Copyright (C) 2012 Oracle.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 __ALLOCATOR(struct token
, "token store", perm_token
);
40 ALLOCATOR(line
, "line of tokens");
42 static struct token
*copy_token(struct token
*token
)
46 new = __alloc_perm_token(0);
47 memcpy(new, token
, sizeof(*token
));
52 static struct line
*cursor
;
54 static void find_line(struct position pos
)
58 if (pos
.line
== cursor
->pos
.line
)
60 if (pos
.line
< cursor
->pos
.line
) {
63 cursor
= cursor
->prev
;
69 if (pos
.line
< cursor
->next
->pos
.line
)
71 cursor
= cursor
->next
;
75 static void insert_into_line(struct token
**current
, struct token
*new)
82 if (new->pos
.pos
< (*current
)->pos
.pos
) {
88 if (new->pos
.pos
== (*current
)->pos
.pos
)
91 insert_into_line(&(*current
)->next
, new);
94 static void store_token(struct token
*token
)
96 token
= copy_token(token
);
98 find_line(token
->pos
);
101 cursor
= __alloc_line(0);
102 cursor
->pos
= token
->pos
;
103 cursor
->token
= token
;
107 if (token
->pos
.line
< cursor
->pos
.line
) {
108 cursor
->prev
= __alloc_line(0);
109 cursor
->prev
->next
= cursor
;
110 cursor
= cursor
->prev
;
111 cursor
->pos
= token
->pos
;
112 cursor
->token
= token
;
116 if (token
->pos
.line
== cursor
->pos
.line
) {
117 insert_into_line(&cursor
->token
, token
);
121 cursor
->next
= __alloc_line(0);
122 cursor
->next
->prev
= cursor
;
123 cursor
= cursor
->next
;
124 cursor
->pos
= token
->pos
;
125 cursor
->token
= token
;
128 void store_all_tokens(struct token
*token
)
130 while (token_type(token
) != TOKEN_STREAMEND
) {
136 struct token
*first_token_from_line(struct position pos
)
143 if (cursor
->pos
.stream
!= pos
.stream
)
145 if (cursor
->pos
.line
!= pos
.line
)
148 return cursor
->token
;
151 struct token
*pos_get_token(struct position pos
)
155 token
= first_token_from_line(pos
);
157 if (pos
.pos
== token
->pos
.pos
)
159 if (pos
.pos
< token
->pos
.pos
)
166 char *pos_ident(struct position pos
)
170 token
= pos_get_token(pos
);
173 if (token_type(token
) != TOKEN_IDENT
)
175 return token
->ident
->name
;