Skip to content
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

Upgrade simple-html-tokenizer #793

Merged
merged 1 commit into from
Mar 20, 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
45 changes: 21 additions & 24 deletions build/broccoli/build-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = function(tsTree, jsTree, packagesTree) {
let nodeTests = transpileNodeTestsToCommonJS(jsTree);

return merge([browserTests, nodeTests]);
}
};

function transpileBrowserTestsToAMD(tsTree, jsTree) {
let testTree = funnel(jsTree, {
Expand All @@ -58,16 +58,12 @@ function transpileBrowserTestsToAMD(tsTree, jsTree) {

function transpileNodeTestsToCommonJS(jsTree) {
let testTree = funnel(jsTree, {
include: [
'@glimmer/**/test/**/*-node-test.js'
]
include: ['@glimmer/**/test/**/*-node-test.js']
});

return babel(testTree, {
sourceMaps: 'inline',
plugins: [
'transform-es2015-modules-commonjs'
]
plugins: ['transform-es2015-modules-commonjs']
});
}

Expand All @@ -80,9 +76,7 @@ function generateTSLintTests(tsTree) {

function includeGlimmerAMD(packages) {
let libAMD = funnel(packages, {
include: [
'@glimmer/*/dist/amd/es5/*.js'
]
include: ['@glimmer/*/dist/amd/es5/*.js']
});

return concat(libAMD, {
Expand All @@ -91,10 +85,12 @@ function includeGlimmerAMD(packages) {
}

function includeVendorDependencies() {
let simpleHTMLTokenizer = funnel('node_modules/simple-html-tokenizer/dist/es6', {
include: ['*.js'],
destDir: 'simple-html-tokenizer'
});
let simpleHTMLTokenizer = funnel(
'node_modules/simple-html-tokenizer/dist/es6',
{
destDir: 'simple-html-tokenizer'
}
);

let simpleDOM = new Rollup('node_modules/simple-dom/lib', {
rollup: {
Expand All @@ -106,13 +102,18 @@ function includeVendorDependencies() {
}
});

let transpiled = transpileES6(merge([simpleHTMLTokenizer, handlebarsInlinedTrees.compiler, simpleDOM]), 'test-dependencies', {
avoidDefine: false
});
let transpiled = transpileES6(
merge([simpleHTMLTokenizer, handlebarsInlinedTrees.compiler, simpleDOM]),
'test-dependencies',
{
avoidDefine: false
}
);

return concat(transpiled, {
inputFiles: ['**/*.js'],
outputFile: 'assets/vendor.js'
})
});
}

function includeTestHarness() {
Expand All @@ -122,19 +123,15 @@ function includeTestHarness() {

let loaderPath = path.parse(require.resolve('loader.js'));
let loader = funnel(loaderPath.dir, {
files: [ loaderPath.base ],
files: [loaderPath.base],
destDir: '/assets'
});

let qunit = funnel(path.join(require.resolve('qunitjs'), '..'), {
destDir: 'assets/'
});

let harnessTrees = [
html,
loader,
qunit
];
let harnessTrees = [html, loader, qunit];

return merge(harnessTrees);
}
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@
"yarn:unlink": "node bin/yarn-link-all --unlink"
},
"ember-addon": {
"paths": [
"build/symlink-dependencies"
]
"paths": ["build/symlink-dependencies"]
},
"dependencies": {
"babel-plugin-debug-macros": "^0.1.11",
"handlebars": "^4.0.6",
"simple-dom": "^0.3.0",
"simple-html-tokenizer": "^0.4.1"
"simple-html-tokenizer": "0.5.0"
},
"devDependencies": {
"@glimmer/build": "^0.8.2",
Expand Down Expand Up @@ -86,4 +84,4 @@
"internal": ":house: Internal"
}
}
}
}
65 changes: 35 additions & 30 deletions packages/@glimmer/syntax/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
EventedTokenizer,
EntityParser,
HTML5NamedCharRefs as namedCharRefs
} from "simple-html-tokenizer";
import { Program } from "./types/nodes";
import * as AST from "./types/nodes";
} from 'simple-html-tokenizer';
import { Program } from './types/nodes';
import * as AST from './types/nodes';
import * as HandlebarsAST from './types/handlebars-ast';
import { Option } from '@glimmer/interfaces';
import { assert, expect } from '@glimmer/util';
Expand Down Expand Up @@ -33,46 +33,49 @@ export interface Attribute {
valueStartColumn: number;
}

export class Parser {
export abstract class Parser {
protected elementStack: Element[] = [];
private source: string[];
public currentAttribute: Option<Attribute> = null;
public currentNode: Option<AST.CommentStatement | AST.TextNode | Tag<'StartTag' | 'EndTag'>> = null;
public currentNode: Option<
AST.CommentStatement | AST.TextNode | Tag<'StartTag' | 'EndTag'>
> = null;
public tokenizer = new EventedTokenizer(this, entityParser);

constructor(source: string) {
this.tokenizer.states.tagOpen = function(this: EventedTokenizer) {
let char = this.consume();
if (char === "!") {
this['state'] = 'markupDeclaration';
} else if (char === "/") {
this['state'] = 'endTagOpen';
} else if (/[A-Za-z]/.test(char)) {
this['state'] = 'tagName';
this['delegate'].beginStartTag();
this['delegate'].appendToTagName(char);
}
};

this.tokenizer.states.endTagOpen = function(this: EventedTokenizer) {
let char = this.consume();
if (/[A-Za-z]/.test(char)) {
this['state'] = 'tagName';
this['delegate'].beginEndTag();
this['delegate'].appendToTagName(char);
}
};

this.source = source.split(/(?:\r\n?|\n)/g);
}

abstract reset(): void;
abstract finishData(): void;
abstract tagOpen(): void;
abstract beginData(): void;
abstract appendToData(char: string): void;
abstract beginStartTag(): void;
abstract appendToTagName(char: string): void;
abstract beginAttribute(): void;
abstract appendToAttributeName(char: string): void;
abstract beginAttributeValue(quoted: boolean): void;
abstract appendToAttributeValue(char: string): void;
abstract finishAttributeValue(): void;
abstract markTagAsSelfClosing(): void;
abstract beginEndTag(): void;
abstract finishTag(): void;
abstract beginComment(): void;
abstract appendToCommentData(char: string): void;
abstract finishComment(): void;
abstract reportSyntaxError(error: string): void;

get currentAttr(): Attribute {
return expect(this.currentAttribute, 'expected attribute');
}

get currentTag(): Tag<'StartTag' | 'EndTag'> {
let node = this.currentNode;
assert(node && (node.type === 'StartTag' || node.type === 'EndTag'), 'expected tag');
assert(
node && (node.type === 'StartTag' || node.type === 'EndTag'),
'expected tag'
);
return node as Tag<'StartTag' | 'EndTag'>;
}

Expand All @@ -98,7 +101,6 @@ export class Parser {
let node = this.currentNode;
assert(node && node.type === 'TextNode', 'expected a text node');
return node as AST.TextNode;

}

acceptNode(node: HandlebarsAST.Program): Program;
Expand All @@ -111,7 +113,10 @@ export class Parser {
return this.elementStack[this.elementStack.length - 1];
}

sourceForNode(node: HandlebarsAST.Node, endNode?: { loc: HandlebarsAST.SourceLocation }): string {
sourceForNode(
node: HandlebarsAST.Node,
endNode?: { loc: HandlebarsAST.SourceLocation }
): string {
let firstLine = node.loc.start.line - 1;
let currentLine = firstLine - 1;
let firstColumn = node.loc.start.column;
Expand Down
Loading