You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/compiler/parse/index.js

173 lines
4.0 KiB

8 years ago
import { locate } from 'locate-character';
8 years ago
import fragment from './state/fragment.js';
import { whitespace } from './patterns.js';
import { trimStart, trimEnd } from './utils/trim.js';
8 years ago
import spaces from './utils/spaces.js';
function tabsToSpaces ( str ) {
return str.replace( /^\t+/, match => match.split( '\t' ).join( ' ' ) );
}
function ParseError ( message, template, index ) {
const { line, column } = locate( template, index );
const lines = template.split( '\n' );
const frameStart = Math.max( 0, line - 2 );
const frameEnd = Math.min( line + 3, lines.length );
const digits = String( frameEnd + 1 ).length;
const frame = lines
.slice( frameStart, frameEnd )
.map( ( str, i ) => {
const isErrorLine = frameStart + i === line;
let lineNum = String( i + frameStart + 1 );
while ( lineNum.length < digits ) lineNum = ` ${lineNum}`;
if ( isErrorLine ) {
const indicator = spaces( digits + 2 + tabsToSpaces( str.slice( 0, column ) ).length ) + '^';
return `${lineNum}: ${tabsToSpaces( str )}\n${indicator}`;
}
return `${lineNum}: ${tabsToSpaces( str )}`;
})
.join( '\n' );
this.name = 'ParseError';
8 years ago
this.message = `${message} (${line + 1}:${column})\n${frame}`;
this.loc = { line: line + 1, column };
this.pos = index;
8 years ago
this.shortMessage = message;
}
8 years ago
export default function parse ( template ) {
8 years ago
const parser = {
index: 0,
template,
stack: [],
current () {
return this.stack[ this.stack.length - 1 ];
},
8 years ago
acornError ( err ) {
parser.error( err.message.replace( / \(\d+:\d+\)$/, '' ), err.pos );
8 years ago
},
error ( message, index = this.index ) {
8 years ago
throw new ParseError( message, this.template, index );
8 years ago
},
8 years ago
eat ( str, required ) {
8 years ago
if ( this.match( str ) ) {
this.index += str.length;
return true;
}
8 years ago
if ( required ) {
this.error( `Expected ${str}` );
}
8 years ago
},
match ( str ) {
return this.template.slice( this.index, this.index + str.length ) === str;
},
allowWhitespace () {
while ( this.index < this.template.length && whitespace.test( this.template[ this.index ] ) ) {
this.index++;
}
},
read ( pattern ) {
const match = pattern.exec( this.template.slice( this.index ) );
if ( !match || match.index !== 0 ) return null;
parser.index += match[0].length;
return match[0];
},
8 years ago
readUntil ( pattern ) {
const match = pattern.exec( this.template.slice( this.index ) );
return this.template.slice( this.index, match ? ( this.index += match.index ) : this.template.length );
},
remaining () {
return this.template.slice( this.index );
8 years ago
},
requireWhitespace () {
if ( !whitespace.test( this.template[ this.index ] ) ) {
this.error( `Expected whitespace` );
}
this.allowWhitespace();
8 years ago
},
8 years ago
8 years ago
html: {
start: null,
end: null,
8 years ago
type: 'Fragment',
children: []
},
8 years ago
8 years ago
css: null,
8 years ago
8 years ago
js: null
};
parser.stack.push( parser.html );
8 years ago
let state = fragment;
8 years ago
while ( parser.index < parser.template.length ) {
8 years ago
state = state( parser ) || fragment;
8 years ago
}
if ( state !== fragment || parser.stack.length > 1 ) {
parser.error( 'Unexpected end of input' );
}
// trim unnecessary whitespace
while ( parser.html.children.length ) {
const firstChild = parser.html.children[0];
parser.html.start = firstChild.start;
if ( firstChild.type !== 'Text' ) break;
const length = firstChild.data.length;
firstChild.data = trimStart( firstChild.data );
if ( firstChild.data === '' ) {
parser.html.children.shift();
} else {
parser.html.start += length - firstChild.data.length;
break;
}
}
while ( parser.html.children.length ) {
const lastChild = parser.html.children[ parser.html.children.length - 1 ];
parser.html.end = lastChild.end;
if ( lastChild.type !== 'Text' ) break;
const length = lastChild.data.length;
lastChild.data = trimEnd( lastChild.data );
if ( lastChild.data === '' ) {
parser.html.children.pop();
} else {
parser.html.end -= length - lastChild.data.length;
break;
}
}
8 years ago
return {
html: parser.html,
css: parser.css,
js: parser.js
};
8 years ago
}