axle.ast.view.ViewXhtml.scala Maven / Gradle / Ivy
The newest version!
package axle.ast.view
import scala.math.max
import scala.math.min
import scala.xml.NodeSeq.seqToNodeSeq
import Emission.emit
import axle.ast.AstNode
import axle.ast.Document
import axle.ast.LLLanguage
import axle.ast.Language
object ViewXhtml extends View[xml.NodeSeq] {
// ...
def makeFormatter(language: Language, node: AstNode): XhtmlAstNodeFormatter =
new XhtmlAstNodeFormatter(
FormatterConfig(language, true, Set(node)),
FormatterState(0, 0, false, 0, Nil, Map()),
Nil)
//
override def AstNode(root: AstNode, language: Language): xml.NodeSeq =
{ emit(language, root, makeFormatter(language, root)).result }
def nodeContext(language: Language, node: AstNode, uri: String): xml.NodeSeq = {
val highlightedHtml = emit(language, node, makeFormatter(language, node)).result // NOTE: python version cached this
val lineNos = max(1, node.lineNo - CONTEXT_PAD) to min(highlightedHtml.size, node.lineNo + CONTEXT_PAD)
}
// def contextHtmlLines(): Option[LinkedHashMap[Int, NodeSeq]] = contextHtml(doc, docNode)
override def docNodeInContext(doc: Document, docNode: AstNode): xml.NodeSeq =
doc.ast.map(ast => nodeContext(doc.grammar, docNode, "/document/" + doc.name))
.getOrElse(Oh no)
def llRules(g: LLLanguage): xml.NodeSeq =
Rules:
{
g.llRules.zipWithIndex.map({
case (rule, id) => {
- { id }:{ rule.from }->{ rule.rhs.mkString("", " ", "") }
}
})
}
def llParseTable(g: LLLanguage): xml.NodeSeq =
Parse Table:
{
g.terminals map { term =>
{ term.label }
}
}
{
g.nonTerminals map { nterm =>
{ nterm }:
{
g.terminals map { term =>
{
if (g.parseTable.contains((nterm, term))) {
g.parseTable((nterm, term)).id
} else {
"-"
}
}
}
}
}
}
def llLanguage(g: LLLanguage): xml.NodeSeq =
{ g.name }
{ llRules(g) }
{ llParseTable(g) }
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy