Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Kkaye/include paths #252

Merged
merged 9 commits into from
Jan 14, 2017
4 changes: 2 additions & 2 deletions src/.idea/runConfigurations/djinni_Main__Test_Suite_.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions src/source/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

package djinni

import java.io.{IOException, FileInputStream, InputStreamReader, File, BufferedWriter, FileWriter}
import java.io.{IOException, FileNotFoundException, FileInputStream, InputStreamReader, File, BufferedWriter, FileWriter}

import djinni.generatorTools._

object Main {

def main(args: Array[String]) {
var idlFile: File = null
var idlIncludePaths: List[String] = List("")
var cppOutFolder: Option[File] = None
var cppNamespace: String = ""
var cppIncludePrefix: String = ""
Expand Down Expand Up @@ -95,6 +96,8 @@ object Main {
help("help")
opt[File]("idl").valueName("<in-file>").required().foreach(idlFile = _)
.text("The IDL file with the type definitions, typically with extension \".djinni\".")
opt[String]("idl-include-path").valueName("<path> ...").optional().unbounded().foreach(x => idlIncludePaths = idlIncludePaths :+ x)
.text("An include path to search for Djinni @import directives. Can specify multiple paths.")
note("")
opt[File]("java-out").valueName("<out-folder>").foreach(x => javaOutFolder = Some(x))
.text("The output for the Java files (Generator disabled if unspecified).")
Expand Down Expand Up @@ -248,10 +251,10 @@ object Main {
None
}
val idl = try {
(new Parser).parseFile(idlFile, inFileListWriter)
(new Parser(idlIncludePaths)).parseFile(idlFile, inFileListWriter)
}
catch {
case ex: IOException =>
case ex @ (_: FileNotFoundException | _: IOException) =>
System.err.println("Error reading from --idl file: " + ex.getMessage)
System.exit(1); return
}
Expand Down
41 changes: 28 additions & 13 deletions src/source/parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package djinni

import java.io.{File, InputStreamReader, FileInputStream, Writer}
import java.io.{File, FileNotFoundException, InputStreamReader, FileInputStream, Writer}

import djinni.ast.Interface.Method
import djinni.ast.Record.DerivingType.DerivingType
Expand All @@ -26,32 +26,47 @@ import java.util.{Map => JMap}
import org.yaml.snakeyaml.Yaml
import scala.collection.JavaConversions._
import scala.collection.mutable
import scala.util.control.Breaks._
import scala.util.parsing.combinator.RegexParsers
import scala.util.parsing.input.{Position, Positional}

case class Parser() {
case class Parser(includePaths: List[String]) {

val visitedFiles = mutable.Set[File]()
val fileStack = mutable.Stack[File]()

private object IdlParser extends RegexParsers {
override protected val whiteSpace = """[ \t\n\r]+""".r

def idlFile(origin: String): Parser[IdlFile] = rep(importFile) ~ rep(typeDecl(origin)) ^^ { case imp~types => IdlFile(imp, types) }

def importFile: Parser[FileRef] = {

def fileParent:String = if (fileStack.top.getParent() != null) return fileStack.top.getParent() + "/" else return ""
def idlFile(origin: String): Parser[IdlFile] = rep(importFileRef) ~ rep(typeDecl(origin)) ^^ { case imp~types => IdlFile(imp, types) }

def importFileRef(): Parser[FileRef] = {
("@" ~> directive) ~ ("\"" ~> filePath <~ "\"") ^^ {
case "import" ~ x =>
val newPath = fileParent + x
new IdlFileRef(new File(newPath))
case "extern" ~ x =>
val newPath = fileParent + x
new ExternFileRef(new File(newPath))
case "import" ~ x => {
new IdlFileRef(importFile(x))
}
case "extern" ~ x => {
new ExternFileRef(importFile(x))
}
}
}

def importFile(fileName: String): File = {
var file: Option[File] = None

val path = includePaths.find(path => {
val relPath = if (path.isEmpty) fileStack.top.getParent() else path
val tmp = new File(relPath, fileName)
val exists = tmp.exists
if (exists) file = Some(tmp)
exists
})

if (file.isEmpty) throw new FileNotFoundException("Unable to find file \"" + fileName + "\" at " + fileStack.top.getCanonicalPath)

return file.get
}

def filePath = "[^\"]*".r

def directive = importDirective | externDirective
Expand Down
4 changes: 2 additions & 2 deletions test-suite/djinni/all.djinni
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import "common.djinni"

@import "date.djinni"
@import "duration.djinni"
@import "vendor/third-party/date.djinni"
@import "third-party/duration.djinni"
8 changes: 4 additions & 4 deletions test-suite/generated-src/inFileList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ djinni/single_language_interfaces.djinni
djinni/extended_record.djinni
djinni/varnames.djinni
djinni/relative_paths.djinni
djinni/date.djinni
djinni/date.yaml
djinni/duration.djinni
djinni/duration.yaml
djinni/vendor/third-party/date.djinni
djinni/vendor/third-party/date.yaml
djinni/vendor/third-party/duration.djinni
djinni/vendor/third-party/duration.yaml
1 change: 1 addition & 0 deletions test-suite/run_djinni.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ fi
--yaml-prefix "test_" \
\
--idl "$in_relative" \
--idl-include-path "djinni/vendor" \
)

# Make sure we can parse back our own generated YAML file
Expand Down