increase parser perf by a lot

This commit is contained in:
alex_s168
2024-09-21 10:27:34 +00:00
parent 8c2325bdd3
commit f18798bb5c
7 changed files with 315 additions and 188 deletions

View File

@@ -18,7 +18,6 @@ dependencies {
// https://mvnrepository.com/artifact/org.json/json
implementation("org.json:json:20240303")
}
tasks.test {

View File

@@ -1,9 +1,16 @@
package blitz
class Either<A: Any, B: Any>(
val a: A?,
val b: B?
class Either<A: Any, B: Any> private constructor(
aIn: A?, bIn: B?
) {
/** DO NOT SET MANUALLY!!! */
@JvmField
var a: A? = aIn
/** DO NOT SET MANUALLY!!! */
@JvmField
var b: B? = bIn
override fun equals(other: Any?): Boolean =
other is Either<*, *> && other.a == a && other.b == b
@@ -13,14 +20,8 @@ class Either<A: Any, B: Any>(
fun assertB(): B =
(b ?: throw Exception("Value of Either is not of type B!"))
val isA: Boolean =
a != null
val isB: Boolean =
b != null
override fun toString(): String =
if (isA) "Either<A>(${a!!})"
if (isA()) "Either<A>(${a!!})"
else "Either<B>(${b!!})"
override fun hashCode(): Int {
@@ -30,14 +31,20 @@ class Either<A: Any, B: Any>(
}
companion object {
inline fun <A: Any, B: Any> ofA(a: A): Either<A, B> =
Either(a, null)
fun <A: Any, B: Any> unsafeCreate(a: A?, b: B?, pool: StupidObjPool<Either<*,*>>? = null): Either<A, B> =
Either(a, b)
inline fun <A: Any, B: Any> ofB(b: B): Either<A, B> =
Either(null, b)
inline fun <A: Any, B: Any> ofA(a: A, pool: StupidObjPool<Either<*,*>>? = null): Either<A, B> =
unsafeCreate(a, null, pool)
inline fun <A: Any, B: Any> ofB(b: B, pool: StupidObjPool<Either<*,*>>? = null): Either<A, B> =
unsafeCreate(null, b, pool)
}
}
inline fun <A: Any, B: Any> Either<A, B>.isA() = a != null
inline fun <A: Any, B: Any> Either<A, B>.isB() = b != null
inline fun <A: Any, B: Any> Either<A, B>.getAOr(prov: Provider<A>): A =
a ?: prov()
@@ -45,35 +52,35 @@ inline fun <A: Any, B: Any> Either<A, B>.getBOr(prov: Provider<B>): B =
b ?: prov()
inline fun <A: Any, B: Any, R> Either<A, B>.then(af: (A) -> R, bf: (B) -> R): R =
if (isA) af(a!!) else bf(b!!)
if (isA()) af(a!!) else bf(b!!)
inline fun <A: Any, B: Any, RA: Any> Either<A, B>.mapA(transform: (A) -> RA): Either<RA, B> =
Either(a?.let(transform), b)
Either.unsafeCreate(a?.let(transform), b)
inline fun <A: Any, B: Any> Either<A, B>.flatMapA(transform: (A) -> Either<A, B>): Either<A, B> =
if (a != null) {
transform(a)
transform(a!!)
} else this
inline fun <A: Any, B: Any> Either<A, B>.flatMapB(transform: (B) -> Either<A, B>): Either<A, B> =
if (b != null) {
transform(b)
transform(b!!)
} else this
@JvmName("flatMapA_changeType")
inline fun <A: Any, B: Any, RA: Any> Either<A, B>.flatMapA(transform: (A) -> Either<RA, B>): Either<RA, B> =
if (a != null) {
transform(a)
transform(a!!)
} else Either.ofB(b!!)
@JvmName("flatMapB_changeType")
inline fun <A: Any, B: Any, RB: Any> Either<A, B>.flatMapB(transform: (B) -> Either<A, RB>): Either<A, RB> =
if (b != null) {
transform(b)
transform(b!!)
} else Either.ofA(a!!)
inline fun <A: Any, B: Any, RB: Any> Either<A, B>.mapB(transform: (B) -> RB): Either<A, RB> =
Either(a, b?.let(transform))
Either.unsafeCreate(a, b?.let(transform))
fun <A, B, R: Any> Either<A, B>.flatten(): R where A: R, B: R =
a ?: assertB()

View File

@@ -0,0 +1,45 @@
package blitz
import blitz.collections.RefVec
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
/**
* all created objects are stored in this pool, and when you call [StupidObjPool.markClear], all objects are marked as free and can be re-used
* this is useful for when you need a ton of objects during a process and the process runs multiple times (caches the objects between executions)
*/
class StupidObjPool<T>(initialCap: Int) {
@JvmField val _objs = RefVec<T>(initialCap)
@JvmField var _nextFreeId = 0
/** only one of constructor or initializer is called */
@OptIn(ExperimentalContracts::class)
inline fun get(constructor: () -> T, initializer: (T) -> Unit): T {
contract {
callsInPlace(constructor, InvocationKind.AT_MOST_ONCE)
callsInPlace(initializer, InvocationKind.AT_MOST_ONCE)
}
if (_nextFreeId < _objs.size) {
val o = _objs[_nextFreeId++]
initializer(o)
return o
}
val o = constructor()
_objs.pushBack(o)
_nextFreeId ++
return o
}
fun clearPool() {
_objs.clear()
_nextFreeId = 0
}
fun markClear() {
_nextFreeId = 0
}
}

View File

@@ -1,75 +1,73 @@
package blitz.collections
import kotlin.system.measureTimeMillis
@Suppress("UNCHECKED_CAST")
class RefVec<T>(private val initCap: Int = 0): Vec<T> {
override var size = 0
private var cap = initCap
private var array: Array<Any?>? = if (initCap > 0) arrayOfNulls(initCap) else null
@JvmField var _cap = initCap
@JvmField var _array: Array<Any?>? = if (initCap > 0) arrayOfNulls(initCap) else null
override fun clear() {
size = 0
if (array == null) return
if (array!!.size <= initCap) {
cap = array!!.size
if (_array == null) return
if (_array!!.size <= initCap) {
_cap = _array!!.size
} else {
cap = 0
array = null
_cap = 0
_array = null
}
}
fun copyAsArray(): Array<Any?> =
array?.copyOfRange(0, size) ?: emptyArray()
inline fun copyAsArray(): Array<Any?> =
_array?.copyOfRange(0, size) ?: emptyArray()
fun copyIntoArray(arr: Array<Any?>, destOff: Int = 0, startOff: Int = 0) =
array?.copyInto(arr, destOff, startOff, size)
inline fun copyIntoArray(arr: Array<Any?>, destOff: Int = 0, startOff: Int = 0) =
_array?.copyInto(arr, destOff, startOff, size)
override fun copy(): RefVec<T> =
override inline fun copy(): RefVec<T> =
RefVec<T>(size).also {
it.array?.let { copyIntoArray(it) }
it._array?.let { copyIntoArray(it) }
}
override fun reserve(amount: Int) {
if (amount > 0 && cap - size >= amount)
if (amount > 0 && _cap - size >= amount)
return
if (array == null) {
cap = size + amount
array = arrayOfNulls(cap)
if (_array == null) {
_cap = size + amount
_array = arrayOfNulls(_cap)
} else {
array = array!!.copyOf(size + amount)
cap = size + amount
_array = _array!!.copyOf(size + amount)
_cap = size + amount
}
}
override fun reserve(need: Int, totalIfRealloc: Int) {
if (need > 0 && cap - size >= need)
if (need > 0 && _cap - size >= need)
return
if (array == null) {
cap = size + totalIfRealloc
array = arrayOfNulls(cap)
if (_array == null) {
_cap = size + totalIfRealloc
_array = arrayOfNulls(_cap)
} else {
array = array!!.copyOf(size + totalIfRealloc)
cap = size + totalIfRealloc
_array = _array!!.copyOf(size + totalIfRealloc)
_cap = size + totalIfRealloc
}
}
override fun popBack(): T =
array!![size - 1].also {
_array!![size - 1].also {
reserve(-1)
size --
} as T
override fun get(index: Int): T =
array!![index] as T
override inline fun get(index: Int): T =
(_array as Array<Any?>)[index] as T
override fun flip() {
array = array?.reversedArray()
_array = _array?.reversedArray()
}
override fun pushBack(elem: T) {
reserve(1, 8)
array!![size] = elem
this[size] = elem
size ++
}
@@ -80,7 +78,7 @@ class RefVec<T>(private val initCap: Int = 0): Vec<T> {
override fun next(): T {
if (!hasNext())
throw NoSuchElementException()
return array!![index++] as T
return _array!![index++] as T
}
}
@@ -88,13 +86,13 @@ class RefVec<T>(private val initCap: Int = 0): Vec<T> {
joinToString(prefix = "[", postfix = "]") { it.toString() }
override fun set(index: Int, value: T) {
array!![index] = value
(_array as Array<Any?>)[index] = value
}
companion object {
fun <T> from(data: Array<T>) =
RefVec<T>(data.size).also {
it.array?.let { data.copyInto(it) }
it._array?.let { data.copyInto(it) }
it.size += data.size
}
@@ -103,9 +101,9 @@ class RefVec<T>(private val initCap: Int = 0): Vec<T> {
data.forEach(bv::pushBack)
}
fun <T> of(vararg elements: T): RefVec<T> =
RefVec<T>(elements.size).also {
it.array?.let { elements.copyInto(it) }
inline fun <T> of(vararg elements: T): RefVec<T> =
RefVec<T>(elements.size shl 2).also {
it._array?.let { elements.copyInto(it) }
}
}
}

View File

@@ -1,37 +1,38 @@
package blitz.parse
import blitz.collections.RefVec
import blitz.parse.comb2.*
import org.json.JSONObject
import kotlin.math.min
import kotlin.system.measureNanoTime
object JSON {
val jsonBool: Parser<Char, Element> = choose {
it(mapValue(seq("true".toList())) { Element.newBool(true) })
it(mapValue(seq("false".toList())) { Element.newBool(false) })
}
val jsonNull: Parser<Char, Element> =
mapValue(seq("null".toList())) { Element.newNull() }
val jsonNum: Parser<Char, Element> =
mapValue(floatLit, Element::newNum)
val jsonString: Parser<Char, Element> =
mapValue(stringLit, Element::newStr)
val jsonElement = futureRec { jsonElement: Parser<Char, Element> ->
val jsonNum: Parser<Char, Element> =
mapValue(floatLit(), ::Number)
val jsonString: Parser<Char, Element> =
mapValue(stringLit(), ::Str)
val jsonArray: Parser<Char, Element> =
thenIgnore(
thenIgnore(
thenOverwrite(just('['),
mapValue(delimitedBy(jsonElement, just(',')))
{ Array(it.toList())}),
whitespaces()),
mapValue(delimitedBy(jsonElement, just(',')), Element::newArr)),
whitespaces),
just(']')
)
val jsonBool: Parser<Char, Element> = choose(
mapValue(seq("true".toList())) { Bool(true) },
mapValue(seq("false".toList())) { Bool(false) },
)
val jsonNull: Parser<Char, Element> =
mapValue(seq("null".toList())) { Nul() }
val jsonObj: Parser<Char, Element> =
mapValue(thenIgnore(thenIgnore(thenOverwrite(
just('{'),
@@ -40,74 +41,99 @@ object JSON {
thenIgnore(
thenIgnore(
thenOverwrite(
whitespaces(),
stringLit()),
whitespaces()),
whitespaces,
stringLit),
whitespaces),
just(':')),
jsonElement),
just(','))),
whitespaces()),
just('}'))) { Obj(it.toMap()) }
whitespaces),
just('}'))) { Element.newObj(it.toMap()) }
thenIgnore(thenOverwrite(
whitespaces(),
choose(
jsonArray,
jsonNum,
jsonString,
jsonObj,
jsonBool,
jsonNull
)),
whitespaces())
whitespaces,
choose {
it(jsonArray)
it(jsonNum)
it(jsonString)
it(jsonObj)
it(jsonBool)
it(jsonNull)
}),
whitespaces)
}
interface Element {
val arr get() = (this as Array).value
val num get() = (this as Number).value
val str get() = (this as Str).value
val obj get() = (this as Obj).value
val bool get() = (this as Bool).value
class Element(
@JvmField val kind: Int,
@JvmField val _boxed: Any? = null,
@JvmField val _num: Double = 0.0,
@JvmField val _bool: Boolean = false,
) {
companion object {
const val NUM = 0
const val BOOL = 1
const val NULL = 2
const val ARR = 3
const val STR = 4
const val OBJ = 5
fun isArr() = this is Array
fun isNum() = this is Number
fun isStr() = this is Str
fun isObj() = this is Obj
fun isBool() = this is Bool
fun isNul() = this is Nul
inline fun newNum(v: Double): Element =
Element(NUM, _num = v)
inline fun newBool(v: Boolean): Element =
Element(BOOL, _bool = v)
inline fun newNull(): Element =
Element(NULL)
inline fun newArr(v: RefVec<Element>): Element =
Element(ARR, _boxed = v)
inline fun newStr(v: String): Element =
Element(STR, _boxed = v)
inline fun newObj(v: Map<String, Element>): Element =
Element(OBJ, _boxed = v)
}
}
data class Array(val value: List<Element>): Element {
override fun toString(): String =
value.joinToString(separator = ", ", prefix = "[", postfix = "]")
inline fun Element.uncheckedAsNum(): Double =
_num
inline fun Element.uncheckedAsBool(): Boolean =
_bool
inline fun Element.uncheckedAsArr(): RefVec<Element> =
_boxed as RefVec<Element>
inline fun Element.uncheckedAsStr(): String =
_boxed as String
inline fun Element.uncheckedAsObj(): Map<String, Element> =
_boxed as Map<String, Element>
inline fun Element.asNum(): Double {
require(kind == Element.NUM) { "Element is not a Number" }
return _num
}
data class Number(val value: Double): Element {
override fun toString(): String =
value.toString()
inline fun Element.asBool(): Boolean {
require(kind == Element.BOOL) { "Element is not a Boolean" }
return _bool
}
data class Str(val value: String): Element {
override fun toString(): String =
"\"$value\""
inline fun Element.asArr(): RefVec<Element> {
require(kind == Element.ARR) { "Element is not an Array" }
return _boxed as RefVec<Element>
}
data class Obj(val value: Map<String, Element>): Element {
override fun toString(): String =
value.map { (k, v) -> "\"$k\": $v" }.joinToString(separator = ", ", prefix = "{", postfix = "}")
inline fun Element.asStr(): String {
require(kind == Element.STR) { "Element is not a String" }
return _boxed as String
}
data class Bool(val value: Boolean): Element {
override fun toString(): String =
value.toString()
inline fun Element.asObj(): Map<String, Element> {
require(kind == Element.OBJ) { "Element is not an Object" }
return _boxed as Map<String, Element>
}
class Nul: Element
fun parse(string: String): ParseResult<Element> =
jsonElement(ParseCtx(string.toList(), 0))
fun parse(string: String): ParseResult<Element> {
val ctx = ParseCtx(string.toList(), 0)
val v = jsonElement(ctx)
return v
}
}
fun main() {

View File

@@ -2,12 +2,11 @@ package blitz.parse.comb2
import blitz.*
import blitz.collections.RefVec
import blitz.collections.contents
import blitz.str.charsToString
data class ParseCtx<I>(
val input: List<I>,
var idx: Int
@JvmField val input: List<I>,
@JvmField var idx: Int
) {
fun loadFrom(old: ParseCtx<I>) {
idx = old.idx
@@ -15,25 +14,39 @@ data class ParseCtx<I>(
}
data class ParseError(
val loc: Int,
val message: String?,
@JvmField val loc: Int, /** can be -1 */
@JvmField val message: String?,
)
typealias ParseResult<O> = Either<O, RefVec<ParseError>>
typealias ParseResult<O> = Either<O, ParseError>
typealias Parser<I, O> = (ParseCtx<I>) -> ParseResult<O>
inline fun <I, M: Any, O: Any> mapValue(crossinline self: Parser<I, M>, crossinline fn: (M) -> O): Parser<I, O> =
{ self(it).mapA { fn(it) } }
{
val r = self(it) as Either<Any, ParseError>
r.a?.let {
r.a = fn(it as M)
}
r as Either<O, ParseError>
}
inline fun <I, O: Any> mapErrors(crossinline self: Parser<I, O>, crossinline fn: (RefVec<ParseError>) -> RefVec<ParseError>): Parser<I, O> =
{ self(it).mapB { fn(it) } }
inline fun <I, O: Any> mapErrors(crossinline self: Parser<I, O>, crossinline fn: (ParseError) -> ParseError): Parser<I, O> =
{
val r = self(it)
r.b?.let { r.b = fn(it) }
r
}
inline fun <I, M: Any, O: Any> then(crossinline self: Parser<I, M>, crossinline other: Parser<I, O>): Parser<I, Pair<M, O>> =
{ ctx ->
self(ctx).flatMapA<_,_,Pair<M,O>> { first ->
other.invoke(ctx)
.mapA { first to it }
}
val r0 = self(ctx) as ParseResult<Any>
r0.a?.let { first ->
val r1 = other(ctx)
r1.a?.let { second ->
(r1 as ParseResult<Any>).a = Pair(first, second)
(r1 as ParseResult<Pair<M, O>>)
} ?: (r1 as ParseResult<Pair<M, O>>)
} ?: (r0 as ParseResult<Pair<M, O>>)
}
inline fun <I, M: Any, O: Any> thenOverwrite(crossinline self: Parser<I, M>, crossinline other: Parser<I, O>): Parser<I, O> =
@@ -57,42 +70,59 @@ inline fun <I, O: Any> orElseVal(crossinline self: Parser<I, O>, value: O): Pars
inline fun <I, O, R: Any> orElse(crossinline self: Parser<I, O>, crossinline other: Parser<I, R>): Parser<I, R> where O: R =
{
val old = it.idx
self(it).mapB { err ->
self(it).mapB { _ ->
it.idx = old
other.invoke(it)
.mapB { err.pushBack(it); err }
}.partiallyFlattenB()
}
/** Use the other choose that takes a function whenever possible because of perf */
fun <I, O: Any> choose(possible: Iterable<Parser<I, O>>): Parser<I, O> =
{ ctx ->
val errors = RefVec<ParseError>(possible.count())
var res: O? = null
for (p in possible) {
val old = ctx.idx
val t = p.invoke(ctx)
if (t.isA) {
if (t.isA()) {
res = t.a!!
break
} else {
ctx.idx = old
errors.pushBack(t.b!!)
}
}
res?.let { Either.ofA(it) }
?: Either.ofB(errors)
?: Either.ofB(ParseError(ctx.idx, "none of the possible parsers match"))
}
fun <I, O: Any> choose(vararg possible: Parser<I, O>): Parser<I, O> =
inline fun <I, O: Any> choose(crossinline fn: (run: (Parser<I, O>) -> Unit) -> Unit): Parser<I, O> =
{ ctx ->
var res: O? = null
fn { p ->
if (res == null) {
val old = ctx.idx
val t = p.invoke(ctx)
if (t.isA()) {
res = t.a!!
} else {
ctx.idx = old
}
}
}
res?.let { Either.ofA(it) }
?: Either.ofB(ParseError(ctx.idx, "none of the possible parsers match"))
}
/** Use the other choose that takes a function whenever possible because of perf */
inline fun <I, O: Any> choose(vararg possible: Parser<I, O>): Parser<I, O> =
choose(possible.toList())
inline fun <I, O: Any> repeated(crossinline what: Parser<I, O>): Parser<I, RefVec<O>> =
{ ctx ->
val out = RefVec<O>(0)
val out = RefVec<O>(16)
while (true) {
val old = ctx.idx
val t = what(ctx)
if (t.isA) {
if (t.isA()) {
out.pushBack(t.a!!)
} else {
ctx.idx = old
@@ -107,7 +137,7 @@ inline fun <I, O: Any> repeatedNoSave(crossinline what: Parser<I, O>): Parser<I,
while (true) {
val old = ctx.idx
val t = what(ctx)
if (t.isB) {
if (t.isB()) {
ctx.idx = old
break
}
@@ -118,7 +148,7 @@ inline fun <I, O: Any> repeatedNoSave(crossinline what: Parser<I, O>): Parser<I,
inline fun <I, O: Any> verifyValue(crossinline self: Parser<I, O>, crossinline verif: (O) -> String?): Parser<I, O> =
{ ctx ->
self(ctx).flatMapA<_,_,_> {
verif(it)?.let { Either.ofB(RefVec.of(ParseError(ctx.idx, it))) }
verif(it)?.let { Either.ofB(ParseError(ctx.idx, it)) }
?: Either.ofA(it)
}
}
@@ -126,7 +156,7 @@ inline fun <I, O: Any> verifyValue(crossinline self: Parser<I, O>, crossinline v
inline fun <I, O: Any> verifyValueWithSpan(crossinline self: Parser<I, Pair<IntRange, O>>, crossinline fn: (O) -> String?): Parser<I, O> =
{ ctx ->
self(ctx).flatMapA<_,_,_> { (span, v) ->
fn(v)?.let { Either.ofB(RefVec.of(ParseError(span.first, it))) }
fn(v)?.let { Either.ofB(ParseError(span.first, it)) }
?: Either.ofA(v)
}
}
@@ -137,7 +167,7 @@ inline fun <I, O: Any> location(crossinline fn: (Int) -> O): Parser<I, O> =
inline fun <I> location(): Parser<I, Int> =
location { it }
inline fun <I, O: Any> withSpan(crossinline p: Parser<I, O>): Parser<I, Pair<IntRange, O>> =
fun <I, O: Any> withSpan(p: Parser<I, O>): Parser<I, Pair<IntRange, O>> =
mapValue(then(then(location(), p), location())) { (beginAndV, end) ->
(beginAndV.first..end) to beginAndV.second
}
@@ -148,17 +178,17 @@ inline fun <I, O: Any> value(value: O): Parser<I, O> =
fun <I, O: Any> chain(parsers: List<Parser<I, O>>): Parser<I, RefVec<O>> =
{ ctx ->
val results = RefVec<O>(parsers.size)
val errs = RefVec<ParseError>(0)
var errs: ParseError? = null
for (p in parsers) {
val r = p.invoke(ctx)
if (r.isA) {
if (r.isA()) {
results.pushBack(r.a!!)
} else {
errs.pushBack(r.b!!)
errs = r.b!!
break
}
}
if (errs.size != 0) Either.ofB(errs)
if (errs != null) Either.ofB(errs)
else Either.ofA(results)
}
@@ -168,19 +198,34 @@ inline fun <I: Any> seq(want: List<I>): Parser<I, RefVec<I>> =
inline fun <I: Any> filter(msg: String, crossinline filter: (I) -> Boolean): Parser<I, I> =
{ ctx ->
if (ctx.idx >= ctx.input.size) {
Either.ofB(RefVec.of(ParseError(ctx.idx, "unexpected end of file")))
Either.ofB(ParseError(ctx.idx, "unexpected end of file"))
} else {
val i = ctx.input[ctx.idx++]
if (filter(i)) Either.ofA(i)
else Either.ofB(RefVec.of(ParseError(ctx.idx - 1, msg)))
else Either.ofB(ParseError(ctx.idx - 1, msg))
}
}
inline fun <I: Any> just(want: I): Parser<I, I> =
filter("expected $want") { it == want }
private class JustParse<I: Any>(wantIn: I): Parser<I, I> {
@JvmField val want = wantIn
@JvmField val uef: ParseResult<I> = Either.ofB(ParseError(-1, "unexpected end of file"))
@JvmField val exdiff: ParseResult<I> = Either.ofB(ParseError(-1, "expected $wantIn"))
@JvmField val eitherOfWant: ParseResult<I> = Either.ofA(want)
override fun invoke(ctx: ParseCtx<I>): ParseResult<I> {
return if (ctx.idx >= ctx.input.size) uef
else {
val i = ctx.input[ctx.idx++]
if (i == want) eitherOfWant
else exdiff
}
}
}
fun <I: Any> just(wantIn: I): Parser<I, I> =
JustParse(wantIn)
inline fun <I: Any> oneOf(possible: Iterable<I>): Parser<I, I> =
filter("expected one of ${possible.contents}") { it in possible }
filter("expected different") { it in possible }
inline fun <I, O: Any> future(crossinline prov: Provider<Parser<I, O>>): Parser<I, O> =
{ prov()(it) }
@@ -197,9 +242,9 @@ fun <O: Any> regex(pattern: Regex, fn: (groups: MatchGroupCollection) -> O): Par
pattern.matchAt(ctx.input.charsToString(), ctx.idx)?.let {
ctx.idx = it.range.last + 1
Either.ofA(fn(it.groups))
} ?: Either.ofB(RefVec.of(
} ?: Either.ofB(
ParseError(ctx.idx, "regular expression \"$pattern\" does not apply")
))
)
}
fun regex(pattern: Regex) = regex(pattern) { it[0]!!.value }

View File

@@ -5,30 +5,33 @@ import blitz.str.charsToString
import kotlin.math.absoluteValue
import kotlin.math.sign
fun whitespaces(): Parser<Char, Unit> =
repeatedNoSave(oneOf("\n\t\r\b ".toList()))
private fun isWhitespace(it: Char) =
it == ' ' || it == '\n' || it == '\t' || it == '\r' || it == '\b'
val whitespaces: Parser<Char, Unit> =
repeatedNoSave(filter("expected whitespace", ::isWhitespace))
fun digit(): Parser<Char, Char> =
oneOf("0123456789".toList())
val digit: Parser<Char, Char> =
filter("expected digit") { it >= '0' && it <= '9' }
fun uintLit(): Parser<Char, RefVec<Char>> =
verifyValueWithSpan(withSpan(repeated(digit())))
val uintLit: Parser<Char, RefVec<Char>> =
verifyValueWithSpan(withSpan(repeated(digit)))
{ if (it.size == 0) "need digits after sign in num lit" else null }
fun intLit(): Parser<Char, Int> =
mapValue(then(choose(mapValue(just('+')) { +1 },
mapValue(just('-')) { -1 },
value(+1)),
uintLit()))
val intLit: Parser<Char, Int> =
mapValue(then(choose<Char,Int> {
it(mapValue(just('+')) { +1 })
it(mapValue(just('-')) { -1 })
it(value(+1))
}, uintLit))
{ (sign, v) -> sign * v.charsToString().toInt() }
fun floatLit(): Parser<Char, Double> =
val floatLit: Parser<Char, Double> =
mapValue(
then(
thenIgnore(
intLit(),
intLit,
just('.')),
orElseVal(uintLit(), RefVec.of('0'))))
orElseVal(uintLit, RefVec.of('0'))))
{ (pre, post) ->
var p = post.charsToString().toDouble()
while (p.absoluteValue >= 1) {
@@ -38,22 +41,26 @@ fun floatLit(): Parser<Char, Double> =
(pre.toDouble().absoluteValue + p) * pre.toDouble().sign
}
fun escapeChar(): Parser<Char, Char> =
val escapeChar: Parser<Char, Char> =
thenOverwrite(just('\\'),
mapErrors(choose(just('"'),
just('\''),
just('\\'),
mapValue(just('n')) { '\n' },
mapValue(just('r')) { '\r' },
mapValue(just('b')) { '\b' },
mapValue(just('t')) { '\t' }))
{ RefVec.of(ParseError(it[0].loc, "invalid escape sequence")) }
mapErrors(choose {
it(just('"'))
it(just('\''))
it(just('\\'))
it(mapValue(just('n')) { '\n' })
it(mapValue(just('r')) { '\r' })
it(mapValue(just('b')) { '\b' })
it(mapValue(just('t')) { '\t' })
})
{ ParseError(it.loc, "invalid escape sequence") }
)
fun stringLit(): Parser<Char, String> =
val stringLit: Parser<Char, String> =
mapValue(thenIgnore(then(just('"'),
repeated(choose(escapeChar(),
filter("a") { it != '"' }))),
repeated(choose<Char,Char>{
it(escapeChar)
it(filter("a") { it != '"' })
})),
just('"')))
{ (_, str) -> str.charsToString() }