Skip to content

Commit

Permalink
Replace resolver/ast.ts with parser/ast.d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Jul 31, 2019
1 parent df9ee87 commit c903692
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 86 deletions.
3 changes: 1 addition & 2 deletions format/lib/tools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {NodeType} from "../resolver/ast";
import {Bundle} from "../resolver/bundle";
import {ErrorKind} from "../resolver/error";
import {Resource} from "../resolver/resource";
Expand All @@ -12,7 +11,7 @@ export function formatResource(resource: Resource, variables: Variables) {

let results = [];
for (let entry of resource.body) {
if (entry.type !== NodeType.Message) {
if (entry.type !== "Message") {
continue;
}
let message = bundle.getMessage(entry.id.name);
Expand Down
73 changes: 0 additions & 73 deletions format/resolver/ast.ts

This file was deleted.

4 changes: 2 additions & 2 deletions format/resolver/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {NodeType, Pattern} from "./ast";
import {Pattern} from "../../syntax/parser/ast";
import {ScopeError} from "./error";
import {Message} from "./message";
import {Resource} from "./resource";
Expand All @@ -15,7 +15,7 @@ export class Bundle {

addResource(resource: Resource) {
for (let message of resource.body) {
if (message.type === NodeType.Message) {
if (message.type === "Message") {
let attributes: Record<string, Pattern> = {};
for (let attribute of message.attributes) {
attributes[attribute.id.name] = attribute.value;
Expand Down
2 changes: 1 addition & 1 deletion format/resolver/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Pattern} from "./ast";
import {Pattern} from "../../syntax/parser/ast";

export interface Message {
readonly id: string;
Expand Down
2 changes: 1 addition & 1 deletion format/resolver/resource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Entry} from "../../syntax/parser/ast";
import {Resource as ResourceParser} from "../../syntax/parser/grammar";
import {Entry} from "./ast";

export class Resource {
public readonly body: Array<Entry>;
Expand Down
10 changes: 5 additions & 5 deletions format/resolver/scope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ast from "./ast";
import * as ast from "../../syntax/parser/ast";
import {ErrorKind, ScopeError} from "./error";
import {Message} from "./message";
import {NoneValue, StringValue, Value} from "./value";
Expand All @@ -14,9 +14,9 @@ export class Scope {

resolveExpression(node: ast.Expression): Value {
switch (node.type) {
case ast.NodeType.StringLiteral:
case "StringLiteral":
return this.resolveStringLiteral(node);
case ast.NodeType.MessageReference:
case "MessageReference":
return this.resolveMessageReference(node);
default:
throw new TypeError("Unknown node type.");
Expand All @@ -43,9 +43,9 @@ export class Scope {

resolvePatternElement(node: ast.PatternElement): Value {
switch (node.type) {
case ast.NodeType.TextElement:
case "TextElement":
return new StringValue(node.value);
case ast.NodeType.Placeable:
case "Placeable":
return this.resolveExpression(node.expression);
default:
throw new TypeError("Unknown node type.");
Expand Down
61 changes: 61 additions & 0 deletions syntax/parser/ast.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export declare class SyntaxNode {
readonly type: string;
}

export declare class Identifier extends SyntaxNode {
readonly type: "Identifer";
readonly name: string;
}

export declare class StringLiteral extends SyntaxNode {
readonly type: "StringLiteral";
readonly value: string;
parse(): {value: string};
}

export declare class MessageReference extends SyntaxNode {
readonly type: "MessageReference";
readonly id: Identifier;
readonly attribute: Identifier | null;
}

export type InlineExpression = StringLiteral | MessageReference;

export type Expression = InlineExpression;

export declare class TextElement extends SyntaxNode {
readonly type: "TextElement";
readonly value: string;
}

export declare class Placeable extends SyntaxNode {
readonly type: "Placeable";
readonly expression: Expression;
}

export type PatternElement = TextElement | Placeable;

export declare class Pattern extends SyntaxNode {
readonly type: "Pattern";
readonly elements: Array<PatternElement>;
}

export declare class Message extends SyntaxNode {
readonly type: "Message";
readonly id: Identifier;
readonly value: Pattern | null;
readonly attributes: Array<Attribute>;
}

export declare class Attribute extends SyntaxNode {
readonly type: "Attribute";
readonly id: Identifier;
readonly value: Pattern;
}

export type Entry = Message;

export declare class Resource extends SyntaxNode {
readonly type: "Resource";
readonly body: Array<Entry>;
}
4 changes: 2 additions & 2 deletions syntax/parser/grammar.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Resource} from "../../format/resolver/ast";
import * as ast from "./ast";
interface Result<T, E> {
fold(s: (value: T) => T, f: (err: E) => never): T;
}

interface Parser {
run(input: string): Result<Resource, string>;
run(input: string): Result<ast.Resource, string>;
}

export declare let Resource: Parser;

0 comments on commit c903692

Please sign in to comment.