]> git.sur5r.net Git - cc65/blob - src/ca65/toklist.c
More lineinfo usage.
[cc65] / src / ca65 / toklist.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 toklist.c                                 */
4 /*                                                                           */
5 /*                  Token list for the ca65 macroassembler                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* common */
39 #include "check.h"
40 #include "xmalloc.h"
41
42 /* ca65 */
43 #include "error.h"
44 #include "istack.h"
45 #include "nexttok.h"
46 #include "scanner.h"
47 #include "toklist.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Code                                    */
53 /*****************************************************************************/
54
55
56
57 TokNode* NewTokNode (void)
58 /* Create and return a token node with the current token value */
59 {
60
61     /* Allocate memory */
62     TokNode* N = xmalloc (sizeof (TokNode));
63
64     /* Initialize the token contents */
65     N->Next     = 0;
66     SB_Init (&N->T.SVal);
67     CopyToken (&N->T, &CurTok);
68
69     /* Return the node */
70     return N;
71 }
72
73
74
75 void FreeTokNode (TokNode* N)
76 /* Free the given token node */
77 {
78     SB_Done (&N->T.SVal);
79     xfree (N);
80 }
81
82
83
84 void TokSet (TokNode* N)
85 /* Set the scanner token from the given token node */
86 {
87     /* Set the values */
88     CopyToken (&CurTok, &N->T);
89     SB_Terminate (&CurTok.SVal);
90 }
91
92
93
94 enum TC TokCmp (const TokNode* N)
95 /* Compare the token given as parameter against the current token */
96 {
97     if (N->T.Tok != CurTok.Tok) {
98         /* Different token */
99         return tcDifferent;
100     }
101
102     /* If the token has string attribute, check it */
103     if (TokHasSVal (N->T.Tok)) {
104         if (SB_Compare (&CurTok.SVal, &N->T.SVal) != 0) {
105             return tcSameToken;
106         }
107     } else if (TokHasIVal (N->T.Tok)) {
108         if (N->T.IVal != CurTok.IVal) {
109             return tcSameToken;
110         }
111     }
112
113     /* Tokens are identical */
114     return tcIdentical;
115 }
116
117
118
119 void InitTokList (TokList* T)
120 /* Initialize a token list structure for later use */
121 {
122     /* Initialize the fields */
123     T->Next     = 0;
124     T->Root     = 0;
125     T->Last     = 0;
126     T->RepCount = 0;
127     T->RepMax   = 1;
128     T->Count    = 0;
129     T->Check    = 0;
130     T->Data     = 0;
131 }
132
133
134
135 TokList* NewTokList (void)
136 /* Create a new, empty token list */
137 {
138     /* Allocate memory for the list structure */
139     TokList* T = xmalloc (sizeof (TokList));
140
141     /* Initialize the fields */
142     InitTokList (T);
143
144     /* Return the new list */
145     return T;
146 }
147
148
149
150 void FreeTokList (TokList* List)
151 /* Delete the token list including all token nodes */
152 {
153     /* Free the token list */
154     TokNode* T = List->Root;
155     while (T) {
156         TokNode* Tmp = T;
157         T = T->Next;
158         FreeTokNode (Tmp);
159     }
160
161     /* If we have associated data, free it */
162     if (List->Data) {
163         xfree (List->Data);
164     }
165
166     /* Free the list structure itself */
167     xfree (List);
168 }
169
170
171
172 enum token_t GetTokListTerm (enum token_t Term)
173 /* Determine if the following token list is enclosed in curly braces. This is
174  * the case if the next token is the opening brace. If so, skip it and return
175  * a closing brace, otherwise return Term.
176  */
177 {
178     if (CurTok.Tok == TOK_LCURLY) {
179         NextTok ();
180         return TOK_RCURLY;
181     } else {
182         return Term;
183     }
184 }
185
186
187
188 void AddCurTok (TokList* List)
189 /* Add the current token to the token list */
190 {
191     /* Create a token node with the current token value */
192     TokNode* T = NewTokNode ();
193
194     /* Insert the node into the list */
195     if (List->Root == 0) {
196         List->Root = T;
197     } else {
198         List->Last->Next = T;
199     }
200     List->Last = T;
201
202     /* Count nodes */
203     List->Count++;
204 }
205
206
207
208 static int ReplayTokList (void* List)
209 /* Function that gets the next token from a token list and sets it. This
210  * function may be used together with the PushInput function from the istack
211  * module.
212  */
213 {
214     /* Cast the generic pointer to an actual list */
215     TokList* L = List;
216
217     /* Last may never be a NULL pointer, otherwise there's a bug in the code */
218     CHECK (L->Last != 0);
219
220     /* Set the next token from the list */
221     TokSet (L->Last);
222
223     /* If a check function is defined, call it, so it may look at the token
224      * just set and changed it as apropriate.
225      */
226     if (L->Check) {
227         L->Check (L);
228     }
229
230     /* Set the pointer to the next token */
231     L->Last = L->Last->Next;
232
233     /* If this was the last token, decrement the repeat counter. If it goes
234      * zero, delete the list and remove the function from the stack.
235      */
236     if (L->Last == 0) {
237         if (++L->RepCount >= L->RepMax) {
238             /* Done with this list */
239             FreeTokList (L);
240             PopInput ();
241         } else {
242             /* Replay one more time */
243             L->Last = L->Root;
244         }
245     }
246
247     /* We have a token */
248     return 1;
249 }
250
251
252
253 void PushTokList (TokList* List, const char* Desc)
254 /* Push a token list to be used as input for InputFromStack. This includes
255  * several initializations needed in the token list structure, so don't use
256  * PushInput directly.
257  */
258 {
259     /* If the list is empty, just delete it and bail out */
260     if (List->Count == 0) {
261         FreeTokList (List);
262         return;
263     }
264
265     /* Reset the last pointer to the first element */
266     List->Last = List->Root;
267
268     /* Insert the list specifying our input function */
269     PushInput (ReplayTokList, List, Desc);
270 }
271
272
273