allowing direct calls against numbers.

This commit is contained in:
Jeremy Ashkenas
2010-05-31 22:56:51 -04:00
parent 45f442bd73
commit c5fd64c72a
3 changed files with 17 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
(function(){
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, CurryNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, IndexNode, LiteralNode, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, _a, compact, del, flatten, helpers, include, index_of, literal, merge, starts, utility;
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, CurryNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, IndexNode, LiteralNode, NUMBER, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, _a, compact, del, flatten, helpers, include, index_of, literal, merge, starts, utility;
var __extends = function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
@@ -448,6 +448,9 @@
ValueNode.prototype.is_statement = function() {
return this.base.is_statement && this.base.is_statement() && !this.has_properties();
};
ValueNode.prototype.is_number = function() {
return this.base instanceof LiteralNode && this.base.value.match(NUMBER);
};
// Works out if the value is the start of a chain.
ValueNode.prototype.is_start = function(o) {
var node;
@@ -471,7 +474,7 @@
props = only ? this.properties.slice(0, this.properties.length - 1) : this.properties;
o.chain_root = o.chain_root || this;
baseline = this.base.compile(o);
if (this.base instanceof ObjectNode && this.has_properties()) {
if (this.has_properties() && (this.base instanceof ObjectNode || this.is_number())) {
baseline = ("(" + baseline + ")");
}
complete = (this.last = baseline);
@@ -1905,8 +1908,9 @@
// Trim out all trailing whitespace, so that the generated code plays nice
// with Git.
TRAILING_WHITESPACE = /[ \t]+$/gm;
// Keep this identifier regex in sync with the Lexer.
// Keep these identifier regexes in sync with the Lexer.
IDENTIFIER = /^[a-zA-Z\$_](\w|\$)*$/;
NUMBER = /^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-]?[0-9]+)?)))\b$/i;
// Is a literal value a string?
IS_STRING = /^['"]/;
// Utility Functions

View File

@@ -318,6 +318,9 @@ exports.ValueNode: class ValueNode extends BaseNode
is_statement: ->
@base.is_statement and @base.is_statement() and not @has_properties()
is_number: ->
@base instanceof LiteralNode and @base.value.match NUMBER
# Works out if the value is the start of a chain.
is_start: (o) ->
return true if this is o.chain_root and @properties[0] instanceof AccessorNode
@@ -335,7 +338,7 @@ exports.ValueNode: class ValueNode extends BaseNode
props: if only then @properties[0...@properties.length - 1] else @properties
o.chain_root: or this
baseline: @base.compile o
baseline: "($baseline)" if @base instanceof ObjectNode and @has_properties()
baseline: "($baseline)" if @has_properties() and (@base instanceof ObjectNode or @is_number())
complete: @last: baseline
for prop, i in props
@@ -1441,8 +1444,9 @@ TAB: ' '
# with Git.
TRAILING_WHITESPACE: /[ \t]+$/gm
# Keep this identifier regex in sync with the Lexer.
# Keep these identifier regexes in sync with the Lexer.
IDENTIFIER: /^[a-zA-Z\$_](\w|\$)*$/
NUMBER : /^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-]?[0-9]+)?)))\b$/i
# Is a literal value a string?
IS_STRING: /^['"]/

View File

@@ -25,6 +25,10 @@ ok [0..10].join(' ') is '0 1 2 3 4 5 6 7 8 9 10'
ok [0...10].join(' ') is '0 1 2 3 4 5 6 7 8 9'
# Can call methods directly on numbers.
4.toFixed(10) is '4.0000000000'
func: ->
return if true