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

Commit 72149f4

Browse files
committed
grammar: support try-catch-after expressions
1 parent a3d1b6b commit 72149f4

File tree

5 files changed

+15310
-10659
lines changed

5 files changed

+15310
-10659
lines changed

grammar.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ module.exports = grammar({
176176

177177
_expression: ($) =>
178178
choice(
179+
$.expr_try,
180+
$.expr_catch,
181+
$.expr_throw,
179182
$.expr_begin_block,
180183
$.expr_list_comprehension,
181184
$.expr_operator,
@@ -193,6 +196,35 @@ module.exports = grammar({
193196
$.match
194197
),
195198

199+
expr_try: ($) =>
200+
seq(
201+
"try",
202+
$.expression,
203+
opt(seq("of", sepBy(SEMI, $.case_clause))),
204+
choice($.expr_try_catch, $.expr_try_after, $._expr_try_catch_after),
205+
"end"
206+
),
207+
208+
expr_try_catch: ($) => seq("catch", sepBy(SEMI, $.catch_clause)),
209+
expr_try_after: ($) => seq("after", $.expression),
210+
_expr_try_catch_after: ($) => seq($.expr_try_catch, $.expr_try_after),
211+
catch_clause: ($) =>
212+
seq(
213+
field("pattern", $.catch_pattern),
214+
opt($.guard_clause),
215+
ARROW,
216+
field("body", sepBy(COMMA, $.expression))
217+
),
218+
catch_pattern: ($) =>
219+
seq(
220+
field("class", $.pattern),
221+
field("exception", opt(seq(COLON, $.pattern))),
222+
field("stacktrace", opt(seq(COLON, $.variable)))
223+
),
224+
225+
expr_catch: ($) => prec.left(seq("catch", $.expression)),
226+
expr_throw: ($) => prec.left(seq("throw", $.expression)),
227+
196228
expr_begin_block: ($) => seq("begin", sepBy(COMMA, $.expression), "end"),
197229

198230
expr_list_comprehension: ($) =>

src/grammar.json

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,18 @@
505505
"_expression": {
506506
"type": "CHOICE",
507507
"members": [
508+
{
509+
"type": "SYMBOL",
510+
"name": "expr_try"
511+
},
512+
{
513+
"type": "SYMBOL",
514+
"name": "expr_catch"
515+
},
516+
{
517+
"type": "SYMBOL",
518+
"name": "expr_throw"
519+
},
508520
{
509521
"type": "SYMBOL",
510522
"name": "expr_begin_block"
@@ -567,6 +579,297 @@
567579
}
568580
]
569581
},
582+
"expr_try": {
583+
"type": "SEQ",
584+
"members": [
585+
{
586+
"type": "STRING",
587+
"value": "try"
588+
},
589+
{
590+
"type": "SYMBOL",
591+
"name": "expression"
592+
},
593+
{
594+
"type": "CHOICE",
595+
"members": [
596+
{
597+
"type": "SEQ",
598+
"members": [
599+
{
600+
"type": "STRING",
601+
"value": "of"
602+
},
603+
{
604+
"type": "SEQ",
605+
"members": [
606+
{
607+
"type": "SYMBOL",
608+
"name": "case_clause"
609+
},
610+
{
611+
"type": "REPEAT",
612+
"content": {
613+
"type": "SEQ",
614+
"members": [
615+
{
616+
"type": "STRING",
617+
"value": ";"
618+
},
619+
{
620+
"type": "SYMBOL",
621+
"name": "case_clause"
622+
}
623+
]
624+
}
625+
}
626+
]
627+
}
628+
]
629+
},
630+
{
631+
"type": "BLANK"
632+
}
633+
]
634+
},
635+
{
636+
"type": "CHOICE",
637+
"members": [
638+
{
639+
"type": "SYMBOL",
640+
"name": "expr_try_catch"
641+
},
642+
{
643+
"type": "SYMBOL",
644+
"name": "expr_try_after"
645+
},
646+
{
647+
"type": "SYMBOL",
648+
"name": "_expr_try_catch_after"
649+
}
650+
]
651+
},
652+
{
653+
"type": "STRING",
654+
"value": "end"
655+
}
656+
]
657+
},
658+
"expr_try_catch": {
659+
"type": "SEQ",
660+
"members": [
661+
{
662+
"type": "STRING",
663+
"value": "catch"
664+
},
665+
{
666+
"type": "SEQ",
667+
"members": [
668+
{
669+
"type": "SYMBOL",
670+
"name": "catch_clause"
671+
},
672+
{
673+
"type": "REPEAT",
674+
"content": {
675+
"type": "SEQ",
676+
"members": [
677+
{
678+
"type": "STRING",
679+
"value": ";"
680+
},
681+
{
682+
"type": "SYMBOL",
683+
"name": "catch_clause"
684+
}
685+
]
686+
}
687+
}
688+
]
689+
}
690+
]
691+
},
692+
"expr_try_after": {
693+
"type": "SEQ",
694+
"members": [
695+
{
696+
"type": "STRING",
697+
"value": "after"
698+
},
699+
{
700+
"type": "SYMBOL",
701+
"name": "expression"
702+
}
703+
]
704+
},
705+
"_expr_try_catch_after": {
706+
"type": "SEQ",
707+
"members": [
708+
{
709+
"type": "SYMBOL",
710+
"name": "expr_try_catch"
711+
},
712+
{
713+
"type": "SYMBOL",
714+
"name": "expr_try_after"
715+
}
716+
]
717+
},
718+
"catch_clause": {
719+
"type": "SEQ",
720+
"members": [
721+
{
722+
"type": "FIELD",
723+
"name": "pattern",
724+
"content": {
725+
"type": "SYMBOL",
726+
"name": "catch_pattern"
727+
}
728+
},
729+
{
730+
"type": "CHOICE",
731+
"members": [
732+
{
733+
"type": "SYMBOL",
734+
"name": "guard_clause"
735+
},
736+
{
737+
"type": "BLANK"
738+
}
739+
]
740+
},
741+
{
742+
"type": "STRING",
743+
"value": "->"
744+
},
745+
{
746+
"type": "FIELD",
747+
"name": "body",
748+
"content": {
749+
"type": "SEQ",
750+
"members": [
751+
{
752+
"type": "SYMBOL",
753+
"name": "expression"
754+
},
755+
{
756+
"type": "REPEAT",
757+
"content": {
758+
"type": "SEQ",
759+
"members": [
760+
{
761+
"type": "STRING",
762+
"value": ","
763+
},
764+
{
765+
"type": "SYMBOL",
766+
"name": "expression"
767+
}
768+
]
769+
}
770+
}
771+
]
772+
}
773+
}
774+
]
775+
},
776+
"catch_pattern": {
777+
"type": "SEQ",
778+
"members": [
779+
{
780+
"type": "FIELD",
781+
"name": "class",
782+
"content": {
783+
"type": "SYMBOL",
784+
"name": "pattern"
785+
}
786+
},
787+
{
788+
"type": "FIELD",
789+
"name": "exception",
790+
"content": {
791+
"type": "CHOICE",
792+
"members": [
793+
{
794+
"type": "SEQ",
795+
"members": [
796+
{
797+
"type": "STRING",
798+
"value": ":"
799+
},
800+
{
801+
"type": "SYMBOL",
802+
"name": "pattern"
803+
}
804+
]
805+
},
806+
{
807+
"type": "BLANK"
808+
}
809+
]
810+
}
811+
},
812+
{
813+
"type": "FIELD",
814+
"name": "stacktrace",
815+
"content": {
816+
"type": "CHOICE",
817+
"members": [
818+
{
819+
"type": "SEQ",
820+
"members": [
821+
{
822+
"type": "STRING",
823+
"value": ":"
824+
},
825+
{
826+
"type": "SYMBOL",
827+
"name": "variable"
828+
}
829+
]
830+
},
831+
{
832+
"type": "BLANK"
833+
}
834+
]
835+
}
836+
}
837+
]
838+
},
839+
"expr_catch": {
840+
"type": "PREC_LEFT",
841+
"value": 0,
842+
"content": {
843+
"type": "SEQ",
844+
"members": [
845+
{
846+
"type": "STRING",
847+
"value": "catch"
848+
},
849+
{
850+
"type": "SYMBOL",
851+
"name": "expression"
852+
}
853+
]
854+
}
855+
},
856+
"expr_throw": {
857+
"type": "PREC_LEFT",
858+
"value": 0,
859+
"content": {
860+
"type": "SEQ",
861+
"members": [
862+
{
863+
"type": "STRING",
864+
"value": "throw"
865+
},
866+
{
867+
"type": "SYMBOL",
868+
"name": "expression"
869+
}
870+
]
871+
}
872+
},
570873
"expr_begin_block": {
571874
"type": "SEQ",
572875
"members": [

0 commit comments

Comments
 (0)