mike-lischke / antlr4-c3

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

I want implement in Golang, but ... #156

Open vczyh opened 3 weeks ago

vczyh commented 3 weeks ago

Thanks for the repo!

I meet https://github.com/antlr/antlr4/issues/4702

Is there another way to implement the function without using ruleToStartState variable?

mike-lischke commented 3 weeks ago

I'm afraid it won't work without this array. You usually don't know which ATN start state is used to start a parser rule. This array is generated when the ATN is loaded from the serialized ATN in your generated parser.

However, you could generate them yourself by walking over all ATN states and collect all that are rule start states. An ATN state always comes with the index of the rule it belongs to.

vczyh commented 2 weeks ago

Thank you very much. I have implement antlr4-c3 in Go. Next I want to implement SQL auto completion. I need some help: From what I understand, it can only parse the correct sql, I want to know how to get completion for

SELECT | FROM t1

Thank you agagin.

mike-lischke commented 2 weeks ago

Great to hear you managed the port to Go! Would you mind opening a PR here to add your port to the official repo (along with the other ports for C++, C# etc.)?

For code completion: important is that the SQL code is correct up to the invocation point of the engine. So in your case it should give you everything what's possible after SELECT.

btw. I have implemented SQL code completion myself, twice actually. Maybe you want to study that code for quicker results? Here's my latest implementation: https://github.com/mysql/mysql-shell-plugins/blob/master/gui/frontend/src/parsing/mysql/MySQLCodeCompletion.ts

vczyh commented 1 week ago

Hi lischke, I am having some problems.

SELECT name FROM users
// caretTokenIndex 4 is 'From' right?
collection := core.CollectCandidates(4, nil)

Collected tokens:
DOT_SYMBOL
AS_SYMBOL
ID
COMMA_SYMBOL
FROM_SYMBOL

By ignoring spaces, collected tokens inlucde ID, but obviously there can't be an ID, so incorret result is SELECT name some_id, that's illegal sql.

My question is:

Thank you very much.

mike-lischke commented 1 week ago

There can be an id: select a b from c is valid SQL. b serves as alias in this case (the as keyword is optional). Inspect the grammar for details.

vczyh commented 1 week ago

There can be an id: select a b from c is valid SQL. b serves as alias in this case (the as keyword is optional). Inspect the grammar for details.

Thank you for your reply, I did miss some details.

vczyh commented 1 week ago

Hello lischke, I'm sorry to bother you again.

// caretTokenIndex: 8 'b'
var c = a.b

PreferredRules:

Example1

simpleExpression:
    simpleExpression (PLUS | MINUS) simpleExpression
    | simpleExpression (MULTIPLY | DIVIDE) simpleExpression
    | functionRef
    | variableRef
;
variableRef: identifier dotIdentifier;
functionRef: identifier dotIdentifier OPEN_PAR CLOSE_PAR;
identifier: ID;
dotIdentifier: DOT identifier;

result:

States processed: 27
Collected rules:
variableRef, path: expression assignment simpleExpression, startTokenIndex: 6
Collected tokens:

Example 2

simpleExpression:
    simpleExpression (PLUS | MINUS) simpleExpression
    | simpleExpression (MULTIPLY | DIVIDE) simpleExpression
    | functionRef
    | variableRef
;
variableRef: identifier DOT identifier;
functionRef: identifier DOT identifier OPEN_PAR CLOSE_PAR;
identifier: ID;

result:

States processed: 26
Collected rules:
variableRef, path: expression assignment simpleExpression, startTokenIndex: 6
functionRef, path: expression assignment simpleExpression, startTokenIndex: 6
Collected tokens:

Why Example1 only has variableRef rule. The only difference between the Example1 and Example2 is dotIdentifier DOT identifier?

Thanks for your help!