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 ...@@ -21,7 +21,13 @@ import djinni.ast.Record.DerivingType.DerivingType
import djinni.meta.MExpr import djinni.meta.MExpr
import djinni.syntax.Loc 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) case class Ident(name: String, file: File, loc: Loc)
class ConstRef(ident: Ident) extends Ident(ident.name, ident.file, ident.loc) class ConstRef(ident: Ident) extends Ident(ident.name, ident.file, ident.loc)
......
...@@ -36,14 +36,20 @@ private object IdlParser extends RegexParsers { ...@@ -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 idlFile(origin: String): Parser[IdlFile] = rep(importFile) ~ rep(typeDecl(origin)) ^^ { case imp~types => IdlFile(imp, types) }
def importFile: Parser[File] = "@import \"" ~> filePath <~ "\"" ^^ { def importFile: Parser[FileRef] = ("@" ~> directive) ~ ("\"" ~> filePath <~ "\"") ^^ {
x => { case "import" ~ x =>
val newPath = fileStack.top.getParent() + "/" + 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 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 ^^ { def typeDecl(origin: String): Parser[TypeDecl] = doc ~ ident ~ typeList(ident ^^ TypeParam) ~ "=" ~ typeDef ^^ {
case doc~ident~typeParams~_~body => TypeDecl(ident, typeParams, body, doc, origin) case doc~ident~typeParams~_~body => TypeDecl(ident, typeParams, body, doc, origin)
} }
...@@ -222,11 +228,16 @@ def parseFile(idlFile: File, inFileListWriter: Option[Writer]): Seq[TypeDecl] = ...@@ -222,11 +228,16 @@ def parseFile(idlFile: File, inFileListWriter: Option[Writer]): Seq[TypeDecl] =
case Right(idl) => { case Right(idl) => {
var types = idl.typeDecls var types = idl.typeDecls
idl.imports.foreach(x => { idl.imports.foreach(x => {
if (fileStack.contains(x)) { if (fileStack.contains(x.file)) {
throw new AssertionError("Circular import detected!") throw new AssertionError("Circular import detected!")
} }
if (!visitedFiles.contains(x)) { if (!visitedFiles.contains(x.file)) {
types = parseFile(x, inFileListWriter) ++ types x match {
case IdlFileRef(file) =>
types = parseFile(file, inFileListWriter) ++ types
case ExternFileRef(file) =>
types
}
} }
}) })
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