+#include <stdio.h>
 #include <string.h>
 
 /* common */
 #include "xmalloc.h"
 
 /* ca65 */
+#include "filetab.h"
 #include "global.h"
 #include "lineinfo.h"
 #include "objfile.h"
 
 
 
+#if 0
+static void DumpLineInfos (const char* Title, const Collection* C)
+/* Dump line infos from the given collection */
+{
+    unsigned I;
+    fprintf (stderr, "%s:\n", Title);
+    for (I = 0; I < CollCount (C); ++I) {
+        const LineInfo* LI = CollConstAt (C, I);
+        const char* Type;
+        switch (GetLineInfoType (LI)) {
+            case LI_TYPE_ASM:           Type = "ASM";           break;
+            case LI_TYPE_EXT:           Type = "EXT";           break;
+            case LI_TYPE_MACRO:         Type = "MACRO";         break;
+            case LI_TYPE_MACPARAM:      Type = "MACPARAM";      break;
+            default:                    Type = "unknown";       break;
+        }
+        fprintf (stderr,
+                 "%2u: %-8s %2u %-16s %u/%u\n",
+                 I, Type, LI->Key.Pos.Name,
+                 SB_GetConstBuf (GetFileName (LI->Key.Pos.Name)),
+                 LI->Key.Pos.Line, LI->Key.Pos.Col);
+    }
+}
+#endif
+
+
+
 void InitLineInfo (void)
 /* Initialize the line infos */
 {
 
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2011, Ullrich von Bassewitz                                      */
+/* (C) 1998-2012, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
 
 
 
+/*****************************************************************************/
+/*                                   Data                                    */
+/*****************************************************************************/
+
+
+
+/* Number of currently pushed token lists */
+static unsigned PushCounter = 0;
+
+
+
 /*****************************************************************************/
 /*                                          Code                                    */
 /*****************************************************************************/
 
 
 
-void InitTokList (TokList* T)
-/* Initialize a token list structure for later use */
+TokList* NewTokList (void)
+/* Create a new, empty token list */
 {
+    /* Allocate memory for the list structure */
+    TokList* T = xmalloc (sizeof (TokList));
+
     /* Initialize the fields */
     T->Next    = 0;
     T->Root    = 0;
     T->Count   = 0;
     T->Check   = 0;
     T->Data    = 0;
-}
-
-
-
-TokList* NewTokList (void)
-/* Create a new, empty token list */
-{
-    /* Allocate memory for the list structure */
-    TokList* T = xmalloc (sizeof (TokList));
-
-    /* Initialize the fields */
-    InitTokList (T);
+    T->LI       = 0;
 
     /* Return the new list */
     return T;
        FreeTokNode (Tmp);
     }
 
+    /* Free associated line info */
+    if (List->LI) {
+        EndLine (List->LI);
+    }
+
     /* If we have associated data, free it */
     if (List->Data) {
        xfree (List->Data);
     /* Cast the generic pointer to an actual list */
     TokList* L = List;
 
-    /* Last may never be a NULL pointer, otherwise there's a bug in the code */
-    CHECK (L->Last != 0);
+    /* If there are no more tokens, decrement the repeat counter. If it goes
+     * zero, delete the list and remove the function from the stack.
+     */
+    if (L->Last == 0) {
+       if (++L->RepCount >= L->RepMax) {
+           /* Done with this list */
+           FreeTokList (L);
+            --PushCounter;
+           PopInput ();
+            return 0;
+       } else {
+           /* Replay one more time */
+           L->Last = L->Root;
+       }
+    }
 
     /* Set the next token from the list */
     TokSet (L->Last);
 
     /* Set the line info for the new token */
-    NewAsmLine ();
+    if (L->LI) {
+        EndLine (L->LI);
+    }
+    L->LI = StartLine (&CurTok.Pos, LI_TYPE_ASM, PushCounter);
 
     /* If a check function is defined, call it, so it may look at the token
      * just set and changed it as apropriate.
     /* Set the pointer to the next token */
     L->Last = L->Last->Next;
 
-    /* If this was the last token, decrement the repeat counter. If it goes
-     * zero, delete the list and remove the function from the stack.
-     */
-    if (L->Last == 0) {
-       if (++L->RepCount >= L->RepMax) {
-           /* Done with this list */
-           FreeTokList (L);
-           PopInput ();
-       } else {
-           /* Replay one more time */
-           L->Last = L->Root;
-       }
-    }
-
     /* We have a token */
     return 1;
 }
     List->Last = List->Root;
 
     /* Insert the list specifying our input function */
+    ++PushCounter;
     PushInput (ReplayTokList, List, Desc);
 }
 
 
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2011, Ullrich von Bassewitz                                      */
+/* (C) 2000-2012, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
 #include "strbuf.h"
 
 /* ca65 */
+#include "lineinfo.h"
 #include "scanner.h"
 
 
 
 /*****************************************************************************/
-/*                                          Data                                    */
+/*                                          Data                                    */
 /*****************************************************************************/
 
 
     unsigned   Count;                  /* Token count */
     void       (*Check)(TokList*);     /* Token check function */
     void*              Data;                   /* Additional data for check */
+    LineInfo*   LI;                     /* Line info for replay */
 };
 
 
 enum TC TokCmp (const TokNode* N);
 /* Compare the token given as parameter against the current token */
 
-void InitTokList (TokList* T);
-/* Initialize a token list structure for later use */
-
 TokList* NewTokList (void);
 /* Create a new, empty token list */