Skip to content

Indentation/Whitespace in 0.7 #284

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 3 commits into from
Sep 24, 2018
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
109 changes: 50 additions & 59 deletions fluent-syntax/src/ftlstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,34 @@ import { ParserStream } from "./stream";
import { ParseError } from "./errors";
import { includes } from "./util";

const INLINE_WS = [" ", "\t"];
const INLINE_WS = " ";
const ANY_WS = [INLINE_WS, "\n"];
const SPECIAL_LINE_START_CHARS = ["}", ".", "[", "*"];

export class FTLParserStream extends ParserStream {
skipInlineWS() {
skipBlankInline() {
while (this.ch) {
if (!includes(INLINE_WS, this.ch)) {
if (this.ch !== INLINE_WS) {
break;
}
this.next();
}
}

peekInlineWS() {
peekBlankInline() {
let ch = this.currentPeek();
while (ch) {
if (!includes(INLINE_WS, ch)) {
if (ch !== INLINE_WS) {
break;
}
ch = this.peek();
}
}

skipBlankLines() {
skipBlankBlock() {
let lineCount = 0;
while (true) {
this.peekInlineWS();
this.peekBlankInline();

if (this.currentPeekIs("\n")) {
this.skipToPeek();
Expand All @@ -43,11 +44,11 @@ export class FTLParserStream extends ParserStream {
}
}

peekBlankLines() {
peekBlankBlock() {
while (true) {
const lineStart = this.getPeekIndex();

this.peekInlineWS();
this.peekBlankInline();

if (this.currentPeekIs("\n")) {
this.peek();
Expand All @@ -58,9 +59,16 @@ export class FTLParserStream extends ParserStream {
}
}

skipIndent() {
this.skipBlankLines();
this.skipInlineWS();
skipBlank() {
while (includes(ANY_WS, this.ch)) {
this.next();
}
}

peekBlank() {
while (includes(ANY_WS, this.currentPeek())) {
this.peek();
}
}

expectChar(ch) {
Expand All @@ -79,9 +87,9 @@ export class FTLParserStream extends ParserStream {

expectIndent() {
this.expectChar("\n");
this.skipBlankLines();
this.skipBlankBlock();
this.expectChar(" ");
this.skipInlineWS();
this.skipBlankInline();
}

expectLineEnd() {
Expand Down Expand Up @@ -143,16 +151,19 @@ export class FTLParserStream extends ParserStream {
return !includes(SPECIAL_LINE_START_CHARS, ch);
}

isPeekValueStart() {
this.peekInlineWS();
isValueStart({skip = true} = {}) {
if (skip === false) throw new Error("Unimplemented");

this.peekBlankInline();
const ch = this.currentPeek();

// Inline Patterns may start with any char.
if (ch !== undefined && ch !== "\n") {
this.skipToPeek();
return true;
}

return this.isPeekNextLineValue();
return this.isNextLineValue();
}

// -1 - any
Expand Down Expand Up @@ -193,82 +204,62 @@ export class FTLParserStream extends ParserStream {
return false;
}

this.peek();

this.peekBlankLines();

const ptr = this.getPeekIndex();

this.peekInlineWS();

if (this.getPeekIndex() - ptr === 0) {
this.resetPeek();
return false;
}
this.peekBlank();

if (this.currentPeekIs("*")) {
this.peek();
}

if (this.currentPeekIs("[") && !this.peekCharIs("[")) {
if (this.currentPeekIs("[")) {
this.resetPeek();
return true;
}
this.resetPeek();
return false;
}

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

this.peek();
isNextLineAttributeStart({skip = true} = {}) {
if (skip === false) throw new Error("Unimplemented");

this.peekBlankLines();

const ptr = this.getPeekIndex();

this.peekInlineWS();

if (this.getPeekIndex() - ptr === 0) {
this.resetPeek();
return false;
}
this.peekBlank();

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

this.resetPeek();
return false;
}

isPeekNextLineValue() {
isNextLineValue({skip = true} = {}) {
if (!this.currentPeekIs("\n")) {
return false;
}

this.peek();

this.peekBlankLines();
this.peekBlankBlock();

const ptr = this.getPeekIndex();

this.peekInlineWS();
this.peekBlankInline();

if (this.getPeekIndex() - ptr === 0) {
this.resetPeek();
return false;
if (!this.currentPeekIs("{")) {
if (this.getPeekIndex() - ptr === 0) {
this.resetPeek();
return false;
}

if (!this.isCharPatternContinuation(this.currentPeek())) {
this.resetPeek();
return false;
}
}

if (!this.isCharPatternContinuation(this.currentPeek())) {
if (skip) {
this.skipToPeek();
} else {
this.resetPeek();
return false;
}

this.resetPeek();
return true;
}

Expand Down
Loading