Skip to content

Commit f375d2d

Browse files
committed
Addressed comments
1 parent a9de025 commit f375d2d

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

lang/src/org/partiql/lang/syntax/SqlParser.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,19 +2254,19 @@ class SqlParser(private val ion: IonSystem) : Parser {
22542254
}
22552255

22562256
private fun ParseNode.throwTopLevelParserError(): Nothing =
2257-
token?.err("Keyword $token only expected at the top level in the query", PARSE_UNEXPECTED_TERM)
2258-
?: throw ParserException("Keyword $token only expected at the top level in the query", PARSE_UNEXPECTED_TERM, PropertyValueMap())
2257+
token?.err("Keyword ${token.text} only expected at the top level in the query", PARSE_UNEXPECTED_TERM)
2258+
?: throw ParserException("Keyword ${token?.text} only expected at the top level in the query", PARSE_UNEXPECTED_TERM, PropertyValueMap())
22592259

22602260
/**
2261-
* Validates tree to make sure that the top level tokens are not found below the top level
2261+
* Validates tree to make sure that the top level tokens are not found below the top level.
2262+
* Top level tokens are the tokens or keywords which are valid to be used only at the top level in the query.
2263+
* i.e. these tokens cannot be used with a mix of other commands. Hence if more than one top level tokens are found
2264+
* in the query then it is invalid.
2265+
* [level] is the current traversal level in the parse tree.
2266+
* If [topLevelTokenSeen] is true, it means it has been encountered at least once before while traversing the parse tree.
22622267
*/
2263-
private fun validateTopLevelNodes(node: ParseNode, level: Int, topLevelTokens: Int) {
2264-
val topTokens = topLevelTokens + when(node.type.isTopLevelType) {
2265-
true -> 1
2266-
false -> 0
2267-
}
2268-
2269-
if (topTokens > 1) {
2268+
private fun validateTopLevelNodes(node: ParseNode, level: Int, topLevelTokenSeen: Boolean) {
2269+
if (topLevelTokenSeen && node.type.isTopLevelType) {
22702270
node.throwTopLevelParserError()
22712271
}
22722272

@@ -2280,7 +2280,7 @@ class SqlParser(private val ion: IonSystem) : Parser {
22802280
node.throwTopLevelParserError()
22812281
}
22822282
}
2283-
node.children.map { validateTopLevelNodes(it, level + 1, topTokens) }
2283+
node.children.map { validateTopLevelNodes(node = it, level = level + 1, topLevelTokenSeen = topLevelTokenSeen || node.type.isTopLevelType) }
22842284
}
22852285

22862286
/** Entry point into the parser. */
@@ -2296,7 +2296,7 @@ class SqlParser(private val ion: IonSystem) : Parser {
22962296
}
22972297
}
22982298

2299-
validateTopLevelNodes(node, 0, 0)
2299+
validateTopLevelNodes(node = node, level = 0, topLevelTokenSeen = false)
23002300

23012301
return node.toExprNode()
23022302
}

lang/test/org/partiql/lang/errors/ParserErrorsTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,8 @@ class ParserErrorsTest : TestBase() {
17401740
Property.TOKEN_TYPE to TokenType.KEYWORD,
17411741
Property.TOKEN_VALUE to ion.newSymbol("exec")))
17421742

1743+
// TODO: The token in the error message here should be "exec" instead of "undrop".
1744+
// Check this issue for more details. https://github.com/partiql/partiql-lang-kotlin/issues/372
17431745
@Test
17441746
fun execAtUnexpectedLocationInExpression() = checkInputThrowingParserException(
17451747
"SELECT * FROM (EXEC undrop 'foo')",

0 commit comments

Comments
 (0)