Skip to content

Commit

Permalink
WIP -- still some failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 committed May 4, 2024
1 parent 28edbb7 commit 87a4498
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 34 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.IDENTIFIER_QUOTED().getStringValue(),
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 @@ -647,9 +666,9 @@ internal class PartiQLPigVisitor(

override fun visitExcludeExprTupleAttr(ctx: PartiQLParser.ExcludeExprTupleAttrContext) = PartiqlAst.build {
val attr = ctx.symbolPrimitive().getString()
val caseSensitivity = when (ctx.symbolPrimitive().ident.type) {
PartiQLParser.IDENTIFIER_QUOTED -> caseSensitive()
PartiQLParser.IDENTIFIER -> caseInsensitive()
val caseSensitivity = when (ctx.symbolPrimitive()) {
is PartiQLParser.QuotedIdentifierContext -> caseSensitive()
is PartiQLParser.UnquotedIdentifierContext -> caseInsensitive()
else -> throw ParserException("Invalid symbol reference.", ErrorCode.PARSE_INVALID_QUERY)
}
excludeTupleAttr(identifier(attr, caseSensitivity))
Expand Down Expand Up @@ -1179,7 +1198,7 @@ internal class PartiQLPigVisitor(

override fun visitVariableKeyword(ctx: PartiQLParser.VariableKeywordContext): PartiqlAst.PartiqlAstNode =
PartiqlAst.build {
val keyword = ctx.nonReservedKeywords().start.text
val keyword = ctx.nonReserved().start.text
val metas = ctx.start.getSourceMetaContainer()
val qualifier = ctx.qualifier?.let { localsFirst() } ?: unqualified()
id(keyword, caseInsensitive(), qualifier, metas)
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.start.getSourceMetaContainer() // TODO ALAN check for unreserved keywords
else -> throw ParserException(
"Unable to get identifier's source meta-container.",
ErrorCode.PARSE_INVALID_QUERY
Expand Down Expand Up @@ -2112,25 +2131,23 @@ internal class PartiQLPigVisitor(
}

private fun PartiQLParser.SymbolPrimitiveContext.getString(): String {
return when {
this.IDENTIFIER_QUOTED() != null -> this.IDENTIFIER_QUOTED().getStringValue()
this.IDENTIFIER() != null -> this.IDENTIFIER().text
return when (this) {
is PartiQLParser.QuotedIdentifierContext -> this.IDENTIFIER_QUOTED().getStringValue()
is PartiQLParser.UnquotedIdentifierContext -> this.text
else -> throw ParserException("Unable to get symbol's text.", ErrorCode.PARSE_INVALID_QUERY)
}
}

private fun getSymbolPathExpr(ctx: PartiQLParser.SymbolPrimitiveContext) = PartiqlAst.build {
when {
ctx.IDENTIFIER_QUOTED() != null -> pathExpr(
when (ctx) {
is PartiQLParser.QuotedIdentifierContext -> pathExpr(
lit(ionString(ctx.IDENTIFIER_QUOTED().getStringValue())), caseSensitive(),
metas = ctx.IDENTIFIER_QUOTED().getSourceMetaContainer()
metas = ctx.getSourceMetaContainer()
)

ctx.IDENTIFIER() != null -> pathExpr(
lit(ionString(ctx.IDENTIFIER().text)), caseInsensitive(),
metas = ctx.IDENTIFIER().getSourceMetaContainer()
is PartiQLParser.UnquotedIdentifierContext -> pathExpr(
lit(ionString(ctx.text)), caseInsensitive(),
metas = ctx.getSourceMetaContainer()
)

else -> throw ParserException("Unable to get symbol's text.", ErrorCode.PARSE_INVALID_QUERY)
}
}
Expand Down
16 changes: 11 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,11 +776,15 @@ 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
| LOWER
| UPPER
| EXISTS
| SIZE
;

/**
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(
// 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.IDENTIFIER_QUOTED().getStringValue(),
Identifier.CaseSensitivity.SENSITIVE,
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 @@ -635,7 +643,7 @@ internal class PartiQLParserDefault : PartiQLParser {
}

override fun visitColumnDeclaration(ctx: GeneratedParser.ColumnDeclarationContext) = translate(ctx) {
val name = visitAs<Identifier.Symbol> (ctx.columnName().symbolPrimitive())
val name = visitSymbolPrimitive(ctx.columnName().symbolPrimitive())
val type = (visit(ctx.type()) as Type).also {
isValidTypeDeclarationOrThrow(it, ctx.type())
}
Expand Down Expand Up @@ -2274,9 +2282,9 @@ internal class PartiQLParserDefault : PartiQLParser {
/**
* 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()
private fun symbolToString(ctx: GeneratedParser.SymbolPrimitiveContext) = when (ctx) {
is GeneratedParser.QuotedIdentifierContext -> ctx.IDENTIFIER_QUOTED().getStringValue()
is GeneratedParser.UnquotedIdentifierContext -> ctx.text
else -> throw error(ctx, "Invalid symbol reference.")
}

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 87a4498

Please sign in to comment.