@@ -2254,19 +2254,19 @@ class SqlParser(private val ion: IonSystem) : Parser {
2254
2254
}
2255
2255
2256
2256
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 ())
2259
2259
2260
2260
/* *
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.
2262
2267
*/
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) {
2270
2270
node.throwTopLevelParserError()
2271
2271
}
2272
2272
@@ -2280,7 +2280,7 @@ class SqlParser(private val ion: IonSystem) : Parser {
2280
2280
node.throwTopLevelParserError()
2281
2281
}
2282
2282
}
2283
- node.children.map { validateTopLevelNodes(it, level + 1 , topTokens ) }
2283
+ node.children.map { validateTopLevelNodes(node = it, level = level + 1 , topLevelTokenSeen = topLevelTokenSeen || node.type.isTopLevelType ) }
2284
2284
}
2285
2285
2286
2286
/* * Entry point into the parser. */
@@ -2296,7 +2296,7 @@ class SqlParser(private val ion: IonSystem) : Parser {
2296
2296
}
2297
2297
}
2298
2298
2299
- validateTopLevelNodes(node, 0 , 0 )
2299
+ validateTopLevelNodes(node = node, level = 0 , topLevelTokenSeen = false )
2300
2300
2301
2301
return node.toExprNode()
2302
2302
}
0 commit comments