diff --git a/lib/command_line.js b/lib/command_line.js index 3fd507ea..f48acc1f 100644 --- a/lib/command_line.js +++ b/lib/command_line.js @@ -78,7 +78,7 @@ // Compile a single source script, containing the given code, according to the // requested options. Both compile_scripts and watch_scripts share this method. compile_script = function compile_script(source, code) { - var js, o; + var __dirname, __filename, js, o; o = options; try { if (o.tokens) { @@ -94,6 +94,8 @@ } else if (o.print || o.eval) { return print(js); } else { + __filename = source; + __dirname = path.dirname(source); return eval(js); } } diff --git a/lib/lexer.js b/lib/lexer.js index 7d0883dd..2db0056e 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -6,7 +6,8 @@ this.exports = this; Rewriter = this.Rewriter; } - // Constants ============================================================ + // Constants + // --------- // Keywords that CoffeScript shares in common with JS. JS_KEYWORDS = ["if", "else", "true", "false", "new", "return", "try", "catch", "finally", "throw", "break", "continue", "for", "in", "while", "delete", "instanceof", "typeof", "switch", "super", "extends", "class"]; // CoffeeScript-only keywords -- which we're more relaxed about allowing. @@ -110,7 +111,8 @@ } return this.literal_token(); }; - // Tokenizers ========================================================== + // Tokenizers + // ---------- // Matches identifying literals: variables, keywords, method names, etc. Lexer.prototype.identifier_token = function identifier_token() { var id, tag; @@ -313,7 +315,8 @@ this.i += value.length; return true; }; - // Token Manipulators ================================================== + // Token Manipulators + // ------------------ // As we consume a new IDENTIFIER, look at the previous token to determine // if it's a special kind of access. Lexer.prototype.name_access_type = function name_access_type() { @@ -344,7 +347,8 @@ Lexer.prototype.assignment_error = function assignment_error() { throw new Error('SyntaxError: Reserved word "' + this.value() + '" on line ' + this.line + ' can\'t be assigned'); }; - // Helpers ============================================================= + // Helpers + // ------- // Add a token to the results, taking note of the line number. Lexer.prototype.token = function token(tag, value) { return this.tokens.push([tag, value, this.line]); diff --git a/src/command_line.coffee b/src/command_line.coffee index f322559f..298fac82 100644 --- a/src/command_line.coffee +++ b/src/command_line.coffee @@ -80,7 +80,10 @@ compile_script: (source, code) -> if o.compile then write_js source, js else if o.lint then lint js else if o.print or o.eval then print js - else eval js + else + __filename: source + __dirname: path.dirname source + eval js catch err if o.watch then puts err.message else throw err diff --git a/src/lexer.coffee b/src/lexer.coffee index 60f81974..4392d197 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -4,7 +4,8 @@ else this.exports: this Rewriter: this.Rewriter -# Constants ============================================================ +# Constants +# --------- # Keywords that CoffeScript shares in common with JS. JS_KEYWORDS: [ @@ -115,7 +116,8 @@ exports.Lexer: class Lexer return if @whitespace_token() return @literal_token() - # Tokenizers ========================================================== + # Tokenizers + # ---------- # Matches identifying literals: variables, keywords, method names, etc. identifier_token: -> @@ -261,7 +263,8 @@ exports.Lexer: class Lexer @i += value.length true - # Token Manipulators ================================================== + # Token Manipulators + # ------------------ # As we consume a new IDENTIFIER, look at the previous token to determine # if it's a special kind of access. @@ -290,7 +293,8 @@ exports.Lexer: class Lexer assignment_error: -> throw new Error 'SyntaxError: Reserved word "' + @value() + '" on line ' + @line + ' can\'t be assigned' - # Helpers ============================================================= + # Helpers + # ------- # Add a token to the results, taking note of the line number. token: (tag, value) ->