Skip to content

Commit

Permalink
DONE
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmorand committed Jun 4, 2019
1 parent 69fc2f8 commit 622615c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 55 deletions.
1 change: 0 additions & 1 deletion escape.twig

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"fastest:all": "tape 'test/tests/**/test.js' | tap-bail | tap-spec",
"fastest:integration": "tape test/tests/integration/**/test.js | tap-bail | tap-spec",
"fastest:integration:browser": "node test/tests/integration/browser.js | browserify - --basedir ./test -t [ stringify --extensions [.html .twig] ] | tape-run --render='tap-spec'",
"fastest:unit": "tape 'test/tests/unit/**/test.js' | tap-bail | tap-spec",
"fastest:unit": "tape 'test/tests/unit/**/token-stream/test.js' | tap-bail | tap-spec",
"cover": "nyc npm t",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"build": "tsc --project .",
Expand Down
29 changes: 0 additions & 29 deletions render.js

This file was deleted.

31 changes: 7 additions & 24 deletions src/token-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@ export class TwingTokenStream {
}).join('');
}

filter(): TwingToken[] {
// while (!stream.test(TwingTokenType.PUNCTUATION, ']')) {

return [];
}

/**
* todo: use a filter function inside
*
* @return TwingTokenStream
*/
toAst(): TwingTokenStream {
Expand All @@ -60,21 +52,19 @@ export class TwingTokenStream {
while (!this.isEOF()) {
let current: TwingToken = this.getCurrent();

if (current.getType() !== TwingTokenType.WHITESPACE &&
current.getType() !== TwingTokenType.WHITESPACE_CONTROL_MODIFIER_TRIMMING &&
current.getType() !== TwingTokenType.WHITESPACE_CONTROL_MODIFIER_LINE_TRIMMING) {
if (!this.test(TwingTokenType.WHITESPACE) &&
!this.test(TwingTokenType.WHITESPACE_CONTROL_MODIFIER_TRIMMING) &&
!this.test(TwingTokenType.WHITESPACE_CONTROL_MODIFIER_LINE_TRIMMING)) {
let tokenContent: string = current.getContent();

if (current.getType() === TwingTokenType.TEXT ||
current.getType() === TwingTokenType.STRING) {
if (this.test(TwingTokenType.TEXT) || this.test(TwingTokenType.STRING)) {
// strip C slashes
tokenContent = stripcslashes(tokenContent);
// streamline line separators
tokenContent = tokenContent.replace(/\r\n|\r/g, '\n');
}

// remove unnecessary operator spaces
if (current.getType() === TwingTokenType.OPERATOR) {
else if (this.test(TwingTokenType.OPERATOR)) {
// remove unnecessary operator spaces
tokenContent = tokenContent.replace(/\s+/, ' ');
}

Expand All @@ -92,16 +82,9 @@ export class TwingTokenStream {
}

// don't push empty TEXT tokens
if (current.getType() !== TwingTokenType.TEXT || (tokenContent.length > 0)) {
if (!this.test(TwingTokenType.TEXT) || (tokenContent.length > 0)) {
tokens.push(new TwingToken(current.getType(), tokenContent, current.getLine(), current.getColumn()));
}

// // insert an empty STRING token after opening quotes immediately followed by a closing quote
// if (current.getType() === TwingTokenType.OPENING_QUOTE) {
// if (this.look(1, false).getType() === TwingTokenType.CLOSING_QUOTE) {
// tokens.push(new TwingToken(TwingTokenType.STRING, '', current.getLine(), current.getColumn()));
// }
// }
}

this.next();
Expand Down
19 changes: 19 additions & 0 deletions test/tests/unit/token-stream/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,24 @@ EOF()`);
test.end();
});

test.test('toAst', function(test) {
let stream = new TwingTokenStream([
new TwingToken(TwingTokenType.TEXT, '\\a', 1, 1),
new TwingToken(TwingTokenType.TEXT, '\\n', 1, 3),
new TwingToken(TwingTokenType.WHITESPACE, ' ', 1, 5),
new TwingToken(TwingTokenType.WHITESPACE_CONTROL_MODIFIER_LINE_TRIMMING, ' ', 1, 6),
new TwingToken(TwingTokenType.WHITESPACE_CONTROL_MODIFIER_TRIMMING, ' ', 1, 7),
new TwingToken(TwingTokenType.EOF, null, 1, 8)
]);

let ast = stream.toAst();

test.true(ast.nextIf(TwingTokenType.TEXT, 'a'));
test.true(ast.nextIf(TwingTokenType.TEXT, '\n'));
test.same(ast.getCurrent().getType(), TwingTokenType.EOF);

test.end();
});

test.end();
});

0 comments on commit 622615c

Please sign in to comment.