Skip to content

Commit 2beedf2

Browse files
authored
Add const block expression (#97)
* Add const block expression * Fix const block pattern test
1 parent 8746bd4 commit 2beedf2

File tree

6 files changed

+62550
-61582
lines changed

6 files changed

+62550
-61582
lines changed

corpus/expressions.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,3 +753,53 @@ const a : A = unsafe { foo() };
753753
(identifier)
754754
(type_identifier)
755755
(unsafe_block (block (call_expression (identifier) (arguments))))))
756+
757+
===========================================
758+
Inline const or Const blocks as expression
759+
===========================================
760+
761+
const { 1 + 3 };
762+
if *x < 0 { const { &4i32.pow(4) } } else { x }
763+
let three_ranges = [const { (0..=5).into_inner() }; 3];
764+
765+
---
766+
767+
(source_file
768+
(const_block
769+
body: (block
770+
(binary_expression
771+
left: (integer_literal)
772+
right: (integer_literal))))
773+
(empty_statement)
774+
(if_expression
775+
condition: (binary_expression
776+
left: (unary_expression
777+
(identifier))
778+
right: (integer_literal))
779+
consequence: (block
780+
(const_block
781+
body: (block
782+
(reference_expression
783+
value: (call_expression
784+
function: (field_expression
785+
value: (integer_literal)
786+
field: (field_identifier))
787+
arguments: (arguments
788+
(integer_literal)))))))
789+
alternative: (else_clause
790+
(block
791+
(identifier))))
792+
(let_declaration
793+
pattern: (identifier)
794+
value: (array_expression
795+
(const_block
796+
body: (block
797+
(call_expression
798+
function: (field_expression
799+
value: (parenthesized_expression
800+
(range_expression
801+
(integer_literal)
802+
(integer_literal)))
803+
field: (field_identifier))
804+
arguments: (arguments))))
805+
length: (integer_literal))))

corpus/patterns.txt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,82 @@ fn foo((1 | 2 | 3): u8) {}
310310
(line_comment)
311311
(line_comment)
312312
(line_comment))
313+
314+
===========================================
315+
Inline const or Const blocks as pattern
316+
===========================================
317+
318+
fn foo(x: i32) {
319+
const CUBE: i32 = 3.pow(3);
320+
match x {
321+
CUBE => println!("three cubed"),
322+
_ => {}
323+
}
324+
}
325+
326+
fn foo(x: i32) {
327+
match x {
328+
const { 3.pow(3) } => println!("three cubed"),
329+
_ => {}
330+
}
331+
}
332+
333+
---
334+
335+
(source_file
336+
(function_item
337+
name: (identifier)
338+
parameters: (parameters
339+
(parameter
340+
pattern: (identifier)
341+
type: (primitive_type)))
342+
body: (block
343+
(const_item
344+
name: (identifier)
345+
type: (primitive_type)
346+
value: (call_expression
347+
function: (field_expression
348+
value: (integer_literal)
349+
field: (field_identifier))
350+
arguments: (arguments
351+
(integer_literal))))
352+
(match_expression
353+
value: (identifier)
354+
body: (match_block
355+
(match_arm
356+
pattern: (match_pattern
357+
(identifier))
358+
value: (macro_invocation
359+
macro: (identifier)
360+
(token_tree
361+
(string_literal))))
362+
(match_arm
363+
pattern: (match_pattern)
364+
value: (block))))))
365+
(function_item
366+
name: (identifier)
367+
parameters: (parameters
368+
(parameter
369+
pattern: (identifier)
370+
type: (primitive_type)))
371+
body: (block
372+
(match_expression
373+
value: (identifier)
374+
body: (match_block
375+
(match_arm
376+
pattern: (match_pattern
377+
(const_block
378+
body: (block
379+
(call_expression
380+
function: (field_expression
381+
value: (integer_literal)
382+
field: (field_identifier))
383+
arguments: (arguments
384+
(integer_literal))))))
385+
value: (macro_invocation
386+
macro: (identifier)
387+
(token_tree
388+
(string_literal))))
389+
(match_arm
390+
pattern: (match_pattern)
391+
value: (block)))))))

grammar.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,8 @@ module.exports = grammar({
870870
$.while_expression,
871871
$.while_let_expression,
872872
$.loop_expression,
873-
$.for_expression
873+
$.for_expression,
874+
$.const_block
874875
),
875876

876877
macro_invocation: $ => seq(
@@ -1160,6 +1161,11 @@ module.exports = grammar({
11601161
field('body', $.block)
11611162
),
11621163

1164+
const_block: $ => seq(
1165+
'const',
1166+
field('body', $.block)
1167+
),
1168+
11631169
closure_expression: $ => prec(PREC.closure, seq(
11641170
optional('move'),
11651171
field('parameters', $.closure_parameters),
@@ -1240,6 +1246,7 @@ module.exports = grammar({
12401246
$.mut_pattern,
12411247
$.range_pattern,
12421248
$.or_pattern,
1249+
$.const_block,
12431250
'_'
12441251
),
12451252

src/grammar.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4829,6 +4829,10 @@
48294829
{
48304830
"type": "SYMBOL",
48314831
"name": "for_expression"
4832+
},
4833+
{
4834+
"type": "SYMBOL",
4835+
"name": "const_block"
48324836
}
48334837
]
48344838
},
@@ -6698,6 +6702,23 @@
66986702
}
66996703
]
67006704
},
6705+
"const_block": {
6706+
"type": "SEQ",
6707+
"members": [
6708+
{
6709+
"type": "STRING",
6710+
"value": "const"
6711+
},
6712+
{
6713+
"type": "FIELD",
6714+
"name": "body",
6715+
"content": {
6716+
"type": "SYMBOL",
6717+
"name": "block"
6718+
}
6719+
}
6720+
]
6721+
},
67016722
"closure_expression": {
67026723
"type": "PREC",
67036724
"value": -1,
@@ -7208,6 +7229,10 @@
72087229
"type": "SYMBOL",
72097230
"name": "or_pattern"
72107231
},
7232+
{
7233+
"type": "SYMBOL",
7234+
"name": "const_block"
7235+
},
72117236
{
72127237
"type": "STRING",
72137238
"value": "_"

src/node-types.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@
137137
"type": "compound_assignment_expr",
138138
"named": true
139139
},
140+
{
141+
"type": "const_block",
142+
"named": true
143+
},
140144
{
141145
"type": "continue_expression",
142146
"named": true
@@ -327,6 +331,10 @@
327331
"type": "captured_pattern",
328332
"named": true
329333
},
334+
{
335+
"type": "const_block",
336+
"named": true
337+
},
330338
{
331339
"type": "identifier",
332340
"named": true
@@ -1015,6 +1023,22 @@
10151023
}
10161024
}
10171025
},
1026+
{
1027+
"type": "const_block",
1028+
"named": true,
1029+
"fields": {
1030+
"body": {
1031+
"multiple": false,
1032+
"required": true,
1033+
"types": [
1034+
{
1035+
"type": "block",
1036+
"named": true
1037+
}
1038+
]
1039+
}
1040+
}
1041+
},
10181042
{
10191043
"type": "const_item",
10201044
"named": true,

0 commit comments

Comments
 (0)