mike-lischke / antlr4-c3

A grammar agnostic code completion engine for ANTLR4 based parsers
MIT License
409 stars 62 forks source link

Return start token index for candidate rules #51

Closed kpainter-atl closed 4 years ago

kpainter-atl commented 4 years ago

In certain grammars, C3 may return a collection of candidate rules which logically start at different tokens in the string. For example the following grammar rule

clause: leftOperand operator rightOperand;

leftOperand: string;
operator: WAS | WAS IN
rightOperand: string;

For the clause status WAS I| we could receive the following candidate rules:

{
  [Parser.rule_operator]: [...ruleList],
  [Parser.rule_rightOperand]: [...ruleList]
}

In my case I'm showing suggestions to the user based on these rules and replacing text in the string when a suggestion is selected. However currently there's no way to determine which text/tokens should be replaced for a given rule.

To get around this I've forked the library and updated the rule collection to include the token index at which a candidate rule starts, e.g.

{
  [Parser.rule_operator]: {
    startTokenIndex: 2,
    ruleList: [...ruleList]
  },
  [Parser.rule_rightOperand]: {
    startTokenIndex: 4,
    ruleList: [...ruleList]
  }
}

I think this makes sense to include for other consumers so they can solve similar use cases in a grammar agnostic way.

mike-lischke commented 4 years ago

Fixed by patch #52.