Skip to content

Commit be594d7

Browse files
committed
commonlib: Add ANTLR
Signed-off-by: kingthorin <[email protected]>
1 parent dc146bf commit be594d7

File tree

7 files changed

+1315
-0
lines changed

7 files changed

+1315
-0
lines changed

addOns/commonlib/commonlib.gradle.kts

+35
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import net.ltgt.gradle.errorprone.errorprone
12
import org.zaproxy.gradle.addon.AddOnStatus
23

34
description = "A common library, for use by other add-ons."
45

6+
plugins {
7+
antlr
8+
}
9+
510
zapAddOn {
611
addOnName.set("Common Library")
712
addOnStatus.set(AddOnStatus.RELEASE)
@@ -32,9 +37,39 @@ dependencies {
3237
api("com.fasterxml.jackson.datatype:jackson-datatype-jdk8")
3338
api("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
3439

40+
val antlrVersion = "4.13.0"
41+
antlr("org.antlr:antlr4:$antlrVersion")
42+
implementation("org.antlr:antlr4-runtime:$antlrVersion")
43+
3544
implementation("commons-io:commons-io:2.16.1")
3645
implementation("org.apache.commons:commons-csv:1.10.0")
3746
implementation("org.apache.commons:commons-collections4:4.4")
3847

3948
testImplementation(project(":testutils"))
4049
}
50+
51+
val jsParserPkg = "org.zaproxy.addon.commonlib.parserapi.impl"
52+
val jsParserDir = jsParserPkg.replace('.', '/')
53+
val generateGrammarSource by tasks.existing(AntlrTask::class) {
54+
val libDir = "$outputDirectory/$jsParserDir"
55+
arguments = arguments + listOf("-package", jsParserPkg, "-lib", libDir)
56+
57+
doFirst {
58+
mkdir(libDir)
59+
}
60+
}
61+
62+
tasks.withType<JavaCompile>().configureEach {
63+
options.errorprone.excludedPaths.set(".*/(generated-src|$jsParserDir)/.*")
64+
}
65+
66+
tasks.named("generateGrammarSource") {
67+
mustRunAfter(tasks.named("generateEffectiveLombokConfig"))
68+
}
69+
70+
spotless {
71+
javaWith3rdPartyFormatted(
72+
project,
73+
listOf("src/**/$jsParserDir/*.java"),
74+
)
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 by Bart Kiers (original author) and Alexandre Vitorelli (contributor -> ported to CSharp)
5+
* Copyright (c) 2017-2020 by Ivan Kochurkin (Positive Technologies):
6+
added ECMAScript 6 support, cleared and transformed to the universal grammar.
7+
* Copyright (c) 2018 by Juan Alvarez (contributor -> ported to Go)
8+
* Copyright (c) 2019 by Student Main (contributor -> ES2020)
9+
*
10+
* Permission is hereby granted, free of charge, to any person
11+
* obtaining a copy of this software and associated documentation
12+
* files (the "Software"), to deal in the Software without
13+
* restriction, including without limitation the rights to use,
14+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
* copies of the Software, and to permit persons to whom the
16+
* Software is furnished to do so, subject to the following
17+
* conditions:
18+
*
19+
* The above copyright notice and this permission notice shall be
20+
* included in all copies or substantial portions of the Software.
21+
*
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
24+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29+
* OTHER DEALINGS IN THE SOFTWARE.
30+
*/
31+
32+
// $antlr-format alignTrailingComments true, columnLimit 150, maxEmptyLinesToKeep 1, reflowComments false, useTab false
33+
// $antlr-format allowShortRulesOnASingleLine true, allowShortBlocksOnASingleLine true, minEmptyLines 0, alignSemicolons ownLine
34+
// $antlr-format alignColons trailing, singleLineOverrulesHangingColon true, alignLexerCommands true, alignLabels true, alignTrailers true
35+
36+
lexer grammar JavaScriptLexer;
37+
38+
channels {
39+
ERROR
40+
}
41+
42+
options {
43+
superClass = JavaScriptLexerBase;
44+
}
45+
46+
// Insert here @header for C++ lexer.
47+
48+
HashBangLine : { this.IsStartOfFile()}? '#!' ~[\r\n\u2028\u2029]*; // only allowed at start
49+
MultiLineComment : '/*' .*? '*/' -> channel(HIDDEN);
50+
SingleLineComment : '//' ~[\r\n\u2028\u2029]* -> channel(HIDDEN);
51+
RegularExpressionLiteral:
52+
'/' RegularExpressionFirstChar RegularExpressionChar* {this.IsRegexPossible()}? '/' IdentifierPart*
53+
;
54+
55+
OpenBracket : '[';
56+
CloseBracket : ']';
57+
OpenParen : '(';
58+
CloseParen : ')';
59+
OpenBrace : '{' {this.ProcessOpenBrace();};
60+
TemplateCloseBrace : {this.IsInTemplateString()}? '}' // Break lines here to ensure proper transformation by Go/transformGrammar.py
61+
{this.ProcessTemplateCloseBrace();} -> popMode;
62+
CloseBrace : '}' {this.ProcessCloseBrace();};
63+
SemiColon : ';';
64+
Comma : ',';
65+
Assign : '=';
66+
QuestionMark : '?';
67+
QuestionMarkDot : '?.';
68+
Colon : ':';
69+
Ellipsis : '...';
70+
Dot : '.';
71+
PlusPlus : '++';
72+
MinusMinus : '--';
73+
Plus : '+';
74+
Minus : '-';
75+
BitNot : '~';
76+
Not : '!';
77+
Multiply : '*';
78+
Divide : '/';
79+
Modulus : '%';
80+
Power : '**';
81+
NullCoalesce : '??';
82+
Hashtag : '#';
83+
RightShiftArithmetic : '>>';
84+
LeftShiftArithmetic : '<<';
85+
RightShiftLogical : '>>>';
86+
LessThan : '<';
87+
MoreThan : '>';
88+
LessThanEquals : '<=';
89+
GreaterThanEquals : '>=';
90+
Equals_ : '==';
91+
NotEquals : '!=';
92+
IdentityEquals : '===';
93+
IdentityNotEquals : '!==';
94+
BitAnd : '&';
95+
BitXOr : '^';
96+
BitOr : '|';
97+
And : '&&';
98+
Or : '||';
99+
MultiplyAssign : '*=';
100+
DivideAssign : '/=';
101+
ModulusAssign : '%=';
102+
PlusAssign : '+=';
103+
MinusAssign : '-=';
104+
LeftShiftArithmeticAssign : '<<=';
105+
RightShiftArithmeticAssign : '>>=';
106+
RightShiftLogicalAssign : '>>>=';
107+
BitAndAssign : '&=';
108+
BitXorAssign : '^=';
109+
BitOrAssign : '|=';
110+
PowerAssign : '**=';
111+
NullishCoalescingAssign : '??=';
112+
ARROW : '=>';
113+
114+
/// Null Literals
115+
116+
NullLiteral: 'null';
117+
118+
/// Boolean Literals
119+
120+
BooleanLiteral: 'true' | 'false';
121+
122+
/// Numeric Literals
123+
124+
DecimalLiteral:
125+
DecimalIntegerLiteral '.' [0-9] [0-9_]* ExponentPart?
126+
| '.' [0-9] [0-9_]* ExponentPart?
127+
| DecimalIntegerLiteral ExponentPart?
128+
;
129+
130+
/// Numeric Literals
131+
132+
HexIntegerLiteral : '0' [xX] [0-9a-fA-F] HexDigit*;
133+
OctalIntegerLiteral : '0' [0-7]+ {!this.IsStrictMode()}?;
134+
OctalIntegerLiteral2 : '0' [oO] [0-7] [_0-7]*;
135+
BinaryIntegerLiteral : '0' [bB] [01] [_01]*;
136+
137+
BigHexIntegerLiteral : '0' [xX] [0-9a-fA-F] HexDigit* 'n';
138+
BigOctalIntegerLiteral : '0' [oO] [0-7] [_0-7]* 'n';
139+
BigBinaryIntegerLiteral : '0' [bB] [01] [_01]* 'n';
140+
BigDecimalIntegerLiteral : DecimalIntegerLiteral 'n';
141+
142+
/// Keywords
143+
144+
Break : 'break';
145+
Do : 'do';
146+
Instanceof : 'instanceof';
147+
Typeof : 'typeof';
148+
Case : 'case';
149+
Else : 'else';
150+
New : 'new';
151+
Var : 'var';
152+
Catch : 'catch';
153+
Finally : 'finally';
154+
Return : 'return';
155+
Void : 'void';
156+
Continue : 'continue';
157+
For : 'for';
158+
Switch : 'switch';
159+
While : 'while';
160+
Debugger : 'debugger';
161+
Function_ : 'function';
162+
This : 'this';
163+
With : 'with';
164+
Default : 'default';
165+
If : 'if';
166+
Throw : 'throw';
167+
Delete : 'delete';
168+
In : 'in';
169+
Try : 'try';
170+
As : 'as';
171+
From : 'from';
172+
Of : 'of';
173+
Yield : 'yield';
174+
YieldStar : 'yield*';
175+
176+
/// Future Reserved Words
177+
178+
Class : 'class';
179+
Enum : 'enum';
180+
Extends : 'extends';
181+
Super : 'super';
182+
Const : 'const';
183+
Export : 'export';
184+
Import : 'import';
185+
186+
Async : 'async';
187+
Await : 'await';
188+
189+
/// The following tokens are also considered to be FutureReservedWords
190+
/// when parsing strict mode
191+
192+
Implements : 'implements' {this.IsStrictMode()}?;
193+
StrictLet : 'let' {this.IsStrictMode()}?;
194+
NonStrictLet : 'let' {!this.IsStrictMode()}?;
195+
Private : 'private' {this.IsStrictMode()}?;
196+
Public : 'public' {this.IsStrictMode()}?;
197+
Interface : 'interface' {this.IsStrictMode()}?;
198+
Package : 'package' {this.IsStrictMode()}?;
199+
Protected : 'protected' {this.IsStrictMode()}?;
200+
Static : 'static' {this.IsStrictMode()}?;
201+
202+
/// Identifier Names and Identifiers
203+
204+
Identifier: IdentifierStart IdentifierPart*;
205+
/// String Literals
206+
StringLiteral:
207+
('"' DoubleStringCharacter* '"' | '\'' SingleStringCharacter* '\'') {this.ProcessStringLiteral();}
208+
;
209+
210+
BackTick: '`' -> pushMode(TEMPLATE);
211+
212+
WhiteSpaces: [\t\u000B\u000C\u0020\u00A0]+ -> channel(HIDDEN);
213+
214+
LineTerminator: [\r\n\u2028\u2029] -> channel(HIDDEN);
215+
216+
/// Comments
217+
218+
HtmlComment : '<!--' .*? '-->' -> channel(HIDDEN);
219+
CDataComment : '<![CDATA[' .*? ']]>' -> channel(HIDDEN);
220+
UnexpectedCharacter : . -> channel(ERROR);
221+
222+
mode TEMPLATE;
223+
224+
BackTickInside : '`' -> type(BackTick), popMode;
225+
TemplateStringStartExpression : '${' {this.ProcessTemplateOpenBrace();} -> pushMode(DEFAULT_MODE);
226+
TemplateStringAtom : ~[`];
227+
228+
// Fragment rules
229+
230+
fragment DoubleStringCharacter: ~["\\\r\n] | '\\' EscapeSequence | LineContinuation;
231+
232+
fragment SingleStringCharacter: ~['\\\r\n] | '\\' EscapeSequence | LineContinuation;
233+
234+
fragment EscapeSequence:
235+
CharacterEscapeSequence
236+
| '0' // no digit ahead! TODO
237+
| HexEscapeSequence
238+
| UnicodeEscapeSequence
239+
| ExtendedUnicodeEscapeSequence
240+
;
241+
242+
fragment CharacterEscapeSequence: SingleEscapeCharacter | NonEscapeCharacter;
243+
244+
fragment HexEscapeSequence: 'x' HexDigit HexDigit;
245+
246+
fragment UnicodeEscapeSequence:
247+
'u' HexDigit HexDigit HexDigit HexDigit
248+
| 'u' '{' HexDigit HexDigit+ '}'
249+
;
250+
251+
fragment ExtendedUnicodeEscapeSequence: 'u' '{' HexDigit+ '}';
252+
253+
fragment SingleEscapeCharacter: ['"\\bfnrtv];
254+
255+
fragment NonEscapeCharacter: ~['"\\bfnrtv0-9xu\r\n];
256+
257+
fragment EscapeCharacter: SingleEscapeCharacter | [0-9] | [xu];
258+
259+
fragment LineContinuation: '\\' [\r\n\u2028\u2029]+;
260+
261+
fragment HexDigit: [_0-9a-fA-F];
262+
263+
fragment DecimalIntegerLiteral: '0' | [1-9] [0-9_]*;
264+
265+
fragment ExponentPart: [eE] [+-]? [0-9_]+;
266+
267+
fragment IdentifierPart: IdentifierStart | [\p{Mn}] | [\p{Nd}] | [\p{Pc}] | '\u200C' | '\u200D';
268+
269+
fragment IdentifierStart: [\p{L}] | [$_] | '\\' UnicodeEscapeSequence;
270+
271+
fragment RegularExpressionFirstChar:
272+
~[*\r\n\u2028\u2029\\/[]
273+
| RegularExpressionBackslashSequence
274+
| '[' RegularExpressionClassChar* ']'
275+
;
276+
277+
fragment RegularExpressionChar:
278+
~[\r\n\u2028\u2029\\/[]
279+
| RegularExpressionBackslashSequence
280+
| '[' RegularExpressionClassChar* ']'
281+
;
282+
283+
fragment RegularExpressionClassChar: ~[\r\n\u2028\u2029\]\\] | RegularExpressionBackslashSequence;
284+
285+
fragment RegularExpressionBackslashSequence: '\\' ~[\r\n\u2028\u2029];

0 commit comments

Comments
 (0)