From 88fd0fda64a2bd6c6a1ac892db231ccb944fb4c2 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Tue, 4 Jun 2013 16:52:03 -0600 Subject: [PATCH] Initial bits for parser --- src/ol/expression/expression.js | 112 ++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/ol/expression/expression.js diff --git a/src/ol/expression/expression.js b/src/ol/expression/expression.js new file mode 100644 index 0000000000..a83e086934 --- /dev/null +++ b/src/ol/expression/expression.js @@ -0,0 +1,112 @@ +/** + * Support a very limited subset of ECMAScript 5.1 + * http://www.ecma-international.org/ecma-262/5.1/ + * + * Inspired by Esprima (https://github.com/ariya/esprima) + * BSD Licensed + */ + + +/** + * @enum {string} + */ +ol.expression.Syntax = { + BinaryExpression: 'BinaryExpression', + CallExpression: 'CallExpression', + Identifier: 'Identifier', + Literal: 'Literal', + LogicalExpression: 'LogicalExpression', + MemberExpression: 'MemberExpression', + Property: 'Property', // dot notation only + UnaryExpression: 'UnaryExpression' // only with logical not +}; + + +/** + * @enum {string} + */ +ol.expression.Token = { + BooleanLiteral: 'Boolean', + EOF: '', + Identifier: 'Identifier', + Keyword: 'Keyword', + NullLiteral: 'Null', + NumericLiteral: 'Numeric', + Punctuator: 'Punctuator', + StringLiteral: 'String' +}; + + +/** + * http://www.ecma-international.org/ecma-262/5.1/#sec-7.2 + * @param {number} ch The unicode of a character. + * @return {boolean} The character is whitespace. + */ +ol.expression.isWhitespace = function(ch) { + return (ch === 32) || // + (ch === 9) || // + (ch === 0xB) || // + (ch === 0xC) || // + (ch === 0xA0) || // + (ch >= 0x1680 && '\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005' + + '\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\uFEFF' + .indexOf(String.fromCharCode(ch)) > 0); +}; + + +/** + * http://www.ecma-international.org/ecma-262/5.1/#sec-7.3 + * @param {number} ch The unicode of a character. + * @return {boolean} The character is a line terminator. + */ +ol.expression.isLineTerminator = function(ch) { + return (ch === 10) || // + (ch === 13) || // + (ch === 0x2028) || // + (ch === 0x2029); // +}; + + +/** + * http://www.ecma-international.org/ecma-262/5.1/#sec-7.6 + * Doesn't deal with non-ascii identifiers. + * @param {number} ch The unicode of a character. + * @return {boolean} The character is a valid identifier start. + */ +ol.expression.isIdentifierStart = function(ch) { + return (ch === 36) || (ch === 95) || // $ (dollar) and _ (underscore) + (ch >= 65 && ch <= 90) || // A..Z + (ch >= 97 && ch <= 122); // a..z +}; + + +/** + * http://www.ecma-international.org/ecma-262/5.1/#sec-7.6 + * Doesn't deal with non-ascii identifiers. + * @param {number} ch The unicode of a character. + * @return {boolean} The character is a valid identifier part. + */ +ol.expression.isIdentifierPart = function(ch) { + return ol.expression.isIdentifierStart(ch) || + (ch >= 48 && ch <= 57); // 0..9 +}; + + +/** + * http://www.ecma-international.org/ecma-262/5.1/#sec-7.6.1.2 + * @param {string} id A string identifier. + * @return {boolean} The identifier is a future reserved word. + */ +ol.expression.isFutureReservedWord = function(id) { + switch (id) { + case 'class': + case 'enum': + case 'export': + case 'extends': + case 'import': + case 'super': + return true; + default: + return false; + } +};