Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add include_file statement #74

Merged
merged 23 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
alancai98 marked this conversation as resolved.
Show resolved Hide resolved

import ::= `(import <path-to-include)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<path-to-import>?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path-to-file actually.


// 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.
dlurton marked this conversation as resolved.
Show resolved Hide resolved


#### 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()
dlurton marked this conversation as resolved.
Show resolved Hide resolved
}
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
dlurton marked this conversation as resolved.
Show resolved Hide resolved
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 @@
/*
dlurton marked this conversation as resolved.
Show resolved Hide resolved
* 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.
*/
dlurton marked this conversation as resolved.
Show resolved Hide resolved
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
almann marked this conversation as resolved.
Show resolved Hide resolved
* 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(sourceName: String) = FileInputStream(sourceName)

override fun getCanonicalName(sourceName: String): String = File(sourceName).canonicalFile.toString()
}
dlurton marked this conversation as resolved.
Show resolved Hide resolved
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)
dlurton marked this conversation as resolved.
Show resolved Hide resolved
: ParserErrorContext({ "Could not find imported type universe: $tag" })
dlurton marked this conversation as resolved.
Show resolved Hide resolved

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.
*/

dlurton marked this conversation as resolved.
Show resolved Hide resolved
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
dlurton marked this conversation as resolved.
Show resolved Hide resolved
* 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