Skip to content

Commit

Permalink
Adds a parseSingle() method for the parser
Browse files Browse the repository at this point in the history
Moves parser builder as nested class of parser
  • Loading branch information
johnedquinn committed Nov 1, 2024
1 parent b40e03e commit b94dfc4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.partiql.ast.v1.Statement
import org.partiql.cli.ErrorCodeString
import org.partiql.eval.Mode
import org.partiql.eval.compiler.PartiQLCompiler
import org.partiql.parser.PartiQLParserBuilderV1
import org.partiql.parser.PartiQLParserV1
import org.partiql.plan.Plan
import org.partiql.planner.PartiQLPlanner
Expand Down Expand Up @@ -35,10 +34,7 @@ internal class Pipeline private constructor(

private fun parse(source: String): Statement {
val result = listen(ctx.errorListener as AppPErrorListener) {
parser.parse(source, ctx)
}
if (result.statements.size != 1) {
throw PipelineException("Expected exactly one statement, got: ${result.statements.size}")
parser.parseSingle(source, ctx)
}
return result.statements[0]
}
Expand Down Expand Up @@ -83,7 +79,7 @@ internal class Pipeline private constructor(
private fun create(mode: Mode, out: PrintStream, config: Config): Pipeline {
val listener = config.getErrorListener(out)
val ctx = Context.of(listener)
val parser = PartiQLParserBuilderV1().build()
val parser = PartiQLParserV1.Builder().build()
val planner = PartiQLPlanner.builder().build()
val compiler = PartiQLCompiler.builder().build()
return Pipeline(parser, planner, compiler, ctx, mode)
Expand Down
13 changes: 7 additions & 6 deletions partiql-parser/api/partiql-parser.api
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ public class org/partiql/parser/PartiQLParserBuilder {
public fun build ()Lorg/partiql/parser/PartiQLParser;
}

public class org/partiql/parser/PartiQLParserBuilderV1 {
public fun <init> ()V
public fun build ()Lorg/partiql/parser/PartiQLParserV1;
}

public abstract interface class org/partiql/parser/PartiQLParserV1 {
public static fun builder ()Lorg/partiql/parser/PartiQLParserBuilderV1;
public static fun builder ()Lorg/partiql/parser/PartiQLParserV1$Builder;
public fun parse (Ljava/lang/String;)Lorg/partiql/parser/PartiQLParserV1$Result;
public abstract fun parse (Ljava/lang/String;Lorg/partiql/spi/Context;)Lorg/partiql/parser/PartiQLParserV1$Result;
public fun parseSingle (Ljava/lang/String;Lorg/partiql/spi/Context;)Lorg/partiql/parser/PartiQLParserV1$Result;
public static fun standard ()Lorg/partiql/parser/PartiQLParserV1;
}

public class org/partiql/parser/PartiQLParserV1$Builder {
public fun <init> ()V
public fun build ()Lorg/partiql/parser/PartiQLParserV1;
}

public final class org/partiql/parser/PartiQLParserV1$Result {
public field locations Lorg/partiql/spi/SourceLocations;
public field statements Ljava/util/List;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
import org.partiql.ast.v1.Statement;
import org.partiql.parser.internal.PartiQLParserDefaultV1;
import org.partiql.spi.Context;
import org.partiql.spi.SourceLocation;
import org.partiql.spi.SourceLocations;
import org.partiql.spi.errors.PError;
import org.partiql.spi.errors.PErrorKind;
import org.partiql.spi.errors.PErrorListenerException;
import org.partiql.spi.errors.Severity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* TODO: Rename to PartiQLParser
Expand All @@ -34,10 +41,40 @@ public interface PartiQLParserV1 {
* @param ctx a configuration object for the parser
* @throws PErrorListenerException when the [org.partiql.spi.errors.PErrorListener] defined in the [ctx] throws an
* [PErrorListenerException], this method halts execution and propagates the exception.
* @see PartiQLParserV1#parseSingle(String, Context)
*/
@NotNull
Result parse(@NotNull String source, @NotNull Context ctx) throws PErrorListenerException;

/**
* TODO
* @param source TODO
* @param ctx TODO
* @return TODO
* @throws PErrorListenerException TODO
* @see PartiQLParserV1#parse(String, Context)
*/
@NotNull
default Result parseSingle(@NotNull String source, @NotNull Context ctx) throws PErrorListenerException {
Result result = parse(source, ctx);
if (result.statements.size() != 1) {
SourceLocation location;
if (result.statements.size() > 1) {
location = result.locations.get(result.statements.get(1).tag);
} else {
location = null;
}
Map<String, Object> properties = new HashMap<String, Object>() {{
put("EXPECTED_TOKENS", new ArrayList<String>() {{
add("EOF");
}});
}};
PError pError = new PError(PError.UNEXPECTED_TOKEN, Severity.ERROR(), PErrorKind.SYNTAX(), location, properties);
ctx.getErrorListener().report(pError);
}
return result;
}

/**
* Parses the [source] into an AST.
* @param source the user's input
Expand Down Expand Up @@ -82,8 +119,8 @@ public Result(@NotNull List<Statement> statements, @NotNull SourceLocations loca
* @return TODO
*/
@NotNull
public static PartiQLParserBuilderV1 builder() {
return new PartiQLParserBuilderV1();
public static Builder builder() {
return new Builder();
}

/**
Expand All @@ -94,4 +131,21 @@ public static PartiQLParserBuilderV1 builder() {
public static PartiQLParserV1 standard() {
return new PartiQLParserDefaultV1();
}

/**
* A builder class to instantiate a {@link PartiQLParserV1}.
*/
public class Builder {
// TODO: Can this be replaced with Lombok?
// TODO: https://github.com/partiql/partiql-lang-kotlin/issues/1632

/**
* TODO
* @return TODO
*/
@NotNull
public PartiQLParserV1 build() {
return new PartiQLParserDefaultV1();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.partiql.planner
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import org.partiql.ast.v1.Statement
import org.partiql.parser.PartiQLParserBuilderV1
import org.partiql.parser.PartiQLParserV1
import org.partiql.plan.Operation
import org.partiql.planner.internal.typer.CompilerType
import org.partiql.planner.internal.typer.PlanTyper.Companion.toCType
Expand Down Expand Up @@ -42,7 +42,7 @@ internal class PlannerPErrorReportingTests {
.catalogs(catalog)
.build()

private val parser = PartiQLParserBuilderV1().build()
private val parser = PartiQLParserV1.Builder().build()

private val statement: ((String) -> Statement) = { query ->
val parseResult = parser.parse(query)
Expand Down

0 comments on commit b94dfc4

Please sign in to comment.