mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
allowing paren-less instance creation, a la 'new Class'
This commit is contained in:
parent
7c426db36a
commit
7d79d73b58
11 changed files with 149 additions and 144 deletions
|
@ -19,7 +19,7 @@ spaced_out_multiline_object: {
|
|||
pi: 3.14159
|
||||
list: [1, 2, 3, 4]
|
||||
regex: /match[ing](every|thing|\/)/gi
|
||||
three: new Idea()
|
||||
three: new Idea
|
||||
|
||||
inner_obj: {
|
||||
freedom: -> _.freedom()
|
||||
|
|
|
@ -90,14 +90,14 @@ class LinkedList
|
|||
|
||||
|
||||
# Tests.
|
||||
list: new LinkedList()
|
||||
list: new LinkedList
|
||||
|
||||
list.add("Hi")
|
||||
puts(list.size() is 1)
|
||||
puts(list.item(0) is "Hi")
|
||||
puts(list.item(1) is null)
|
||||
|
||||
list: new LinkedList()
|
||||
list: new LinkedList
|
||||
list.add("zero").add("one").add("two")
|
||||
puts(list.size() is 3)
|
||||
puts(list.item(2) is "two")
|
||||
|
|
|
@ -60,7 +60,7 @@ class Person
|
|||
# p = Person ()
|
||||
# p /name string print
|
||||
|
||||
p: new Person()
|
||||
p: new Person
|
||||
print p.name
|
||||
|
||||
|
||||
|
@ -188,9 +188,9 @@ HomePage::get: (url) ->
|
|||
# b /right = BTree ()
|
||||
|
||||
BTree: ->
|
||||
b: new BTree()
|
||||
b.left: new BTree()
|
||||
b.right: new BTree()
|
||||
b: new BTree
|
||||
b.left: new BTree
|
||||
b.right: new BTree
|
||||
|
||||
|
||||
# BTree = class: /left, /right.
|
||||
|
@ -200,6 +200,6 @@ b.right: new BTree()
|
|||
# 'left path found!' print.
|
||||
|
||||
BTree: ->
|
||||
b: new BTree()
|
||||
b: new BTree
|
||||
|
||||
print('left path found!') if b.left?
|
||||
|
|
|
@ -232,9 +232,11 @@
|
|||
})
|
||||
],
|
||||
Call: [
|
||||
o("Invocation"), o("NEW Invocation", function() {
|
||||
o("Invocation"), o("Super"), o("NEW Invocation", function() {
|
||||
return $2.newInstance();
|
||||
}), o("Super")
|
||||
}), o("NEW Value", function() {
|
||||
return (new CallNode($2, [])).newInstance();
|
||||
})
|
||||
],
|
||||
Extends: [
|
||||
o("SimpleAssignable EXTENDS Value", function() {
|
||||
|
|
234
lib/parser.js
234
lib/parser.js
File diff suppressed because one or more lines are too long
|
@ -25,7 +25,7 @@ else
|
|||
exports.VERSION: '0.7.0'
|
||||
|
||||
# Instantiate a Lexer for our use here.
|
||||
lexer: new Lexer()
|
||||
lexer: new Lexer
|
||||
|
||||
# Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison
|
||||
# compiler.
|
||||
|
|
|
@ -53,8 +53,8 @@ grammar: {
|
|||
# The **Root** is the top-level node in the syntax tree. Since we parse bottom-up,
|
||||
# all parsing must end here.
|
||||
Root: [
|
||||
o "", -> new Expressions()
|
||||
o "TERMINATOR", -> new Expressions()
|
||||
o "", -> new Expressions
|
||||
o "TERMINATOR", -> new Expressions
|
||||
o "Body"
|
||||
o "Block TERMINATOR"
|
||||
]
|
||||
|
@ -106,7 +106,7 @@ grammar: {
|
|||
# token stream.
|
||||
Block: [
|
||||
o "INDENT Body OUTDENT", -> $2
|
||||
o "INDENT OUTDENT", -> new Expressions()
|
||||
o "INDENT OUTDENT", -> new Expressions
|
||||
]
|
||||
|
||||
# A literal identifier, a variable name or property.
|
||||
|
@ -285,8 +285,9 @@ grammar: {
|
|||
# and calling `super()`
|
||||
Call: [
|
||||
o "Invocation"
|
||||
o "NEW Invocation", -> $2.newInstance()
|
||||
o "Super"
|
||||
o "NEW Invocation", -> $2.newInstance()
|
||||
o "NEW Value", -> (new CallNode($2, [])).newInstance()
|
||||
]
|
||||
|
||||
# Extending an object by setting its prototype chain to reference a parent
|
||||
|
|
|
@ -54,7 +54,7 @@ exports.Lexer: class Lexer
|
|||
@extractNextToken()
|
||||
@closeIndentation()
|
||||
return @tokens if o.rewrite is off
|
||||
(new Rewriter()).rewrite @tokens
|
||||
(new Rewriter).rewrite @tokens
|
||||
|
||||
# At every position, run through this list of attempted matches,
|
||||
# short-circuiting if any of them succeed. Their order determines precedence:
|
||||
|
@ -383,7 +383,7 @@ exports.Lexer: class Lexer
|
|||
if str.length < 3 or not starts str, '"'
|
||||
@token 'STRING', str
|
||||
else
|
||||
lexer: new Lexer()
|
||||
lexer: new Lexer
|
||||
tokens: []
|
||||
quote: str.substring 0, 1
|
||||
[i, pi]: [1, 1]
|
||||
|
|
|
@ -630,7 +630,7 @@ exports.ClassNode: class ClassNode extends BaseNode
|
|||
# constructor, property assignments, and inheritance getting built out below.
|
||||
compileNode: (o) ->
|
||||
extension: @parent and new ExtendsNode(@variable, @parent)
|
||||
props: new Expressions()
|
||||
props: new Expressions
|
||||
o.top: true
|
||||
me: null
|
||||
className: @variable.compile o
|
||||
|
@ -642,7 +642,7 @@ exports.ClassNode: class ClassNode extends BaseNode
|
|||
new CallNode(applied, [literal('this'), literal('arguments')])
|
||||
]))
|
||||
else
|
||||
constructor: new CodeNode()
|
||||
constructor: new CodeNode
|
||||
|
||||
for prop in @properties
|
||||
[pvar, func]: [prop.variable, prop.value]
|
||||
|
@ -787,7 +787,7 @@ exports.CodeNode: class CodeNode extends BaseNode
|
|||
|
||||
constructor: (params, body, tag) ->
|
||||
@params: params or []
|
||||
@body: body or new Expressions()
|
||||
@body: body or new Expressions
|
||||
@bound: tag is 'boundfunc'
|
||||
|
||||
# Compilation creates a new scope unless explicitly asked to share with the
|
||||
|
|
|
@ -22,7 +22,7 @@ class ThirdChild extends SecondChild
|
|||
func: (string) ->
|
||||
super('three/') + string
|
||||
|
||||
result: (new ThirdChild()).func 'four'
|
||||
result: (new ThirdChild).func 'four'
|
||||
|
||||
ok result is 'zero/one/two/three/four'
|
||||
ok Base.static('word') is 'static/word'
|
||||
|
@ -40,7 +40,7 @@ class SubClass extends SuperClass
|
|||
constructor: ->
|
||||
super 'sub'
|
||||
|
||||
ok (new SubClass()).prop is 'top-super-sub'
|
||||
ok (new SubClass).prop is 'top-super-sub'
|
||||
|
||||
|
||||
class OneClass
|
||||
|
@ -75,11 +75,11 @@ ThirdChild extends SecondChild
|
|||
ThirdChild::func: (string) ->
|
||||
super('three/') + string
|
||||
|
||||
result: (new ThirdChild()).func 'four'
|
||||
result: (new ThirdChild).func 'four'
|
||||
|
||||
ok result is 'zero/one/two/three/four'
|
||||
|
||||
ok (new ThirdChild())['func-func']('thing') is 'dynamic-thing'
|
||||
ok (new ThirdChild)['func-func']('thing') is 'dynamic-thing'
|
||||
|
||||
|
||||
TopClass: (arg) ->
|
||||
|
@ -97,7 +97,7 @@ SubClass: ->
|
|||
SuperClass extends TopClass
|
||||
SubClass extends SuperClass
|
||||
|
||||
ok (new SubClass()).prop is 'top-super-sub'
|
||||
ok (new SubClass).prop is 'top-super-sub'
|
||||
|
||||
|
||||
# '@' referring to the current instance, and not being coerced into a call.
|
||||
|
@ -105,7 +105,7 @@ class ClassName
|
|||
amI: ->
|
||||
@ instanceof ClassName
|
||||
|
||||
obj: new ClassName()
|
||||
obj: new ClassName
|
||||
ok obj.amI()
|
||||
|
||||
|
||||
|
@ -125,7 +125,7 @@ class Class
|
|||
class: 'class'
|
||||
name: -> @class
|
||||
|
||||
instance: new Class()
|
||||
instance: new Class
|
||||
ok instance.class is 'class'
|
||||
ok instance.name() is 'class'
|
||||
|
||||
|
|
|
@ -89,7 +89,7 @@ class Child extends Parent
|
|||
nums: [3, 2, 1]
|
||||
super nums...
|
||||
|
||||
ok (new Child()).meth().join(' ') is '3 2 1'
|
||||
ok (new Child).meth().join(' ') is '3 2 1'
|
||||
|
||||
|
||||
# Functions with splats being called with too few arguments.
|
||||
|
|
Loading…
Add table
Reference in a new issue