Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 committed May 2, 2024
1 parent 28edbb7 commit 02ad345
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ internal class PartiQLPigVisitor(

override fun visitByIdent(ctx: PartiQLParser.ByIdentContext) = visitSymbolPrimitive(ctx.symbolPrimitive())

/*
override fun visitSymbolPrimitive(ctx: PartiQLParser.SymbolPrimitiveContext) = PartiqlAst.build {
val metas = ctx.ident.getSourceMetaContainer()
when (ctx.ident.type) {
Expand All @@ -211,6 +212,24 @@ internal class PartiQLPigVisitor(
PartiQLParser.IDENTIFIER -> id(ctx.IDENTIFIER().getStringValue(), caseInsensitive(), unqualified(), metas)
else -> throw ParserException("Invalid symbol reference.", ErrorCode.PARSE_INVALID_QUERY)
}
}*/

fun visitSymbolPrimitive(ctx: PartiQLParser.SymbolPrimitiveContext) = PartiqlAst.build {
when (ctx) {
is PartiQLParser.QuotedIdentifierContext -> id(
ctx.text,
caseSensitive(),
unqualified(),
ctx.IDENTIFIER_QUOTED().getSourceMetaContainer()
)
is PartiQLParser.UnquotedIdentifierContext -> id(
ctx.text,
caseInsensitive(),
unqualified(),
ctx.IDENTIFIER().getSourceMetaContainer()
)
else -> throw ParserException("Invalid symbol reference.", ErrorCode.PARSE_INVALID_QUERY)
}
}

/**
Expand Down Expand Up @@ -1806,9 +1825,9 @@ internal class PartiQLPigVisitor(
}
}

private fun PartiQLParser.SymbolPrimitiveContext.getSourceMetaContainer() = when (this.ident.type) {
PartiQLParser.IDENTIFIER -> this.IDENTIFIER().getSourceMetaContainer()
PartiQLParser.IDENTIFIER_QUOTED -> this.IDENTIFIER_QUOTED().getSourceMetaContainer()
private fun PartiQLParser.SymbolPrimitiveContext.getSourceMetaContainer() = when (this) {
is PartiQLParser.QuotedIdentifierContext -> this.IDENTIFIER_QUOTED().getSourceMetaContainer()
is PartiQLParser.UnquotedIdentifierContext -> this.IDENTIFIER().getSourceMetaContainer()
else -> throw ParserException(
"Unable to get identifier's source meta-container.",
ErrorCode.PARSE_INVALID_QUERY
Expand Down Expand Up @@ -2112,7 +2131,7 @@ internal class PartiQLPigVisitor(
}

private fun PartiQLParser.SymbolPrimitiveContext.getString(): String {
return when {
return when (this) {
this.IDENTIFIER_QUOTED() != null -> this.IDENTIFIER_QUOTED().getStringValue()
this.IDENTIFIER() != null -> this.IDENTIFIER().text
else -> throw ParserException("Unable to get symbol's text.", ErrorCode.PARSE_INVALID_QUERY)
Expand Down
12 changes: 7 additions & 5 deletions partiql-parser/src/main/antlr/PartiQL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ byIdent
: BY symbolPrimitive;

symbolPrimitive
: ident=( IDENTIFIER | IDENTIFIER_QUOTED )
: IDENTIFIER # UnquotedIdentifier
| IDENTIFIER_QUOTED # QuotedIdentifier
| nonReserved # UnquotedIdentifier
;

/**
Expand Down Expand Up @@ -751,8 +753,8 @@ functionCall

// SQL-99 10.4 — <routine name> ::= [ <schema name> <period> ] <qualified identifier>
functionName
: (qualifier+=symbolPrimitive PERIOD)* name=( CHAR_LENGTH | CHARACTER_LENGTH | OCTET_LENGTH | BIT_LENGTH | UPPER | LOWER | SIZE | EXISTS | COUNT | MOD ) # FunctionNameReserved
| (qualifier+=symbolPrimitive PERIOD)* name=symbolPrimitive # FunctionNameSymbol
: (qualifier+=symbolPrimitive PERIOD)* name=( CHAR_LENGTH | CHARACTER_LENGTH | MOD ) # FunctionNameReserved
| (qualifier+=symbolPrimitive PERIOD)* name=symbolPrimitive # FunctionNameSymbol
;

pathStep
Expand All @@ -774,10 +776,10 @@ parameter

varRefExpr
: qualifier=AT_SIGN? ident=(IDENTIFIER|IDENTIFIER_QUOTED) # VariableIdentifier
| qualifier=AT_SIGN? key=nonReservedKeywords # VariableKeyword
| qualifier=AT_SIGN? key=nonReserved # VariableKeyword
;

nonReservedKeywords
nonReserved
: EXCLUDED
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,15 +560,23 @@ internal class PartiQLParserDefault : PartiQLParser {

override fun visitByIdent(ctx: GeneratedParser.ByIdentContext) = visitSymbolPrimitive(ctx.symbolPrimitive())

override fun visitSymbolPrimitive(ctx: GeneratedParser.SymbolPrimitiveContext) = translate(ctx) {
when (ctx.ident.type) {
GeneratedParser.IDENTIFIER_QUOTED -> identifierSymbol(
ctx.IDENTIFIER_QUOTED().getStringValue(),
Identifier.CaseSensitivity.SENSITIVE,
// override fun visitQuotedIdentifier(ctx: org.partiql.parser.internal.antlr.PartiQLParser.QuotedIdentifierContext?): AstNode {
// return super.visitQuotedIdentifier(ctx)
// }
//
// override fun visitUnquotedIdentifier(ctx: org.partiql.parser.internal.antlr.PartiQLParser.UnquotedIdentifierContext?): AstNode {
// return super.visitUnquotedIdentifier(ctx)
// }

fun visitSymbolPrimitive(ctx: GeneratedParser.SymbolPrimitiveContext) = translate(ctx) {
when (ctx) {
is GeneratedParser.QuotedIdentifierContext -> identifierSymbol(
ctx.text,
Identifier.CaseSensitivity.SENSITIVE
)
GeneratedParser.IDENTIFIER -> identifierSymbol(
ctx.IDENTIFIER().getStringValue(),
Identifier.CaseSensitivity.INSENSITIVE,
is GeneratedParser.UnquotedIdentifierContext -> identifierSymbol(
ctx.text,
Identifier.CaseSensitivity.INSENSITIVE
)
else -> throw error(ctx, "Invalid symbol reference.")
}
Expand Down Expand Up @@ -668,9 +676,9 @@ internal class PartiQLParserDefault : PartiQLParser {
}

override fun visitColumnConstraint(ctx: GeneratedParser.ColumnConstraintContext) = translate(ctx) {
val identifier = ctx.constraintName()?.let { symbolToString(it.symbolPrimitive()) }
val identifier = ctx.constraintName()?.let { visitSymbolPrimitive(it.symbolPrimitive()) }
val body = visit(ctx.columnConstraintDef()) as Constraint.Definition
constraint(identifier, body)
constraint(identifier?.symbol, body)
}

override fun visitColConstrNotNull(ctx: GeneratedParser.ColConstrNotNullContext) = translate(ctx) {
Expand Down Expand Up @@ -705,9 +713,9 @@ internal class PartiQLParserDefault : PartiQLParser {
}

override fun visitTableConstrDeclaration(ctx: GeneratedParser.TableConstrDeclarationContext) = translate(ctx) {
val identifier = ctx.constraintName()?.let { symbolToString(it.symbolPrimitive()) }
val identifier = ctx.constraintName()?.let { visitSymbolPrimitive(it.symbolPrimitive()) }
val body = visit(ctx.tableConstraintDef()) as Constraint.Definition
constraint(identifier, body)
constraint(identifier?.symbol, body)
}

/**
Expand Down Expand Up @@ -2271,14 +2279,15 @@ internal class PartiQLParserDefault : PartiQLParser {

private inline fun <reified T : AstNode> visitAs(ctx: ParserRuleContext): T = visit(ctx) as T

/**
* Visiting a symbol to get a string, skip the wrapping, unwrapping, and location tracking.
*/
private fun symbolToString(ctx: GeneratedParser.SymbolPrimitiveContext) = when (ctx.ident.type) {
GeneratedParser.IDENTIFIER_QUOTED -> ctx.IDENTIFIER_QUOTED().getStringValue()
GeneratedParser.IDENTIFIER -> ctx.IDENTIFIER().getStringValue()
else -> throw error(ctx, "Invalid symbol reference.")
}
// TODO ALAN make correct
// /**
// * Visiting a symbol to get a string, skip the wrapping, unwrapping, and location tracking.
// */
// private fun symbolToString(ctx: GeneratedParser.SymbolPrimitiveContext) = when (ctx.ident.type) {
// GeneratedParser.IDENTIFIER_QUOTED -> ctx.IDENTIFIER_QUOTED().getStringValue()
// GeneratedParser.IDENTIFIER -> ctx.IDENTIFIER().getStringValue()
// else -> throw error(ctx, "Invalid symbol reference.")
// }

/**
* Convert [ALL|DISTINCT] to SetQuantifier Enum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ class PartiQLParserFunctionCallTests {
}
)

@Test
fun foo() = assertExpression(
"EXI(true)",
query {
exprCall(
function = identifierSymbol("upper", Identifier.CaseSensitivity.INSENSITIVE),
args = emptyList()
)
}
)

@Test
fun callUnqualifiedReservedSensitive() = assertExpression(
"\"upper\"()",
Expand Down

0 comments on commit 02ad345

Please sign in to comment.