From d7e096aeee82323f96d61a6d7c4e8c1c176660c6 Mon Sep 17 00:00:00 2001
From: Geoffrey Booth Running The CoffeeScript parser is generated by Jison
+ The CoffeeScript parser is generated by Jison
from this grammar file. Jison is a bottom-up parser generator, similar in
style to Bison, implemented in JavaScript.
-It can recognize LALR(1), LR(0), SLR(1), and LR(1)
+It can recognize LALR(1), LR(0), SLR(1), and LR(1)
type grammars. To create the Jison parser, we list the pattern to match
on the left-hand side, and the action to take (usually the creation of syntax
tree nodes) on the right. As the parser runs, it
shifts tokens from our token stream, from left to right, and
-attempts to match
+attempts to match
the token sequence against the rules below. When a match can be made, it
-reduces into the nonterminal
+reduces into the nonterminal
(the enclosing name at the top), and we proceed from there. If you run the cake
is a simplified version of Make
-(Rake, Jake)
+(Rake, Jake)
for CoffeeScript. You define tasks with names and descriptions in a Cakefile,
and can call them from the command line, or invoke them from other tasks.cake
with no arguments will print out a list of all the tasks in the
diff --git a/docs/v1/annotated-source/grammar.html b/docs/v1/annotated-source/grammar.html
index eb724d82..ab1f3ad1 100644
--- a/docs/v1/annotated-source/grammar.html
+++ b/docs/v1/annotated-source/grammar.html
@@ -115,17 +115,17 @@
cake build:parser
command, Jison constructs a parse table
from our rules and saves it into lib/parser.js
.
Our handy DSL for Jison grammar generation, thanks to -Tim Caswell. For every rule in the grammar, +Tim Caswell. For every rule in the grammar, we pass the pattern-defining string, the action to run, and extra options, optionally. If no action is specified, we simply pass the value of the previous nonterminal.
diff --git a/docs/v1/annotated-source/lexer.html b/docs/v1/annotated-source/lexer.html index 6ae28040..bd843b65 100644 --- a/docs/v1/annotated-source/lexer.html +++ b/docs/v1/annotated-source/lexer.html @@ -121,7 +121,7 @@ a token is produced, we consume the match, and start again. Tokens are in the form:[tag, value, locationData]
where locationData is {first_line, first_column, last_line, last_column}, which is a
-format that can be fed directly into Jison. These
+format that can be fed directly into Jison. These
are read by jison in the parser.lexer
function defined in coffee-script.coffee.
Am I capable of -Python-style comparison chaining?
+Python-style comparison chaining? diff --git a/docs/v1/annotated-source/sourcemap.html b/docs/v1/annotated-source/sourcemap.html index 67525c1b..4b9ac673 100644 --- a/docs/v1/annotated-source/sourcemap.html +++ b/docs/v1/annotated-source/sourcemap.html @@ -406,7 +406,7 @@ column for the current line:Note that SourceMap VLQ encoding is “backwards”. MIDI-style VLQ encoding puts the most-significant-bit (MSB) from the original value into the MSB of the VLQ -encoded value (see Wikipedia). +encoded value (see Wikipedia). SourceMap VLQ does things the other way around, with the least significat four bits of the original value encoded into the first byte of the VLQ encoded value.
diff --git a/docs/v1/index.html b/docs/v1/index.html index 22c352bd..de27322e 100644 --- a/docs/v1/index.html +++ b/docs/v1/index.html @@ -591,18 +591,18 @@ pre .xml .cdata { Annotated Source @@ -612,7 +612,7 @@ pre .xml .cdata {CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.
The golden rule of CoffeeScript is: “It’s just JavaScript”. The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable, pretty-printed, and tends to run as fast or faster than the equivalent handwritten JavaScript.
The CoffeeScript compiler goes to great lengths to generate output JavaScript that runs in every JavaScript runtime, but there are exceptions. Use generator functions, for…from
, or tagged template literals only if you know that your target runtimes can support them. If you use modules, you will need to use an additional tool to resolve them.
Latest Version: 1.12.5
+Latest Version: 1.12.5
npm install -g coffee-script
CoffeeScript 2 is coming! It adds support for ES2015 classes, async
/await
, and generates JavaScript using ES2015+ syntax. Learn more.
The command-line version of coffee
is available as a Node.js utility. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser (see Try CoffeeScript).
To install, first make sure you have a working copy of the latest stable version of Node.js. You can then install CoffeeScript globally with npm:
+The command-line version of coffee
is available as a Node.js utility. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser (see Try CoffeeScript).
To install, first make sure you have a working copy of the latest stable version of Node.js. You can then install CoffeeScript globally with npm:
npm install --global coffee-script
When you need CoffeeScript as a dependency of a project, within that project’s folder you can install it locally:
@@ -842,8 +842,7 @@ cubes = (function() {Besides being used as an ordinary programming language, CoffeeScript may also be written in “literate” mode. If you name your file with a .litcoffee
extension, you can write it as a Markdown document — a document that also happens to be executable CoffeeScript code. The compiler will treat any indented blocks (Markdown’s way of indicating source code) as code, and ignore the rest as comments.
Just for kicks, a little bit of the compiler is currently implemented in this fashion: See it as a document, raw, and properly highlighted in a text editor.
-I’m fairly excited about this direction for the language, and am looking forward to writing (and more importantly, reading) more programs in this style. More information about Literate CoffeeScript, including an example program, are available in this blog post.
+Just for kicks, a little bit of the compiler is currently implemented in this fashion: See it as a document, raw, and properly highlighted in a text editor.
unless
can be used as the inverse of if
.
As a shortcut for this.property
, you can use @property
.
You can use in
to test for array presence, and of
to test for JavaScript object-key presence.
To simplify math expressions, **
can be used for exponentiation and //
performs integer division. %
works just like in JavaScript, while %%
provides “dividend dependent modulo”:
To simplify math expressions, **
can be used for exponentiation and //
performs integer division. %
works just like in JavaScript, while %%
provides “dividend dependent modulo”:
-7 % 5 == -2 # The remainder of 7 / 5
-7 %% 5 == 3 # n %% 5 is always between 0 and 4
@@ -1625,7 +1624,7 @@ zip = typeof lottery.drawWinner ===
Classes, Inheritance, and Super
-JavaScript’s prototypal inheritance has always been a bit of a brain-bender, with a whole family tree of libraries that provide a cleaner syntax for classical inheritance on top of JavaScript’s prototypes: Base2, Prototype.js, JS.Class, etc. The libraries provide syntactic sugar, but the built-in inheritance would be completely usable if it weren’t for a couple of small exceptions: it’s awkward to call super (the prototype object’s implementation of the current function), and it’s awkward to correctly set the prototype chain.
+JavaScript’s prototypal inheritance has always been a bit of a brain-bender, with a whole family tree of libraries that provide a cleaner syntax for classical inheritance on top of JavaScript’s prototypes: Base2, Prototype.js, JS.Class, etc. The libraries provide syntactic sugar, but the built-in inheritance would be completely usable if it weren’t for a couple of small exceptions: it’s awkward to call super (the prototype object’s implementation of the current function), and it’s awkward to correctly set the prototype chain.
Instead of repetitively attaching functions to a prototype, CoffeeScript provides a basic class
structure that allows you to name your class, set the superclass, assign prototypal properties, and define the constructor, in a single assignable expression.
Constructor functions are named, to better support helpful stack traces. In the first class in the example below, this.constructor.name is "Animal"
.
class Animal
@@ -1950,6 +1949,7 @@ Account = function(
If we had used ->
in the callback above, @customer
would have referred to the undefined “customer” property of the DOM element, and trying to call purchase()
on it would have raised an exception.
When used in a class definition, methods declared with the fat arrow will be automatically bound to each instance of the class when the instance is constructed.
+
CoffeeScript functions also support ES2015 generator functions through the yield
keyword. There’s no function*(){}
nonsense — a generator in CoffeeScript is simply a function that yields.
perfectSquares = ->
num = 0
@@ -1984,6 +1984,7 @@ perfectSquares = function*() {
window.ps || (window.ps = perfectSquares());
;alert(ps.next().value);">run: ps.next().value
yield*
is called yield from
, and yield return
may be used if you need to force a generator that doesn’t yield.
You can iterate over a generator function using for…from
.
fibonacci = ->
[previous, current] = [1, 1]
@@ -2184,7 +2185,7 @@ grade = (function(
CoffeeScript borrows chained comparisons from Python — making it easy to test if a value falls within a certain range.
+CoffeeScript borrows chained comparisons from Python — making it easy to test if a value falls within a certain range.
cholesterol = 127
healthy = 200 > cholesterol > 60
@@ -2405,7 +2406,8 @@ OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>&g
max,
min
} from 'underscore';
-
Note that the CoffeeScript compiler does not resolve modules; writing an import
or export
statement in CoffeeScript will produce an import
or export
statement in the resulting output. It is your responsibility attach another transpiler, such as Traceur Compiler, Babel or Rollup, to convert this ES2015 syntax into code that will work in your target runtimes.
Note that the CoffeeScript compiler does not resolve modules; writing an import
or export
statement in CoffeeScript will produce an import
or export
statement in the resulting output. It is your responsibility attach another transpiler, such as Traceur Compiler, Babel or Rollup, to convert this ES2015 syntax into code that will work in your target runtimes.
Also note that any file with an import
or export
statement will be output without a top-level function safety wrapper; in other words, importing or exporting modules will automatically trigger bare mode for that file. This is because per the ES2015 spec, import
or export
statements must occur at the topmost scope.
If you need to invoke one task before another — for example, running build
before test
, you can use the invoke
function: invoke 'build'
. Cake tasks are a minimal way to expose your CoffeeScript functions to the command line, so don’t expect any fanciness built-in. If you need dependencies, or async callbacks, it’s best to put them in your code itself — not the cake task.
If you need to invoke one task before another — for example, running build
before test
, you can use the invoke
function: invoke 'build'
. Cake tasks are a minimal way to expose your CoffeeScript functions to the command line, so don’t expect any fanciness built-in. If you need dependencies, or async callbacks, it’s best to put them in your code itself — not the cake task.
CoffeeScript 1.6.1 and above include support for generating source maps, a way to tell your JavaScript engine what part of your CoffeeScript program matches up with the code being evaluated. Browsers that support it can automatically use source maps to show your original source code in the debugger. To generate source maps alongside your JavaScript files, pass the --map
or -m
flag to the compiler.
For a full introduction to source maps, how they work, and how to hook them up in your browser, read the HTML5 Tutorial.
+For a full introduction to source maps, how they work, and how to hook them up in your browser, read the HTML5 Tutorial.
While it’s not recommended for serious use, CoffeeScripts may be included directly within the browser using <script type="text/coffeescript">
tags. The source includes a compressed and minified version of the compiler (Download current version here, 51k when gzipped) as v1/browser-compiler/coffee-script.js
. Include this file on a page with inline CoffeeScript tags, and it will compile and evaluate them in order.
While it’s not recommended for serious use, CoffeeScripts may be included directly within the browser using <script type="text/coffeescript">
tags. The source includes a compressed and minified version of the compiler (Download current version here, 51k when gzipped) as v1/browser-compiler/coffee-script.js
. Include this file on a page with inline CoffeeScript tags, and it will compile and evaluate them in order.
In fact, the little bit of glue script that runs “Try CoffeeScript” above, as well as the jQuery for the menu, is implemented in just this way. View source and look at the bottom of the page to see the example. Including the script also gives you access to CoffeeScript.compile()
so you can pop open Firebug and try compiling some strings.
The usual caveats about CoffeeScript apply — your inline scripts will run within a closure wrapper, so if you want to expose global variables or functions, attach them to the window
object.
There are a number of excellent resources to help you get started with CoffeeScript, some of which are freely available online.
The best list of open-source CoffeeScript examples can be found on GitHub. But just to throw out a few more:
Source Code
Use bin/coffee
to test your changes,
bin/cake test
to run the test suite,
bin/cake build
to rebuild the full CoffeeScript compiler, and
@@ -2495,11 +2496,11 @@ Use bin/coffee
to test your changes,
git checkout lib && bin/cake build:full
is a good command to run when you’re working on the core language. It’ll refresh the lib
folder (in case you broke something), build your altered compiler, use that to rebuild itself (a good sanity test) and then run all of the tests. If they pass, there’s a good chance you’ve made a successful change.
Browser Tests
Run CoffeeScript’s test suite in your current browser.
CoffeeScript Issues
Bug reports, feature proposals, and ideas for changes to the language belong here.
The CoffeeScript Wiki
-If you’ve ever learned a neat CoffeeScript tip or trick, or ran into a gotcha — share it on the wiki. The wiki also serves as a directory of handy text editor extensions, web framework plugins, and general CoffeeScript build tools.
The CoffeeScript Wiki
+If you’ve ever learned a neat CoffeeScript tip or trick, or ran into a gotcha — share it on the wiki. The wiki also serves as a directory of handy text editor extensions, web framework plugins, and general CoffeeScript build tools.
The FAQ
+
The FAQ
Perhaps your CoffeeScript-related question has been asked before. Check the FAQ first.
MINIFY=false cake build:browser
.Error.prepareStackTrace
has been removed.for…from
syntax for outputting ES2015 for…of
. (Sorry they couldn’t match, but we came up with for…of
first for something else.) This allows iterating over generators or any other iterable object. Note that using for…from
in your code makes you responsible for ensuring that either your runtime supports for…of
or that you transpile the output JavaScript further to a version your target runtime(s) support.```
) allow the creation of embedded JavaScript blocks where escaping single backticks is not required, which should improve interoperability with ES2015 template literals and with Markdown.\`
.import
s and export
s are now ignored.--map
flag to the compiler, and off you go. Direct all your thanks over to Jason Walton.--map
flag to the compiler, and off you go. Direct all your thanks over to Jason Walton..coffee.md
is now also supported as a Literate CoffeeScript file extension, for existing tooling. .litcoffee
remains the canonical one.super
in class declarations.0.9.5 should be considered the first release candidate for CoffeeScript 1.0. There have been a large number of internal changes since the previous release, many contributed from satyr’s Coco dialect of CoffeeScript. Heregexes (extended regexes) were added. Functions can now have default arguments. Class bodies are now executable code. Improved syntax errors for invalid CoffeeScript. undefined
now works like null
, and cannot be assigned a new value. There was a precedence change with respect to single-line comprehensions: result = i for i in list
+
0.9.5 should be considered the first release candidate for CoffeeScript 1.0. There have been a large number of internal changes since the previous release, many contributed from satyr’s Coco dialect of CoffeeScript. Heregexes (extended regexes) were added. Functions can now have default arguments. Class bodies are now executable code. Improved syntax errors for invalid CoffeeScript. undefined
now works like null
, and cannot be assigned a new value. There was a precedence change with respect to single-line comprehensions: result = i for i in list
used to parse as result = (i for i in list)
by default … it now parses as
(result = i) for i in list
.
result = (i for i in list)
by default … it now p
String interpolation, contributed by Stan Angeloff. Since --run
has been the default since 0.5.3, updating --stdio
and --eval
to run by default, pass --compile
as well if you’d like to print the result.
String interpolation, contributed by Stan Angeloff. Since --run
has been the default since 0.5.3, updating --stdio
and --eval
to run by default, pass --compile
as well if you’d like to print the result.
result = (i for i in list)
by default … it now p
0.5.2
Added a compressed version of the compiler for inclusion in web pages as
-v1/browser-compiler/coffee-script.js
. It’ll automatically run any script tags with type text/coffeescript
for you. Added a --stdio
option to the coffee
command, for piped-in compiles.
/v1/browser-compiler/coffee-script.js
. It’ll automatically run any script tags with type text/coffeescript
for you. Added a --stdio
option to the coffee
command, for piped-in compiles.
extends
keyword now functions identically to goog.inherit
0.2.0
-Major release. Significant whitespace. Better statement-to-expression conversion. Splats. Splice literals. Object comprehensions. Blocks. The existential operator. Many thanks to all the folks who posted issues, with special thanks to Liam O’Connor-Davis for whitespace and expression help.
+
Major release. Significant whitespace. Better statement-to-expression conversion. Splats. Splice literals. Object comprehensions. Blocks. The existential operator. Many thanks to all the folks who posted issues, with special thanks to Liam O’Connor-Davis for whitespace and expression help.
extends
keyword now functions identically to goog.inherit
0.1.4
-The official CoffeeScript extension is now .coffee
instead of .cs
, which properly belongs to C#. Due to popular demand, you can now also use =
to assign. Unlike JavaScript, =
can also be used within object literals, interchangeably with :
. Made a grammatical fix for chained function calls like func(1)(2)(3)(4)
. Inheritance and super no longer use __proto__
, so they should be IE-compatible now.
+
The official CoffeeScript extension is now .coffee
instead of .cs
, which properly belongs to C#. Due to popular demand, you can now also use =
to assign. Unlike JavaScript, =
can also be used within object literals, interchangeably with :
. Made a grammatical fix for chained function calls like func(1)(2)(3)(4)
. Inheritance and super no longer use __proto__
, so they should be IE-compatible now.
cake
is a simplified version of Make
-(Rake, Jake)
+(Rake, Jake)
for CoffeeScript. You define tasks with names and descriptions in a Cakefile,
and can call them from the command line, or invoke them from other tasks.
Running cake
with no arguments will print out a list of all the tasks in the
diff --git a/docs/v2/annotated-source/grammar.html b/docs/v2/annotated-source/grammar.html
index fe08fb27..b04b9034 100644
--- a/docs/v2/annotated-source/grammar.html
+++ b/docs/v2/annotated-source/grammar.html
@@ -115,17 +115,17 @@
The CoffeeScript parser is generated by Jison +
The CoffeeScript parser is generated by Jison from this grammar file. Jison is a bottom-up parser generator, similar in style to Bison, implemented in JavaScript. -It can recognize LALR(1), LR(0), SLR(1), and LR(1) +It can recognize LALR(1), LR(0), SLR(1), and LR(1) type grammars. To create the Jison parser, we list the pattern to match on the left-hand side, and the action to take (usually the creation of syntax tree nodes) on the right. As the parser runs, it shifts tokens from our token stream, from left to right, and -attempts to match +attempts to match the token sequence against the rules below. When a match can be made, it -reduces into the nonterminal +reduces into the nonterminal (the enclosing name at the top), and we proceed from there.
If you run the cake build:parser
command, Jison constructs a parse table
from our rules and saves it into lib/parser.js
.
Our handy DSL for Jison grammar generation, thanks to -Tim Caswell. For every rule in the grammar, +Tim Caswell. For every rule in the grammar, we pass the pattern-defining string, the action to run, and extra options, optionally. If no action is specified, we simply pass the value of the previous nonterminal.
diff --git a/docs/v2/annotated-source/lexer.html b/docs/v2/annotated-source/lexer.html index e16bee93..b843c25f 100644 --- a/docs/v2/annotated-source/lexer.html +++ b/docs/v2/annotated-source/lexer.html @@ -121,7 +121,7 @@ a token is produced, we consume the match, and start again. Tokens are in the form:[tag, value, locationData]
where locationData is {first_line, first_column, last_line, last_column}, which is a
-format that can be fed directly into Jison. These
+format that can be fed directly into Jison. These
are read by jison in the parser.lexer
function defined in coffeescript.coffee.
Am I capable of -Python-style comparison chaining?
+Python-style comparison chaining? diff --git a/docs/v2/annotated-source/sourcemap.html b/docs/v2/annotated-source/sourcemap.html index 7ca78df9..3a51418f 100644 --- a/docs/v2/annotated-source/sourcemap.html +++ b/docs/v2/annotated-source/sourcemap.html @@ -406,7 +406,7 @@ column for the current line:Note that SourceMap VLQ encoding is “backwards”. MIDI-style VLQ encoding puts the most-significant-bit (MSB) from the original value into the MSB of the VLQ -encoded value (see Wikipedia). +encoded value (see Wikipedia). SourceMap VLQ does things the other way around, with the least significat four bits of the original value encoded into the first byte of the VLQ encoded value.
diff --git a/docs/v2/index.html b/docs/v2/index.html index e40aa11c..5fdd742a 100644 --- a/docs/v2/index.html +++ b/docs/v2/index.html @@ -714,7 +714,7 @@ textarea {CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.
The golden rule of CoffeeScript is: “It’s just JavaScript.” The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable, pretty-printed, and tends to run as fast or faster than the equivalent handwritten JavaScript.
-Latest Version: 2.0.0-beta1
+Latest Version: 2.0.0-beta1
@@ -825,8 +825,8 @@ coffee -p *.coffee | babel --presets env > app.jsnpm install -g coffeescript@next
The command-line version of coffee
is available as a Node.js utility. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser (see Try CoffeeScript).
To install, first make sure you have a working copy of the latest stable version of Node.js. You can then install CoffeeScript globally with npm:
+The command-line version of coffee
is available as a Node.js utility. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser (see Try CoffeeScript).
To install, first make sure you have a working copy of the latest stable version of Node.js. You can then install CoffeeScript globally with npm:
npm install --global coffeescript@next
When you need CoffeeScript as a dependency of a project, within that project’s folder you can install it locally:
@@ -1798,7 +1798,7 @@ alert((function() {unless
can be used as the inverse of if
.
As a shortcut for this.property
, you can use @property
.
You can use in
to test for array presence, and of
to test for JavaScript object-key presence.
To simplify math expressions, **
can be used for exponentiation and //
performs integer division. %
works just like in JavaScript, while %%
provides “dividend dependent modulo”:
To simplify math expressions, **
can be used for exponentiation and //
performs integer division. %
works just like in JavaScript, while %%
provides “dividend dependent modulo”: