scoop:scoop.terminal:0.1.0
src/protocol/parser.scoop
Selected production source · src/protocol/parser.scoop
1
package scoop.terminal.protocol3
import scoop.collections.ArrayDeque4
import scoop.terminal.event.*5
import scoop.terminal.internal.input.*6
import scoop.time.Duration8
/**9
* A pure incremental terminal byte parser. Feed monotonically nondecreasing times so Escape10
* ambiguity can expire deterministically; parser methods perform no terminal I/O.11
*/12
public class InputParser public constructor(internal val escapeDelay: Duration = Duration.milliseconds(50L)) {13
internal var mode: InputMode = InputMode.Ground14
internal val pending: ArrayList<Byte> = ArrayList<Byte>()15
internal val output: ArrayDeque<InputItem> = ArrayDeque<InputItem>()16
internal var escapeStarted: Duration? = None17
internal var alt: Boolean = false18
internal var utf8Width: Long = 0L19
internal var raw: Boolean = true20
private var lastTime: Duration = Duration.ZERO21
private var ended: Boolean = false23
init {24
if (escapeDelay < Duration.ZERO) throw IllegalArgumentException(Some("negative Escape delay"))25
}27
/**28
* Whether a parsed item is ready to remove.29
*/30
public val hasPending: Boolean get() = output.size != 0L32
/**33
* Consumes a byte chunk at the supplied monotonic elapsed time and raw-mode interpretation.34
*/35
public fun feed(bytes: List<Byte>, now: Duration, rawMode: Boolean = true) {36
observeTime(now, false)37
raw = rawMode38
for (byte in bytes) acceptByte(byte.toInt() and 255, now)39
}41
/**42
* Advances parser time without new bytes and expires pending Escape ambiguity when due.43
*/44
public fun advance(now: Duration) { observeTime(now, true) }46
/**47
* Queues a resize event with nonnegative terminal dimensions.48
*/49
public fun feedResize(columns: Long, rows: Long) {50
if (ended) throw IllegalStateException(Some("input parser has ended"))51
if (columns < 0L || rows < 0L) throw IllegalArgumentException(Some("negative terminal size"))52
output.addLast(InputItem.User(Event.Resize(columns, rows)))53
}55
private fun observeTime(now: Duration, allowImmediate: Boolean) {56
if (ended) throw IllegalStateException(Some("input parser has ended"))57
if (now < lastTime) throw IllegalArgumentException(Some("terminal input time moved backwards"))58
lastTime = now59
if (escapeStarted != None && (allowImmediate || escapeDelay != Duration.ZERO)60
&& now - escapeStarted!! >= escapeDelay) expireEscape()61
}63
/**64
* The remaining Escape ambiguity delay, or None when no Escape sequence is pending.65
*/66
public val escapeRemaining: Duration? get() {67
if (escapeStarted == None) return None68
val elapsed = lastTime - escapeStarted!!69
return Some(if (elapsed >= escapeDelay) Duration.ZERO else escapeDelay - elapsed)70
}72
/**73
* Removes the next queued item, or None when the queue is empty.74
*/75
public fun next(): InputItem? = output.removeFirstOrNone()77
/**78
* Marks input ended, emitting a lone Escape key or an invalid incomplete sequence as needed.79
* Repeated finish is harmless.80
*/81
public fun finish() {82
if (ended) return83
if (mode == InputMode.Escape) output.addLast(userKey(KeyCode.Esc))84
else if (mode != InputMode.Ground) output.addLast(InputItem.Invalid("incomplete terminal input at EOF"))85
reset()86
ended = true87
}89
internal fun reset() {90
mode = InputMode.Ground91
pending.clear()92
escapeStarted = None93
alt = false94
utf8Width = 0L95
}97
internal fun emit(item: InputItem) {98
output.addLast(item)99
reset()100
}102
private fun expireEscape() {103
val item = when (mode) {104
case InputMode.Escape -> userKey(KeyCode.Esc)105
case InputMode.Csi -> if (pending.size == 0L)106
userKey(KeyCode.Char('['), KeyModifiers().with(KeyModifier.Alt))107
else InputItem.Invalid("incomplete terminal escape sequence")108
case InputMode.Ss3 -> userKey(KeyCode.Char('O'), KeyModifiers().with(KeyModifier.Alt))109
case _ -> InputItem.Invalid("incomplete terminal escape sequence")110
}111
emit(item)112
}113
}