Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vczyh opened this issue Sep 26, 2024 · 7 comments
Open

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

vczyh opened this issue Sep 26, 2024 · 7 comments

Comments

@vczyh
Copy link

vczyh commented Sep 26, 2024

Thanks for the repo!

I meet antlr/antlr4#4702

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

@vczyh vczyh changed the title I wantimplement in Golang, but ... I want implement in Golang, but ... Sep 26, 2024
@mike-lischke
Copy link
Owner

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
Copy link
Author

vczyh commented Sep 30, 2024

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
Copy link
Owner

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:/mysql/mysql-shell-plugins/blob/master/gui/frontend/src/parsing/mysql/MySQLCodeCompletion.ts

@vczyh
Copy link
Author

vczyh commented Oct 9, 2024

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:

  • Is that consistent with antlr4-c3? I actually got the same result using antlr4-c3.
  • Do I need to add ID to ignoreTokes to get the right result?

Thank you very much.

@mike-lischke
Copy link
Owner

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
Copy link
Author

vczyh commented Oct 9, 2024

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
Copy link
Author

vczyh commented Oct 12, 2024

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

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

PreferredRules:

  • functionRef
  • variableRef

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants