Add 'scriptless' mode to orx-olive
This commit is contained in:
53
orx-kotlin-parser/src/main/kotlin/ExtractProgram.kt
Normal file
53
orx-kotlin-parser/src/main/kotlin/ExtractProgram.kt
Normal file
@@ -0,0 +1,53 @@
|
||||
package org.openrndr.extra.kotlinparser
|
||||
|
||||
import org.antlr.v4.runtime.CharStreams
|
||||
import org.antlr.v4.runtime.CommonTokenStream
|
||||
import org.antlr.v4.runtime.ParserRuleContext
|
||||
import org.antlr.v4.runtime.misc.Interval
|
||||
import org.antlr.v4.runtime.tree.ParseTreeWalker
|
||||
import org.openrndr.extra.kotlin.antlr.KotlinLexer
|
||||
import org.openrndr.extra.kotlin.antlr.KotlinParser
|
||||
import org.openrndr.extra.kotlin.antlr.KotlinParserBaseListener
|
||||
import java.io.File
|
||||
|
||||
fun ParserRuleContext.verbatimText(marginLeft:Int = 0, marginRight:Int = 0): String {
|
||||
val startIndex = start.startIndex + marginLeft
|
||||
val stopIndex = stop.stopIndex - marginRight
|
||||
val interval = Interval(startIndex, stopIndex)
|
||||
return start.inputStream.getText(interval)
|
||||
}
|
||||
|
||||
class ImportsExtractor : KotlinParserBaseListener() {
|
||||
var result: String? = null
|
||||
|
||||
override fun enterImportList(ctx: KotlinParser.ImportListContext) {
|
||||
result = ctx.verbatimText()
|
||||
}
|
||||
}
|
||||
|
||||
class LambdaExtractor(val lambdaName: String) : KotlinParserBaseListener() {
|
||||
var result: String? = null
|
||||
override fun enterAnnotatedLambda(ctx: KotlinParser.AnnotatedLambdaContext?) {
|
||||
val puec = ctx!!.parent!!.parent!!.parent!! as KotlinParser.PostfixUnaryExpressionContext
|
||||
val identifier = puec.primaryExpression().simpleIdentifier().Identifier().text
|
||||
if (identifier == lambdaName) {
|
||||
result = ctx.verbatimText(1, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
class ProgramSource(val imports: String, val programLambda: String)
|
||||
|
||||
|
||||
|
||||
fun extractProgram(source: String) : ProgramSource {
|
||||
val parser = KotlinParser(CommonTokenStream(KotlinLexer(CharStreams.fromString(source))))
|
||||
val root = parser.kotlinFile()
|
||||
|
||||
val importsExtractor = ImportsExtractor()
|
||||
ParseTreeWalker.DEFAULT.walk(importsExtractor, root)
|
||||
|
||||
val lambdaExtractor = LambdaExtractor("program")
|
||||
ParseTreeWalker.DEFAULT.walk(lambdaExtractor, root)
|
||||
|
||||
return ProgramSource(importsExtractor.result!!, lambdaExtractor.result!!)
|
||||
}
|
||||
47
orx-kotlin-parser/src/main/kotlin/TreeUtils.kt
Normal file
47
orx-kotlin-parser/src/main/kotlin/TreeUtils.kt
Normal file
@@ -0,0 +1,47 @@
|
||||
import org.antlr.v4.runtime.misc.Utils
|
||||
import org.antlr.v4.runtime.tree.Tree
|
||||
import org.antlr.v4.runtime.tree.Trees
|
||||
|
||||
object TreeUtils {
|
||||
/** Platform dependent end-of-line marker */
|
||||
val Eol = System.lineSeparator()
|
||||
|
||||
/** The literal indent char(s) used for pretty-printing */
|
||||
const val Indents = " "
|
||||
private var level = 0
|
||||
|
||||
/**
|
||||
* Pretty print out a whole tree. [.getNodeText] is used on the node payloads to get the text
|
||||
* for the nodes. (Derived from Trees.toStringTree(....))
|
||||
*/
|
||||
fun toPrettyTree(t: Tree, ruleNames: List<String>): String {
|
||||
level = 0
|
||||
return process(t, ruleNames).replace("(?m)^\\s+$".toRegex(), "").replace("\\r?\\n\\r?\\n".toRegex(), Eol)
|
||||
}
|
||||
|
||||
private fun process(t: Tree, ruleNames: List<String>): String {
|
||||
if (t.getChildCount() === 0) return Utils.escapeWhitespace(Trees.getNodeText(t, ruleNames), false)
|
||||
val sb = StringBuilder()
|
||||
sb.append(lead(level))
|
||||
level++
|
||||
val s: String = Utils.escapeWhitespace(Trees.getNodeText(t, ruleNames), false)
|
||||
sb.append("$s ")
|
||||
for (i in 0 until t.getChildCount()) {
|
||||
sb.append(process(t.getChild(i), ruleNames))
|
||||
}
|
||||
level--
|
||||
sb.append(lead(level))
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
private fun lead(level: Int): String {
|
||||
val sb = StringBuilder()
|
||||
if (level > 0) {
|
||||
sb.append(Eol)
|
||||
for (cnt in 0 until level) {
|
||||
sb.append(Indents)
|
||||
}
|
||||
}
|
||||
return sb.toString()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user