diff --git a/compiler/Builder.js b/compiler/Builder.js index 3e77da297..a8e58881d 100644 --- a/compiler/Builder.js +++ b/compiler/Builder.js @@ -46,6 +46,7 @@ var Scriptlet = require('./ast/Scriptlet'); var ContainerNode = require('./ast/ContainerNode'); var parseExpression = require('./util/parseExpression'); +var parseStatement = require('./util/parseStatement'); var parseJavaScriptArgs = require('./util/parseJavaScriptArgs'); var isValidJavaScriptIdentifier = require('./util/isValidJavaScriptIdentifier'); @@ -385,6 +386,12 @@ class Builder { return parseJavaScriptArgs(args, DEFAULT_BUILDER); } + parseStatement(str, options) { + ok(typeof str === 'string', '"str" should be a string expression'); + var parsed = parseStatement(str, DEFAULT_BUILDER); + return parsed; + } + program(body) { return new Program({body}); } diff --git a/compiler/util/parseExpression.js b/compiler/util/parseExpression.js index 89d2299d3..accf3f8ee 100644 --- a/compiler/util/parseExpression.js +++ b/compiler/util/parseExpression.js @@ -1,235 +1,5 @@ -'use strict'; -var ok = require('assert').ok; -var charProps = require('char-props'); +var parseJavaScript = require('./parseJavaScript'); -const esprima = require('esprima'); - -function parseExpression(src, builder) { - ok(typeof src === 'string', '"src" should be a string expression'); - ok(builder, '"builder" is required'); - - function convert(node) { - - if (Array.isArray(node)) { - let nodes = node; - for (let i=0; i= 0) { - var srcCharProps = charProps(src); - errorIndex--; // Account for extra paren added to start - let line = srcCharProps.lineAt(errorIndex)+1; - let column = srcCharProps.columnAt(errorIndex)+1; - errorMessage += ' [Line ' + line + ', Col: ' + column + ']'; - } - var wrappedError = new Error('Invalid JavaScript expression: ' + src + ' - ' + errorMessage); - wrappedError.index = errorIndex; - wrappedError.src = src; - wrappedError.code = 'ERR_INVALID_JAVASCRIPT_EXPRESSION'; - throw wrappedError; - } - - var converted = convert(jsAST); - if (converted == null) { - converted = builder.expression(src); - } - - return converted; -} - -module.exports = parseExpression; \ No newline at end of file +module.exports = function(src, builder) { + return parseJavaScript(src, builder, true /* isExpression */ ); +}; \ No newline at end of file diff --git a/compiler/util/parseJavaScript.js b/compiler/util/parseJavaScript.js new file mode 100644 index 000000000..5249687c4 --- /dev/null +++ b/compiler/util/parseJavaScript.js @@ -0,0 +1,249 @@ +'use strict'; +var ok = require('assert').ok; +var charProps = require('char-props'); + +const esprima = require('esprima'); + +function parseExpression(src, builder, isExpression) { + ok(typeof src === 'string', '"src" should be a string expression'); + ok(builder, '"builder" is required'); + + function convert(node) { + + if (Array.isArray(node)) { + let nodes = node; + for (let i=0; i= 0) { + var srcCharProps = charProps(src); + if (isExpression) { + errorIndex--; // Account for extra paren added to start + } + + let line = srcCharProps.lineAt(errorIndex)+1; + let column = srcCharProps.columnAt(errorIndex)+1; + errorMessage += ' [Line ' + line + ', Col: ' + column + ']'; + } + var wrappedError = new Error('Invalid JavaScript expression: ' + src + ' - ' + errorMessage); + wrappedError.index = errorIndex; + wrappedError.src = src; + wrappedError.code = 'ERR_INVALID_JAVASCRIPT_EXPRESSION'; + throw wrappedError; + } + + var converted = convert(jsAST); + if (converted == null) { + converted = builder.expression(src); + } + + return converted; +} + +module.exports = parseExpression; \ No newline at end of file diff --git a/compiler/util/parseStatement.js b/compiler/util/parseStatement.js new file mode 100644 index 000000000..6d5453b5d --- /dev/null +++ b/compiler/util/parseStatement.js @@ -0,0 +1,5 @@ +var parseJavaScript = require('./parseJavaScript'); + +module.exports = function(src, builder) { + return parseJavaScript(src, builder, false /* isExpression */ ); +}; \ No newline at end of file