1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

fixing exports for the browser

This commit is contained in:
Jeremy Ashkenas 2010-03-10 09:47:02 -05:00
parent 1f9bb6a1c4
commit ccb7f63b8a
7 changed files with 40 additions and 35 deletions

File diff suppressed because one or more lines are too long

View file

@ -13,8 +13,9 @@
Lexer = require('lexer').Lexer;
parser = require('parser').parser;
} else {
parser = exports.parser;
this.exports = (this.CoffeeScript = {});
Lexer = this.Lexer;
parser = this.parser;
}
// The current CoffeeScript version number.
exports.VERSION = '0.5.5';

View file

@ -1,4 +1,5 @@
(function(){
var balanced_string, compact, count, del, flatten, include, merge, starts;
var __hasProp = Object.prototype.hasOwnProperty;
// This file contains the common helper functions that we'd like to share among
// the **Lexer**, **Rewriter**, and the **Nodes**. Merge objects, flatten
@ -8,15 +9,15 @@
this.exports = this;
}
// Does a list include a value?
exports.include = function include(list, value) {
exports.include = (include = function include(list, value) {
return list.indexOf(value) >= 0;
};
});
// Peek at the beginning of a given string to see if it matches a sequence.
exports.starts = function starts(string, literal, start) {
exports.starts = (starts = function starts(string, literal, start) {
return string.substring(start, (start || 0) + literal.length) === literal;
};
});
// Trim out all falsy values from an array.
exports.compact = function compact(array) {
exports.compact = (compact = function compact(array) {
var _a, _b, _c, _d, item;
_a = []; _b = array;
for (_c = 0, _d = _b.length; _c < _d; _c++) {
@ -26,9 +27,9 @@
}
}
return _a;
};
});
// Count the number of occurences of a character in a string.
exports.count = function count(string, letter) {
exports.count = (count = function count(string, letter) {
var num, pos;
num = 0;
pos = string.indexOf(letter);
@ -37,11 +38,11 @@
pos = string.indexOf(letter, pos + 1);
}
return num;
};
});
// Merge objects, returning a fresh copy with attributes from both sides.
// Used every time `BaseNode#compile` is called, to allow properties in the
// options hash to propagate down the tree without polluting other branches.
exports.merge = function merge(options, overrides) {
exports.merge = (merge = function merge(options, overrides) {
var _a, _b, fresh, key, val;
fresh = {};
_a = options;
@ -57,10 +58,10 @@
}}
}
return fresh;
};
});
// Return a completely flattened version of an array. Handy for getting a
// list of `children` from the nodes.
exports.flatten = function flatten(array) {
exports.flatten = (flatten = function flatten(array) {
var _a, _b, _c, item, memo;
memo = [];
_a = array;
@ -69,27 +70,27 @@
item instanceof Array ? (memo = memo.concat(item)) : memo.push(item);
}
return memo;
};
});
// Delete a key from an object, returning the value. Useful when a node is
// looking for a particular method in an options hash.
exports.del = function del(obj, key) {
exports.del = (del = function del(obj, key) {
var val;
val = obj[key];
delete obj[key];
return val;
};
});
// Matches a balanced group such as a single or double-quoted string. Pass in
// a series of delimiters, all of which must be nested correctly within the
// contents of the string. This method allows us to have strings within
// interpolations within strings, ad infinitum.
exports.balanced_string = function balanced_string(str, delimited, options) {
exports.balanced_string = (balanced_string = function balanced_string(str, delimited, options) {
var _a, _b, _c, _d, close, i, levels, open, pair, slash;
options = options || {};
slash = delimited[0][0] === '/';
levels = [];
i = 0;
while (i < str.length) {
if (levels.length && exports.starts(str, '\\', i)) {
if (levels.length && starts(str, '\\', i)) {
i += 1;
} else {
_a = delimited;
@ -98,21 +99,21 @@
_d = pair;
open = _d[0];
close = _d[1];
if (levels.length && exports.starts(str, close, i) && levels[levels.length - 1] === pair) {
if (levels.length && starts(str, close, i) && levels[levels.length - 1] === pair) {
levels.pop();
i += close.length - 1;
if (!(levels.length)) {
i += 1;
}
break;
} else if (exports.starts(str, open, i)) {
} else if (starts(str, open, i)) {
levels.push(pair);
i += open.length - 1;
break;
}
}
}
if (!levels.length || slash && exports.starts(str, '\n', i)) {
if (!levels.length || slash && starts(str, '\n', i)) {
break;
}
i += 1;
@ -124,5 +125,5 @@
throw new Error("SyntaxError: Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1));
}
return !i ? false : str.substring(0, i);
};
});
})();

View file

@ -12,6 +12,7 @@
process.mixin(require('./helpers'));
} else {
this.exports = this;
Rewriter = this.Rewriter;
}
// The Lexer Class
// ---------------

View file

@ -13,8 +13,9 @@ if process?
Lexer: require('lexer').Lexer
parser: require('parser').parser
else
parser: exports.parser
this.exports: this.CoffeeScript: {}
Lexer: this.Lexer
parser: this.parser
# The current CoffeeScript version number.
exports.VERSION: '0.5.5'

View file

@ -6,18 +6,18 @@
this.exports: this unless process?
# Does a list include a value?
exports.include: (list, value) ->
exports.include: include: (list, value) ->
list.indexOf(value) >= 0
# Peek at the beginning of a given string to see if it matches a sequence.
exports.starts: (string, literal, start) ->
exports.starts: starts: (string, literal, start) ->
string.substring(start, (start or 0) + literal.length) is literal
# Trim out all falsy values from an array.
exports.compact: (array) -> item for item in array when item
exports.compact: compact: (array) -> item for item in array when item
# Count the number of occurences of a character in a string.
exports.count: (string, letter) ->
exports.count: count: (string, letter) ->
num: 0
pos: string.indexOf(letter)
while pos isnt -1
@ -28,7 +28,7 @@ exports.count: (string, letter) ->
# Merge objects, returning a fresh copy with attributes from both sides.
# Used every time `BaseNode#compile` is called, to allow properties in the
# options hash to propagate down the tree without polluting other branches.
exports.merge: (options, overrides) ->
exports.merge: merge: (options, overrides) ->
fresh: {}
(fresh[key]: val) for key, val of options
(fresh[key]: val) for key, val of overrides if overrides
@ -36,7 +36,7 @@ exports.merge: (options, overrides) ->
# Return a completely flattened version of an array. Handy for getting a
# list of `children` from the nodes.
exports.flatten: (array) ->
exports.flatten: flatten: (array) ->
memo: []
for item in array
if item instanceof Array then memo: memo.concat(item) else memo.push(item)
@ -44,7 +44,7 @@ exports.flatten: (array) ->
# Delete a key from an object, returning the value. Useful when a node is
# looking for a particular method in an options hash.
exports.del: (obj, key) ->
exports.del: del: (obj, key) ->
val: obj[key]
delete obj[key]
val
@ -53,27 +53,27 @@ exports.del: (obj, key) ->
# a series of delimiters, all of which must be nested correctly within the
# contents of the string. This method allows us to have strings within
# interpolations within strings, ad infinitum.
exports.balanced_string: (str, delimited, options) ->
exports.balanced_string: balanced_string: (str, delimited, options) ->
options ||= {}
slash: delimited[0][0] is '/'
levels: []
i: 0
while i < str.length
if levels.length and exports.starts str, '\\', i
if levels.length and starts str, '\\', i
i += 1
else
for pair in delimited
[open, close]: pair
if levels.length and exports.starts(str, close, i) and levels[levels.length - 1] is pair
if levels.length and starts(str, close, i) and levels[levels.length - 1] is pair
levels.pop()
i += close.length - 1
i += 1 unless levels.length
break
else if exports.starts str, open, i
else if starts str, open, i
levels.push(pair)
i += open.length - 1
break
break if not levels.length or slash and exports.starts str, '\n', i
break if not levels.length or slash and starts str, '\n', i
i += 1
if levels.length
return false if slash

View file

@ -13,6 +13,7 @@ if process?
process.mixin require './helpers'
else
this.exports: this
Rewriter: this.Rewriter
# The Lexer Class
# ---------------