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/read/directives.js

44 lines
935 B

import { tokenizer, tokTypes, parseExpressionAt } from 'acorn';
8 years ago
export function readEventHandlerDirective ( parser, start, name ) {
const quoteMark = (
parser.eat( `'` ) ? `'` :
parser.eat( `"` ) ? `"` :
null
);
8 years ago
const expressionStart = parser.index;
let end = null;
let depth = 0;
for ( const token of tokenizer( parser.remaining() ) ) {
if ( token.type === tokTypes.parenL ) depth += 1;
if ( token.type === tokTypes.parenR ) {
depth -= 1;
if ( depth === 0 ) {
8 years ago
end = expressionStart + token.end;
break;
}
}
}
8 years ago
const expression = parseExpressionAt( parser.template.slice( 0, end ), expressionStart );
parser.index = expression.end;
if ( expression.type !== 'CallExpression' ) {
8 years ago
parser.error( `Expected call expression`, expressionStart );
}
if ( quoteMark ) {
parser.eat( quoteMark, true );
}
return {
8 years ago
start,
end: parser.index,
type: 'EventHandler',
name,
expression
};
}