Skip to content

Commit 861f194

Browse files
committed
Add AST.Placeable
1 parent ba452b8 commit 861f194

20 files changed

+419
-136
lines changed

fluent-syntax/src/ast.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ export class TextElement extends SyntaxNode {
6767
}
6868
}
6969

70+
export class Placeable extends SyntaxNode {
71+
constructor(expression) {
72+
super();
73+
this.type = 'Placeable';
74+
this.expression = expression;
75+
}
76+
}
77+
7078
export class Expression extends SyntaxNode {
7179
constructor() {
7280
super();

fluent-syntax/src/ftlstream.js

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22

33
import { ParserStream } from './stream';
44
import { ParseError } from './errors';
5+
import { includes } from './util';
6+
7+
const INLINE_WS = [' ', '\t'];
58

69
export class FTLParserStream extends ParserStream {
7-
peekLineWS() {
10+
peekInlineWS() {
811
let ch = this.currentPeek();
912
while (ch) {
10-
if (ch !== ' ' && ch !== '\t') {
13+
if (!includes(INLINE_WS, ch)) {
1114
break;
1215
}
1316
ch = this.peek();
1417
}
1518
}
1619

17-
skipWSLines() {
20+
skipBlankLines() {
1821
while (true) {
19-
this.peekLineWS();
22+
this.peekInlineWS();
2023

2124
if (this.currentPeek() === '\n') {
2225
this.skipToPeek();
@@ -28,9 +31,9 @@ export class FTLParserStream extends ParserStream {
2831
}
2932
}
3033

31-
skipLineWS() {
34+
skipInlineWS() {
3235
while (this.ch) {
33-
if (this.ch !== ' ' && this.ch !== '\t') {
36+
if (!includes(INLINE_WS, this.ch)) {
3437
break;
3538
}
3639
this.next();
@@ -84,22 +87,6 @@ export class FTLParserStream extends ParserStream {
8487
return ((cc >= 48 && cc <= 57) || cc === 45); // 0-9
8588
}
8689

87-
isPeekNextLineIndented() {
88-
if (!this.currentPeekIs('\n')) {
89-
return false;
90-
}
91-
92-
this.peek();
93-
94-
if (this.currentPeekIs(' ')) {
95-
this.resetPeek();
96-
return true;
97-
}
98-
99-
this.resetPeek();
100-
return false;
101-
}
102-
10390
isPeekNextLineVariantStart() {
10491
if (!this.currentPeekIs('\n')) {
10592
return false;
@@ -109,7 +96,7 @@ export class FTLParserStream extends ParserStream {
10996

11097
const ptr = this.getPeekIndex();
11198

112-
this.peekLineWS();
99+
this.peekInlineWS();
113100

114101
if (this.getPeekIndex() - ptr === 0) {
115102
this.resetPeek();
@@ -137,7 +124,7 @@ export class FTLParserStream extends ParserStream {
137124

138125
const ptr = this.getPeekIndex();
139126

140-
this.peekLineWS();
127+
this.peekInlineWS();
141128

142129
if (this.getPeekIndex() - ptr === 0) {
143130
this.resetPeek();
@@ -162,7 +149,7 @@ export class FTLParserStream extends ParserStream {
162149

163150
const ptr = this.getPeekIndex();
164151

165-
this.peekLineWS();
152+
this.peekInlineWS();
166153

167154
if (this.getPeekIndex() - ptr === 0) {
168155
this.resetPeek();
@@ -191,7 +178,7 @@ export class FTLParserStream extends ParserStream {
191178

192179
const ptr = this.getPeekIndex();
193180

194-
this.peekLineWS();
181+
this.peekInlineWS();
195182

196183
if (this.getPeekIndex() - ptr === 0) {
197184
this.resetPeek();
@@ -208,7 +195,7 @@ export class FTLParserStream extends ParserStream {
208195
}
209196

210197
skipToNextEntryStart() {
211-
while (this.next()) {
198+
while (this.ch) {
212199
if (this.currentIs('\n') && !this.peekCharIs('\n')) {
213200
this.next();
214201
if (this.ch === undefined || this.isIDStart() ||
@@ -217,6 +204,7 @@ export class FTLParserStream extends ParserStream {
217204
break;
218205
}
219206
}
207+
this.next();
220208
}
221209
}
222210

fluent-syntax/src/index.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ export function serialize(resource, opts) {
1515
}
1616

1717
export function lineOffset(source, pos) {
18-
// Substract 1 to get the offset.
18+
// Subtract 1 to get the offset.
1919
return source.substring(0, pos).split('\n').length - 1;
2020
}
2121

2222
export function columnOffset(source, pos) {
23-
const lastLineBreak = source.lastIndexOf('\n', pos);
24-
return lastLineBreak === -1
25-
? pos
26-
// Substracting two offsets gives length; substract 1 to get the offset.
27-
: pos - lastLineBreak - 1;
23+
// Find the last line break starting backwards from the index just before
24+
// pos. This allows us to correctly handle ths case where the character at
25+
// pos is a line break as well.
26+
const fromIndex = pos - 1;
27+
const prevLineBreak = source.lastIndexOf('\n', fromIndex);
28+
29+
// pos is a position in the first line of source.
30+
if (prevLineBreak === -1) {
31+
return pos;
32+
}
33+
34+
// Subtracting two offsets gives length; subtract 1 to get the offset.
35+
return pos - prevLineBreak - 1;
2836
}

0 commit comments

Comments
 (0)