Skip to content

Commit 935a643

Browse files
authored
Merge pull request #694 from Kipper-Lang/654-feature-implement-trycatch-statement-to-handle-exceptions
Implemented `try-catch` block statements for catching and handling errors
2 parents b40ada9 + faf1cb7 commit 935a643

78 files changed

Lines changed: 4428 additions & 2921 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.run/kipper run.run.xml

Whitespace-only changes.

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,33 @@ To use development versions of Kipper download the
1818

1919
### Added
2020

21+
- Error handling support using `try {} catch (e) {} finally {}` statements to catch and handle runtime errors.
22+
([#654](https://github.com/Kipper-Lang/Kipper/issues/654))
2123
- New cast keywords `cast as`, `force as` and `try as`, which allow for various type-safe cast operations.
2224
([#685](https://github.com/Kipper-Lang/Kipper/issues/685))
2325
- Implemented new `T?` and `T??` nullable type operator which marks the given type as `T | null` and `T | undefined`
2426
respectively.
2527
- New classes:
2628
- `NullableTypeSpecifierExpression`, which represents an AST nullable type specifier expression that marks a given
2729
type as nullable with either `null` or `undefined`.
30+
- `TryCatchStatement`, which represents an AST try-catch statement that handles exceptions thrown in the try block.
31+
- `CatchClause`, which represents an AST catch clause that is part of a try-catch statement.
32+
- `ErrorBindingDeclaration`, which represents an AST error binding declaration that is part of a catch clause. This
33+
declaration binds the error object to a variable in the catch clause scope.
2834
- New interfaces:
2935
- `NullableTypeSpecifierExpressionSemantics`, which represents the semantics of a nullable type specifier expression.
3036
- `NullableTypeSpecifierExpressionTypeSemantics`, which represents the type semantics of a nullable type specifier
3137
expression.
38+
- `TryCatchStatementSemantics`, which represents the semantics of a try-catch statement.
39+
- `TryCatchStatementTypeSemantics`, which represents the type semantics of a try-catch statement.
40+
- `CatchClauseSemantics`, which represents the semantics of a catch clause.
41+
- `CatchClauseTypeSemantics`, which represents the type semantics of a catch clause.
42+
- `ErrorBindingDeclarationSemantics`, which represents the semantics of an error binding declaration.
43+
- `ErrorBindingDeclarationTypeSemantics`, which represents the type semantics of an error binding declaration.
3244
- New functions:
3345
- `KipperTypeScriptTarget.getRuntimeType`, which gets the corresponding runtime representation of a type.
46+
- `KipperTypeChecker.isValidErrorBindingArgument`, which checks if a type is a valid type for an error binding
47+
argument.
3448
- New properties:
3549
- `CompilableASTNode.closestHigherLevelParent`, which returns the closest parent of the node that is of a higher level
3650
than the node itself. (Higher level as in statement or declaration level, undefined means the item itself is already
@@ -42,6 +56,8 @@ To use development versions of Kipper download the
4256
- `CustomType.sourceNode`, which returns the source node of the custom type.
4357
- `CustomType.clsConstructor`, which returns the constructor of the class if the type is a class and the class has a
4458
constructor.
59+
- `ProcessedType.isInterface`, which returns true if the type is an interface.
60+
- `ProcessedType.isClass`, which returns true if the type is a class.
4561
- New types:
4662
- `KipperNullableNullOperator`, which is the `null` nullable operator that can be used in Kipper.
4763
- `KipperNullableUndefinedOperator`, which is the `undefined` nullable operator that can be used in Kipper.

kipper/cli/README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ and the [Kipper website](https://kipper-lang.org)._
2222
[![DOI](https://zenodo.org/badge/411260595.svg)](https://zenodo.org/badge/latestdoi/411260595)
2323

2424
<!-- toc -->
25-
* [Kipper CLI - `@kipper/cli` 🦊✨](#kipper-cli---kippercli-)
26-
* [Usage](#usage)
27-
* [Commands](#commands)
25+
26+
- [Kipper CLI - `@kipper/cli` 🦊✨](#kipper-cli---kippercli-)
27+
- [Usage](#usage)
28+
- [Commands](#commands)
2829
<!-- tocstop -->
2930

3031
## General Information
@@ -39,6 +40,7 @@ and the [Kipper website](https://kipper-lang.org)._
3940
# Usage
4041

4142
<!-- usage -->
43+
4244
```sh-session
4345
$ npm install -g @kipper/cli
4446
$ kipper COMMAND
@@ -50,16 +52,18 @@ USAGE
5052
$ kipper COMMAND
5153
...
5254
```
55+
5356
<!-- usagestop -->
5457

5558
# Commands
5659

5760
<!-- commands -->
58-
* [`kipper compile [FILE]`](#kipper-compile-file)
59-
* [`kipper help [COMMAND]`](#kipper-help-command)
60-
* [`kipper new [LOCATION]`](#kipper-new-location)
61-
* [`kipper run [FILE]`](#kipper-run-file)
62-
* [`kipper version`](#kipper-version)
61+
62+
- [`kipper compile [FILE]`](#kipper-compile-file)
63+
- [`kipper help [COMMAND]`](#kipper-help-command)
64+
- [`kipper new [LOCATION]`](#kipper-new-location)
65+
- [`kipper run [FILE]`](#kipper-run-file)
66+
- [`kipper version`](#kipper-version)
6367

6468
## `kipper compile [FILE]`
6569

@@ -202,6 +206,7 @@ USAGE
202206
```
203207

204208
_See code: [src/commands/version.ts](https://github.com/Kipper-Lang/Kipper/blob/v0.13.0-alpha.2/kipper/cli/src/commands/version.ts)_
209+
205210
<!-- commandsstop -->
206211

207212
## Contributing to Kipper

kipper/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
}
4949
},
5050
"engines": {
51-
"node": "16.x || 18.x || 20.x || 22.x",
51+
"node": "16.x || 18.x || 20.x || 22.x || 24.x",
5252
"pnpm": "8"
5353
},
5454
"files": [

kipper/config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"typescript": "5.1.3"
1818
},
1919
"engines": {
20-
"node": "16.x || 18.x || 20.x || 22.x",
20+
"node": "16.x || 18.x || 20.x || 22.x || 24.x",
2121
"pnpm": "8"
2222
},
2323
"homepage": "https://kipper-lang.org",

kipper/core/KipperLexer.g4

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ Matches : 'matches';
9898
// typeof operator
9999
Typeof : 'typeof';
100100

101+
// try-catch-finally
102+
Try : 'try';
103+
Catch : 'catch';
104+
Finally : 'finally';
105+
101106
// Constant undefined, void and null identifiers
102107
Void : 'void';
103108
Null : 'null';

kipper/core/KipperLexer.tokens

Lines changed: 0 additions & 170 deletions
This file was deleted.

kipper/core/KipperParser.g4

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ statement
133133
| jumpStatement
134134
| returnStatement
135135
| compoundStatement
136+
| tryCatchStatement
136137
;
137138

138139
compoundStatement
@@ -192,6 +193,26 @@ returnStatement
192193
: 'return' expression? SemiColon
193194
;
194195

196+
tryCatchStatement
197+
: tryClause catchClause* finallyClause?
198+
;
199+
200+
tryClause
201+
: 'try' compoundStatement
202+
;
203+
204+
catchClause
205+
: 'catch' '(' errorBindingDeclaration ')' compoundStatement
206+
;
207+
208+
errorBindingDeclaration
209+
: declarator (':' typeSpecifierExpression)?
210+
;
211+
212+
finallyClause
213+
: 'finally' compoundStatement
214+
;
215+
195216
// -- Expressions
196217

197218
primaryExpression // Primary expressions, which build up the rest of the more complex expressions

0 commit comments

Comments
 (0)