Skip to content

Commit

Permalink
change MiniKoreUtils to case class
Browse files Browse the repository at this point in the history
  • Loading branch information
msaxena2 committed Jan 24, 2017
1 parent ac75396 commit bcef4d1
Showing 1 changed file with 87 additions and 85 deletions.
172 changes: 87 additions & 85 deletions java-backend/src/main/scala/MiniKoreUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import scala.collection.Seq
*/
object MiniKoreUtils {


def getMainModule(definition: Definition): Module = {
val mainModuleName = findAtt(definition.att, iMainModule) match {
case Seq(DomainValue("S", name)) => name;
Expand All @@ -32,111 +31,114 @@ object MiniKoreUtils {
}


/**
* Given a module m and a definition, return all sentences from modules imported (recursively) by m.
*/
def allSentences(m: Module, definition: Definition): Seq[Sentence] = {
val mainModuleImports: Set[String] = m.sentences collect {
case Import(name, _) => name
} toSet
case class ModuleUtils(m: Module, definition: Definition) {

val importedSentences: Seq[Sentence] = definition.modules.filter(p => mainModuleImports.contains(p.name))
.flatMap(x => allSentences(x, definition))
m.sentences ++ importedSentences
}

lazy val importedSentences = getSentencesFromImports()

def signatureFor(m: Module, definition: Definition): Map[String, Set[(Seq[String], String)]] = {
allSentences(m, definition) collect {
case SymbolDeclaration(sort: String, label: String, args: Seq[String], _)
=> (label, (args, sort))
} groupBy {_._1} mapValues { x => x map {_._2} toSet }
}
lazy val localSentences = m.sentences

lazy val allSentences: Seq[Sentence] = localSentences ++ importedSentences

def attributesFor(m: Module, d: Definition): Map[String, Seq[Pattern]] = {
val filterSet = Set(iTerminal, iNonTerminal, iRegexTerminal)
val labelDecsMap: Map[String, Seq[Pattern]] =
allSentences(m, d) collect {
case SymbolDeclaration(_, label: String, _, att) => (label, att)
} groupBy { x => x._1 } mapValues { z => z map {_._2} } mapValues {_.flatten}
labelDecsMap.mapValues({ s =>
s.flatMap({ p =>
val decodedTup = decodePatternAttributes(p)
decodedTup._2 :+ decodedTup._1
})
}) mapValues { atts => filterAtts(filterSet, atts) }
}
def getSentencesFromImports(): Seq[Sentence] = {
val mainModuleImports: Set[String] = localSentences collect {
case Import(name, _) => name
} toSet

def filterAtts(filterSet: Set[String], atts: Seq[Pattern]): Seq[Pattern] = {
atts.filter(p => p match {
case Application(label, _) => !filterSet.contains(label)
case _ => true
})
}
definition.modules.filter { m: Module => mainModuleImports.contains(m.name) } flatMap {
mod: Module => ModuleUtils(mod, definition).allSentences
}
}

lazy val signatureFor: Map[String, Set[(Seq[String], String)]] = {
allSentences collect {
case SymbolDeclaration(sort: String, label: String, args: Seq[String], _)
=> (label, (args, sort))
} groupBy {_._1} mapValues { x => x map {_._2} toSet }
}

/** Recursively retrieve all defined sorts from the current module, and imported modules.
*
*/
def definedSorts(m: Module, d: Definition): Set[String] = {
allSentences(m, d) collect {
case SymbolDeclaration(sort: String, _, _, _) => sort
case SortDeclaration(sort: String, _) => sort
} toSet
}
lazy val attributesFor: Map[String, Seq[Pattern]] = {
val filterSet = Set(iTerminal, iNonTerminal, iRegexTerminal)
val labelDecsMap: Map[String, Seq[Pattern]] =
allSentences collect {
case SymbolDeclaration(_, label: String, _, att) => (label, att)
} groupBy { x => x._1 } mapValues { z => z map {_._2} } mapValues {_.flatten}
labelDecsMap.mapValues({ s =>
s.flatMap({ p =>
val decodedTup = decodePatternAttributes(p)
decodedTup._2 :+ decodedTup._1
})
}) mapValues { atts => filterAtts(filterSet, atts) }
}

/**
* Given a Module m and Definition d, generates a powerset containing all subsorts
*/
def filterAtts(filterSet: Set[String], atts: Seq[Pattern]): Seq[Pattern] = {
atts.filter(p => p match {
case Application(label, _) => !filterSet.contains(label)
case _ => true
})
}

def subsorts(m: Module, d: Definition): POSet[String] = {
val symbolDecs: Seq[(String, Seq[Pattern])] = allSentences(m, d) collect {
case SymbolDeclaration(sort, _, _, atts) => (sort, atts)
/** Recursively retrieve all defined sorts from the current module, and imported modules.
*
*/
lazy val definedSorts: Set[String] = {
allSentences collect {
case SymbolDeclaration(sort: String, _, _, _) => sort
case SortDeclaration(sort: String, _) => sort
} toSet
}
val subsortProductions: Set[(String, String)] = symbolDecs map { x =>
(x._1, x._2 collect {
case Application(`iNonTerminal`, Seq(DomainValue("S", s))) => s
case Application(`iTerminal`, _) => iTerminal
case Application(`iRegexTerminal`, _) => iTerminal
})
} filter (x => x._2.size == 1 && !x._2.head.startsWith(iTerminal)) map { x => (x._2.head, x._1) } toSet

POSet[String](subsortProductions)
}
/**
* Given a Module m and Definition d, generates a powerset containing all subsorts
*/

def freshFunctionFor(m: Module, d: Definition): Map[String, String] = {
val productions = allSentences(m, d) collect {
case SymbolDeclaration(sort, label, _, atts) if findAtt(atts, "freshGenerator").size >= 1
=> (sort, label)
} groupBy (_._1) mapValues (x => x.toSet)
lazy val subsorts: POSet[String] = {
val symbolDecs: Seq[(String, Seq[Pattern])] = allSentences collect {
case SymbolDeclaration(sort, _, _, atts) => (sort, atts)
}
val subsortProductions: Set[(String, String)] = symbolDecs map { x =>
(x._1, x._2 collect {
case Application(`iNonTerminal`, Seq(DomainValue("S", s))) => s
case Application(`iTerminal`, _) => iTerminal
case Application(`iRegexTerminal`, _) => iTerminal
})
} filter (x => x._2.size == 1 && !x._2.head.startsWith(iTerminal)) map { x => (x._2.head, x._1) } toSet

POSet[String](subsortProductions)
}

productions.foreach(x => {
if (x._2.size > 1) throw KEMException.compilerError("Found more than one fresh generator for sort " + x._1 + ". Found" + x._2.map(y => y._2))
})
lazy val freshFunctionFor: Map[String, String] = {
val productions = allSentences collect {
case SymbolDeclaration(sort, label, _, atts) if findAtt(atts, "freshGenerator").size >= 1
=> (sort, label)
} groupBy (_._1) mapValues (x => x.toSet)

productions.map(x => (x._1, x._2.head._2))
}
productions.foreach(x => {
if (x._2.size > 1) throw KEMException.compilerError("Found more than one fresh generator for sort " + x._1 + ". Found" + x._2.map(y => y._2))
})

def getSymbolDecs(m: Module, d: Definition): Seq[SymbolDeclaration] = {
allSentences(m, d) collect {
case s@SymbolDeclaration(_, _, _, _) => s
productions.map(x => (x._1, x._2.head._2))
}
}

lazy val symbolDecs: Seq[SymbolDeclaration] = {
allSentences collect {
case s@SymbolDeclaration(_, _, _, _) => s
}
}

def rules(m: Module, d: Definition): Set[Rule] = {
allSentences(m, d) collect {
case x@Rule(_, _) => x
} toSet
}
lazy val rules: Set[Rule] = {
allSentences collect {
case x@Rule(_, _) => x
} toSet
}

def decodePatternAttributes(p: Pattern): (Pattern, Seq[Pattern]) = {
p match {
case Application(`iAtt`, Seq(pat, att)) => decodePatternAttributes(pat) match {
case (finalPat, attList) => (finalPat, attList :+ att)
def decodePatternAttributes(p: Pattern): (Pattern, Seq[Pattern]) = {
p match {
case Application(`iAtt`, Seq(pat, att)) => decodePatternAttributes(pat) match {
case (finalPat, attList) => (finalPat, attList :+ att)
}
case any@_ => (any, Seq())
}
case any@_ => (any, Seq())
}
}

Expand Down

0 comments on commit bcef4d1

Please sign in to comment.