1 /*****************************************************************************/
5 /* Token list for the ca65 macroassembler */
9 /* (C) 1998-2010, 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 */
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);
101 enum TC TokCmp (const TokNode* T)
102 /* Compare the token given as parameter against the current token */
105 /* Different token */
109 /* If the token has string attribute, check it */
110 if (TokHasSVal (T->Tok)) {
111 if (SB_Compare (&SVal, &T->SVal) != 0) {
114 } else if (TokHasIVal (T->Tok)) {
115 if (T->IVal != IVal) {
120 /* Tokens are identical */
126 void InitTokList (TokList* T)
127 /* Initialize a token list structure for later use */
129 /* Initialize the fields */
142 TokList* NewTokList (void)
143 /* Create a new, empty token list */
145 /* Allocate memory for the list structure */
146 TokList* T = xmalloc (sizeof (TokList));
148 /* Initialize the fields */
151 /* Return the new list */
157 void FreeTokList (TokList* List)
158 /* Delete the token list including all token nodes */
160 /* Free the token list */
161 TokNode* T = List->Root;
168 /* If we have associated data, free it */
173 /* Free the list structure itself */
179 enum Token GetTokListTerm (enum Token Term)
180 /* Determine if the following token list is enclosed in curly braces. This is
181 * the case if the next token is the opening brace. If so, skip it and return
182 * a closing brace, otherwise return Term.
185 if (Tok == TOK_LCURLY) {
195 void AddCurTok (TokList* List)
196 /* Add the current token to the token list */
198 /* Create a token node with the current token value */
199 TokNode* T = NewTokNode ();
201 /* Insert the node into the list */
202 if (List->Root == 0) {
205 List->Last->Next = T;
215 static int ReplayTokList (void* List)
216 /* Function that gets the next token from a token list and sets it. This
217 * function may be used together with the PushInput function from the istack
221 /* Cast the generic pointer to an actual list */
224 /* Last may never be a NULL pointer, otherwise there's a bug in the code */
225 CHECK (L->Last != 0);
227 /* Set the next token from the list */
230 /* If a check function is defined, call it, so it may look at the token
231 * just set and changed it as apropriate.
237 /* Set the pointer to the next token */
238 L->Last = L->Last->Next;
240 /* If this was the last token, decrement the repeat counter. If it goes
241 * zero, delete the list and remove the function from the stack.
244 if (++L->RepCount >= L->RepMax) {
245 /* Done with this list */
249 /* Replay one more time */
254 /* We have a token */
260 void PushTokList (TokList* List, const char* Desc)
261 /* Push a token list to be used as input for InputFromStack. This includes
262 * several initializations needed in the token list structure, so don't use
263 * PushInput directly.
266 /* If the list is empty, just delete it and bail out */
267 if (List->Count == 0) {
272 /* Reset the last pointer to the first element */
273 List->Last = List->Root;
275 /* Insert the list specifying our input function */
276 PushInput (ReplayTokList, List, Desc);