|
|
@ -1,177 +1,204 @@
|
|
|
|
import { locate } from 'locate-character';
|
|
|
|
import { locate, Location } from 'locate-character';
|
|
|
|
import fragment from './state/fragment.ts';
|
|
|
|
import fragment from './state/fragment';
|
|
|
|
import { whitespace } from '../utils/patterns.ts';
|
|
|
|
import { whitespace } from '../utils/patterns';
|
|
|
|
import { trimStart, trimEnd } from '../utils/trim.ts';
|
|
|
|
import { trimStart, trimEnd } from '../utils/trim';
|
|
|
|
import getCodeFrame from '../utils/getCodeFrame.ts';
|
|
|
|
import getCodeFrame from '../utils/getCodeFrame';
|
|
|
|
import hash from './utils/hash.ts';
|
|
|
|
import hash from './utils/hash';
|
|
|
|
|
|
|
|
import { Node } from './interfaces';
|
|
|
|
function ParseError ( message, template, index, filename ) {
|
|
|
|
|
|
|
|
const { line, column } = locate( template, index );
|
|
|
|
class ParseError extends Error {
|
|
|
|
|
|
|
|
frame: string
|
|
|
|
this.name = 'ParseError';
|
|
|
|
loc: { line: number, column: number }
|
|
|
|
this.message = message;
|
|
|
|
pos: number
|
|
|
|
this.frame = getCodeFrame( template, line, column );
|
|
|
|
filename: string
|
|
|
|
|
|
|
|
|
|
|
|
this.loc = { line: line + 1, column };
|
|
|
|
constructor ( message: string, template: string, index: number, filename: string ) {
|
|
|
|
this.pos = index;
|
|
|
|
super( message );
|
|
|
|
this.filename = filename;
|
|
|
|
|
|
|
|
}
|
|
|
|
const { line, column } = locate( template, index );
|
|
|
|
|
|
|
|
|
|
|
|
ParseError.prototype.toString = function () {
|
|
|
|
this.name = 'ParseError';
|
|
|
|
return `${this.message} (${this.loc.line}:${this.loc.column})\n${this.frame}`;
|
|
|
|
this.loc = { line: line + 1, column };
|
|
|
|
};
|
|
|
|
this.pos = index;
|
|
|
|
|
|
|
|
this.filename = filename;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.frame = getCodeFrame( template, line, column );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default function parse ( template, options = {} ) {
|
|
|
|
toString () {
|
|
|
|
if ( typeof template !== 'string' ) {
|
|
|
|
return `${this.message} (${this.loc.line}:${this.loc.column})\n${this.frame}`;
|
|
|
|
throw new TypeError( 'Template must be a string' );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template = template.replace( /\s+$/, '' );
|
|
|
|
interface ParserOptions {
|
|
|
|
|
|
|
|
filename?: string
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const parser = {
|
|
|
|
export class Parser {
|
|
|
|
index: 0,
|
|
|
|
readonly template: string;
|
|
|
|
template,
|
|
|
|
readonly filename?: string;
|
|
|
|
stack: [],
|
|
|
|
|
|
|
|
metaTags: {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current () {
|
|
|
|
index: number;
|
|
|
|
return this.stack[ this.stack.length - 1 ];
|
|
|
|
stack: Array<Node>;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
acornError ( err ) {
|
|
|
|
html: Node;
|
|
|
|
parser.error( err.message.replace( / \(\d+:\d+\)$/, '' ), err.pos );
|
|
|
|
css: Node;
|
|
|
|
},
|
|
|
|
js: Node;
|
|
|
|
|
|
|
|
metaTags: {}
|
|
|
|
|
|
|
|
|
|
|
|
error ( message, index = this.index ) {
|
|
|
|
constructor ( template: string, options: ParserOptions ) {
|
|
|
|
throw new ParseError( message, this.template, index, options.filename );
|
|
|
|
if ( typeof template !== 'string' ) {
|
|
|
|
},
|
|
|
|
throw new TypeError( 'Template must be a string' );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
eat ( str, required ) {
|
|
|
|
this.template = template.replace( /\s+$/, '' );
|
|
|
|
if ( this.match( str ) ) {
|
|
|
|
this.filename = options.filename;
|
|
|
|
this.index += str.length;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( required ) {
|
|
|
|
this.index = 0;
|
|
|
|
this.error( `Expected ${str}` );
|
|
|
|
this.stack = [];
|
|
|
|
}
|
|
|
|
this.metaTags = {};
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
this.html = {
|
|
|
|
|
|
|
|
start: null,
|
|
|
|
|
|
|
|
end: null,
|
|
|
|
|
|
|
|
type: 'Fragment',
|
|
|
|
|
|
|
|
children: []
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
match ( str ) {
|
|
|
|
this.css = null;
|
|
|
|
return this.template.slice( this.index, this.index + str.length ) === str;
|
|
|
|
this.js = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
allowWhitespace () {
|
|
|
|
this.stack.push( this.html );
|
|
|
|
while ( this.index < this.template.length && whitespace.test( this.template[ this.index ] ) ) {
|
|
|
|
|
|
|
|
this.index++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
read ( pattern ) {
|
|
|
|
let state = fragment;
|
|
|
|
const match = pattern.exec( this.template.slice( this.index ) );
|
|
|
|
|
|
|
|
if ( !match || match.index !== 0 ) return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parser.index += match[0].length;
|
|
|
|
while ( this.index < this.template.length ) {
|
|
|
|
|
|
|
|
state = state( this ) || fragment;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return match[0];
|
|
|
|
if ( this.stack.length > 1 ) {
|
|
|
|
},
|
|
|
|
const current = this.current();
|
|
|
|
|
|
|
|
|
|
|
|
readUntil ( pattern ) {
|
|
|
|
const type = current.type === 'Element' ? `<${current.name}>` : 'Block';
|
|
|
|
if ( this.index >= this.template.length ) parser.error( 'Unexpected end of input' );
|
|
|
|
this.error( `${type} was left open`, current.start );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const start = this.index;
|
|
|
|
if ( state !== fragment ) {
|
|
|
|
const match = pattern.exec( this.template.slice( start ) );
|
|
|
|
this.error( 'Unexpected end of input' );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( match ) {
|
|
|
|
// trim unnecessary whitespace
|
|
|
|
const start = this.index;
|
|
|
|
while ( this.html.children.length ) {
|
|
|
|
this.index = start + match.index;
|
|
|
|
const firstChild = this.html.children[0];
|
|
|
|
return this.template.slice( start, this.index );
|
|
|
|
this.html.start = firstChild.start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.index = this.template.length;
|
|
|
|
if ( firstChild.type !== 'Text' ) break;
|
|
|
|
return this.template.slice( start );
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
remaining () {
|
|
|
|
const length = firstChild.data.length;
|
|
|
|
return this.template.slice( this.index );
|
|
|
|
firstChild.data = trimStart( firstChild.data );
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requireWhitespace () {
|
|
|
|
if ( firstChild.data === '' ) {
|
|
|
|
if ( !whitespace.test( this.template[ this.index ] ) ) {
|
|
|
|
this.html.children.shift();
|
|
|
|
this.error( `Expected whitespace` );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.html.start += length - firstChild.data.length;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.allowWhitespace();
|
|
|
|
while ( this.html.children.length ) {
|
|
|
|
},
|
|
|
|
const lastChild = this.html.children[ this.html.children.length - 1 ];
|
|
|
|
|
|
|
|
this.html.end = lastChild.end;
|
|
|
|
|
|
|
|
|
|
|
|
html: {
|
|
|
|
if ( lastChild.type !== 'Text' ) break;
|
|
|
|
start: null,
|
|
|
|
|
|
|
|
end: null,
|
|
|
|
|
|
|
|
type: 'Fragment',
|
|
|
|
|
|
|
|
children: []
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
css: null,
|
|
|
|
const length = lastChild.data.length;
|
|
|
|
|
|
|
|
lastChild.data = trimEnd( lastChild.data );
|
|
|
|
|
|
|
|
|
|
|
|
js: null
|
|
|
|
if ( lastChild.data === '' ) {
|
|
|
|
};
|
|
|
|
this.html.children.pop();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.html.end -= length - lastChild.data.length;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
parser.stack.push( parser.html );
|
|
|
|
current () {
|
|
|
|
|
|
|
|
return this.stack[ this.stack.length - 1 ];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let state = fragment;
|
|
|
|
acornError ( err: Error ) {
|
|
|
|
|
|
|
|
this.error( err.message.replace( / \(\d+:\d+\)$/, '' ), err.pos );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while ( parser.index < parser.template.length ) {
|
|
|
|
error ( message: string, index = this.index ) {
|
|
|
|
state = state( parser ) || fragment;
|
|
|
|
throw new ParseError( message, this.template, index, this.filename );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( parser.stack.length > 1 ) {
|
|
|
|
eat ( str: string, required?: boolean ) {
|
|
|
|
const current = parser.current();
|
|
|
|
if ( this.match( str ) ) {
|
|
|
|
|
|
|
|
this.index += str.length;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const type = current.type === 'Element' ? `<${current.name}>` : 'Block';
|
|
|
|
if ( required ) {
|
|
|
|
parser.error( `${type} was left open`, current.start );
|
|
|
|
this.error( `Expected ${str}` );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( state !== fragment ) {
|
|
|
|
match ( str: string ) {
|
|
|
|
parser.error( 'Unexpected end of input' );
|
|
|
|
return this.template.slice( this.index, this.index + str.length ) === str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// trim unnecessary whitespace
|
|
|
|
allowWhitespace () {
|
|
|
|
while ( parser.html.children.length ) {
|
|
|
|
while ( this.index < this.template.length && whitespace.test( this.template[ this.index ] ) ) {
|
|
|
|
const firstChild = parser.html.children[0];
|
|
|
|
this.index++;
|
|
|
|
parser.html.start = firstChild.start;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( firstChild.type !== 'Text' ) break;
|
|
|
|
read ( pattern: RegExp ) {
|
|
|
|
|
|
|
|
const match = pattern.exec( this.template.slice( this.index ) );
|
|
|
|
|
|
|
|
if ( !match || match.index !== 0 ) return null;
|
|
|
|
|
|
|
|
|
|
|
|
const length = firstChild.data.length;
|
|
|
|
this.index += match[0].length;
|
|
|
|
firstChild.data = trimStart( firstChild.data );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( firstChild.data === '' ) {
|
|
|
|
return match[0];
|
|
|
|
parser.html.children.shift();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
parser.html.start += length - firstChild.data.length;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while ( parser.html.children.length ) {
|
|
|
|
readUntil ( pattern: RegExp ) {
|
|
|
|
const lastChild = parser.html.children[ parser.html.children.length - 1 ];
|
|
|
|
if ( this.index >= this.template.length ) this.error( 'Unexpected end of input' );
|
|
|
|
parser.html.end = lastChild.end;
|
|
|
|
|
|
|
|
|
|
|
|
const start = this.index;
|
|
|
|
|
|
|
|
const match = pattern.exec( this.template.slice( start ) );
|
|
|
|
|
|
|
|
|
|
|
|
if ( lastChild.type !== 'Text' ) break;
|
|
|
|
if ( match ) {
|
|
|
|
|
|
|
|
const start = this.index;
|
|
|
|
|
|
|
|
this.index = start + match.index;
|
|
|
|
|
|
|
|
return this.template.slice( start, this.index );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const length = lastChild.data.length;
|
|
|
|
this.index = this.template.length;
|
|
|
|
lastChild.data = trimEnd( lastChild.data );
|
|
|
|
return this.template.slice( start );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( lastChild.data === '' ) {
|
|
|
|
remaining () {
|
|
|
|
parser.html.children.pop();
|
|
|
|
return this.template.slice( this.index );
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
parser.html.end -= length - lastChild.data.length;
|
|
|
|
|
|
|
|
break;
|
|
|
|
requireWhitespace () {
|
|
|
|
|
|
|
|
if ( !whitespace.test( this.template[ this.index ] ) ) {
|
|
|
|
|
|
|
|
this.error( `Expected whitespace` );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.allowWhitespace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function parse ( template: string, options: ParserOptions = {} ) {
|
|
|
|
|
|
|
|
const parser = new Parser( template, options );
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
hash: hash( template ),
|
|
|
|
hash: hash( parser.template ),
|
|
|
|
html: parser.html,
|
|
|
|
html: parser.html,
|
|
|
|
css: parser.css,
|
|
|
|
css: parser.css,
|
|
|
|
js: parser.js
|
|
|
|
js: parser.js
|
|
|
|