Skip to content

Commit

Permalink
Add import statement (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlurton committed May 14, 2021
1 parent 7e9ae0f commit 79ef1e9
Show file tree
Hide file tree
Showing 25 changed files with 573 additions and 257 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ assertEquals(onePlusOne, anotherOnePlusOne)
// Top level
type_universe ::= <stmt>...
definition ::= '(' 'define' symbol <domain_definition> ')'
stmt ::= <definition> | <transform>
stmt ::= <definition> | <transform> | <include>
import ::= `(import <path-to-include)`
// Domain
domain_definition ::= <domain> | <permute_domain>
Expand Down Expand Up @@ -400,6 +402,20 @@ Unlike record elements, product element defintions must include identifiers.
(product int_pair first::int second::int)
```

#### Imports

It is possible to split type universe definitions among multiple files:

```
// root.ion:
(import "a.ion")
(import "b.ion")
```

The resulting type universe will contain all type domains from both `a.ion` and `b.ion`, `root.ion` may also
define additional type domains. The primary purpose of this is to be able to permute domains defined in another
file.


#### Using PIG In Your Project

Expand Down
2 changes: 1 addition & 1 deletion pig/src/org/partiql/pig/cmdline/Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import java.io.File
sealed class Command {
object ShowHelp : Command()
data class InvalidCommandLineArguments(val message: String) : Command()
data class Generate(val typeUniverseFile: File, val outputFile: File, val target: TargetLanguage) : Command()
data class Generate(val typeUniverseFile: String, val outputFile: String, val target: TargetLanguage) : Command()
}
2 changes: 1 addition & 1 deletion pig/src/org/partiql/pig/cmdline/CommandLineParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class CommandLineParser {
LanguageTargetType.CUSTOM -> TargetLanguage.Custom(optSet.valueOf(templateOpt))
}

Command.Generate(typeUniverseFile, outputFile, target)
Command.Generate(typeUniverseFile.toString(), outputFile.toString(), target)
}
}
} catch(ex: OptionException) {
Expand Down
6 changes: 4 additions & 2 deletions pig/src/org/partiql/pig/domain/model/SemanticErrorContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package org.partiql.pig.domain.model
import com.amazon.ionelement.api.IonLocation
import com.amazon.ionelement.api.MetaContainer
import com.amazon.ionelement.api.location
import org.partiql.pig.domain.parser.SourceLocation
import org.partiql.pig.domain.parser.sourceLocation
import org.partiql.pig.errors.PigException
import org.partiql.pig.errors.ErrorContext
import org.partiql.pig.errors.PigError
Expand Down Expand Up @@ -109,9 +111,9 @@ sealed class SemanticErrorContext(val msgFormatter: () -> String): ErrorContext
* Shortcut for throwing [PigException] with the specified metas and [PigError].
*/
fun semanticError(blame: MetaContainer, context: ErrorContext): Nothing =
semanticError(blame.location, context)
semanticError(blame.sourceLocation, context)
/**
* Shortcut for throwing [PigException] with the specified metas and [PigError].
*/
fun semanticError(blame: IonLocation?, context: ErrorContext): Nothing =
fun semanticError(blame: SourceLocation?, context: ErrorContext): Nothing =
throw PigException(PigError(blame, context))
34 changes: 34 additions & 0 deletions pig/src/org/partiql/pig/domain/parser/ImportSourceOpener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package org.partiql.pig.domain.parser

import com.amazon.ion.IonReader
import com.amazon.ion.system.IonReaderBuilder
import java.io.Closeable
import java.io.File
import java.io.FileInputStream
import java.nio.file.Path

//class ImportSource(
// val fullyQualifiedName: String,
// val reader: IonReader
//) : Closeable {
// override fun close() {
// reader.close()
// }
//}

// TODO: names
45 changes: 45 additions & 0 deletions pig/src/org/partiql/pig/domain/parser/InputSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package org.partiql.pig.domain.parser

import java.io.File
import java.io.FileInputStream
import java.io.InputStream

/**
* Provides an abstraction for file-system related functions used when importing files.
*/
internal interface InputSource {
/**
* Opens an input stream for the given source name.
*
* The [sourceName] is implementation-defined. In the case of a file system implementaiton it is the path to a
* file, either relative to the working directory or absolute.
*/
fun openStream(sourceName: String): InputStream

/**
* Returns the "canonical name" of the given source. In the case of a file system, this converts the relative
* path to an absolute path.
*/
fun getCanonicalName(sourceName: String): String
}

internal val FILE_SYSTEM_SOURCE = object : InputSource {
override fun openStream(qualifiedSource: String) = FileInputStream(qualifiedSource)

override fun getCanonicalName(sourceName: String): String = File(sourceName).canonicalFile.toString()
}
11 changes: 4 additions & 7 deletions pig/src/org/partiql/pig/domain/parser/ParserErrorContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ sealed class ParserErrorContext(val msgFormatter: () -> String): ErrorContext {
override fun hashCode(): Int = 0
}

data class CouldNotFindImportedTypeUniverse(val tag: String)
: ParserErrorContext({ "Could not find imported type universe: $tag" })

data class UnknownConstructor(val tag: String)
: ParserErrorContext({ "Unknown constructor: '$tag' (expected constructors are 'domain' or 'permute_domain')" })

Expand Down Expand Up @@ -79,8 +82,7 @@ sealed class ParserErrorContext(val msgFormatter: () -> String): ErrorContext {
: ParserErrorContext({ "Element has multiple name annotations"})
}


fun parseError(blame: IonLocation?, context: ErrorContext): Nothing =
fun parseError(blame: SourceLocation?, context: ErrorContext): Nothing =
PigError(blame, context).let {
throw when (context) {
is ParserErrorContext.IonElementError -> {
Expand All @@ -91,8 +93,3 @@ fun parseError(blame: IonLocation?, context: ErrorContext): Nothing =
}
}

fun parseError(blame: IonElement, context: ErrorContext): Nothing {
val loc = blame.metas.location
parseError(loc, context)
}

36 changes: 36 additions & 0 deletions pig/src/org/partiql/pig/domain/parser/SourceLocation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package org.partiql.pig.domain.parser

import com.amazon.ionelement.api.IonLocation
import com.amazon.ionelement.api.MetaContainer

/**
* Includes path to a source file and a position within it.
*
* Used to construct helpful error messages for the end-user, who will be able to know the file, line & column of
* a given error.
*/
data class SourceLocation(val path: String, val location: IonLocation) {
override fun toString(): String {
return "$path:$location"
}
}

internal const val SOURCE_LOCATION_META_TAG = "\$pig_source_location"

internal val MetaContainer.sourceLocation
get() = this[SOURCE_LOCATION_META_TAG] as? SourceLocation
Loading

0 comments on commit 79ef1e9

Please sign in to comment.