Skip to content

Add AST.Placeable #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fluent-syntax/src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ export class TextElement extends SyntaxNode {
}
}

export class Placeable extends SyntaxNode {
constructor(expression) {
super();
this.type = 'Placeable';
this.expression = expression;
}
}

export class Expression extends SyntaxNode {
constructor() {
super();
Expand Down
44 changes: 16 additions & 28 deletions fluent-syntax/src/ftlstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

import { ParserStream } from './stream';
import { ParseError } from './errors';
import { includes } from './util';

const INLINE_WS = [' ', '\t'];

export class FTLParserStream extends ParserStream {
peekLineWS() {
peekInlineWS() {
let ch = this.currentPeek();
while (ch) {
if (ch !== ' ' && ch !== '\t') {
if (!includes(INLINE_WS, ch)) {
break;
}
ch = this.peek();
}
}

skipWSLines() {
skipBlankLines() {
while (true) {
this.peekLineWS();
this.peekInlineWS();

if (this.currentPeek() === '\n') {
if (this.currentPeekIs('\n')) {
this.skipToPeek();
this.next();
} else {
Expand All @@ -28,9 +31,9 @@ export class FTLParserStream extends ParserStream {
}
}

skipLineWS() {
skipInlineWS() {
while (this.ch) {
if (this.ch !== ' ' && this.ch !== '\t') {
if (!includes(INLINE_WS, this.ch)) {
break;
}
this.next();
Expand Down Expand Up @@ -84,22 +87,6 @@ export class FTLParserStream extends ParserStream {
return ((cc >= 48 && cc <= 57) || cc === 45); // 0-9
}

isPeekNextLineIndented() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't used anywhere.

if (!this.currentPeekIs('\n')) {
return false;
}

this.peek();

if (this.currentPeekIs(' ')) {
this.resetPeek();
return true;
}

this.resetPeek();
return false;
}

isPeekNextLineVariantStart() {
if (!this.currentPeekIs('\n')) {
return false;
Expand All @@ -109,7 +96,7 @@ export class FTLParserStream extends ParserStream {

const ptr = this.getPeekIndex();

this.peekLineWS();
this.peekInlineWS();

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

const ptr = this.getPeekIndex();

this.peekLineWS();
this.peekInlineWS();

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

const ptr = this.getPeekIndex();

this.peekLineWS();
this.peekInlineWS();

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

const ptr = this.getPeekIndex();

this.peekLineWS();
this.peekInlineWS();

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

skipToNextEntryStart() {
while (this.next()) {
while (this.ch) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the syntax error happens exactly on the newline character, this.next() would overshoot the beginning of the entry on the next line, if present.

if (this.currentIs('\n') && !this.peekCharIs('\n')) {
this.next();
if (this.ch === undefined || this.isIDStart() ||
Expand All @@ -217,6 +204,7 @@ export class FTLParserStream extends ParserStream {
break;
}
}
this.next();
}
}

Expand Down
20 changes: 14 additions & 6 deletions fluent-syntax/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ export function serialize(resource, opts) {
}

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

export function columnOffset(source, pos) {
const lastLineBreak = source.lastIndexOf('\n', pos);
return lastLineBreak === -1
? pos
// Substracting two offsets gives length; substract 1 to get the offset.
: pos - lastLineBreak - 1;
// Find the last line break starting backwards from the index just before
// pos. This allows us to correctly handle ths case where the character at
// pos is a line break as well.
const fromIndex = pos - 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, columnOffset was returning 0 when the character at pos was a new line.

const prevLineBreak = source.lastIndexOf('\n', fromIndex);

// pos is a position in the first line of source.
if (prevLineBreak === -1) {
return pos;
}

// Subtracting two offsets gives length; subtract 1 to get the offset.
return pos - prevLineBreak - 1;
}
Loading