mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 05:10:55 +00:00
Validate token soundness.
This commit is contained in:
parent
ce1e262c05
commit
624b05605a
@ -15,17 +15,33 @@ fun eval(ast: Program) {
|
|||||||
println("> ${scope.call("main", Arguments.Zero)}")
|
println("> ${scope.call("main", Arguments.Zero)}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun validateTokenSoundness(input: String, stream: TokenStream) {
|
||||||
|
var expectedIndex = 0
|
||||||
|
for (token in stream.tokens) {
|
||||||
|
if (token.start != expectedIndex) {
|
||||||
|
throw RuntimeException("Expected token to be at index $expectedIndex but was ${token.start}")
|
||||||
|
}
|
||||||
|
val slice = input.slice(token.start until token.start + token.text.length)
|
||||||
|
if (slice != token.text) {
|
||||||
|
throw RuntimeException(
|
||||||
|
"Expected index ${token.start} for length ${token.text.length} to" +
|
||||||
|
" equal '${token.text}' but was '$slice'")
|
||||||
|
}
|
||||||
|
expectedIndex += token.text.length
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
val code = Path(args[0]).readText()
|
val code = Path(args[0]).readText()
|
||||||
val stream = tokenize(code).excludeAllWhitespace()
|
val stream = tokenize(code)
|
||||||
println(stream.tokens.joinToString("\n"))
|
println(stream.tokens.joinToString("\n"))
|
||||||
val program = parse(stream)
|
val program = parse(stream)
|
||||||
eval(program)
|
eval(program)
|
||||||
|
|
||||||
val exactStream = tokenize(code)
|
val exactCode = stream.tokens.joinToString("") { it.text }
|
||||||
val exactCode = exactStream.tokens.joinToString("") { it.text }
|
|
||||||
println(exactCode)
|
println(exactCode)
|
||||||
println(code == exactCode)
|
println(code == exactCode)
|
||||||
|
validateTokenSoundness(code, stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun tokenize(input: String): TokenStream =
|
fun tokenize(input: String): TokenStream =
|
||||||
|
@ -2,7 +2,8 @@ package gay.pizza.pork.parse
|
|||||||
|
|
||||||
class StringCharSource(val input: String) : CharSource {
|
class StringCharSource(val input: String) : CharSource {
|
||||||
private var index = 0
|
private var index = 0
|
||||||
override val currentIndex: Int = index
|
override val currentIndex: Int
|
||||||
|
get() = index
|
||||||
|
|
||||||
override fun next(): Char {
|
override fun next(): Char {
|
||||||
if (index == input.length) {
|
if (index == input.length) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package gay.pizza.pork.parse
|
package gay.pizza.pork.parse
|
||||||
|
|
||||||
class Token(val type: TokenType, val start: Int, val text: String) {
|
class Token(val type: TokenType, val start: Int, val text: String) {
|
||||||
override fun toString(): String = "${type.name} $text"
|
override fun toString(): String = "$start ${type.name} '${text.replace("\n", "\\n")}'"
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun endOfFile(size: Int): Token =
|
fun endOfFile(size: Int): Token =
|
||||||
|
@ -2,7 +2,8 @@ package gay.pizza.pork.parse
|
|||||||
|
|
||||||
class TokenStreamSource(val stream: TokenStream) : TokenSource {
|
class TokenStreamSource(val stream: TokenStream) : TokenSource {
|
||||||
private var index = 0
|
private var index = 0
|
||||||
override val currentIndex: Int = index
|
override val currentIndex: Int
|
||||||
|
get() = index
|
||||||
|
|
||||||
override fun next(): Token {
|
override fun next(): Token {
|
||||||
if (index == stream.tokens.size) {
|
if (index == stream.tokens.size) {
|
||||||
|
Loading…
Reference in New Issue
Block a user