Skip to content

Commit

Permalink
Add ScopeError stub
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Jun 4, 2019
1 parent 4307eb5 commit 5b22e6e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion resolver/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {Scope} from "./scope";
import {Value, NoneValue} from "./value";
import {Message} from "./message";
import {hello, exclamation, select} from "./fixtures";
import {ScopeError} from "./error";

export interface Formatted {
readonly value: string | null;
readonly errors: Array<string>;
readonly errors: Array<ScopeError>;
}

export class Bundle {
Expand Down
16 changes: 16 additions & 0 deletions resolver/src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export enum ErrorKind {
UnknownVariable,
UnknownMessage,
MissingValue,
}

export class ScopeError extends Error {
public kind: ErrorKind;
public arg: string;

constructor(kind: ErrorKind, arg: string) {
super();
this.kind = kind;
this.arg = arg;
}
}
3 changes: 2 additions & 1 deletion resolver/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as ast from "./ast";
import {Scope} from "./scope";
import {Failure, Result} from "./result";
import {Value, StringValue} from "./value";
import {ScopeError, ErrorKind} from "./error";

export class Message {
private readonly id: string;
Expand All @@ -16,7 +17,7 @@ export class Message {
if (this.value !== null) {
return scope.resolvePattern(this.value);
} else {
scope.errors.push(`Message ${this.id} has a null value.`);
scope.errors.push(new ScopeError(ErrorKind.MissingValue, this.id));
return new Failure(new StringValue(this.id));
}
}
Expand Down
7 changes: 4 additions & 3 deletions resolver/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import * as ast from "./ast";
import {Message} from "./message";
import {Value, StringValue} from "./value";
import {Result, Success, Failure} from "./result";
import {ScopeError, ErrorKind} from "./error";

export class Scope {
private readonly messages: Map<string, Message>;
private readonly variables: Map<string, Value>;
public errors: Array<string>;
public errors: Array<ScopeError>;

constructor(messages: Map<string, Message>, variables: Map<string, Value>) {
this.messages = messages;
Expand All @@ -32,7 +33,7 @@ export class Scope {
if (value !== undefined) {
return new Success(value);
} else {
this.errors.push(`Unknown variable: $${node.id.name}.`);
this.errors.push(new ScopeError(ErrorKind.UnknownVariable, node.id.name));
return new Failure(new StringValue(`$${node.id.name}`));
}
}
Expand All @@ -42,7 +43,7 @@ export class Scope {
if (message !== undefined) {
return message.resolveValue(this);
} else {
this.errors.push(`Unknown message: ${node.id.name}.`);
this.errors.push(new ScopeError(ErrorKind.UnknownMessage, node.id.name));
return new Failure(new StringValue(`${node.id.name}`));
}
}
Expand Down

0 comments on commit 5b22e6e

Please sign in to comment.