mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
touch-ups cleanups to the lexer and rebuilding the narwhal libs from whitespace'd versions
This commit is contained in:
parent
d3ab60df78
commit
dd3c9abe15
3 changed files with 20 additions and 38 deletions
|
@ -23,12 +23,12 @@ module CoffeeScript
|
|||
STRING = /\A(""|''|"(.*?)[^\\]"|'(.*?)[^\\]')/m
|
||||
JS = /\A(``|`(.*?)[^\\]`)/m
|
||||
OPERATOR = /\A([+\*&|\/\-%=<>:!]+)/
|
||||
WHITESPACE = /\A([ \t\r]+)/
|
||||
WHITESPACE = /\A([ \t]+)/
|
||||
COMMENT = /\A((#[^\n]*\s*)+)/m
|
||||
CODE = /\A(=>)/
|
||||
REGEX = /\A(\/(.*?)[^\\]\/[imgy]{0,4})/
|
||||
INDENT = /\A\n([ \t\r]*)/
|
||||
NEWLINE = /\A(\n+)([ \t\r]*)/
|
||||
MULTI_DENT = /\A((\n+[ \t]*)+)/
|
||||
LAST_DENT = /\n+([ \t]*)\Z/
|
||||
|
||||
# Token cleaning regexes.
|
||||
JS_CLEANER = /(\A`|`\Z)/
|
||||
|
@ -61,7 +61,6 @@ module CoffeeScript
|
|||
extract_next_token
|
||||
end
|
||||
close_indentation
|
||||
remove_empty_outdents
|
||||
remove_mid_expression_newlines
|
||||
move_commas_outside_outdents
|
||||
ensure_balance(*BALANCED_PAIRS)
|
||||
|
@ -137,8 +136,10 @@ module CoffeeScript
|
|||
|
||||
# Record tokens for indentation differing from the previous line.
|
||||
def indent_token
|
||||
return false unless indent = @chunk[INDENT, 1]
|
||||
size = indent.size
|
||||
return false unless indent = @chunk[MULTI_DENT, 1]
|
||||
@line += indent.scan(MULTILINER).size
|
||||
@i += indent.size
|
||||
size = indent[LAST_DENT, 1].length
|
||||
return newline_token(indent) if size == @indent
|
||||
if size > @indent
|
||||
token(:INDENT, size - @indent)
|
||||
|
@ -147,8 +148,6 @@ module CoffeeScript
|
|||
outdent_token(@indent - size)
|
||||
end
|
||||
@indent = size
|
||||
@line += 1
|
||||
@i += (size + 1)
|
||||
end
|
||||
|
||||
def outdent_token(move_out)
|
||||
|
@ -158,7 +157,6 @@ module CoffeeScript
|
|||
move_out -= last_indent
|
||||
end
|
||||
token("\n", "\n")
|
||||
@indent = @indents.last || 0
|
||||
end
|
||||
|
||||
# Matches and consumes non-meaningful whitespace.
|
||||
|
@ -170,11 +168,11 @@ module CoffeeScript
|
|||
# Multiple newlines get merged together.
|
||||
# Use a trailing \ to escape newlines.
|
||||
def newline_token(newlines)
|
||||
return false unless newlines = @chunk[NEWLINE, 1]
|
||||
@line += newlines.length
|
||||
lines = newlines.scan(MULTILINER).length
|
||||
@line += lines
|
||||
token("\n", "\n") unless ["\n", "\\"].include?(last_value)
|
||||
@tokens.pop if last_value == "\\"
|
||||
@i += newlines.length
|
||||
true
|
||||
end
|
||||
|
||||
# We treat all other single characters as a token. Eg.: ( ) , . !
|
||||
|
@ -228,22 +226,6 @@ module CoffeeScript
|
|||
end
|
||||
end
|
||||
|
||||
# You should be able to put blank lines within indented expressions.
|
||||
# To that end, remove redundant outdent/indents from the token stream.
|
||||
def remove_empty_outdents
|
||||
scan_tokens do |prev, token, post, i|
|
||||
if prev && post && prev[0] == :OUTDENT && token[1] == "\n" && post[0] == :INDENT && prev[1] == post[1]
|
||||
@tokens.delete_at(i + 1)
|
||||
@tokens.delete_at(i - 1)
|
||||
end
|
||||
if prev[0] == :OUTDENT && token[0] == :INDENT && prev[1] == token[1]
|
||||
@tokens.delete_at(i)
|
||||
@tokens.delete_at(i - 1)
|
||||
@tokens.insert(i - 1, ["\n", Value.new("\n", prev[1].line)])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Some blocks occur in the middle of expressions -- when we're expecting
|
||||
# this, remove their trailing newlines.
|
||||
def remove_mid_expression_newlines
|
||||
|
|
|
@ -15,14 +15,14 @@ coffeePath: File.path(module.path).dirname().dirname().dirname().dirname().dirna
|
|||
checkForErrors: coffeeProcess =>
|
||||
return true if coffeeProcess.wait() is 0
|
||||
system.stderr.print(coffeeProcess.stderr.read())
|
||||
throw new Error("CoffeeScript compile error").
|
||||
throw new Error("CoffeeScript compile error")
|
||||
|
||||
# Run a simple REPL, round-tripping to the CoffeeScript compiler for every
|
||||
# command.
|
||||
exports.run: args =>
|
||||
if args.length
|
||||
exports.evalCS(File.read(path)) for path in args.
|
||||
return true.
|
||||
exports.evalCS(File.read(path)) for path in args
|
||||
return true
|
||||
|
||||
while true
|
||||
try
|
||||
|
@ -30,24 +30,24 @@ exports.run: args =>
|
|||
result: exports.evalCS(Readline.readline())
|
||||
print(result) if result isnt undefined
|
||||
catch e
|
||||
print(e)...
|
||||
print(e)
|
||||
|
||||
# Compile a given CoffeeScript file into JavaScript.
|
||||
exports.compileFile: path =>
|
||||
coffee: OS.popen([coffeePath, "--print", "--no-wrap", path])
|
||||
checkForErrors(coffee)
|
||||
coffee.stdout.read().
|
||||
coffee.stdout.read()
|
||||
|
||||
# Compile a string of CoffeeScript into JavaScript.
|
||||
exports.compile: source =>
|
||||
coffee: OS.popen([coffeePath, "--eval", "--no-wrap"])
|
||||
coffee.stdin.write(source).flush().close()
|
||||
checkForErrors(coffee)
|
||||
coffee.stdout.read().
|
||||
coffee.stdout.read()
|
||||
|
||||
# Evaluating a string of CoffeeScript first compiles it externally.
|
||||
exports.evalCS: source =>
|
||||
eval(exports.compile(source)).
|
||||
eval(exports.compile(source))
|
||||
|
||||
# Make a factory for the CoffeeScript environment.
|
||||
exports.makeNarwhalFactory: path =>
|
||||
|
@ -57,4 +57,4 @@ exports.makeNarwhalFactory: path =>
|
|||
Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null)
|
||||
else
|
||||
# eval requires parentheses, but parentheses break compileFunction.
|
||||
eval("(" + factoryText + ")")..
|
||||
eval("(" + factoryText + ")")
|
||||
|
|
|
@ -8,11 +8,11 @@ loader: {
|
|||
# Reload the coffee-script environment from source.
|
||||
reload: topId, path =>
|
||||
coffeescript ||= require('coffee-script')
|
||||
factories[topId]: coffeescript.makeNarwhalFactory(path).
|
||||
factories[topId]: coffeescript.makeNarwhalFactory(path)
|
||||
|
||||
# Ensure that the coffee-script environment is loaded.
|
||||
load: topId, path =>
|
||||
factories[topId] ||= this.reload(topId, path).
|
||||
factories[topId] ||= this.reload(topId, path)
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue