Skip to content

Commit 30a3661

Browse files
committed
moved grammar to Antlr
1 parent ba8a957 commit 30a3661

32 files changed

+1992
-49
lines changed

compile.antlr.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
REM Run from src/main/resources
2+
@echo off
3+
antlr -listener -lib . -o ..\..\..\src\main\java\com\github\odiszapc\nginxparser\antlr -visitor Nginx.g4
File renamed without changes.

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
</properties>
6969

7070
<dependencies>
71+
<dependency>
72+
<groupId>org.antlr</groupId>
73+
<artifactId>antlr4-runtime</artifactId>
74+
<version>4.3</version>
75+
</dependency>
76+
7177
<dependency>
7278
<groupId>junit</groupId>
7379
<artifactId>junit</artifactId>

src/main/java/com/github/odiszapc/nginxparser/NgxAbstractEntry.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public void addValue(NgxToken token) {
3838
tokens.add(token);
3939
}
4040

41+
public void addValue(String value) {
42+
addValue(new NgxToken(value));
43+
}
44+
4145
@Override
4246
public String toString() {
4347
StringBuilder builder = new StringBuilder();

src/main/java/com/github/odiszapc/nginxparser/NgxBlock.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,4 @@ public <T extends NgxEntry> List<NgxEntry> findAll(Class<T> clazz, List<NgxEntry
108108

109109
return res;
110110
}
111-
112-
113111
}

src/main/java/com/github/odiszapc/nginxparser/NgxConfig.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616

1717
package com.github.odiszapc.nginxparser;
1818

19-
import com.github.odiszapc.nginxparser.parser.NginxConfigParser;
20-
import com.github.odiszapc.nginxparser.parser.ParseException;
19+
import com.github.odiszapc.nginxparser.antlr.NginxLexer;
20+
import com.github.odiszapc.nginxparser.antlr.NginxListenerImpl;
21+
import com.github.odiszapc.nginxparser.antlr.NginxParser;
22+
import com.github.odiszapc.nginxparser.javacc.NginxConfigParser;
23+
import com.github.odiszapc.nginxparser.javacc.ParseException;
24+
import org.antlr.v4.runtime.ANTLRInputStream;
25+
import org.antlr.v4.runtime.CommonTokenStream;
26+
import org.antlr.v4.runtime.tree.ParseTree;
27+
import org.antlr.v4.runtime.tree.ParseTreeWalker;
2128

2229
import java.io.FileInputStream;
2330
import java.io.IOException;
@@ -48,18 +55,37 @@ public static NgxConfig read(String path) throws IOException, ParseException {
4855
return parser.parse();
4956
}
5057

58+
public static NgxConfig read(InputStream in) throws IOException {
59+
return readAntlr(in);
60+
}
61+
5162
/**
5263
* Read config from existing stream
5364
* @param input stream to read from
5465
* @return Config object
5566
* @throws IOException
5667
* @throws ParseException
5768
*/
58-
public static NgxConfig read(InputStream input) throws IOException, ParseException {
69+
public static NgxConfig readJavaCC(InputStream input) throws IOException, ParseException {
5970
NginxConfigParser parser = new NginxConfigParser(input);
6071
return parser.parse();
6172
}
6273

74+
75+
public static NgxConfig readAntlr(InputStream in) throws IOException {
76+
ANTLRInputStream input = new ANTLRInputStream(in);
77+
NginxLexer lexer = new NginxLexer(input);
78+
CommonTokenStream tokens = new CommonTokenStream(lexer);
79+
NginxParser parser = new NginxParser(tokens);
80+
ParseTreeWalker walker = new ParseTreeWalker();
81+
82+
ParseTree tree = parser.config(); // begin parsing at init rule
83+
NginxListenerImpl listener = new NginxListenerImpl();
84+
walker.walk(listener, tree);
85+
86+
return listener.getResult();
87+
}
88+
6389
@Override
6490
public Collection<NgxToken> getTokens() {
6591
throw new IllegalStateException("Not implemented");

src/main/java/com/github/odiszapc/nginxparser/NgxIfBlock.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@
1717
package com.github.odiszapc.nginxparser;
1818

1919
public class NgxIfBlock extends NgxBlock {
20+
21+
public String toString() {
22+
return super.toString(); // TODO: add parentheses (looks like "if $a !~ ^1(2)3$ {" currently)
23+
}
2024
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Value=15
2+
WS=20
3+
Comment=17
4+
T__12=2
5+
T__11=3
6+
T__13=1
7+
T__1=13
8+
T__0=14
9+
T__10=4
10+
T__3=11
11+
T__2=12
12+
QUOTED_STRING=18
13+
SINGLE_QUOTED=19
14+
T__9=5
15+
T__8=6
16+
T__7=7
17+
STR_EXT=16
18+
T__6=8
19+
T__5=9
20+
T__4=10
21+
'rewrite'=14
22+
';'=13
23+
'}'=12
24+
'\\.'=11
25+
'if'=10
26+
'('=9
27+
'break'=8
28+
'location'=7
29+
')'=6
30+
'{'=5
31+
'last'=4
32+
'^'=3
33+
'permanent'=2
34+
'redirect'=1
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// Generated from Nginx.g4 by ANTLR 4.3
2+
3+
package com.github.odiszapc.nginxparser.antlr;
4+
import com.github.odiszapc.nginxparser.*;
5+
6+
7+
import org.antlr.v4.runtime.ParserRuleContext;
8+
import org.antlr.v4.runtime.misc.NotNull;
9+
import org.antlr.v4.runtime.tree.ErrorNode;
10+
import org.antlr.v4.runtime.tree.TerminalNode;
11+
12+
/**
13+
* This class provides an empty implementation of {@link NginxListener},
14+
* which can be extended to create a listener which only needs to handle a subset
15+
* of the available methods.
16+
*/
17+
public class NginxBaseListener implements NginxListener {
18+
/**
19+
* {@inheritDoc}
20+
*
21+
* <p>The default implementation does nothing.</p>
22+
*/
23+
@Override public void enterStatement(@NotNull NginxParser.StatementContext ctx) { }
24+
/**
25+
* {@inheritDoc}
26+
*
27+
* <p>The default implementation does nothing.</p>
28+
*/
29+
@Override public void exitStatement(@NotNull NginxParser.StatementContext ctx) { }
30+
31+
/**
32+
* {@inheritDoc}
33+
*
34+
* <p>The default implementation does nothing.</p>
35+
*/
36+
@Override public void enterIf_body(@NotNull NginxParser.If_bodyContext ctx) { }
37+
/**
38+
* {@inheritDoc}
39+
*
40+
* <p>The default implementation does nothing.</p>
41+
*/
42+
@Override public void exitIf_body(@NotNull NginxParser.If_bodyContext ctx) { }
43+
44+
/**
45+
* {@inheritDoc}
46+
*
47+
* <p>The default implementation does nothing.</p>
48+
*/
49+
@Override public void enterGenericBlockHeader(@NotNull NginxParser.GenericBlockHeaderContext ctx) { }
50+
/**
51+
* {@inheritDoc}
52+
*
53+
* <p>The default implementation does nothing.</p>
54+
*/
55+
@Override public void exitGenericBlockHeader(@NotNull NginxParser.GenericBlockHeaderContext ctx) { }
56+
57+
/**
58+
* {@inheritDoc}
59+
*
60+
* <p>The default implementation does nothing.</p>
61+
*/
62+
@Override public void enterIf_statement(@NotNull NginxParser.If_statementContext ctx) { }
63+
/**
64+
* {@inheritDoc}
65+
*
66+
* <p>The default implementation does nothing.</p>
67+
*/
68+
@Override public void exitIf_statement(@NotNull NginxParser.If_statementContext ctx) { }
69+
70+
/**
71+
* {@inheritDoc}
72+
*
73+
* <p>The default implementation does nothing.</p>
74+
*/
75+
@Override public void enterBlock(@NotNull NginxParser.BlockContext ctx) { }
76+
/**
77+
* {@inheritDoc}
78+
*
79+
* <p>The default implementation does nothing.</p>
80+
*/
81+
@Override public void exitBlock(@NotNull NginxParser.BlockContext ctx) { }
82+
83+
/**
84+
* {@inheritDoc}
85+
*
86+
* <p>The default implementation does nothing.</p>
87+
*/
88+
@Override public void enterConfig(@NotNull NginxParser.ConfigContext ctx) { }
89+
/**
90+
* {@inheritDoc}
91+
*
92+
* <p>The default implementation does nothing.</p>
93+
*/
94+
@Override public void exitConfig(@NotNull NginxParser.ConfigContext ctx) { }
95+
96+
/**
97+
* {@inheritDoc}
98+
*
99+
* <p>The default implementation does nothing.</p>
100+
*/
101+
@Override public void enterRewriteStatement(@NotNull NginxParser.RewriteStatementContext ctx) { }
102+
/**
103+
* {@inheritDoc}
104+
*
105+
* <p>The default implementation does nothing.</p>
106+
*/
107+
@Override public void exitRewriteStatement(@NotNull NginxParser.RewriteStatementContext ctx) { }
108+
109+
/**
110+
* {@inheritDoc}
111+
*
112+
* <p>The default implementation does nothing.</p>
113+
*/
114+
@Override public void enterGenericStatement(@NotNull NginxParser.GenericStatementContext ctx) { }
115+
/**
116+
* {@inheritDoc}
117+
*
118+
* <p>The default implementation does nothing.</p>
119+
*/
120+
@Override public void exitGenericStatement(@NotNull NginxParser.GenericStatementContext ctx) { }
121+
122+
/**
123+
* {@inheritDoc}
124+
*
125+
* <p>The default implementation does nothing.</p>
126+
*/
127+
@Override public void enterRegexp(@NotNull NginxParser.RegexpContext ctx) { }
128+
/**
129+
* {@inheritDoc}
130+
*
131+
* <p>The default implementation does nothing.</p>
132+
*/
133+
@Override public void exitRegexp(@NotNull NginxParser.RegexpContext ctx) { }
134+
135+
/**
136+
* {@inheritDoc}
137+
*
138+
* <p>The default implementation does nothing.</p>
139+
*/
140+
@Override public void enterLocationBlockHeader(@NotNull NginxParser.LocationBlockHeaderContext ctx) { }
141+
/**
142+
* {@inheritDoc}
143+
*
144+
* <p>The default implementation does nothing.</p>
145+
*/
146+
@Override public void exitLocationBlockHeader(@NotNull NginxParser.LocationBlockHeaderContext ctx) { }
147+
148+
/**
149+
* {@inheritDoc}
150+
*
151+
* <p>The default implementation does nothing.</p>
152+
*/
153+
@Override public void enterEveryRule(@NotNull ParserRuleContext ctx) { }
154+
/**
155+
* {@inheritDoc}
156+
*
157+
* <p>The default implementation does nothing.</p>
158+
*/
159+
@Override public void exitEveryRule(@NotNull ParserRuleContext ctx) { }
160+
/**
161+
* {@inheritDoc}
162+
*
163+
* <p>The default implementation does nothing.</p>
164+
*/
165+
@Override public void visitTerminal(@NotNull TerminalNode node) { }
166+
/**
167+
* {@inheritDoc}
168+
*
169+
* <p>The default implementation does nothing.</p>
170+
*/
171+
@Override public void visitErrorNode(@NotNull ErrorNode node) { }
172+
}

0 commit comments

Comments
 (0)