More char code tests
This commit is contained in:
+129
-66
@@ -1,5 +1,8 @@
|
|||||||
|
goog.provide('ol.expression');
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Support a very limited subset of ECMAScript 5.1
|
* Support an extremely limited subset of ECMAScript 5.1
|
||||||
* http://www.ecma-international.org/ecma-262/5.1/
|
* http://www.ecma-international.org/ecma-262/5.1/
|
||||||
*
|
*
|
||||||
* Inspired by Esprima (https://github.com/ariya/esprima)
|
* Inspired by Esprima (https://github.com/ariya/esprima)
|
||||||
@@ -7,88 +10,70 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @enum {number}
|
||||||
|
*/
|
||||||
|
ol.expression.Char = {
|
||||||
|
CARRIAGE_RETURN: 13,
|
||||||
|
DIGIT_0: 48,
|
||||||
|
DIGIT_7: 55,
|
||||||
|
DIGIT_9: 57,
|
||||||
|
DOLLAR: 36,
|
||||||
|
FORM_FEED: 0xC,
|
||||||
|
LINE_FEED: 10,
|
||||||
|
LINE_SEPARATOR: 0x2028,
|
||||||
|
LOWER_A: 97,
|
||||||
|
LOWER_F: 102,
|
||||||
|
LOWER_Z: 122,
|
||||||
|
NONBREAKING_SPACE: 0xA0,
|
||||||
|
PARAGRAPH_SEPARATOR: 0x2029,
|
||||||
|
SPACE: 32,
|
||||||
|
TAB: 9,
|
||||||
|
UNDERSCORE: 95,
|
||||||
|
UPPER_A: 65,
|
||||||
|
UPPER_F: 70,
|
||||||
|
UPPER_Z: 90,
|
||||||
|
VERTICAL_TAB: 0xB
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @enum {string}
|
* @enum {string}
|
||||||
*/
|
*/
|
||||||
ol.expression.Syntax = {
|
ol.expression.Syntax = {
|
||||||
BinaryExpression: 'BinaryExpression',
|
BINARY_EXPRESSION: 'BinaryExpression',
|
||||||
CallExpression: 'CallExpression',
|
CALL_EXPRESSION: 'CallExpression',
|
||||||
Identifier: 'Identifier',
|
IDENTIFIER: 'Identifier',
|
||||||
Literal: 'Literal',
|
LITERAL: 'Literal',
|
||||||
LogicalExpression: 'LogicalExpression',
|
LOGICAL_EXPRESSION: 'LogicalExpression',
|
||||||
MemberExpression: 'MemberExpression',
|
MEMBER_EXPRESSION: 'MemberExpression',
|
||||||
Property: 'Property', // dot notation only
|
PROPERTY: 'Property', // dot notation only
|
||||||
UnaryExpression: 'UnaryExpression' // only with logical not
|
UNARY_EXPRESSION: 'UnaryExpression' // only with logical not
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @enum {string}
|
* @enum {string}
|
||||||
*/
|
*/
|
||||||
ol.expression.Token = {
|
ol.expression.TokenType = {
|
||||||
BooleanLiteral: 'Boolean',
|
BOOLEAN_LITERAL: 'Boolean',
|
||||||
EOF: '<end>',
|
EOF: '<end>',
|
||||||
Identifier: 'Identifier',
|
IDENTIFIER: 'Identifier',
|
||||||
Keyword: 'Keyword',
|
KEYWORD: 'Keyword',
|
||||||
NullLiteral: 'Null',
|
NULL_LITERAL: 'Null',
|
||||||
NumericLiteral: 'Numeric',
|
NUMERIC_LITERAL: 'Numeric',
|
||||||
Punctuator: 'Punctuator',
|
PUNCTUATOR: 'Punctuator',
|
||||||
StringLiteral: 'String'
|
STRING_LITERAL: 'String'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* http://www.ecma-international.org/ecma-262/5.1/#sec-7.2
|
* http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.3
|
||||||
* @param {number} ch The unicode of a character.
|
* @param {number} ch The unicode of a character.
|
||||||
* @return {boolean} The character is whitespace.
|
* @return {boolean} The character is a decimal digit.
|
||||||
*/
|
*/
|
||||||
ol.expression.isWhitespace = function(ch) {
|
ol.expression.isDecimalDigit = function(ch) {
|
||||||
return (ch === 32) || // <SP>
|
return (ch >= ol.expression.Char.DIGIT_0 && ch <= ol.expression.Char.DIGIT_9);
|
||||||
(ch === 9) || // <TAB>
|
|
||||||
(ch === 0xB) || // <VT>
|
|
||||||
(ch === 0xC) || // <FF>
|
|
||||||
(ch === 0xA0) || // <NBSP>
|
|
||||||
(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) || // <LF>
|
|
||||||
(ch === 13) || // <CR>
|
|
||||||
(ch === 0x2028) || // <LS>
|
|
||||||
(ch === 0x2029); // <PS>
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -110,3 +95,81 @@ ol.expression.isFutureReservedWord = function(id) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.3
|
||||||
|
* @param {number} ch The unicode of a character.
|
||||||
|
* @return {boolean} The character is a hex digit.
|
||||||
|
*/
|
||||||
|
ol.expression.isHexDigit = function(ch) {
|
||||||
|
return ol.expression.isDecimalDigit(ch) ||
|
||||||
|
(ch >= ol.expression.Char.LOWER_A && ch <= ol.expression.Char.LOWER_F) ||
|
||||||
|
(ch >= ol.expression.Char.UPPER_A && ch <= ol.expression.Char.UPPER_F);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 >= ol.expression.Char.DIGIT_0 && ch <= ol.expression.Char.DIGIT_9);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http://www.ecma-international.org/ecma-262/5.1/#sec-7.6
|
||||||
|
* Doesn't yet 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 === ol.expression.Char.DOLLAR) ||
|
||||||
|
(ch === ol.expression.Char.UNDERSCORE) ||
|
||||||
|
(ch >= ol.expression.Char.UPPER_A && ch <= ol.expression.Char.UPPER_Z) ||
|
||||||
|
(ch >= ol.expression.Char.LOWER_A && ch <= ol.expression.Char.LOWER_Z);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 === ol.expression.Char.LINE_FEED) ||
|
||||||
|
(ch === ol.expression.Char.CARRIAGE_RETURN) ||
|
||||||
|
(ch === ol.expression.Char.LINE_SEPARATOR) ||
|
||||||
|
(ch === ol.expression.Char.PARAGRAPH_SEPARATOR);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.3
|
||||||
|
* @param {number} ch The unicode of a character.
|
||||||
|
* @return {boolean} The character is an octal digit.
|
||||||
|
*/
|
||||||
|
ol.expression.isOctalDigit = function(ch) {
|
||||||
|
return (ch >= ol.expression.Char.DIGIT_0 && ch <= ol.expression.Char.DIGIT_7);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 === ol.expression.Char.SPACE) ||
|
||||||
|
(ch === ol.expression.Char.TAB) ||
|
||||||
|
(ch === ol.expression.Char.VERTICAL_TAB) ||
|
||||||
|
(ch === ol.expression.Char.FORM_FEED) ||
|
||||||
|
(ch === ol.expression.Char.NONBREAKING_SPACE) ||
|
||||||
|
(ch >= 0x1680 && '\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005' +
|
||||||
|
'\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\uFEFF'
|
||||||
|
.indexOf(String.fromCharCode(ch)) > 0);
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user