Skip to content

Commit

Permalink
Enhanced query parser: Added ANTLR parser for query lang (#49)
Browse files Browse the repository at this point in the history
* Added ANTLR parser for query lang

- Now query parsing and conversion should be handled by ANTLR defined grammar

* Added robust condition based filtering support

* Stage 1: Commit for preserving last changes

* Added expr-lang with antlr

expr-lang helps in evaluating conditions and antlr in parsing query

* incorporating antlr replacing query parser and condition eval

* hooked antlr expression, alias, select into entity filtering

This is now effectively bridged between ANTLR parser and Source Code struct

* make lint happy 😄

* make test happy again 🚀

* Fix go version

* Add few more testcases

* Mapped filtering methods to the expr-lang 🚀

* Create alias based method invocation

* Added progress bar and multiple workers to parse the source code

* few cleanup

* Update query entity to include the methods

* Update query methods

* Add few testcase and make lint happy

* Add testcase files
  • Loading branch information
shivasurya authored Aug 3, 2024
1 parent 4d88c7c commit 266e0f6
Show file tree
Hide file tree
Showing 69 changed files with 5,768 additions and 703 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.22.0'

- name: Check out code
uses: actions/checkout@v3
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
example-java-project/
main
sourcecode-parser/pathfinder
.idea/
.gradle/
Expand Down
25 changes: 25 additions & 0 deletions sourcecode-parser/antlr/Query.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
grammar Query;

query : 'FIND' select_list ('WHERE' expression)? ;
select_list : select_item (',' select_item)* ;
select_item : entity 'AS' alias ;
entity : IDENTIFIER ;
alias : IDENTIFIER ;
expression : orExpression ;
orExpression : andExpression ( '||' andExpression )* ;
andExpression : primary ( '&&' primary )* ;
primary : condition
| '(' expression ')' ;
condition : alias '.' method_chain comparator (value | method_chain | '(' value_list ')') ;
method_chain : method_or_variable ('.' method_or_variable)* ;
method_or_variable : method | variable ;
method : IDENTIFIER '(' ')' ;
variable : IDENTIFIER ;
comparator : '==' | '!=' | '<' | '>' | '<=' | '>=' | 'LIKE' | 'IN' ;
value : STRING | NUMBER | STRING_WITH_WILDCARD ;
value_list : value (',' value)* ;
STRING : '"' ( ~('"' | '\\') | '\\' . )* '"' ;
STRING_WITH_WILDCARD : '"' ( ~('"' | '\\') | '\\' . | '%' )* '"' ;
NUMBER : [0-9]+ ('.' [0-9]+)? ;
IDENTIFIER : [a-zA-Z_][a-zA-Z0-9_]* ;
WS : [ \t\r\n]+ -> skip ;
72 changes: 72 additions & 0 deletions sourcecode-parser/antlr/Query.interp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
token literal names:
null
'FIND'
'WHERE'
','
'AS'
'||'
'&&'
'('
')'
'.'
'=='
'!='
'<'
'>'
'<='
'>='
'LIKE'
'IN'
null
null
null
null
null

token symbolic names:
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
STRING
STRING_WITH_WILDCARD
NUMBER
IDENTIFIER
WS

rule names:
query
select_list
select_item
entity
alias
expression
orExpression
andExpression
primary
condition
method_chain
method_or_variable
method
variable
comparator
value
value_list


atn:
[4, 1, 22, 124, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 1, 0, 1, 0, 1, 0, 1, 0, 3, 0, 39, 8, 0, 1, 1, 1, 1, 1, 1, 5, 1, 44, 8, 1, 10, 1, 12, 1, 47, 9, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 4, 1, 4, 1, 5, 1, 5, 1, 6, 1, 6, 1, 6, 5, 6, 62, 8, 6, 10, 6, 12, 6, 65, 9, 6, 1, 7, 1, 7, 1, 7, 5, 7, 70, 8, 7, 10, 7, 12, 7, 73, 9, 7, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 3, 8, 80, 8, 8, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 3, 9, 92, 8, 9, 1, 10, 1, 10, 1, 10, 5, 10, 97, 8, 10, 10, 10, 12, 10, 100, 9, 10, 1, 11, 1, 11, 3, 11, 104, 8, 11, 1, 12, 1, 12, 1, 12, 1, 12, 1, 13, 1, 13, 1, 14, 1, 14, 1, 15, 1, 15, 1, 16, 1, 16, 1, 16, 5, 16, 119, 8, 16, 10, 16, 12, 16, 122, 9, 16, 1, 16, 0, 0, 17, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 0, 2, 1, 0, 10, 17, 1, 0, 18, 20, 116, 0, 34, 1, 0, 0, 0, 2, 40, 1, 0, 0, 0, 4, 48, 1, 0, 0, 0, 6, 52, 1, 0, 0, 0, 8, 54, 1, 0, 0, 0, 10, 56, 1, 0, 0, 0, 12, 58, 1, 0, 0, 0, 14, 66, 1, 0, 0, 0, 16, 79, 1, 0, 0, 0, 18, 81, 1, 0, 0, 0, 20, 93, 1, 0, 0, 0, 22, 103, 1, 0, 0, 0, 24, 105, 1, 0, 0, 0, 26, 109, 1, 0, 0, 0, 28, 111, 1, 0, 0, 0, 30, 113, 1, 0, 0, 0, 32, 115, 1, 0, 0, 0, 34, 35, 5, 1, 0, 0, 35, 38, 3, 2, 1, 0, 36, 37, 5, 2, 0, 0, 37, 39, 3, 10, 5, 0, 38, 36, 1, 0, 0, 0, 38, 39, 1, 0, 0, 0, 39, 1, 1, 0, 0, 0, 40, 45, 3, 4, 2, 0, 41, 42, 5, 3, 0, 0, 42, 44, 3, 4, 2, 0, 43, 41, 1, 0, 0, 0, 44, 47, 1, 0, 0, 0, 45, 43, 1, 0, 0, 0, 45, 46, 1, 0, 0, 0, 46, 3, 1, 0, 0, 0, 47, 45, 1, 0, 0, 0, 48, 49, 3, 6, 3, 0, 49, 50, 5, 4, 0, 0, 50, 51, 3, 8, 4, 0, 51, 5, 1, 0, 0, 0, 52, 53, 5, 21, 0, 0, 53, 7, 1, 0, 0, 0, 54, 55, 5, 21, 0, 0, 55, 9, 1, 0, 0, 0, 56, 57, 3, 12, 6, 0, 57, 11, 1, 0, 0, 0, 58, 63, 3, 14, 7, 0, 59, 60, 5, 5, 0, 0, 60, 62, 3, 14, 7, 0, 61, 59, 1, 0, 0, 0, 62, 65, 1, 0, 0, 0, 63, 61, 1, 0, 0, 0, 63, 64, 1, 0, 0, 0, 64, 13, 1, 0, 0, 0, 65, 63, 1, 0, 0, 0, 66, 71, 3, 16, 8, 0, 67, 68, 5, 6, 0, 0, 68, 70, 3, 16, 8, 0, 69, 67, 1, 0, 0, 0, 70, 73, 1, 0, 0, 0, 71, 69, 1, 0, 0, 0, 71, 72, 1, 0, 0, 0, 72, 15, 1, 0, 0, 0, 73, 71, 1, 0, 0, 0, 74, 80, 3, 18, 9, 0, 75, 76, 5, 7, 0, 0, 76, 77, 3, 10, 5, 0, 77, 78, 5, 8, 0, 0, 78, 80, 1, 0, 0, 0, 79, 74, 1, 0, 0, 0, 79, 75, 1, 0, 0, 0, 80, 17, 1, 0, 0, 0, 81, 82, 3, 8, 4, 0, 82, 83, 5, 9, 0, 0, 83, 84, 3, 20, 10, 0, 84, 91, 3, 28, 14, 0, 85, 92, 3, 30, 15, 0, 86, 92, 3, 20, 10, 0, 87, 88, 5, 7, 0, 0, 88, 89, 3, 32, 16, 0, 89, 90, 5, 8, 0, 0, 90, 92, 1, 0, 0, 0, 91, 85, 1, 0, 0, 0, 91, 86, 1, 0, 0, 0, 91, 87, 1, 0, 0, 0, 92, 19, 1, 0, 0, 0, 93, 98, 3, 22, 11, 0, 94, 95, 5, 9, 0, 0, 95, 97, 3, 22, 11, 0, 96, 94, 1, 0, 0, 0, 97, 100, 1, 0, 0, 0, 98, 96, 1, 0, 0, 0, 98, 99, 1, 0, 0, 0, 99, 21, 1, 0, 0, 0, 100, 98, 1, 0, 0, 0, 101, 104, 3, 24, 12, 0, 102, 104, 3, 26, 13, 0, 103, 101, 1, 0, 0, 0, 103, 102, 1, 0, 0, 0, 104, 23, 1, 0, 0, 0, 105, 106, 5, 21, 0, 0, 106, 107, 5, 7, 0, 0, 107, 108, 5, 8, 0, 0, 108, 25, 1, 0, 0, 0, 109, 110, 5, 21, 0, 0, 110, 27, 1, 0, 0, 0, 111, 112, 7, 0, 0, 0, 112, 29, 1, 0, 0, 0, 113, 114, 7, 1, 0, 0, 114, 31, 1, 0, 0, 0, 115, 120, 3, 30, 15, 0, 116, 117, 5, 3, 0, 0, 117, 119, 3, 30, 15, 0, 118, 116, 1, 0, 0, 0, 119, 122, 1, 0, 0, 0, 120, 118, 1, 0, 0, 0, 120, 121, 1, 0, 0, 0, 121, 33, 1, 0, 0, 0, 122, 120, 1, 0, 0, 0, 9, 38, 45, 63, 71, 79, 91, 98, 103, 120]
39 changes: 39 additions & 0 deletions sourcecode-parser/antlr/Query.tokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
T__0=1
T__1=2
T__2=3
T__3=4
T__4=5
T__5=6
T__6=7
T__7=8
T__8=9
T__9=10
T__10=11
T__11=12
T__12=13
T__13=14
T__14=15
T__15=16
T__16=17
STRING=18
STRING_WITH_WILDCARD=19
NUMBER=20
IDENTIFIER=21
WS=22
'FIND'=1
'WHERE'=2
','=3
'AS'=4
'||'=5
'&&'=6
'('=7
')'=8
'.'=9
'=='=10
'!='=11
'<'=12
'>'=13
'<='=14
'>='=15
'LIKE'=16
'IN'=17
83 changes: 83 additions & 0 deletions sourcecode-parser/antlr/QueryLexer.interp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
token literal names:
null
'FIND'
'WHERE'
','
'AS'
'||'
'&&'
'('
')'
'.'
'=='
'!='
'<'
'>'
'<='
'>='
'LIKE'
'IN'
null
null
null
null
null

token symbolic names:
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
STRING
STRING_WITH_WILDCARD
NUMBER
IDENTIFIER
WS

rule names:
T__0
T__1
T__2
T__3
T__4
T__5
T__6
T__7
T__8
T__9
T__10
T__11
T__12
T__13
T__14
T__15
T__16
STRING
STRING_WITH_WILDCARD
NUMBER
IDENTIFIER
WS

channel names:
DEFAULT_TOKEN_CHANNEL
HIDDEN

mode names:
DEFAULT_MODE

atn:
[4, 0, 22, 147, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 2, 18, 7, 18, 2, 19, 7, 19, 2, 20, 7, 20, 2, 21, 7, 21, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 5, 1, 5, 1, 5, 1, 6, 1, 6, 1, 7, 1, 7, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 10, 1, 10, 1, 10, 1, 11, 1, 11, 1, 12, 1, 12, 1, 13, 1, 13, 1, 13, 1, 14, 1, 14, 1, 14, 1, 15, 1, 15, 1, 15, 1, 15, 1, 15, 1, 16, 1, 16, 1, 16, 1, 17, 1, 17, 1, 17, 1, 17, 5, 17, 102, 8, 17, 10, 17, 12, 17, 105, 9, 17, 1, 17, 1, 17, 1, 18, 1, 18, 1, 18, 1, 18, 1, 18, 5, 18, 114, 8, 18, 10, 18, 12, 18, 117, 9, 18, 1, 18, 1, 18, 1, 19, 4, 19, 122, 8, 19, 11, 19, 12, 19, 123, 1, 19, 1, 19, 4, 19, 128, 8, 19, 11, 19, 12, 19, 129, 3, 19, 132, 8, 19, 1, 20, 1, 20, 5, 20, 136, 8, 20, 10, 20, 12, 20, 139, 9, 20, 1, 21, 4, 21, 142, 8, 21, 11, 21, 12, 21, 143, 1, 21, 1, 21, 0, 0, 22, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8, 17, 9, 19, 10, 21, 11, 23, 12, 25, 13, 27, 14, 29, 15, 31, 16, 33, 17, 35, 18, 37, 19, 39, 20, 41, 21, 43, 22, 1, 0, 5, 2, 0, 34, 34, 92, 92, 1, 0, 48, 57, 3, 0, 65, 90, 95, 95, 97, 122, 4, 0, 48, 57, 65, 90, 95, 95, 97, 122, 3, 0, 9, 10, 13, 13, 32, 32, 156, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 15, 1, 0, 0, 0, 0, 17, 1, 0, 0, 0, 0, 19, 1, 0, 0, 0, 0, 21, 1, 0, 0, 0, 0, 23, 1, 0, 0, 0, 0, 25, 1, 0, 0, 0, 0, 27, 1, 0, 0, 0, 0, 29, 1, 0, 0, 0, 0, 31, 1, 0, 0, 0, 0, 33, 1, 0, 0, 0, 0, 35, 1, 0, 0, 0, 0, 37, 1, 0, 0, 0, 0, 39, 1, 0, 0, 0, 0, 41, 1, 0, 0, 0, 0, 43, 1, 0, 0, 0, 1, 45, 1, 0, 0, 0, 3, 50, 1, 0, 0, 0, 5, 56, 1, 0, 0, 0, 7, 58, 1, 0, 0, 0, 9, 61, 1, 0, 0, 0, 11, 64, 1, 0, 0, 0, 13, 67, 1, 0, 0, 0, 15, 69, 1, 0, 0, 0, 17, 71, 1, 0, 0, 0, 19, 73, 1, 0, 0, 0, 21, 76, 1, 0, 0, 0, 23, 79, 1, 0, 0, 0, 25, 81, 1, 0, 0, 0, 27, 83, 1, 0, 0, 0, 29, 86, 1, 0, 0, 0, 31, 89, 1, 0, 0, 0, 33, 94, 1, 0, 0, 0, 35, 97, 1, 0, 0, 0, 37, 108, 1, 0, 0, 0, 39, 121, 1, 0, 0, 0, 41, 133, 1, 0, 0, 0, 43, 141, 1, 0, 0, 0, 45, 46, 5, 70, 0, 0, 46, 47, 5, 73, 0, 0, 47, 48, 5, 78, 0, 0, 48, 49, 5, 68, 0, 0, 49, 2, 1, 0, 0, 0, 50, 51, 5, 87, 0, 0, 51, 52, 5, 72, 0, 0, 52, 53, 5, 69, 0, 0, 53, 54, 5, 82, 0, 0, 54, 55, 5, 69, 0, 0, 55, 4, 1, 0, 0, 0, 56, 57, 5, 44, 0, 0, 57, 6, 1, 0, 0, 0, 58, 59, 5, 65, 0, 0, 59, 60, 5, 83, 0, 0, 60, 8, 1, 0, 0, 0, 61, 62, 5, 124, 0, 0, 62, 63, 5, 124, 0, 0, 63, 10, 1, 0, 0, 0, 64, 65, 5, 38, 0, 0, 65, 66, 5, 38, 0, 0, 66, 12, 1, 0, 0, 0, 67, 68, 5, 40, 0, 0, 68, 14, 1, 0, 0, 0, 69, 70, 5, 41, 0, 0, 70, 16, 1, 0, 0, 0, 71, 72, 5, 46, 0, 0, 72, 18, 1, 0, 0, 0, 73, 74, 5, 61, 0, 0, 74, 75, 5, 61, 0, 0, 75, 20, 1, 0, 0, 0, 76, 77, 5, 33, 0, 0, 77, 78, 5, 61, 0, 0, 78, 22, 1, 0, 0, 0, 79, 80, 5, 60, 0, 0, 80, 24, 1, 0, 0, 0, 81, 82, 5, 62, 0, 0, 82, 26, 1, 0, 0, 0, 83, 84, 5, 60, 0, 0, 84, 85, 5, 61, 0, 0, 85, 28, 1, 0, 0, 0, 86, 87, 5, 62, 0, 0, 87, 88, 5, 61, 0, 0, 88, 30, 1, 0, 0, 0, 89, 90, 5, 76, 0, 0, 90, 91, 5, 73, 0, 0, 91, 92, 5, 75, 0, 0, 92, 93, 5, 69, 0, 0, 93, 32, 1, 0, 0, 0, 94, 95, 5, 73, 0, 0, 95, 96, 5, 78, 0, 0, 96, 34, 1, 0, 0, 0, 97, 103, 5, 34, 0, 0, 98, 102, 8, 0, 0, 0, 99, 100, 5, 92, 0, 0, 100, 102, 9, 0, 0, 0, 101, 98, 1, 0, 0, 0, 101, 99, 1, 0, 0, 0, 102, 105, 1, 0, 0, 0, 103, 101, 1, 0, 0, 0, 103, 104, 1, 0, 0, 0, 104, 106, 1, 0, 0, 0, 105, 103, 1, 0, 0, 0, 106, 107, 5, 34, 0, 0, 107, 36, 1, 0, 0, 0, 108, 115, 5, 34, 0, 0, 109, 114, 8, 0, 0, 0, 110, 111, 5, 92, 0, 0, 111, 114, 9, 0, 0, 0, 112, 114, 5, 37, 0, 0, 113, 109, 1, 0, 0, 0, 113, 110, 1, 0, 0, 0, 113, 112, 1, 0, 0, 0, 114, 117, 1, 0, 0, 0, 115, 113, 1, 0, 0, 0, 115, 116, 1, 0, 0, 0, 116, 118, 1, 0, 0, 0, 117, 115, 1, 0, 0, 0, 118, 119, 5, 34, 0, 0, 119, 38, 1, 0, 0, 0, 120, 122, 7, 1, 0, 0, 121, 120, 1, 0, 0, 0, 122, 123, 1, 0, 0, 0, 123, 121, 1, 0, 0, 0, 123, 124, 1, 0, 0, 0, 124, 131, 1, 0, 0, 0, 125, 127, 5, 46, 0, 0, 126, 128, 7, 1, 0, 0, 127, 126, 1, 0, 0, 0, 128, 129, 1, 0, 0, 0, 129, 127, 1, 0, 0, 0, 129, 130, 1, 0, 0, 0, 130, 132, 1, 0, 0, 0, 131, 125, 1, 0, 0, 0, 131, 132, 1, 0, 0, 0, 132, 40, 1, 0, 0, 0, 133, 137, 7, 2, 0, 0, 134, 136, 7, 3, 0, 0, 135, 134, 1, 0, 0, 0, 136, 139, 1, 0, 0, 0, 137, 135, 1, 0, 0, 0, 137, 138, 1, 0, 0, 0, 138, 42, 1, 0, 0, 0, 139, 137, 1, 0, 0, 0, 140, 142, 7, 4, 0, 0, 141, 140, 1, 0, 0, 0, 142, 143, 1, 0, 0, 0, 143, 141, 1, 0, 0, 0, 143, 144, 1, 0, 0, 0, 144, 145, 1, 0, 0, 0, 145, 146, 6, 21, 0, 0, 146, 44, 1, 0, 0, 0, 10, 0, 101, 103, 113, 115, 123, 129, 131, 137, 143, 1, 6, 0, 0]
39 changes: 39 additions & 0 deletions sourcecode-parser/antlr/QueryLexer.tokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
T__0=1
T__1=2
T__2=3
T__3=4
T__4=5
T__5=6
T__6=7
T__7=8
T__8=9
T__9=10
T__10=11
T__11=12
T__12=13
T__13=14
T__14=15
T__15=16
T__16=17
STRING=18
STRING_WITH_WILDCARD=19
NUMBER=20
IDENTIFIER=21
WS=22
'FIND'=1
'WHERE'=2
','=3
'AS'=4
'||'=5
'&&'=6
'('=7
')'=8
'.'=9
'=='=10
'!='=11
'<'=12
'>'=13
'<='=14
'>='=15
'LIKE'=16
'IN'=17
93 changes: 93 additions & 0 deletions sourcecode-parser/antlr/listener_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package parser

import (
"strings"

"github.com/antlr4-go/antlr/v4"
)

type Query struct {
SelectList []SelectList
Expression string
}

type CustomQueryListener struct {
BaseQueryListener
expression strings.Builder
selectList []SelectList
}

type SelectList struct {
Entity string
Alias string
}

func NewCustomQueryListener() *CustomQueryListener {
return &CustomQueryListener{
BaseQueryListener: BaseQueryListener{},
}
}

//nolint:all
func (l *CustomQueryListener) EnterSelect_list(ctx *Select_listContext) {
for i := 0; i < ctx.GetChildCount(); i++ {
child := ctx.GetChild(i).(antlr.ParseTree)
if child, ok := child.(ISelect_itemContext); ok {
l.selectList = append(l.selectList, SelectList{
Entity: child.Entity().GetText(),
Alias: child.Alias().GetText(),
})
}
}
}

func (l *CustomQueryListener) EnterExpression(ctx *ExpressionContext) {
if l.expression.Len() > 0 {
l.expression.WriteString(" ")
}
l.expression.WriteString(ctx.GetText())
}

func (l *CustomQueryListener) ExitOrExpression(ctx *OrExpressionContext) {
if ctx.GetChildCount() > 1 {
var result strings.Builder
for i := 0; i < ctx.GetChildCount(); i++ {
child := ctx.GetChild(i).(antlr.ParseTree) //nolint:all
if child.GetText() == "||" {
result.WriteString(" || ")
} else {
result.WriteString(child.GetText())
}
}
l.expression.Reset()
l.expression.WriteString(result.String())
}
}

func (l *CustomQueryListener) ExitAndExpression(ctx *AndExpressionContext) {
if ctx.GetChildCount() > 1 {
var result strings.Builder
for i := 0; i < ctx.GetChildCount(); i++ {
child := ctx.GetChild(i).(antlr.ParseTree) //nolint:all
if child.GetText() == "&&" {
result.WriteString(" && ")
} else {
result.WriteString(child.GetText())
}
}
l.expression.Reset()
l.expression.WriteString(result.String())
}
}

func ParseQuery(inputQuery string) Query {
inputStream := antlr.NewInputStream(inputQuery)
lexer := NewQueryLexer(inputStream)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
p := NewQueryParser(stream)

listener := NewCustomQueryListener()
antlr.ParseTreeWalkerDefault.Walk(listener, p.Query())

return Query{SelectList: listener.selectList, Expression: listener.expression.String()}
}
Loading

0 comments on commit 266e0f6

Please sign in to comment.