Skip to content

Commit f9421e4

Browse files
authored
Add '$' syntax as a shortcut for 'field' in Painless (#80518)
This adds a shortcut for the field method when a script context supports the scripting fields API. This shortcut replaces field('fieldname').get(defaultValue) with $('fieldname', defaultValue). This change builds this into the Painless compiler, but eventually this can be easily refactored out to make it so the '$' could be specialized per context.
1 parent 0630461 commit f9421e4

File tree

11 files changed

+974
-727
lines changed

11 files changed

+974
-727
lines changed

modules/lang-painless/src/main/antlr/PainlessLexer.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ LBRACE: '[';
2222
RBRACE: ']';
2323
LP: '(';
2424
RP: ')';
25+
DOLLAR: '$';
2526
// We switch modes after a dot to ensure there are not conflicts
2627
// between shortcuts and decimal values. Without the mode switch
2728
// shortcuts such as id.0.0 will fail because 0.0 will be interpreted

modules/lang-painless/src/main/antlr/PainlessLexer.tokens

Lines changed: 146 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -6,153 +6,155 @@ LBRACE=5
66
RBRACE=6
77
LP=7
88
RP=8
9-
DOT=9
10-
NSDOT=10
11-
COMMA=11
12-
SEMICOLON=12
13-
IF=13
14-
IN=14
15-
ELSE=15
16-
WHILE=16
17-
DO=17
18-
FOR=18
19-
CONTINUE=19
20-
BREAK=20
21-
RETURN=21
22-
NEW=22
23-
TRY=23
24-
CATCH=24
25-
THROW=25
26-
THIS=26
27-
INSTANCEOF=27
28-
BOOLNOT=28
29-
BWNOT=29
30-
MUL=30
31-
DIV=31
32-
REM=32
33-
ADD=33
34-
SUB=34
35-
LSH=35
36-
RSH=36
37-
USH=37
38-
LT=38
39-
LTE=39
40-
GT=40
41-
GTE=41
42-
EQ=42
43-
EQR=43
44-
NE=44
45-
NER=45
46-
BWAND=46
47-
XOR=47
48-
BWOR=48
49-
BOOLAND=49
50-
BOOLOR=50
51-
COND=51
52-
COLON=52
53-
ELVIS=53
54-
REF=54
55-
ARROW=55
56-
FIND=56
57-
MATCH=57
58-
INCR=58
59-
DECR=59
60-
ASSIGN=60
61-
AADD=61
62-
ASUB=62
63-
AMUL=63
64-
ADIV=64
65-
AREM=65
66-
AAND=66
67-
AXOR=67
68-
AOR=68
69-
ALSH=69
70-
ARSH=70
71-
AUSH=71
72-
OCTAL=72
73-
HEX=73
74-
INTEGER=74
75-
DECIMAL=75
76-
STRING=76
77-
REGEX=77
78-
TRUE=78
79-
FALSE=79
80-
NULL=80
81-
PRIMITIVE=81
82-
DEF=82
83-
ID=83
84-
DOTINTEGER=84
85-
DOTID=85
9+
DOLLAR=9
10+
DOT=10
11+
NSDOT=11
12+
COMMA=12
13+
SEMICOLON=13
14+
IF=14
15+
IN=15
16+
ELSE=16
17+
WHILE=17
18+
DO=18
19+
FOR=19
20+
CONTINUE=20
21+
BREAK=21
22+
RETURN=22
23+
NEW=23
24+
TRY=24
25+
CATCH=25
26+
THROW=26
27+
THIS=27
28+
INSTANCEOF=28
29+
BOOLNOT=29
30+
BWNOT=30
31+
MUL=31
32+
DIV=32
33+
REM=33
34+
ADD=34
35+
SUB=35
36+
LSH=36
37+
RSH=37
38+
USH=38
39+
LT=39
40+
LTE=40
41+
GT=41
42+
GTE=42
43+
EQ=43
44+
EQR=44
45+
NE=45
46+
NER=46
47+
BWAND=47
48+
XOR=48
49+
BWOR=49
50+
BOOLAND=50
51+
BOOLOR=51
52+
COND=52
53+
COLON=53
54+
ELVIS=54
55+
REF=55
56+
ARROW=56
57+
FIND=57
58+
MATCH=58
59+
INCR=59
60+
DECR=60
61+
ASSIGN=61
62+
AADD=62
63+
ASUB=63
64+
AMUL=64
65+
ADIV=65
66+
AREM=66
67+
AAND=67
68+
AXOR=68
69+
AOR=69
70+
ALSH=70
71+
ARSH=71
72+
AUSH=72
73+
OCTAL=73
74+
HEX=74
75+
INTEGER=75
76+
DECIMAL=76
77+
STRING=77
78+
REGEX=78
79+
TRUE=79
80+
FALSE=80
81+
NULL=81
82+
PRIMITIVE=82
83+
DEF=83
84+
ID=84
85+
DOTINTEGER=85
86+
DOTID=86
8687
'{'=3
8788
'}'=4
8889
'['=5
8990
']'=6
9091
'('=7
9192
')'=8
92-
'.'=9
93-
'?.'=10
94-
','=11
95-
';'=12
96-
'if'=13
97-
'in'=14
98-
'else'=15
99-
'while'=16
100-
'do'=17
101-
'for'=18
102-
'continue'=19
103-
'break'=20
104-
'return'=21
105-
'new'=22
106-
'try'=23
107-
'catch'=24
108-
'throw'=25
109-
'this'=26
110-
'instanceof'=27
111-
'!'=28
112-
'~'=29
113-
'*'=30
114-
'/'=31
115-
'%'=32
116-
'+'=33
117-
'-'=34
118-
'<<'=35
119-
'>>'=36
120-
'>>>'=37
121-
'<'=38
122-
'<='=39
123-
'>'=40
124-
'>='=41
125-
'=='=42
126-
'==='=43
127-
'!='=44
128-
'!=='=45
129-
'&'=46
130-
'^'=47
131-
'|'=48
132-
'&&'=49
133-
'||'=50
134-
'?'=51
135-
':'=52
136-
'?:'=53
137-
'::'=54
138-
'->'=55
139-
'=~'=56
140-
'==~'=57
141-
'++'=58
142-
'--'=59
143-
'='=60
144-
'+='=61
145-
'-='=62
146-
'*='=63
147-
'/='=64
148-
'%='=65
149-
'&='=66
150-
'^='=67
151-
'|='=68
152-
'<<='=69
153-
'>>='=70
154-
'>>>='=71
155-
'true'=78
156-
'false'=79
157-
'null'=80
158-
'def'=82
93+
'$'=9
94+
'.'=10
95+
'?.'=11
96+
','=12
97+
';'=13
98+
'if'=14
99+
'in'=15
100+
'else'=16
101+
'while'=17
102+
'do'=18
103+
'for'=19
104+
'continue'=20
105+
'break'=21
106+
'return'=22
107+
'new'=23
108+
'try'=24
109+
'catch'=25
110+
'throw'=26
111+
'this'=27
112+
'instanceof'=28
113+
'!'=29
114+
'~'=30
115+
'*'=31
116+
'/'=32
117+
'%'=33
118+
'+'=34
119+
'-'=35
120+
'<<'=36
121+
'>>'=37
122+
'>>>'=38
123+
'<'=39
124+
'<='=40
125+
'>'=41
126+
'>='=42
127+
'=='=43
128+
'==='=44
129+
'!='=45
130+
'!=='=46
131+
'&'=47
132+
'^'=48
133+
'|'=49
134+
'&&'=50
135+
'||'=51
136+
'?'=52
137+
':'=53
138+
'?:'=54
139+
'::'=55
140+
'->'=56
141+
'=~'=57
142+
'==~'=58
143+
'++'=59
144+
'--'=60
145+
'='=61
146+
'+='=62
147+
'-='=63
148+
'*='=64
149+
'/='=65
150+
'%='=66
151+
'&='=67
152+
'^='=68
153+
'|='=69
154+
'<<='=70
155+
'>>='=71
156+
'>>>='=72
157+
'true'=79
158+
'false'=80
159+
'null'=81
160+
'def'=83

modules/lang-painless/src/main/antlr/PainlessParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ primary
163163
| listinitializer # listinit
164164
| mapinitializer # mapinit
165165
| ID # variable
166-
| ID arguments # calllocal
166+
| (ID | DOLLAR) arguments # calllocal
167167
| NEW type arguments # newobject
168168
;
169169

0 commit comments

Comments
 (0)