1 /*****************************************************************************/
5 /* Token list for the ca65 macroassembler */
9 /* (C) 1998-2008, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 TokNode* NewTokNode (void)
58 /* Create and return a token node with the current token value */
63 T = xmalloc (sizeof (TokNode));
65 /* Initialize the token contents */
70 T->SVal = AUTO_STRBUF_INITIALIZER;
71 SB_Copy (&T->SVal, &SVal);
79 void FreeTokNode (TokNode* T)
80 /* Free the given token node */
88 void TokSet (TokNode* T)
89 /* Set the scanner token from the given token node */
95 SB_Copy (&SVal, &T->SVal);
100 enum TC TokCmp (const TokNode* T)
101 /* Compare the token given as parameter against the current token */
104 /* Different token */
108 /* If the token has string attribute, check it */
109 if (TokHasSVal (T->Tok)) {
110 if (SB_Compare (&SVal, &T->SVal) != 0) {
113 } else if (TokHasIVal (T->Tok)) {
114 if (T->IVal != IVal) {
119 /* Tokens are identical */
125 void InitTokList (TokList* T)
126 /* Initialize a token list structure for later use */
128 /* Initialize the fields */
141 TokList* NewTokList (void)
142 /* Create a new, empty token list */
144 /* Allocate memory for the list structure */
145 TokList* T = xmalloc (sizeof (TokList));
147 /* Initialize the fields */
150 /* Return the new list */
156 void FreeTokList (TokList* List)
157 /* Delete the token list including all token nodes */
159 /* Free the token list */
160 TokNode* T = List->Root;
167 /* If we have associated data, free it */
172 /* Free the list structure itself */
178 enum Token GetTokListTerm (enum Token Term)
179 /* Determine if the following token list is enclosed in curly braces. This is
180 * the case if the next token is the opening brace. If so, skip it and return
181 * a closing brace, otherwise return Term.
184 if (Tok == TOK_LCURLY) {
194 void AddCurTok (TokList* List)
195 /* Add the current token to the token list */
197 /* Create a token node with the current token value */
198 TokNode* T = NewTokNode ();
200 /* Insert the node into the list */
201 if (List->Root == 0) {
204 List->Last->Next = T;
214 static int ReplayTokList (void* List)
215 /* Function that gets the next token from a token list and sets it. This
216 * function may be used together with the PushInput function from the istack
220 /* Cast the generic pointer to an actual list */
223 /* Last may never be a NULL pointer, otherwise there's a bug in the code */
224 CHECK (L->Last != 0);
226 /* Set the next token from the list */
229 /* If a check function is defined, call it, so it may look at the token
230 * just set and changed it as apropriate.
236 /* Set the pointer to the next token */
237 L->Last = L->Last->Next;
239 /* If this was the last token, decrement the repeat counter. If it goes
240 * zero, delete the list and remove the function from the stack.
243 if (++L->RepCount >= L->RepMax) {
244 /* Done with this list */
248 /* Replay one more time */
253 /* We have a token */
259 void PushTokList (TokList* List, const char* Desc)
260 /* Push a token list to be used as input for InputFromStack. This includes
261 * several initializations needed in the token list structure, so don't use
262 * PushInput directly.
265 /* If the list is empty, just delete it and bail out */
266 if (List->Count == 0) {
271 /* Reset the last pointer to the first element */
272 List->Last = List->Root;
274 /* Insert the list specifying our input function */
275 PushInput (ReplayTokList, List, Desc);