com.github.treepat.target_tree.default_tree.DefaultTargetTree.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Treepat Show documentation
Show all versions of Treepat Show documentation
Treepat is a language to recognise patterns in trees in a similar way as regular expressions recognize patterns in strings. Treepat includes analogous operators to regex union, concatenation, and closure, which are extended to the notion of trees.
package com.github.treepat.target_tree.default_tree
import antlr.tree_format.TreeFormatLexer
import antlr.tree_format.TreeFormatParser
import com.github.treepat.grammars.antlr.tree_format.TreeFormatVisitorImplementation
import com.github.treepat.target_tree.TargetTree
import com.github.treepat.target_tree.TargetTreeNode
import java.nio.file.Path
import org.antlr.v4.runtime.CharStreams
import org.antlr.v4.runtime.CommonTokenStream
import org.antlr.v4.runtime.tree.ParseTree
class DefaultTargetTree(override var root: T? = null) :
TargetTree {
/**
* Read tree from grammar.antlr.
*/
constructor(tree_file: Path) : this() {
val lexer = TreeFormatLexer(CharStreams.fromPath(tree_file))
val tokenStream = CommonTokenStream(lexer)
val fileParser = TreeFormatParser(tokenStream)
val tree: ParseTree = fileParser.subtree()
val treeVisitor = TreeFormatVisitorImplementation()
@Suppress("UNCHECKED_CAST")
root = treeVisitor.visit(tree) as? T
}
}