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