grammars-v4 icon indicating copy to clipboard operation
grammars-v4 copied to clipboard

Tsql Spaces are deleted

Open qt-wu opened this issue 3 months ago • 7 comments

When using tsql parsing, spaces are eliminated in the parsing statement

My sql statement is SELECT * FROM Customers WHERE CustomerName IN ('Jane Smith');, but the parsing statement is SELECT*FROMCustomersWHERECustomerNameIN('Jane Smith');

qt-wu avatar Mar 11 '24 09:03 qt-wu

image

qt-wu avatar Mar 11 '24 09:03 qt-wu

They are discarded during tokenization: https://github.com/antlr/grammars-v4/blob/master/antlr/antlr4/examples/TSqlLexer.g4#L846

If you want to keep them, change -> skip into -> channel(HIDDEN)

bkiers avatar Mar 11 '24 10:03 bkiers

If you want to keep them, change -> skip into -> channel(HIDDEN)

I don't think this is the problem. HIDDEN tokens are not included in parse tree anyway. The correct way to extract plain text over tree node is about getting startOffset and endOffset from left and right tree node's tokens and getting substring from plain text over this location.

KvanTTT avatar Mar 11 '24 11:03 KvanTTT

They're not in the parse tree, but the hidden tokens are being displayed when calling ParseTreeContext#ToString/GetText. At least, it used to work like that, did not try it now.

bkiers avatar Mar 11 '24 12:03 bkiers

Ok. But anyway I think -> channel(HIDDEN) should be used always and -> skip should be deprecated at all.

KvanTTT avatar Mar 11 '24 12:03 KvanTTT

Ok.

~Right. So it is still the case, it seems.~ (see comment below)

But anyway I think -> channel(HIDDEN) should be used always and -> skip should be deprecated at all.

That is a bit besides the point. The question was why the spaces are not shown when calling GetText(). Feel free to create a PR that replaces the -> skip in said grammar 😉

bkiers avatar Mar 11 '24 12:03 bkiers

EDIT: just tried the Java runtime, and ParserRuleContext#getText() does not include the text from the hidden tokens (anymore?). @qt-wu, you could try with C#, but I'd guess that does not include the text from hidden token as well.

bkiers avatar Mar 11 '24 12:03 bkiers