From c5fd64c72a0cf08b2eba4dc9088ad00385bf6695 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 31 May 2010 22:56:51 -0400 Subject: [PATCH] allowing direct calls against numbers. --- lib/nodes.js | 10 +++++++--- src/nodes.coffee | 8 ++++++-- test/test_literals.coffee | 4 ++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index 49116605..b43b7d5d 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -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 diff --git a/src/nodes.coffee b/src/nodes.coffee index 91ad3ff4..96da2d85 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -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: /^['"]/ diff --git a/test/test_literals.coffee b/test/test_literals.coffee index ab27f207..5c3bc808 100644 --- a/test/test_literals.coffee +++ b/test/test_literals.coffee @@ -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