Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 0b3f9cf

Browse files
authored
Merge pull request tree-sitter#157 from brandonspark/string-interpolations
feat(java 21): add string templates
2 parents 8d012be + 4ee1f0b commit 0b3f9cf

File tree

6 files changed

+29285
-28446
lines changed

6 files changed

+29285
-28446
lines changed

grammar.js

+15
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ module.exports = grammar({
158158
repeat(choice(
159159
$.string_fragment,
160160
$.escape_sequence,
161+
$.string_interpolation,
161162
)),
162163
'"'
163164
),
@@ -166,6 +167,7 @@ module.exports = grammar({
166167
repeat(choice(
167168
alias($._multiline_string_fragment, $.multiline_string_fragment),
168169
$._escape_sequence,
170+
$.string_interpolation,
169171
)),
170172
'"""'
171173
),
@@ -181,6 +183,12 @@ module.exports = grammar({
181183
seq(/"[^"]*/, repeat(/[^"]+/))
182184
)),
183185

186+
string_interpolation: $ => seq(
187+
'\\{',
188+
$.expression,
189+
'}'
190+
),
191+
184192
_escape_sequence: $ =>
185193
choice(
186194
prec(2, token.immediate(seq('\\', /[^abfnrtvxu'\"\\\?]/))),
@@ -336,6 +344,7 @@ module.exports = grammar({
336344
$.method_invocation,
337345
$.method_reference,
338346
$.array_creation_expression,
347+
$.template_expression
339348
),
340349

341350
array_creation_expression: $ => prec.right(seq(
@@ -386,6 +395,12 @@ module.exports = grammar({
386395
field('field', choice($.identifier, $._reserved_identifier, $.this))
387396
),
388397

398+
template_expression: $ => seq(
399+
field('template_processor', $.primary_expression),
400+
'.',
401+
field('template_argument', $.string_literal)
402+
),
403+
389404
array_access: $ => seq(
390405
field('array', $.primary_expression),
391406
'[',

src/grammar.json

+54
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,10 @@
12681268
{
12691269
"type": "SYMBOL",
12701270
"name": "escape_sequence"
1271+
},
1272+
{
1273+
"type": "SYMBOL",
1274+
"name": "string_interpolation"
12711275
}
12721276
]
12731277
}
@@ -1302,6 +1306,10 @@
13021306
{
13031307
"type": "SYMBOL",
13041308
"name": "_escape_sequence"
1309+
},
1310+
{
1311+
"type": "SYMBOL",
1312+
"name": "string_interpolation"
13051313
}
13061314
]
13071315
}
@@ -1352,6 +1360,23 @@
13521360
]
13531361
}
13541362
},
1363+
"string_interpolation": {
1364+
"type": "SEQ",
1365+
"members": [
1366+
{
1367+
"type": "STRING",
1368+
"value": "\\{"
1369+
},
1370+
{
1371+
"type": "SYMBOL",
1372+
"name": "expression"
1373+
},
1374+
{
1375+
"type": "STRING",
1376+
"value": "}"
1377+
}
1378+
]
1379+
},
13551380
"_escape_sequence": {
13561381
"type": "CHOICE",
13571382
"members": [
@@ -2751,6 +2776,10 @@
27512776
{
27522777
"type": "SYMBOL",
27532778
"name": "array_creation_expression"
2779+
},
2780+
{
2781+
"type": "SYMBOL",
2782+
"name": "template_expression"
27542783
}
27552784
]
27562785
},
@@ -3077,6 +3106,31 @@
30773106
}
30783107
]
30793108
},
3109+
"template_expression": {
3110+
"type": "SEQ",
3111+
"members": [
3112+
{
3113+
"type": "FIELD",
3114+
"name": "template_processor",
3115+
"content": {
3116+
"type": "SYMBOL",
3117+
"name": "primary_expression"
3118+
}
3119+
},
3120+
{
3121+
"type": "STRING",
3122+
"value": "."
3123+
},
3124+
{
3125+
"type": "FIELD",
3126+
"name": "template_argument",
3127+
"content": {
3128+
"type": "SYMBOL",
3129+
"name": "string_literal"
3130+
}
3131+
}
3132+
]
3133+
},
30803134
"array_access": {
30813135
"type": "SEQ",
30823136
"members": [

src/node-types.json

+53
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@
279279
"type": "parenthesized_expression",
280280
"named": true
281281
},
282+
{
283+
"type": "template_expression",
284+
"named": true
285+
},
282286
{
283287
"type": "this",
284288
"named": true
@@ -3351,6 +3355,21 @@
33513355
]
33523356
}
33533357
},
3358+
{
3359+
"type": "string_interpolation",
3360+
"named": true,
3361+
"fields": {},
3362+
"children": {
3363+
"multiple": false,
3364+
"required": true,
3365+
"types": [
3366+
{
3367+
"type": "expression",
3368+
"named": true
3369+
}
3370+
]
3371+
}
3372+
},
33543373
{
33553374
"type": "string_literal",
33563375
"named": true,
@@ -3370,6 +3389,10 @@
33703389
{
33713390
"type": "string_fragment",
33723391
"named": true
3392+
},
3393+
{
3394+
"type": "string_interpolation",
3395+
"named": true
33733396
}
33743397
]
33753398
}
@@ -3540,6 +3563,32 @@
35403563
]
35413564
}
35423565
},
3566+
{
3567+
"type": "template_expression",
3568+
"named": true,
3569+
"fields": {
3570+
"template_argument": {
3571+
"multiple": false,
3572+
"required": true,
3573+
"types": [
3574+
{
3575+
"type": "string_literal",
3576+
"named": true
3577+
}
3578+
]
3579+
},
3580+
"template_processor": {
3581+
"multiple": false,
3582+
"required": true,
3583+
"types": [
3584+
{
3585+
"type": "primary_expression",
3586+
"named": true
3587+
}
3588+
]
3589+
}
3590+
}
3591+
},
35433592
{
35443593
"type": "ternary_expression",
35453594
"named": true,
@@ -4143,6 +4192,10 @@
41434192
"type": "[",
41444193
"named": false
41454194
},
4195+
{
4196+
"type": "\\{",
4197+
"named": false
4198+
},
41464199
{
41474200
"type": "]",
41484201
"named": false

0 commit comments

Comments
 (0)