Skip to content

Commit e1d2b8d

Browse files
author
Psilo
committed
add antlr
1 parent 1794ac0 commit e1d2b8d

24 files changed

+1027
-435
lines changed

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
<attribute name="maven.pomderived" value="true"/>
3535
</attributes>
3636
</classpathentry>
37+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
3738
<classpathentry kind="output" path="target/classes"/>
3839
</classpath>

antlr4/.antlr/Rql.interp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
token literal names:
22
null
3-
'('
4-
')'
53
null
64
null
75
null
86
null
97
null
108
null
119
null
10+
'('
11+
')'
12+
null
1213
null
13-
14-
token symbolic names:
1514
null
1615
null
16+
17+
token symbolic names:
1718
null
1819
Operator
20+
Like
21+
NLike
1922
NullOp
2023
NotNullOp
2124
And
2225
Or
26+
ParOpen
27+
ParClose
2328
Identifier
29+
OptIdentifier
2430
JpqlIdentifier
2531
Whitespace
2632

@@ -29,11 +35,14 @@ eval
2935
orExpression
3036
andExpression
3137
atomExpression
38+
atomTerm
39+
and
40+
or
41+
parOpen
42+
parClose
43+
optTerm
3244
term
33-
operator
34-
identifier
35-
jpqlIdentifier
3645

3746

3847
atn:
39-
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 12, 72, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 29, 10, 3, 12, 3, 14, 3, 32, 11, 3, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 7, 4, 41, 10, 4, 12, 4, 14, 4, 44, 11, 4, 3, 5, 3, 5, 3, 5, 3,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3, 2, 2, 2, 54, 9, 3, 2, 2, 2, 55, 56, 5, 14, 8, 2, 56, 57, 8, 6, 1, 2, 57, 58, 7, 5, 2, 2, 58, 59, 8, 6, 1, 2, 59, 60, 5, 16, 9, 2, 60, 61, 8, 6, 1, 2, 61, 11, 3, 2, 2, 2, 62, 63, 7, 5, 2, 2, 63, 64, 8, 7, 1, 2, 64, 13, 3, 2, 2, 2, 65, 66, 7, 10, 2, 2, 66, 67, 8, 8, 1, 2, 67, 15, 3, 2, 2, 2, 68, 69, 7, 11, 2, 2, 69, 70, 8, 9, 1, 2, 70, 17, 3, 2, 2, 2, 5, 30, 42, 53]
48+
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 15, 72, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 31, 10, 3, 12, 3, 14, 3, 34, 11, 3, 3, 4, 3, 4, 3, 4, 3, 4, 7, 4, 40, 10, 4, 12, 4, 14, 4, 43, 11, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 5, 5, 50, 10, 5, 3, 6, 3, 6, 5, 6, 54, 10, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 10, 3, 10, 3, 11, 3, 11, 3, 11, 3, 11, 3, 12, 3, 12, 3, 12, 3, 12, 3, 12, 2, 2, 13, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 2, 2, 2, 64, 2, 24, 3, 2, 2, 2, 4, 26, 3, 2, 2, 2, 6, 35, 3, 2, 2, 2, 8, 49, 3, 2, 2, 2, 10, 53, 3, 2, 2, 2, 12, 55, 3, 2, 2, 2, 14, 57, 3, 2, 2, 2, 16, 59, 3, 2, 2, 2, 18, 61, 3, 2, 2, 2, 20, 63, 3, 2, 2, 2, 22, 67, 3, 2, 2, 2, 24, 25, 5, 4, 3, 2, 25, 3, 3, 2, 2, 2, 26, 32, 5, 6, 4, 2, 27, 28, 5, 14, 8, 2, 28, 29, 5, 6, 4, 2, 29, 31, 3, 2, 2, 2, 30, 27, 3, 2, 2, 2, 31, 34, 3, 2, 2, 2, 32, 30, 3, 2, 2, 2, 32, 33, 3, 2, 2, 2, 33, 5, 3, 2, 2, 2, 34, 32, 3, 2, 2, 2, 35, 41, 5, 8, 5, 2, 36, 37, 5, 12, 7, 2, 37, 38, 5, 8, 5, 2, 38, 40, 3, 2, 2, 2, 39, 36, 3, 2, 2, 2, 40, 43, 3, 2, 2, 2, 41, 39, 3, 2, 2, 2, 41, 42, 3, 2, 2, 2, 42, 7, 3, 2, 2, 2, 43, 41, 3, 2, 2, 2, 44, 50, 5, 10, 6, 2, 45, 46, 5, 16, 9, 2, 46, 47, 5, 4, 3, 2, 47, 48, 5, 18, 10, 2, 48, 50, 3, 2, 2, 2, 49, 44, 3, 2, 2, 2, 49, 45, 3, 2, 2, 2, 50, 9, 3, 2, 2, 2, 51, 54, 5, 20, 11, 2, 52, 54, 5, 22, 12, 2, 53, 51, 3, 2, 2, 2, 53, 52, 3, 2, 2, 2, 54, 11, 3, 2, 2, 2, 55, 56, 7, 8, 2, 2, 56, 13, 3, 2, 2, 2, 57, 58, 7, 9, 2, 2, 58, 15, 3, 2, 2, 2, 59, 60, 7, 10, 2, 2, 60, 17, 3, 2, 2, 2, 61, 62, 7, 11, 2, 2, 62, 19, 3, 2, 2, 2, 63, 64, 7, 13, 2, 2, 64, 65, 7, 3, 2, 2, 65, 66, 7, 14, 2, 2, 66, 21, 3, 2, 2, 2, 67, 68, 7, 12, 2, 2, 68, 69, 7, 3, 2, 2, 69, 70, 7, 14, 2, 2, 70, 23, 3, 2, 2, 2, 6, 32, 41, 49, 53]

antlr4/.antlr/RqlLexer.interp

Lines changed: 16 additions & 7 deletions
Large diffs are not rendered by default.

antlr4/.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "2.0.0",
6+
"configurations": [
7+
{
8+
"name": "Debug ANTLR4 grammar",
9+
"type": "antlr-debug",
10+
"request": "launch",
11+
"input": "testInput.txt",
12+
"grammar": "Rql.g4",
13+
"startRule": "eval",
14+
"printParseTree": true,
15+
"visualParseTree": true
16+
}
17+
]
18+
}

antlr4/Rql.g4

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,42 @@ grammar Rql;
33
/*
44
* Parser Rules start with lower-case characters.
55
*/
6-
eval returns [String r]
7-
: op=orExpression {$r = $op.text;}
8-
;
6+
eval : orExpression ;
97

10-
orExpression returns [String r]
11-
: op=andExpression {$r = $op.text;}
12-
(an=Or {$r += " " + $an.text + " ";}
13-
ae=andExpression {$r += $ae.text;}
14-
)*;
8+
orExpression : andExpression (or andExpression)* ;
159

16-
andExpression returns [String r]
17-
: op=atomExpression {$r = $op.text;}
18-
(an=And {$r += " " + $an.text + " ";}
19-
ae=atomExpression {$r += $ae.text;}
20-
)*;
10+
andExpression : atomExpression (and atomExpression)* ;
2111

22-
atomExpression returns [String r]
23-
: op=term {$r = $op.text;}
24-
| '(' or=orExpression {$r = $or.text;}
25-
')';
12+
atomExpression : atomTerm | (parOpen orExpression parClose) ;
2613

27-
term returns [String r]
28-
: id=identifier {$r = $id.text;}
29-
op=Operator {$r += $op.text;}
30-
jd=jpqlIdentifier {$r += $jd.text;}
31-
;
14+
atomTerm : optTerm | term ;
3215

33-
operator returns [String r]
34-
: op=Operator {$r = " " + $op.text + " ";}
35-
;
16+
and : And ;
3617

37-
identifier returns [String r]
38-
: op=Identifier {$r = $op.text;}
39-
;
18+
or : Or ;
4019

41-
jpqlIdentifier returns [String r]
42-
: op=JpqlIdentifier {$r = $op.text;}
43-
;
20+
parOpen : ParOpen ;
21+
22+
parClose : ParClose ;
23+
24+
optTerm : OptIdentifier Operator JpqlIdentifier ;
25+
term : Identifier Operator JpqlIdentifier ;
4426

4527
/*
4628
* Lexer Rules start with upper-case characters.
4729
*/
48-
Operator : ST | SEQ | GT | GEQ | EQ | NEQ | NullOp | NotNullOp;
30+
Operator : ST | SEQ | GT | GEQ | EQ | NEQ | Like | NLike | NullOp | NotNullOp;
31+
Like : L I K E;
32+
NLike : Not L I K E;
4933
NullOp : Is Null;
5034
NotNullOp : Is Not Null;
5135
And : A N D;
5236
Or : O R;
53-
Identifier : Alpha AlphaNum*;
54-
JpqlIdentifier : ':' Alpha AlphaNum*;
37+
ParOpen : '(';
38+
ParClose : ')';
39+
Identifier : (Alpha AlphaNum* '.')* Alpha AlphaNum*;
40+
OptIdentifier : '?' Identifier;
41+
JpqlIdentifier : ':' Alpha AlphaNum*;
5542
Whitespace : [ \t\r\n]+ -> channel(HIDDEN);
5643

5744
// a fragment rule can't be used inside parser rules, only in lexer rules

antlr4/testInput.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
userId = :userId AND (?timeFrom <= :timeFrom OR timeTo > :timeTo)

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>info.unterrainer.commons</groupId>
77
<artifactId>parent-pom</artifactId>
8-
<version>0.0.26</version>
8+
<version>0.0.28</version>
99
</parent>
1010

1111
<properties>

src/main/java/info/unterrainer/commons/httpserver/GenericHandlerGroupBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
import info.unterrainer.commons.httpserver.daos.BasicDao;
1010
import info.unterrainer.commons.httpserver.daos.DaoTransactionManager;
1111
import info.unterrainer.commons.httpserver.enums.Endpoint;
12+
import info.unterrainer.commons.httpserver.interceptors.InterceptorParamBuilder;
1213
import info.unterrainer.commons.httpserver.interceptors.delegates.GetListInterceptor;
14+
import info.unterrainer.commons.httpserver.rql.RqlUtils;
1315
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
1416
import info.unterrainer.commons.serialization.JsonMapper;
1517
import info.unterrainer.commons.serialization.jsons.BasicJson;
1618
import io.javalin.core.security.Role;
19+
import io.javalin.http.Context;
1720
import lombok.AccessLevel;
1821
import lombok.RequiredArgsConstructor;
1922
import ma.glasnost.orika.MapperFactory;
@@ -34,6 +37,10 @@ public class GenericHandlerGroupBuilder<P extends BasicJpa, J extends BasicJson,
3437
private List<GetListInterceptor> getListInterceptors = new ArrayList<>();
3538
private ExecutorService executorService;
3639

40+
private Context ctx;
41+
private HandlerUtils hu = new HandlerUtils();
42+
private RqlUtils rqlUtils = new RqlUtils(ctx, hu);
43+
3744
HandlerExtensions<P, J, E> extensions = new HandlerExtensions<>();
3845
private LinkedHashMap<Endpoint, Role[]> accessRoles = new LinkedHashMap<>();
3946

@@ -94,4 +101,10 @@ public GenericHandlerGroupBuilder<P, J, E> getListInterceptor(final GetListInter
94101
getListInterceptors.add(interceptor);
95102
return this;
96103
}
104+
105+
public InterceptorParamBuilder<P, J, E> getListInterceptor() {
106+
return new InterceptorParamBuilder<>(this, rqlUtils, data -> {
107+
getListInterceptors.add((ctx, hu) -> data);
108+
});
109+
}
97110
}

src/main/java/info/unterrainer/commons/httpserver/antlr/RqlBaseListener.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,49 +64,85 @@ public class RqlBaseListener implements RqlListener {
6464
*
6565
* <p>The default implementation does nothing.</p>
6666
*/
67-
@Override public void enterTerm(RqlParser.TermContext ctx) { }
67+
@Override public void enterAtomTerm(RqlParser.AtomTermContext ctx) { }
6868
/**
6969
* {@inheritDoc}
7070
*
7171
* <p>The default implementation does nothing.</p>
7272
*/
73-
@Override public void exitTerm(RqlParser.TermContext ctx) { }
73+
@Override public void exitAtomTerm(RqlParser.AtomTermContext ctx) { }
74+
/**
75+
* {@inheritDoc}
76+
*
77+
* <p>The default implementation does nothing.</p>
78+
*/
79+
@Override public void enterAnd(RqlParser.AndContext ctx) { }
80+
/**
81+
* {@inheritDoc}
82+
*
83+
* <p>The default implementation does nothing.</p>
84+
*/
85+
@Override public void exitAnd(RqlParser.AndContext ctx) { }
86+
/**
87+
* {@inheritDoc}
88+
*
89+
* <p>The default implementation does nothing.</p>
90+
*/
91+
@Override public void enterOr(RqlParser.OrContext ctx) { }
7492
/**
7593
* {@inheritDoc}
7694
*
7795
* <p>The default implementation does nothing.</p>
7896
*/
79-
@Override public void enterOperator(RqlParser.OperatorContext ctx) { }
97+
@Override public void exitOr(RqlParser.OrContext ctx) { }
8098
/**
8199
* {@inheritDoc}
82100
*
83101
* <p>The default implementation does nothing.</p>
84102
*/
85-
@Override public void exitOperator(RqlParser.OperatorContext ctx) { }
103+
@Override public void enterParOpen(RqlParser.ParOpenContext ctx) { }
86104
/**
87105
* {@inheritDoc}
88106
*
89107
* <p>The default implementation does nothing.</p>
90108
*/
91-
@Override public void enterIdentifier(RqlParser.IdentifierContext ctx) { }
109+
@Override public void exitParOpen(RqlParser.ParOpenContext ctx) { }
92110
/**
93111
* {@inheritDoc}
94112
*
95113
* <p>The default implementation does nothing.</p>
96114
*/
97-
@Override public void exitIdentifier(RqlParser.IdentifierContext ctx) { }
115+
@Override public void enterParClose(RqlParser.ParCloseContext ctx) { }
98116
/**
99117
* {@inheritDoc}
100118
*
101119
* <p>The default implementation does nothing.</p>
102120
*/
103-
@Override public void enterJpqlIdentifier(RqlParser.JpqlIdentifierContext ctx) { }
121+
@Override public void exitParClose(RqlParser.ParCloseContext ctx) { }
104122
/**
105123
* {@inheritDoc}
106124
*
107125
* <p>The default implementation does nothing.</p>
108126
*/
109-
@Override public void exitJpqlIdentifier(RqlParser.JpqlIdentifierContext ctx) { }
127+
@Override public void enterOptTerm(RqlParser.OptTermContext ctx) { }
128+
/**
129+
* {@inheritDoc}
130+
*
131+
* <p>The default implementation does nothing.</p>
132+
*/
133+
@Override public void exitOptTerm(RqlParser.OptTermContext ctx) { }
134+
/**
135+
* {@inheritDoc}
136+
*
137+
* <p>The default implementation does nothing.</p>
138+
*/
139+
@Override public void enterTerm(RqlParser.TermContext ctx) { }
140+
/**
141+
* {@inheritDoc}
142+
*
143+
* <p>The default implementation does nothing.</p>
144+
*/
145+
@Override public void exitTerm(RqlParser.TermContext ctx) { }
110146

111147
/**
112148
* {@inheritDoc}

src/main/java/info/unterrainer/commons/httpserver/antlr/RqlBaseVisitor.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,47 @@ public class RqlBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements Rq
4545
* <p>The default implementation returns the result of calling
4646
* {@link #visitChildren} on {@code ctx}.</p>
4747
*/
48-
@Override public T visitTerm(RqlParser.TermContext ctx) { return visitChildren(ctx); }
48+
@Override public T visitAtomTerm(RqlParser.AtomTermContext ctx) { return visitChildren(ctx); }
49+
/**
50+
* {@inheritDoc}
51+
*
52+
* <p>The default implementation returns the result of calling
53+
* {@link #visitChildren} on {@code ctx}.</p>
54+
*/
55+
@Override public T visitAnd(RqlParser.AndContext ctx) { return visitChildren(ctx); }
56+
/**
57+
* {@inheritDoc}
58+
*
59+
* <p>The default implementation returns the result of calling
60+
* {@link #visitChildren} on {@code ctx}.</p>
61+
*/
62+
@Override public T visitOr(RqlParser.OrContext ctx) { return visitChildren(ctx); }
63+
/**
64+
* {@inheritDoc}
65+
*
66+
* <p>The default implementation returns the result of calling
67+
* {@link #visitChildren} on {@code ctx}.</p>
68+
*/
69+
@Override public T visitParOpen(RqlParser.ParOpenContext ctx) { return visitChildren(ctx); }
4970
/**
5071
* {@inheritDoc}
5172
*
5273
* <p>The default implementation returns the result of calling
5374
* {@link #visitChildren} on {@code ctx}.</p>
5475
*/
55-
@Override public T visitOperator(RqlParser.OperatorContext ctx) { return visitChildren(ctx); }
76+
@Override public T visitParClose(RqlParser.ParCloseContext ctx) { return visitChildren(ctx); }
5677
/**
5778
* {@inheritDoc}
5879
*
5980
* <p>The default implementation returns the result of calling
6081
* {@link #visitChildren} on {@code ctx}.</p>
6182
*/
62-
@Override public T visitIdentifier(RqlParser.IdentifierContext ctx) { return visitChildren(ctx); }
83+
@Override public T visitOptTerm(RqlParser.OptTermContext ctx) { return visitChildren(ctx); }
6384
/**
6485
* {@inheritDoc}
6586
*
6687
* <p>The default implementation returns the result of calling
6788
* {@link #visitChildren} on {@code ctx}.</p>
6889
*/
69-
@Override public T visitJpqlIdentifier(RqlParser.JpqlIdentifierContext ctx) { return visitChildren(ctx); }
90+
@Override public T visitTerm(RqlParser.TermContext ctx) { return visitChildren(ctx); }
7091
}

0 commit comments

Comments
 (0)