Skip to content

Commit

Permalink
Merge pull request #62 from definiti/57-improve-helpers
Browse files Browse the repository at this point in the history
57 improve helpers
  • Loading branch information
grizio committed May 8, 2018
2 parents 62e5bb9 + abd3ae5 commit 604e92b
Show file tree
Hide file tree
Showing 138 changed files with 1,945 additions and 2,938 deletions.
108 changes: 108 additions & 0 deletions src/main/scala/definiti/common/ast/ClassDefinition.scala
@@ -0,0 +1,108 @@
package definiti.common.ast

sealed trait ClassDefinition extends NamespaceElement {
def name: String

def fullName: String

def genericTypes: Seq[String]
}

sealed trait ProjectClassDefinition extends ClassDefinition {
def comment: Option[String]

def location: Location
}

case class DefinedType(
name: String,
fullName: String,
genericTypes: Seq[String],
parameters: Seq[ParameterDefinition],
attributes: Seq[AttributeDefinition],
verifications: Seq[TypeVerification],
inherited: Seq[VerificationReference],
comment: Option[String],
location: Location
) extends ProjectClassDefinition

case class AliasType(
name: String,
fullName: String,
genericTypes: Seq[String],
parameters: Seq[ParameterDefinition],
alias: TypeDeclaration,
inherited: Seq[VerificationReference],
verifications: Seq[TypeVerification],
comment: Option[String],
location: Location
) extends ProjectClassDefinition

sealed trait TypeVerification {
def message: VerificationMessage

def function: DefinedFunction

def location: Location
}

case class AtomicTypeVerification(
message: VerificationMessage,
function: DefinedFunction,
location: Location
) extends TypeVerification

case class DependentTypeVerification(
name: String,
message: VerificationMessage,
function: DefinedFunction,
location: Location
) extends TypeVerification

case class Enum(
name: String,
fullName: String,
cases: Seq[EnumCase],
comment: Option[String],
location: Location
) extends ProjectClassDefinition {
override def genericTypes: Seq[String] = Seq.empty
}


case class NativeClassDefinition(
name: String,
fullName: String,
genericTypes: Seq[String],
attributes: Seq[AttributeDefinition],
methods: Seq[MethodDefinition],
comment: Option[String]
) extends ClassDefinition

case class EnumCase(
name: String,
comment: Option[String],
location: Location
)

case class AttributeDefinition(
name: String,
typeDeclaration: TypeDeclaration,
comment: Option[String],
verifications: Seq[VerificationReference],
location: Location
)

case class MethodDefinition(
name: String,
genericTypes: Seq[String],
parameters: Seq[ParameterDefinition],
returnType: TypeReference,
comment: Option[String]
)

case class VerificationReference(
verificationName: String,
parameters: Seq[AtomicExpression],
location: Location
)
@@ -1,4 +1,4 @@
package definiti.core.ast
package definiti.common.ast

sealed trait Expression {
def location: Location
Expand All @@ -12,15 +12,15 @@ case class LogicalExpression(
operator: LogicalOperator.Value,
left: Expression,
right: Expression,
returnType: TypeReference,
returnType: AbstractTypeReference,
location: Location
) extends Expression

case class CalculatorExpression(
operator: CalculatorOperator.Value,
left: Expression,
right: Expression,
returnType: TypeReference,
returnType: AbstractTypeReference,
location: Location
) extends Expression

Expand Down
7 changes: 7 additions & 0 deletions src/main/scala/definiti/common/ast/ExtendedContext.scala
@@ -0,0 +1,7 @@
package definiti.common.ast

case class ExtendedContext[A](
name: String,
content: A,
location: Location
) extends NamespaceElement
@@ -1,14 +1,12 @@
package definiti.core.ast

import scala.collection.mutable.ListBuffer
package definiti.common.ast

case class Library(root: Root, core: Seq[ClassDefinition]) {
lazy val namespacesMap: Map[String, Namespace] = namespaces.map(n => n.fullName -> n).toMap
lazy val verificationsMap: Map[String, Verification] = verifications.map(v => v.fullName -> v).toMap
lazy val typesMap: Map[String, ClassDefinition] = types.map(t => t.fullName -> t).toMap
lazy val namedFunctionsMap: Map[String, NamedFunction] = namedFunctions.map(n => n.fullName -> n).toMap

lazy val namespaces: Seq[Namespace] = Library.extractPackages(root)
lazy val namespaces: Seq[Namespace] = root.namespaces
lazy val verifications: Seq[Verification] = fromNamespaces { case verification: Verification => verification }
lazy val types: Seq[ClassDefinition] = projectTypes ++ core
lazy val projectTypes: Seq[ProjectClassDefinition] = aliasTypes ++ definedTypes ++ enums
Expand All @@ -18,27 +16,6 @@ case class Library(root: Root, core: Seq[ClassDefinition]) {
lazy val namedFunctions: Seq[NamedFunction] = fromNamespaces { case namedFunction: NamedFunction => namedFunction }

private def fromNamespaces[A](extractor: PartialFunction[NamespaceElement, A]): Seq[A] = {
root.elements.collect(extractor) ++ namespaces.flatMap(_.elements.collect(extractor))
}
}

object Library {
def extractPackages(root: Root): Seq[Namespace] = {
val namespacesBuffer: ListBuffer[Namespace] = ListBuffer()

def process(namespace: Namespace): Unit = {
namespacesBuffer += namespace
namespace.elements.foreach {
case subNamespace: Namespace => process(subNamespace)
case _ => // do nothing
}
}

root.elements.foreach {
case namespace: Namespace => process(namespace)
case _ => // do nothing
}

namespacesBuffer.toList
root.namespaces.flatMap(_.elements).collect(extractor)
}
}
@@ -1,4 +1,4 @@
package definiti.core.ast
package definiti.common.ast

case class Location(
file: String,
Expand Down
11 changes: 11 additions & 0 deletions src/main/scala/definiti/common/ast/NamedFunction.scala
@@ -0,0 +1,11 @@
package definiti.common.ast

case class NamedFunction(
name: String,
fullName: String,
genericTypes: Seq[String],
parameters: Seq[ParameterDefinition],
returnType: TypeReference,
body: Expression,
location: Location
) extends NamespaceElement
7 changes: 7 additions & 0 deletions src/main/scala/definiti/common/ast/ParameterDefinition.scala
@@ -0,0 +1,7 @@
package definiti.common.ast

case class ParameterDefinition(
name: String,
typeReference: AbstractTypeReference,
location: Location
)
13 changes: 13 additions & 0 deletions src/main/scala/definiti/common/ast/Root.scala
@@ -0,0 +1,13 @@
package definiti.common.ast

case class Root(
namespaces: Seq[Namespace]
)

case class Namespace(
name: String,
fullName: String,
elements: Seq[NamespaceElement]
)

trait NamespaceElement
50 changes: 50 additions & 0 deletions src/main/scala/definiti/common/ast/TypeReference.scala
@@ -0,0 +1,50 @@
package definiti.common.ast

sealed trait AbstractTypeReference {
def readableString: String
}

case object Unset extends AbstractTypeReference {
override def readableString: String = "***Unset***"
}

case class TypeReference(
typeName: String,
genericTypes: Seq[TypeReference] = Seq.empty
) extends AbstractTypeReference {
def readableString: String = {
if (genericTypes.nonEmpty) {
s"$typeName[${genericTypes.map(_.readableString).mkString(",")}]"
} else {
typeName
}
}
}

case class LambdaReference(
inputTypes: Seq[TypeReference],
outputType: TypeReference
) extends AbstractTypeReference {
def readableString: String = s"(${inputTypes.map(_.readableString).mkString(", ")}) => ${outputType.readableString}"
}

case class NamedFunctionReference(
functionName: String
) extends AbstractTypeReference {
def readableString: String = functionName
}

case class TypeDeclaration(
typeName: String,
genericTypes: Seq[TypeDeclaration],
parameters: Seq[AtomicExpression],
location: Location
) {
def readableString: String = {
if (genericTypes.nonEmpty) {
s"$typeName[${genericTypes.map(_.readableString).mkString(", ")}]"
} else {
typeName
}
}
}
32 changes: 32 additions & 0 deletions src/main/scala/definiti/common/ast/Verification.scala
@@ -0,0 +1,32 @@
package definiti.common.ast

case class Verification(
name: String,
fullName: String,
parameters: Seq[ParameterDefinition],
message: VerificationMessage,
function: DefinedFunction,
comment: Option[String],
location: Location
) extends NamespaceElement

sealed trait VerificationMessage {
def prettyPrint: String
}

case class LiteralMessage(message: String, location: Location) extends VerificationMessage {
override def prettyPrint: String = message
}

case class TypedMessage(message: String, types: Seq[TypeReference], location: Location) extends VerificationMessage {
override def prettyPrint: String = {
if (types.nonEmpty) {
s"""message("${message}", ${types.map(_.readableString).mkString(", ")})"""
} else {
s"""message("${message}")"""
}

}
}

case class DefinedFunction(parameters: Seq[ParameterDefinition], body: Expression, genericTypes: Seq[String], location: Location)
@@ -1,7 +1,7 @@
package definiti.core.validation
package definiti.common.control

import definiti.core.ast.{Library, Location, Root}
import definiti.core.{Alert, AlertControl}
import definiti.common.ast.{Library, Location, Root}
import definiti.common.validation.{Alert, AlertControl}

trait Control {
val name: String = {
Expand Down
@@ -1,7 +1,7 @@
package definiti.core.validation
package definiti.common.control

import definiti.core.ast.Location
import definiti.core.{Alert, AlertControl}
import definiti.common.ast.Location
import definiti.common.validation.{Alert, AlertControl}

case class ControlResult(alerts: Seq[Alert]) {
def +(control: ControlResult): ControlResult = ControlResult(alerts ++ control.alerts)
Expand Down
17 changes: 17 additions & 0 deletions src/main/scala/definiti/common/plugin/ContextPlugin.scala
@@ -0,0 +1,17 @@
package definiti.common.plugin

import definiti.common.ast.{Library, Location}
import definiti.common.program.ProgramResult.NoResult
import definiti.common.validation.Validated

trait ContextPlugin[A] extends Plugin {
def contextName: String

def parse(content: String, packageName: String, imports: Map[String, String], location: Location): A

def validate(context: A, library: Library): Validated[NoResult]

def contextToJson(context: A): String

def contextFromJson(json: String): A
}
10 changes: 10 additions & 0 deletions src/main/scala/definiti/common/plugin/GeneratorPlugin.scala
@@ -0,0 +1,10 @@
package definiti.common.plugin

import java.nio.file.Path

import definiti.common.ast.{Library, Root}

trait GeneratorPlugin extends Plugin {
def generate(root: Root, library: Library): Map[Path, String]
}

8 changes: 8 additions & 0 deletions src/main/scala/definiti/common/plugin/ParserPlugin.scala
@@ -0,0 +1,8 @@
package definiti.common.plugin

import definiti.common.ast.Root
import definiti.common.validation.Validated

trait ParserPlugin extends Plugin {
def transform(root: Root): Validated[Root]
}
5 changes: 5 additions & 0 deletions src/main/scala/definiti/common/plugin/Plugin.scala
@@ -0,0 +1,5 @@
package definiti.common.plugin

trait Plugin {
def name: String
}
9 changes: 9 additions & 0 deletions src/main/scala/definiti/common/plugin/ValidatorPlugin.scala
@@ -0,0 +1,9 @@
package definiti.common.plugin

import definiti.common.ast.{Library, Root}
import definiti.common.program.ProgramResult.NoResult
import definiti.common.validation.Validated

trait ValidatorPlugin extends Plugin {
def validate(root: Root, library: Library): Validated[NoResult]
}

0 comments on commit 604e92b

Please sign in to comment.