package org.spoofax.jsglr2.inputstack;

import org.metaborg.parsetable.characterclasses.CharacterClassFactory;
import org.spoofax.jsglr2.parser.Position;

public class LayoutSensitiveInputStack extends InputStack {

    private static final int TAB_SIZE = 8;

    private int currentLine = 1;
    private int currentColumn = 1;
    public Position previousPosition = new Position(-1, 1, 0);

    public LayoutSensitiveInputStack(String inputString) {
        super(inputString);
    }

    public Position currentPosition() {
        return new Position(currentOffset, currentLine, currentColumn);
    }

    @Override public void next() {
        super.next();
        
        previousPosition = currentPosition();

        if(currentOffset < inputLength) {
            if(CharacterClassFactory.isNewLine(currentChar)) {
                currentLine++;
                currentColumn = 0;
            } else if(CharacterClassFactory.isCarriageReturn(currentChar)) {
            
            } else if(CharacterClassFactory.isTab(currentChar)) {
                currentColumn = (currentColumn / TAB_SIZE + 1) * TAB_SIZE;
            } else {
                currentColumn++;
            }
        }
        if(currentOffset == inputLength)
            currentColumn++;
    }

}

@@ -280,20 +280,16 @@
 rentLine
- = 1
 ;%0A    pr
@@ -315,12 +315,8 @@
 lumn
- = 1
 ;%0A  
@@ -353,33 +353,8 @@
 tion
- = new Position(-1, 1, 0)
 ;%0A%0A 
@@ -439,16 +439,220 @@
 tring);%0A
+        %0A        this.currentLine = 1;%0A        this.currentColumn = CharacterClassFactory.isNewLine(currentChar) ? 0 : 1;%0A        this.previousPosition = new Position(-1, currentLine, currentColumn - 1);%0A
     %7D%0A%0A 

Fix position calculation of second character when first character is a newline