All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tmarsteel.ktprolog.parser.lexer.SourceLocationAwareCharIterator.kt Maven / Gradle / Ivy

The newest version!
package com.github.tmarsteel.ktprolog.parser.lexer

import com.github.tmarsteel.ktprolog.parser.source.SourceLocation

class SourceLocationAwareCharIterator(private val initial: SourceLocation, private val source: Iterator) : Iterator> {
    private var currentLine = initial.line
    private var currentColumn = initial.column
    private var sourceIndex = initial.sourceIndex

    private val currentLocation: SourceLocation
        get() = SourceLocation(initial.unit, currentLine, currentColumn, sourceIndex)

    override fun hasNext() = source.hasNext()

    override fun next(): Pair {
        val char = source.next()
        val location = currentLocation

        if (char == '\n') {
            currentLine++
            currentColumn = 1
        }
        else {
            currentColumn++
        }
        sourceIndex++

        return Pair(char, location)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy