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