Commit 18b37727 authored by Miro Knejp's avatar Miro Knejp

Distinguish between @import and @extern in IDL files

parent 4c062e36
......@@ -21,7 +21,13 @@ import djinni.ast.Record.DerivingType.DerivingType
import djinni.meta.MExpr
import djinni.syntax.Loc
case class IdlFile(imports: Seq[File], typeDecls: Seq[TypeDecl])
case class IdlFile(imports: Seq[FileRef], typeDecls: Seq[TypeDecl])
abstract sealed class FileRef {
val file: File
}
case class IdlFileRef(override val file: File) extends FileRef
case class ExternFileRef(override val file: File) extends FileRef
case class Ident(name: String, file: File, loc: Loc)
class ConstRef(ident: Ident) extends Ident(ident.name, ident.file, ident.loc)
......
......@@ -36,14 +36,20 @@ private object IdlParser extends RegexParsers {
def idlFile(origin: String): Parser[IdlFile] = rep(importFile) ~ rep(typeDecl(origin)) ^^ { case imp~types => IdlFile(imp, types) }
def importFile: Parser[File] = "@import \"" ~> filePath <~ "\"" ^^ {
x => {
def importFile: Parser[FileRef] = ("@" ~> directive) ~ ("\"" ~> filePath <~ "\"") ^^ {
case "import" ~ x =>
val newPath = fileStack.top.getParent() + "/" + x
new File(newPath)
}
new IdlFileRef(new File(newPath))
case "extern" ~ x =>
val newPath = fileStack.top.getParent() + "/" + x
new ExternFileRef(new File(newPath))
}
def filePath = "[^\"]*".r
def directive = importDirective | externDirective
def importDirective = "import".r
def externDirective = "extern".r
def typeDecl(origin: String): Parser[TypeDecl] = doc ~ ident ~ typeList(ident ^^ TypeParam) ~ "=" ~ typeDef ^^ {
case doc~ident~typeParams~_~body => TypeDecl(ident, typeParams, body, doc, origin)
}
......@@ -222,11 +228,16 @@ def parseFile(idlFile: File, inFileListWriter: Option[Writer]): Seq[TypeDecl] =
case Right(idl) => {
var types = idl.typeDecls
idl.imports.foreach(x => {
if (fileStack.contains(x)) {
if (fileStack.contains(x.file)) {
throw new AssertionError("Circular import detected!")
}
if (!visitedFiles.contains(x)) {
types = parseFile(x, inFileListWriter) ++ types
if (!visitedFiles.contains(x.file)) {
x match {
case IdlFileRef(file) =>
types = parseFile(file, inFileListWriter) ++ types
case ExternFileRef(file) =>
types
}
}
})
types
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment