-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parser: various code and quality enhancements
- Loading branch information
Showing
23 changed files
with
316 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
common/src/main/kotlin/gay/pizza/pork/common/IndentBuffer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package gay.pizza.pork.common | ||
|
||
class IndentBuffer( | ||
val buffer: StringBuilder = StringBuilder(), | ||
indent: String = " " | ||
) : IndentTracked(indent), Appendable by buffer, CharSequence by buffer { | ||
override fun emit(text: String) { | ||
append(text) | ||
} | ||
|
||
override fun emitLine(text: String) { | ||
appendLine(text) | ||
} | ||
|
||
override fun toString(): String = buffer.toString() | ||
} |
35 changes: 5 additions & 30 deletions
35
common/src/main/kotlin/gay/pizza/pork/common/IndentPrinter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,11 @@ | ||
package gay.pizza.pork.common | ||
|
||
class IndentPrinter( | ||
val buffer: StringBuilder = StringBuilder(), | ||
val indent: String = " " | ||
) : Appendable by buffer, CharSequence by buffer { | ||
private var indentLevel: Int = 0 | ||
private var indentLevelText: String = "" | ||
|
||
fun emitIndent() { | ||
append(indentLevelText) | ||
} | ||
|
||
fun emitIndentedLine(line: String) { | ||
emitIndent() | ||
appendLine(line) | ||
} | ||
|
||
fun increaseIndent() { | ||
indentLevel++ | ||
indentLevelText += indent | ||
} | ||
|
||
fun decreaseIndent() { | ||
indentLevel-- | ||
indentLevelText = indent.repeat(indentLevel) | ||
class IndentPrinter(indent: String = " ") : IndentTracked(indent) { | ||
override fun emit(text: String) { | ||
print(text) | ||
} | ||
|
||
inline fun indented(block: IndentPrinter.() -> Unit) { | ||
increaseIndent() | ||
block(this) | ||
decreaseIndent() | ||
override fun emitLine(text: String) { | ||
println(text) | ||
} | ||
|
||
override fun toString(): String = buffer.toString() | ||
} |
37 changes: 37 additions & 0 deletions
37
common/src/main/kotlin/gay/pizza/pork/common/IndentTracked.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package gay.pizza.pork.common | ||
|
||
abstract class IndentTracked(val indent: String) { | ||
private var internalIndentLevel = 0 | ||
private var indentLevelText = "" | ||
|
||
val indentLevel: Int | ||
get() = internalIndentLevel | ||
|
||
fun emitIndent() { | ||
emit(indentLevelText) | ||
} | ||
|
||
fun emitIndentedLine(line: String) { | ||
emitIndent() | ||
emitLine(line) | ||
} | ||
|
||
fun increaseIndent() { | ||
internalIndentLevel++ | ||
indentLevelText += indent | ||
} | ||
|
||
fun decreaseIndent() { | ||
internalIndentLevel-- | ||
indentLevelText = indent.repeat(indentLevel) | ||
} | ||
|
||
inline fun indented(block: IndentTracked.() -> Unit) { | ||
increaseIndent() | ||
block(this) | ||
decreaseIndent() | ||
} | ||
|
||
abstract fun emit(text: String) | ||
abstract fun emitLine(text: String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package gay.pizza.pork.parser | ||
|
||
interface CharConsumer { | ||
fun consume(type: TokenType, tokenizer: Tokenizer): String? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package gay.pizza.pork.parser | ||
|
||
interface CharSource : PeekableSource<Char> { | ||
fun peek(index: Int): Char | ||
|
||
companion object { | ||
@Suppress("ConstPropertyName") | ||
const val NullChar = 0.toChar() | ||
const val EndOfFile = 0.toChar() | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
parser/src/main/kotlin/gay/pizza/pork/parser/CharSourceExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package gay.pizza.pork.parser | ||
|
||
fun CharSource.readToString(): String = buildString { | ||
while (peek() != CharSource.NullChar) { | ||
while (peek() != CharSource.EndOfFile) { | ||
append(next()) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
parser/src/main/kotlin/gay/pizza/pork/parser/MatchedCharConsumer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package gay.pizza.pork.parser | ||
|
||
@Suppress("CanBeParameter") | ||
class MatchedCharConsumer( | ||
val start: CharSequence, | ||
val end: CharSequence, | ||
vararg val options: Options | ||
) : CharConsumer { | ||
private val eofTerminationAllowed = options.contains(Options.AllowEofTermination) | ||
|
||
override fun consume(type: TokenType, tokenizer: Tokenizer): String? { | ||
if (!tokenizer.peek(start)) { | ||
return null | ||
} | ||
val buffer = StringBuilder() | ||
tokenizer.read(start.length, buffer) | ||
var endsNeededToTerminate = 1 | ||
while (true) { | ||
if (tokenizer.peek(start)) { | ||
endsNeededToTerminate++ | ||
tokenizer.read(start.length, buffer) | ||
continue | ||
} | ||
|
||
if (tokenizer.peek(end)) { | ||
endsNeededToTerminate-- | ||
tokenizer.read(end.length, buffer) | ||
} | ||
|
||
if (endsNeededToTerminate == 0) { | ||
return buffer.toString() | ||
} | ||
|
||
val char = tokenizer.source.next() | ||
if (char == CharSource.EndOfFile) { | ||
if (eofTerminationAllowed) { | ||
return buffer.toString() | ||
} | ||
throw UnterminatedTokenError(type.name, tokenizer.source.currentSourceIndex()) | ||
} | ||
buffer.append(char) | ||
} | ||
} | ||
|
||
enum class Options { | ||
AllowEofTermination | ||
} | ||
} |
Oops, something went wrong.