Skip to content

Commit

Permalink
string_literal.md
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Jul 31, 2019
1 parent 035453d commit 52ee622
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
9 changes: 8 additions & 1 deletion format/ast.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum NodeType {
Identifer = "Identifier",
StringLiteral = "StringLiteral",
MessageReference = "MessageReference",
TextElement = "TextElement",
Placeable = "Placeable",
Expand All @@ -18,13 +19,19 @@ export interface Identifier extends SyntaxNode {
readonly name: string;
}

export interface StringLiteral extends SyntaxNode {
readonly type: NodeType.StringLiteral;
readonly value: string;
parse(): {value: string};
}

export interface MessageReference extends SyntaxNode {
readonly type: NodeType.MessageReference;
readonly id: Identifier;
readonly attribute: Identifier | null;
}

export type InlineExpression = MessageReference;
export type InlineExpression = StringLiteral | MessageReference;

export type Expression = InlineExpression;

Expand Down
7 changes: 7 additions & 0 deletions format/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ export class Scope {

resolveExpression(node: ast.Expression): Value {
switch (node.type) {
case ast.NodeType.StringLiteral:
return this.resolveStringLiteral(node as ast.StringLiteral);
case ast.NodeType.MessageReference:
return this.resolveMessageReference(node as ast.MessageReference);
default:
throw new TypeError("Unknown node type.");
}
}

resolveStringLiteral(node: ast.StringLiteral): Value {
let {value} = node.parse();
return new StringValue(value);
}

resolveMessageReference(node: ast.MessageReference): Value {
let message = this.messages.get(node.id.name);
if (message == undefined) {
Expand Down
88 changes: 88 additions & 0 deletions spec/format/string_literal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# StringLiteral

`StringLiterals` can be interpolated as `Placeables` inside `Patterns`. They
format to their text content.

```properties
test = {"Text"}
```

```json
{
"value": "Text",
"errors": []
}
```

All whitespace is preserved in `StringLiterals`.

```properties
test = A {" "} B
```

```json
{
"value": "A B",
"errors": []
}
```

When positioned at the front or at the end of a `Pattern`, `StringLiterals`
can be used to preserve leading and trailing whitespace which would be
otherwise trimmed inside `TextElement`.

```properties
test = {" "}Text
```

```json
{
"value": " Text",
"errors": []
}
```

## Escape Sequences

Escape sequences are supported in `StringLiterals`.

Characters from the Basic Multilingual Plane (BMP) can be escaped using the
four-hexdigits escape sequences, starting with `\u`.

```properties
test = {"\u0041"}
```

```json
{
"value": "A",
"errors": []
}
```

Astral characters can be escaped using the six-hexdigit escape sequences,
starting with `\U`.

```properties
test = {"\U01F602"}
```

```json
{
"value": "😂",
"errors": []
}
```

A double-backslash can be used to obtain a single backslash in the formatting output.

```properties
test = {"\\U01F602"}
```

```json
{
"value": "\\U01F602",
"errors": []
}
```
15 changes: 15 additions & 0 deletions spec/format/text_element.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ test =
"errors": []
}
```

## Escape Sequences

Escape sequences are not supported in `TextElements`.

```properties
test = \U01F602
```

```json
{
"value": "\\U01F602",
"errors": []
}
```

0 comments on commit 52ee622

Please sign in to comment.