Antlr4: Please allow labelling without restriction

Created on 20 Oct 2019  路  3Comments  路  Source: antlr/antlr4

value
    : '"' text=((ESC | SAFECODEPOINT)*) '"' #jsonString
    | NUMBER #jsonNumber
    | obj #jsonObject
    | array #jsonArray
    | 'true' #jsonConstant
    | 'false' #jsonConstant
    | 'null' #jsonConstant
    ;

The text label cannot assign there, it cause error label text assigned to a block which is not a set

But I wish I could assign a label anywhere. So I could selectively grab only the text I care about from label directly instead of writing code to remove unused character of the syntax. Like a json string for example

All 3 comments

The current syntax for this would be:

text+=(ESC | SAFECODEPOINT)*

This would give you a list of tokens so you still need to concatenate them, or you could take the first and last tokens of the list and extract the corresponding range from the input CharStream.

Thank you, but are there any reason antlr need to put this restriction on labelling?

Originally, it was implementation complexity. It would be a fairly big feature to automatically extract a subrule for the labeled item, and then find a way to "inline" the subrule such that the final API makes sense to users (even today I have no idea what this API would look like). This is a large amount of work for something that's proven over time to not be needed in most cases.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tzmfreedom picture tzmfreedom  路  8Comments

dzavodnikov picture dzavodnikov  路  5Comments

WalkerCodeRanger picture WalkerCodeRanger  路  10Comments

miromannino picture miromannino  路  5Comments

nevetS picture nevetS  路  7Comments