WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit dd09bda

Browse files
authored
Merge pull request #22 from alaviss/extra-structures
grammar: add a few missing structures * `defer` block support * pragmas for `do` blocks
2 parents 8882cdb + 28f8434 commit dd09bda

File tree

5 files changed

+188
-102
lines changed

5 files changed

+188
-102
lines changed

corpus/expressions.txt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ of 1, 2:
397397
cmp() do (x, y: int) -> int:
398398
x - y
399399

400+
cmp() do (x: int) -> int {.noSideEffect.}:
401+
x - y
402+
400403
fn() do:
401404
x
402405
do:
@@ -497,7 +500,7 @@ collect(for x in s:
497500
function: (identifier)
498501
(argument_list
499502
(do_block
500-
(parameter_declaration_list
503+
parameters: (parameter_declaration_list
501504
(parameter_declaration
502505
(symbol_declaration_list
503506
(symbol_declaration
@@ -511,6 +514,24 @@ collect(for x in s:
511514
left: (identifier)
512515
operator: (infix_operator)
513516
right: (identifier))))))
517+
(call
518+
function: (identifier)
519+
(argument_list
520+
(do_block
521+
parameters: (parameter_declaration_list
522+
(parameter_declaration
523+
(symbol_declaration_list
524+
(symbol_declaration
525+
name: (identifier)))
526+
type: (identifier)))
527+
return_type: (identifier)
528+
pragmas: (pragma_list
529+
(identifier))
530+
body: (statement_list
531+
(infix_expression
532+
left: (identifier)
533+
operator: (infix_operator)
534+
right: (identifier))))))
514535
(call
515536
function: (identifier)
516537
(argument_list

corpus/statements.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,37 @@ x = foo.add proc =
431431
(proc_expression
432432
(statement_list
433433
(identifier)))))))
434+
435+
================================================================================
436+
Defer statements
437+
================================================================================
438+
439+
defer: echo y
440+
441+
defer:
442+
doX()
443+
doY()
444+
assert y, "foo"
445+
446+
--------------------------------------------------------------------------------
447+
448+
(source_file
449+
(defer
450+
(statement_list
451+
(call
452+
(identifier)
453+
(argument_list
454+
(identifier)))))
455+
(defer
456+
(statement_list
457+
(call
458+
(identifier)
459+
(argument_list))
460+
(call
461+
(identifier)
462+
(argument_list))
463+
(call
464+
(identifier)
465+
(argument_list
466+
(identifier)
467+
(string_literal))))))

grammar.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ module.exports = grammar({
241241
"sigil",
242242
"suffix",
243243
"unary",
244-
$.pragma_expression,
245244
"type_modifiers",
246245
"binary_10",
247246
"binary_9",
@@ -257,6 +256,7 @@ module.exports = grammar({
257256
$._expression,
258257
$._simple_expression_command_start,
259258
$._type_expression,
259+
$.pragma_expression,
260260
],
261261
[$._routine_expression_tail, $.pragma_expression],
262262
[$._routine_expression_tail, "proc_type"],
@@ -382,7 +382,7 @@ module.exports = grammar({
382382

383383
_statement: $ => choice($._simple_statement, $._complex_statement),
384384
_complex_statement: $ =>
385-
choice($.while, $.static_statement, $._declaration),
385+
choice($.while, $.static_statement, $.defer, $._declaration),
386386
_simple_statement: $ =>
387387
choice($._expression_statement, $._simple_statement_no_expression),
388388
_simple_statement_no_expression: $ =>
@@ -455,6 +455,7 @@ module.exports = grammar({
455455
prec.right(
456456
seq($.pragma_list, optional(seq(":", field("body", $.statement_list))))
457457
),
458+
defer: $ => seq(keyword("defer"), ":", field("body", $.statement_list)),
458459

459460
_declaration: $ =>
460461
choice(
@@ -804,8 +805,9 @@ module.exports = grammar({
804805
do_block: $ =>
805806
seq(
806807
keyword("do"),
807-
optional($.parameter_declaration_list),
808+
field("parameters", optional($.parameter_declaration_list)),
808809
field("return_type", optional(seq("->", $._type_expression))),
810+
field("pragmas", optional($.pragma_list)),
809811
":",
810812
field("body", $.statement_list)
811813
),
@@ -970,14 +972,6 @@ module.exports = grammar({
970972
pointer_type: () => keyword("ptr"),
971973
proc_type: $ => Templates.proc_type($, keyword("proc")),
972974
iterator_type: $ => Templates.proc_type($, keyword("iterator")),
973-
_routine_type_tail: $ =>
974-
prec.right(
975-
seq(
976-
field("parameters", optional($.parameter_declaration_list)),
977-
field("return_type", optional(seq(":", $._type_expression))),
978-
field("pragmas", optional($.pragma_list))
979-
)
980-
),
981975

982976
_infix_extended: $ =>
983977
prec("post_expr", seq($._infix_expression, $._post_expression_block)),

src/grammar.json

Lines changed: 71 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@
126126
"type": "SYMBOL",
127127
"name": "static_statement"
128128
},
129+
{
130+
"type": "SYMBOL",
131+
"name": "defer"
132+
},
129133
{
130134
"type": "SYMBOL",
131135
"name": "_declaration"
@@ -854,6 +858,39 @@
854858
]
855859
}
856860
},
861+
"defer": {
862+
"type": "SEQ",
863+
"members": [
864+
{
865+
"type": "ALIAS",
866+
"content": {
867+
"type": "TOKEN",
868+
"content": {
869+
"type": "PREC",
870+
"value": 1,
871+
"content": {
872+
"type": "PATTERN",
873+
"value": "d_?[eE]_?[fF]_?[eE]_?[rR]"
874+
}
875+
}
876+
},
877+
"named": false,
878+
"value": "defer"
879+
},
880+
{
881+
"type": "STRING",
882+
"value": ":"
883+
},
884+
{
885+
"type": "FIELD",
886+
"name": "body",
887+
"content": {
888+
"type": "SYMBOL",
889+
"name": "statement_list"
890+
}
891+
}
892+
]
893+
},
857894
"_declaration": {
858895
"type": "CHOICE",
859896
"members": [
@@ -3394,16 +3431,20 @@
33943431
"value": "do"
33953432
},
33963433
{
3397-
"type": "CHOICE",
3398-
"members": [
3399-
{
3400-
"type": "SYMBOL",
3401-
"name": "parameter_declaration_list"
3402-
},
3403-
{
3404-
"type": "BLANK"
3405-
}
3406-
]
3434+
"type": "FIELD",
3435+
"name": "parameters",
3436+
"content": {
3437+
"type": "CHOICE",
3438+
"members": [
3439+
{
3440+
"type": "SYMBOL",
3441+
"name": "parameter_declaration_list"
3442+
},
3443+
{
3444+
"type": "BLANK"
3445+
}
3446+
]
3447+
}
34073448
},
34083449
{
34093450
"type": "FIELD",
@@ -3430,6 +3471,22 @@
34303471
]
34313472
}
34323473
},
3474+
{
3475+
"type": "FIELD",
3476+
"name": "pragmas",
3477+
"content": {
3478+
"type": "CHOICE",
3479+
"members": [
3480+
{
3481+
"type": "SYMBOL",
3482+
"name": "pragma_list"
3483+
},
3484+
{
3485+
"type": "BLANK"
3486+
}
3487+
]
3488+
}
3489+
},
34333490
{
34343491
"type": "STRING",
34353492
"value": ":"
@@ -4588,72 +4645,6 @@
45884645
]
45894646
}
45904647
},
4591-
"_routine_type_tail": {
4592-
"type": "PREC_RIGHT",
4593-
"value": 0,
4594-
"content": {
4595-
"type": "SEQ",
4596-
"members": [
4597-
{
4598-
"type": "FIELD",
4599-
"name": "parameters",
4600-
"content": {
4601-
"type": "CHOICE",
4602-
"members": [
4603-
{
4604-
"type": "SYMBOL",
4605-
"name": "parameter_declaration_list"
4606-
},
4607-
{
4608-
"type": "BLANK"
4609-
}
4610-
]
4611-
}
4612-
},
4613-
{
4614-
"type": "FIELD",
4615-
"name": "return_type",
4616-
"content": {
4617-
"type": "CHOICE",
4618-
"members": [
4619-
{
4620-
"type": "SEQ",
4621-
"members": [
4622-
{
4623-
"type": "STRING",
4624-
"value": ":"
4625-
},
4626-
{
4627-
"type": "SYMBOL",
4628-
"name": "_type_expression"
4629-
}
4630-
]
4631-
},
4632-
{
4633-
"type": "BLANK"
4634-
}
4635-
]
4636-
}
4637-
},
4638-
{
4639-
"type": "FIELD",
4640-
"name": "pragmas",
4641-
"content": {
4642-
"type": "CHOICE",
4643-
"members": [
4644-
{
4645-
"type": "SYMBOL",
4646-
"name": "pragma_list"
4647-
},
4648-
{
4649-
"type": "BLANK"
4650-
}
4651-
]
4652-
}
4653-
}
4654-
]
4655-
}
4656-
},
46574648
"_infix_extended": {
46584649
"type": "PREC",
46594650
"value": "post_expr",
@@ -10337,10 +10328,6 @@
1033710328
"type": "STRING",
1033810329
"value": "unary"
1033910330
},
10340-
{
10341-
"type": "SYMBOL",
10342-
"name": "pragma_expression"
10343-
},
1034410331
{
1034510332
"type": "STRING",
1034610333
"value": "type_modifiers"
@@ -10400,6 +10387,10 @@
1040010387
{
1040110388
"type": "SYMBOL",
1040210389
"name": "_type_expression"
10390+
},
10391+
{
10392+
"type": "SYMBOL",
10393+
"name": "pragma_expression"
1040310394
}
1040410395
],
1040510396
[

0 commit comments

Comments
 (0)