Skip to content

Commit

Permalink
Adds files reformatted by ktlintFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 committed Mar 3, 2022
1 parent 5df233c commit a22e6e0
Show file tree
Hide file tree
Showing 293 changed files with 15,398 additions and 11,671 deletions.
22 changes: 12 additions & 10 deletions cli/src/org/partiql/cli/Cli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ import java.io.OutputStreamWriter
/**
* TODO builder, kdoc
*/
internal class Cli(private val valueFactory: ExprValueFactory,
private val input: InputStream,
private val output: OutputStream,
private val format: OutputFormat,
private val compilerPipeline: CompilerPipeline,
private val globals: Bindings<ExprValue>,
private val query: String) : PartiQLCommand {
internal class Cli(
private val valueFactory: ExprValueFactory,
private val input: InputStream,
private val output: OutputStream,
private val format: OutputFormat,
private val compilerPipeline: CompilerPipeline,
private val globals: Bindings<ExprValue>,
private val query: String
) : PartiQLCommand {

companion object {
val ionTextWriterBuilder: IonTextWriterBuilder = IonTextWriterBuilder.standard()
.withWriteTopLevelValuesOnNewLines(true)
.withWriteTopLevelValuesOnNewLines(true)
}

override fun run() {
Expand All @@ -51,7 +53,7 @@ internal class Cli(private val valueFactory: ExprValueFactory,
val inputExprValue = valueFactory.newBag(inputIonValue)
val bindings = Bindings.buildLazyBindings<ExprValue> {
// If `input` is a class of `EmptyInputStream`, it means there is no input data provided by user.
if (input !is EmptyInputStream){ addBinding("input_data") { inputExprValue } }
if (input !is EmptyInputStream) { addBinding("input_data") { inputExprValue } }
}.delegate(globals)

val result = compilerPipeline.compile(query).eval(EvaluationSession.build { globals(bindings) })
Expand All @@ -74,7 +76,7 @@ internal class Cli(private val valueFactory: ExprValueFactory,
when (value.type) {
// writes top level bags as a datagram
ExprValueType.BAG -> value.iterator().forEach { v -> v.ionValue.writeTo(ionWriter) }
else -> value.ionValue.writeTo(ionWriter)
else -> value.ionValue.writeTo(ionWriter)
}
}
}
84 changes: 42 additions & 42 deletions cli/src/org/partiql/cli/Repl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private class GlobalBinding(private val valueFactory: ExprValueFactory) {
}
Bindings.empty<ExprValue>() -> {
} // nothing to do
else -> throw IllegalArgumentException("Invalid binding type for global environment: $bindings")
else -> throw IllegalArgumentException("Invalid binding type for global environment: $bindings")
}

return this
Expand Down Expand Up @@ -112,24 +112,27 @@ interface Timer {
/**
* TODO builder, kdoc
*/
internal class Repl(private val valueFactory: ExprValueFactory,
input: InputStream,
output: OutputStream,
private val parser: Parser,
private val compiler: CompilerPipeline,
initialGlobal: Bindings<ExprValue>,
private val timer: Timer = object : Timer {}
internal class Repl(
private val valueFactory: ExprValueFactory,
input: InputStream,
output: OutputStream,
private val parser: Parser,
private val compiler: CompilerPipeline,
initialGlobal: Bindings<ExprValue>,
private val timer: Timer = object : Timer {}
) : PartiQLCommand {

private val outputWriter = OutputStreamWriter(output, "UTF-8")

private inner class ReplCommands {
operator fun get(commandName: String): (String) -> ExprValue? = commands[commandName]
?: throw IllegalArgumentException("REPL command: '$commandName' not found! " + "use '!list_commands' to see all available commands")
?: throw IllegalArgumentException("REPL command: '$commandName' not found! " + "use '!list_commands' to see all available commands")

private val commands: Map<String, (String) -> ExprValue?> = mapOf("add_to_global_env" to ::addToGlobalEnv,
"global_env" to ::globalEnv,
"list_commands" to ::listCommands)
private val commands: Map<String, (String) -> ExprValue?> = mapOf(
"add_to_global_env" to ::addToGlobalEnv,
"global_env" to ::globalEnv,
"list_commands" to ::listCommands
)

private fun addToGlobalEnv(source: String): ExprValue? {
if (source == "") {
Expand All @@ -147,12 +150,14 @@ internal class Repl(private val valueFactory: ExprValueFactory,

private fun listCommands(@Suppress("UNUSED_PARAMETER") source: String): ExprValue? {
outputWriter.write("\n")
outputWriter.write("""
outputWriter.write(
"""
|!add_to_global_env: adds a value to the global environment
|!global_env: displays the current global environment
|!list_commands: print this message
|
""".trimMargin())
""".trimMargin()
)
return null
}
}
Expand All @@ -162,8 +167,7 @@ internal class Repl(private val valueFactory: ExprValueFactory,
val splitIndex = source.indexOfFirst { it == ' ' }.let {
if (it == -1) {
source.length
}
else {
} else {
it
}
}
Expand Down Expand Up @@ -213,7 +217,7 @@ internal class Repl(private val valueFactory: ExprValueFactory,
private fun printPrompt() {
when {
buffer.isEmpty() -> outputWriter.write(PROMPT_1)
else -> outputWriter.write(PROMPT_2)
else -> outputWriter.write(PROMPT_2)
}
outputWriter.flush()
}
Expand Down Expand Up @@ -244,17 +248,15 @@ internal class Repl(private val valueFactory: ExprValueFactory,
outputWriter.write("OK!")
outputWriter.write("\n")
outputWriter.flush()
}
catch (e: Exception) {
} catch (e: Exception) {
e.printStackTrace(PrintWriter(outputWriter))
outputWriter.write("ERROR!")
outputWriter.write("\n")
}

return if (line == null) {
ReplState.FINAL
}
else {
} else {
ReplState.READY
}
}
Expand All @@ -264,8 +266,7 @@ internal class Repl(private val valueFactory: ExprValueFactory,
val locals = Bindings.buildLazyBindings<ExprValue> { addBinding("_") { previousResult } }.delegate(globals.bindings)

compiler.compile(source).eval(EvaluationSession.build { globals(locals) })
}
else {
} else {
null
}
}
Expand All @@ -275,65 +276,64 @@ internal class Repl(private val valueFactory: ExprValueFactory,
val astStatementSexp = parser.parseAstStatement(source).toIonElement()
val astStatmentIonValue = astStatementSexp.asAnyElement().toIonValue(valueFactory.ion)
valueFactory.newFromIonValue(astStatmentIonValue)
}
else {
} else {
null
}
}

override fun run() {
while (state != ReplState.FINAL) {
state = when (state) {
ReplState.INIT -> {
ReplState.INIT -> {
printWelcomeMessage()
printVersionNumber()
ReplState.READY
}

ReplState.READY -> {
ReplState.READY -> {
line = readLine()
when {
line == null -> ReplState.FINAL
line == null -> ReplState.FINAL
arrayOf("!!", "").any { it == line } -> ReplState.EXECUTE_PARTIQL
line!!.startsWith("!") -> ReplState.READ_REPL_COMMAND
line!!.endsWith(";") -> ReplState.LAST_PARTIQL_LINE
else -> ReplState.READ_PARTIQL
line!!.startsWith("!") -> ReplState.READ_REPL_COMMAND
line!!.endsWith(";") -> ReplState.LAST_PARTIQL_LINE
else -> ReplState.READ_PARTIQL
}
}

ReplState.READ_PARTIQL -> {
ReplState.READ_PARTIQL -> {
buffer.appendln(line)
line = readLine()
when {
line == null -> ReplState.FINAL
line == "" -> ReplState.EXECUTE_PARTIQL
line == null -> ReplState.FINAL
line == "" -> ReplState.EXECUTE_PARTIQL
line!!.endsWith(";") -> ReplState.LAST_PARTIQL_LINE
line == "!!" -> ReplState.PARSE_PARTIQL_WITH_FILTER
else -> ReplState.READ_PARTIQL
line == "!!" -> ReplState.PARSE_PARTIQL_WITH_FILTER
else -> ReplState.READ_PARTIQL
}
}

ReplState.LAST_PARTIQL_LINE -> {
ReplState.LAST_PARTIQL_LINE -> {
buffer.appendln(line)
ReplState.EXECUTE_PARTIQL
}

ReplState.READ_REPL_COMMAND -> {
ReplState.READ_REPL_COMMAND -> {
buffer.appendln(line)
line = readLine()
when (line) {
null -> ReplState.FINAL
"" -> ReplState.EXECUTE_REPL_COMMAND
"" -> ReplState.EXECUTE_REPL_COMMAND
else -> ReplState.READ_REPL_COMMAND
}
}

ReplState.EXECUTE_PARTIQL -> executePartiQL()
ReplState.EXECUTE_PARTIQL -> executePartiQL()
ReplState.PARSE_PARTIQL_WITH_FILTER -> parsePartiQLWithFilters()
ReplState.EXECUTE_REPL_COMMAND -> executeReplCommand()
ReplState.EXECUTE_REPL_COMMAND -> executeReplCommand()

// shouldn't really happen
ReplState.FINAL -> ReplState.FINAL
ReplState.FINAL -> ReplState.FINAL
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions cli/src/org/partiql/cli/functions/BaseFunction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ import org.partiql.lang.eval.ExprValue
import org.partiql.lang.eval.ExprValueFactory

internal abstract class BaseFunction(val valueFactory: ExprValueFactory) : ExprFunction {
protected fun optionsStruct(requiredArity: Int,
args: List<ExprValue>,
optionsIndex: Int = requiredArity): IonStruct = when (args.size) {
requiredArity -> valueFactory.ion.newEmptyStruct()
protected fun optionsStruct(
requiredArity: Int,
args: List<ExprValue>,
optionsIndex: Int = requiredArity
): IonStruct = when (args.size) {
requiredArity -> valueFactory.ion.newEmptyStruct()
requiredArity + 1 -> extractOptVal(args, optionsIndex)
else -> throw IllegalArgumentException("Bad number of arguments: ${args.size}")
else -> throw IllegalArgumentException("Bad number of arguments: ${args.size}")
}

private fun extractOptVal(args: List<ExprValue>, optionsIndex: Int): IonStruct {
val optVal = args[optionsIndex].ionValue
return when (optVal) {
is IonStruct -> optVal
else -> throw IllegalArgumentException("Invalid option: $optVal")
else -> throw IllegalArgumentException("Invalid option: $optVal")
}
}
}

19 changes: 9 additions & 10 deletions cli/src/org/partiql/cli/functions/ReadFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ internal class ReadFile(valueFactory: ExprValueFactory) : BaseFunction(valueFact
)

private fun conversionModeFor(name: String) =
ConversionMode.values().find { it.name.toLowerCase() == name } ?:
throw IllegalArgumentException( "Unknown conversion: $name")
ConversionMode.values().find { it.name.toLowerCase() == name }
?: throw IllegalArgumentException("Unknown conversion: $name")

private fun fileReadHandler(csvFormat: CSVFormat): (InputStream, IonStruct) -> ExprValue = { input, options ->
val encoding = options["encoding"]?.stringValue() ?: "UTF-8"
Expand All @@ -58,13 +58,13 @@ internal class ReadFile(valueFactory: ExprValueFactory) : BaseFunction(valueFact
val quote = options["quote"]?.stringValue()?.first() // CSVParser library only accepts a single character as quote

val csvFormatWithOptions = csvFormat.withIgnoreEmptyLines(ignoreEmptyLine)
.withIgnoreSurroundingSpaces(ignoreSurroundingSpace)
.withTrim(trim)
.let { if (hasHeader) it.withFirstRecordAsHeader() else it }
.let { if (delimiter != null) it.withDelimiter(delimiter) else it }
.let { if (record != null) it.withRecordSeparator(record) else it }
.let { if (escape != null) it.withEscape(escape) else it }
.let { if (quote != null) it.withQuote(quote) else it }
.withIgnoreSurroundingSpaces(ignoreSurroundingSpace)
.withTrim(trim)
.let { if (hasHeader) it.withFirstRecordAsHeader() else it }
.let { if (delimiter != null) it.withDelimiter(delimiter) else it }
.let { if (record != null) it.withRecordSeparator(record) else it }
.let { if (escape != null) it.withEscape(escape) else it }
.let { if (quote != null) it.withQuote(quote) else it }

DelimitedValues.exprValue(valueFactory, reader, csvFormatWithOptions, conversionModeFor(conversion))
}
Expand Down Expand Up @@ -111,4 +111,3 @@ internal class ReadFile(valueFactory: ExprValueFactory) : BaseFunction(valueFact
return valueFactory.newBag(seq)
}
}

9 changes: 4 additions & 5 deletions cli/src/org/partiql/cli/functions/WriteFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ internal class WriteFile(valueFactory: ExprValueFactory) : BaseFunction(valueFac
private val writeHandlers = mapOf(
"tsv" to delimitedWriteHandler('\t'),
"csv" to delimitedWriteHandler(','),
"ion" to PRETTY_ION_WRITER)
"ion" to PRETTY_ION_WRITER
)

override fun callWithRequired(env: Environment, required: List<ExprValue>): ExprValue {
val fileName = required[0].stringValue()
Expand All @@ -72,8 +73,7 @@ internal class WriteFile(valueFactory: ExprValueFactory) : BaseFunction(valueFac
handler(results, it, valueFactory.ion.newEmptyStruct())
}
valueFactory.newBoolean(true)
}
catch (e: Exception) {
} catch (e: Exception) {
e.printStackTrace()
valueFactory.newBoolean(false)
}
Expand All @@ -91,8 +91,7 @@ internal class WriteFile(valueFactory: ExprValueFactory) : BaseFunction(valueFac
handler(results, it, options)
}
valueFactory.newBoolean(true)
}
catch (e: Exception) {
} catch (e: Exception) {
e.printStackTrace()
valueFactory.newBoolean(false)
}
Expand Down
Loading

0 comments on commit a22e6e0

Please sign in to comment.