oranoran / antlr4-autosuggest-js

JavaScript auto-suggest engine for ANTLR4 grammars
MIT License
42 stars 16 forks source link

Stack overflow Issue when fetching suggestions #5

Closed debashish2014 closed 6 years ago

debashish2014 commented 6 years ago

Hi @oranoran,

I am using the following grammar file for testing. If user types 'SHOW EMPLOYEE ' then the suggestion should come as 'FOR' and 'WHERE', however, the autosuggest function is going into an infinite loop and is eventually causing stack overflow.

grammar autocomplete;

query : query_stmt EOF ;

query_stmt : start_keyword entity_name ( filter_name expr )? ;

expr : column_name | expr operator_exp literal_value | expr logical_exp expr ;

operator_exp :OPERATOR ;

logical_exp :K_AND ;

entity_name : any_name ;

column_name : any_name ;

any_name : IDENTIFIER | STRING_LITERAL ;

filter_name : K_FOR | K_WHERE ;

start_keyword : K_SHOW | K_SELECT ;

literal_value : NUMERIC_LITERAL | IDENTIFIER | STRING_LITERAL ;

K_SHOW : S H O W; K_SELECT : S E L E C T; K_AND : A N D; K_FOR : F O R; K_WHERE : W H E R E;

OPERATOR : ('=' | '!=' | '>=' | '<=' ) ;

IDENTIFIER : '"' (~'"' | '""') '"' | '' (~'' | '``') '`' | '[' ~']' ']' | [a-zA-Z_] [a-zA-Z_0-9] ;

STRING_LITERAL : '\'' ( ~'\'' | '\'\'' )* '\'' ;

NUMERIC_LITERAL : DIGIT+ ( '.' DIGIT* )? ( E [-+]? DIGIT+ )? | '.' DIGIT+ ( E [-+]? DIGIT+ )? ;

SPACES : [ \u000B\t\r\n] -> channel(HIDDEN) ;

UNEXPECTED_CHAR : . ;

fragment DIGIT : [0-9];

fragment A : [aA]; fragment B : [bB]; fragment C : [cC]; fragment D : [dD]; fragment E : [eE]; fragment F : [fF]; fragment G : [gG]; fragment H : [hH]; fragment I : [iI]; fragment J : [jJ]; fragment K : [kK]; fragment L : [lL]; fragment M : [mM]; fragment N : [nN]; fragment O : [oO]; fragment P : [pP]; fragment Q : [qQ]; fragment R : [rR]; fragment S : [sS]; fragment T : [tT]; fragment U : [uU]; fragment V : [vV]; fragment W : [wW]; fragment X : [xX]; fragment Y : [yY]; fragment Z : [zZ];

oranoran commented 6 years ago

The fix above fixes the stack overflow.

This grammar could also use a feature I haven't yet ported from the Java site - support for "case preference". Instead of suggesting 'FOR', 'For', 'FOr', 'FoR', etc - the autosuggest engine can be configured to suggest just uppercase or lowercase.

Also, with this grammar there are currently some more suggestions than expected - the word AND and operators. I think this has to do with a flaw in the grammar itself, rather than with the autosuggest engine.

debashish2014 commented 6 years ago

Thank you @oranoran for the update. I will retest and let you know. I will check the grammar file as well.

WiseBird commented 6 years ago

Version 0.0.27 still throws stack overflow for

clause
    : clause AND clause
    | action
    ;

action  : 'action' ;

AND : 'AND' ;

with action AND input

oranoran commented 6 years ago

@WiseBird thanks for the input, can you please open a separate issue for this?

WiseBird commented 6 years ago

@oranoran Sorry, I thought cause is the same, created #6

oranoran commented 6 years ago

Issue seems to have been indeed fixed in January. Case preference enhancement added today (commit ecfff1415acf1250b93a2f40a672425381ba28eb).