All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eepat.Treepat.1.2.6.source-code.Main.kt Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 2.0.0
Show newest version
import antlr.tree_format.TreeFormatLexer
import antlr.tree_format.TreeFormatParser
import antlr.treepat.TreepatLexer
import antlr.treepat.TreepatParser
import com.github.treepat.expression.TreepatExpression
import com.github.treepat.grammars.antlr.tree_format.TreeFormatVisitorImplementation
import com.github.treepat.grammars.antlr.treepat.TreepatVisitorImplementation
import com.github.treepat.grammars.ast.ASTNode
import com.github.treepat.target_tree.TargetTreeNode
import com.github.treepat.target_tree.default_tree.DefaultTargetTree
import com.github.treepat.target_tree.default_tree.DefaultTargetTreeNode
import java.nio.file.Paths
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JScrollPane
import org.antlr.v4.gui.TreeViewer
import org.antlr.v4.runtime.CharStreams
import org.antlr.v4.runtime.CommonTokenStream
import org.antlr.v4.runtime.Parser
import org.antlr.v4.runtime.tree.ParseTree

object Main {

    private const val jFrameTitle = "Antlr AST"

    @Throws(Exception::class)
    @JvmStatic
    fun main(args: Array) {
        // Treepat Parsing
        val treepatExpression = TreepatExpression.createFromFile(Paths.get(args[0]))
        // Tree File Parsing
        val targetTree =
            DefaultTargetTree(Paths.get(args[1]))

        val functionResult = targetTree.findMatchesRaw(treepatExpression)

        val solutions: List
        solutions = if (functionResult.hasMatch) {
            functionResult.responses.map { targetTree.root?.matchedNodesString(it.matches)?.trimIndent() ?: "" }
        } else {
            listOf("Match not found")
        }
        print(solutions.joinToString("\n-\n"))
    }

    private fun showASTNodeFrame(parser: Parser, tree: ParseTree) {
        val frame = JFrame(jFrameTitle)
        val panel = JPanel()
        val viewer = TreeViewer(listOf(*parser.ruleNames), tree)
        viewer.scale = 1.0
        panel.add(viewer)
        val scrollPane = JScrollPane(panel)
        scrollPane.horizontalScrollBarPolicy = JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
        scrollPane.verticalScrollBarPolicy = JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
        scrollPane.setBounds(50, 30, 300, 50)
        frame.add(JPanel().add(scrollPane))
        frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
        frame.pack()
        frame.isVisible = true
    }

    private fun parseTreeFile(fileName: String): TargetTreeNode {
        val lexer = TreeFormatLexer(CharStreams.fromFileName(fileName))
        val tokenStream = CommonTokenStream(lexer)
        val fileParser = TreeFormatParser(tokenStream)
        val tree: ParseTree = fileParser.subtree()
        val treeVisitor = TreeFormatVisitorImplementation()

        showASTNodeFrame(fileParser, tree)

        return treeVisitor.visit(tree)
    }

    private fun parseTreepat(fileName: String): ASTNode {
        val lexer = TreepatLexer(CharStreams.fromFileName(fileName))
        val tokenStream = CommonTokenStream(lexer)
        val treepatParser = TreepatParser(tokenStream)
        val tree: ParseTree = treepatParser.treepat()
        val treepatVisitor = TreepatVisitorImplementation()

        showASTNodeFrame(treepatParser, tree)

        return treepatVisitor.visit(tree)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy