2009-12-17 22:13:29 -05:00
|
|
|
module CoffeeScript
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# The lexer reads a stream of CoffeeScript and divvys it up into tagged
|
|
|
|
# tokens. A minor bit of the ambiguity in the grammar has been avoided by
|
|
|
|
# pushing some extra smarts into the Lexer.
|
2009-12-17 22:13:29 -05:00
|
|
|
class Lexer
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# The list of keywords passed verbatim to the parser.
|
2009-12-17 22:13:29 -05:00
|
|
|
KEYWORDS = ["if", "else", "then", "unless",
|
2009-12-23 20:24:55 -05:00
|
|
|
"true", "false", "yes", "no", "on", "off",
|
2009-12-24 20:21:20 -05:00
|
|
|
"and", "or", "is", "isnt", "not",
|
2009-12-17 22:13:29 -05:00
|
|
|
"new", "return",
|
|
|
|
"try", "catch", "finally", "throw",
|
|
|
|
"break", "continue",
|
2009-12-29 08:52:26 -05:00
|
|
|
"for", "in", "where", "while",
|
2009-12-24 04:33:59 -05:00
|
|
|
"switch", "when",
|
2009-12-24 19:49:23 -05:00
|
|
|
"super", "extends",
|
2009-12-24 14:50:44 -05:00
|
|
|
"delete", "instanceof", "typeof"]
|
2009-12-17 22:13:29 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Token matching regexes.
|
2009-12-17 22:13:29 -05:00
|
|
|
IDENTIFIER = /\A([a-zA-Z$_]\w*)/
|
2009-12-26 23:35:43 -05:00
|
|
|
NUMBER = /\A((\b|-)((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?)))\b/i
|
2009-12-18 09:55:31 -05:00
|
|
|
STRING = /\A(""|''|"(.*?)[^\\]"|'(.*?)[^\\]')/m
|
2009-12-23 20:24:55 -05:00
|
|
|
JS = /\A(``|`(.*?)[^\\]`)/m
|
2009-12-23 21:00:04 -05:00
|
|
|
OPERATOR = /\A([+\*&|\/\-%=<>:!]+)/
|
2009-12-17 22:13:29 -05:00
|
|
|
WHITESPACE = /\A([ \t\r]+)/
|
2009-12-22 11:27:19 -05:00
|
|
|
COMMENT = /\A((#[^\n]*\s*)+)/m
|
2009-12-17 22:13:29 -05:00
|
|
|
CODE = /\A(=>)/
|
|
|
|
REGEX = /\A(\/(.*?)[^\\]\/[imgy]{0,4})/
|
2009-12-26 16:22:53 -05:00
|
|
|
INDENT = /\A\n([ \t\r]*)/
|
2009-12-29 08:52:26 -05:00
|
|
|
NEWLINE = /\A(\n+)([ \t\r]*)/
|
2009-12-17 22:13:29 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Token cleaning regexes.
|
2009-12-17 22:13:29 -05:00
|
|
|
JS_CLEANER = /(\A`|`\Z)/
|
2009-12-22 11:27:19 -05:00
|
|
|
MULTILINER = /\n/
|
2009-12-22 11:50:43 -05:00
|
|
|
COMMENT_CLEANER = /(^\s*#|\n\s*$)/
|
2009-12-17 22:13:29 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Tokens that always constitute the start of an expression.
|
2009-12-17 22:13:29 -05:00
|
|
|
EXP_START = ['{', '(', '[']
|
2009-12-17 22:54:24 -05:00
|
|
|
|
|
|
|
# Tokens that always constitute the end of an expression.
|
2009-12-17 22:13:29 -05:00
|
|
|
EXP_END = ['}', ')', ']']
|
|
|
|
|
2009-12-25 16:21:17 -05:00
|
|
|
# Assignment tokens.
|
|
|
|
ASSIGN = [':', '=']
|
|
|
|
|
2009-12-28 21:02:40 -05:00
|
|
|
# Tokens that must be balanced.
|
|
|
|
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], [:INDENT, :OUTDENT]]
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Scan by attempting to match tokens one character at a time. Slow and steady.
|
2009-12-17 22:13:29 -05:00
|
|
|
def tokenize(code)
|
2009-12-17 22:54:24 -05:00
|
|
|
@code = code.chomp # Cleanup code by remove extra line breaks
|
|
|
|
@i = 0 # Current character position we're parsing
|
|
|
|
@line = 1 # The current line.
|
2009-12-24 16:48:46 -05:00
|
|
|
@indent = 0 # The current indent level.
|
|
|
|
@indents = [] # The stack of all indent levels we are currently within.
|
2009-12-17 22:54:24 -05:00
|
|
|
@tokens = [] # Collection of all parsed tokens in the form [:TOKEN_TYPE, value]
|
2009-12-17 22:13:29 -05:00
|
|
|
while @i < @code.length
|
|
|
|
@chunk = @code[@i..-1]
|
|
|
|
extract_next_token
|
|
|
|
end
|
2009-12-26 16:22:53 -05:00
|
|
|
close_indentation
|
|
|
|
remove_empty_outdents
|
2009-12-28 21:02:40 -05:00
|
|
|
ensure_balance(*BALANCED_PAIRS)
|
2009-12-26 16:22:53 -05:00
|
|
|
rewrite_closing_parens
|
2009-12-17 22:13:29 -05:00
|
|
|
@tokens
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# At every position, run this list of match attempts, short-circuiting if
|
|
|
|
# any of them succeed.
|
2009-12-17 22:13:29 -05:00
|
|
|
def extract_next_token
|
|
|
|
return if identifier_token
|
|
|
|
return if number_token
|
|
|
|
return if string_token
|
|
|
|
return if js_token
|
|
|
|
return if regex_token
|
2009-12-22 11:27:19 -05:00
|
|
|
return if comment_token
|
2009-12-24 16:48:46 -05:00
|
|
|
return if indent_token
|
2009-12-17 22:13:29 -05:00
|
|
|
return if whitespace_token
|
|
|
|
return literal_token
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Matches identifying literals: variables, keywords, method names, etc.
|
2009-12-17 22:13:29 -05:00
|
|
|
def identifier_token
|
|
|
|
return false unless identifier = @chunk[IDENTIFIER, 1]
|
|
|
|
# Keywords are special identifiers tagged with their own name, 'if' will result
|
|
|
|
# in an [:IF, "if"] token
|
|
|
|
tag = KEYWORDS.include?(identifier) ? identifier.upcase.to_sym : :IDENTIFIER
|
2009-12-25 19:20:28 -05:00
|
|
|
@tokens[-1][0] = :PROPERTY_ACCESS if tag == :IDENTIFIER && last_value == '.' && !(@tokens[-2][1] == '.')
|
2009-12-17 22:13:29 -05:00
|
|
|
token(tag, identifier)
|
|
|
|
@i += identifier.length
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Matches numbers, including decimals, hex, and exponential notation.
|
2009-12-17 22:13:29 -05:00
|
|
|
def number_token
|
|
|
|
return false unless number = @chunk[NUMBER, 1]
|
|
|
|
token(:NUMBER, number)
|
|
|
|
@i += number.length
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Matches strings, including multi-line strings.
|
2009-12-17 22:13:29 -05:00
|
|
|
def string_token
|
|
|
|
return false unless string = @chunk[STRING, 1]
|
|
|
|
escaped = string.gsub(MULTILINER) do |match|
|
|
|
|
@line += 1
|
2009-12-24 01:22:41 -05:00
|
|
|
" \\\n"
|
2009-12-17 22:13:29 -05:00
|
|
|
end
|
|
|
|
token(:STRING, escaped)
|
|
|
|
@i += string.length
|
2009-12-17 09:29:49 -05:00
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Matches interpolated JavaScript.
|
2009-12-17 22:13:29 -05:00
|
|
|
def js_token
|
|
|
|
return false unless script = @chunk[JS, 1]
|
|
|
|
token(:JS, script.gsub(JS_CLEANER, ''))
|
|
|
|
@i += script.length
|
|
|
|
end
|
2009-12-15 09:11:27 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Matches regular expression literals.
|
2009-12-17 22:13:29 -05:00
|
|
|
def regex_token
|
|
|
|
return false unless regex = @chunk[REGEX, 1]
|
|
|
|
token(:REGEX, regex)
|
|
|
|
@i += regex.length
|
|
|
|
end
|
2009-12-13 18:37:29 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Matches and consumes comments.
|
2009-12-22 11:27:19 -05:00
|
|
|
def comment_token
|
2009-12-17 22:13:29 -05:00
|
|
|
return false unless comment = @chunk[COMMENT, 1]
|
2009-12-23 19:42:18 -05:00
|
|
|
@line += comment.scan(MULTILINER).length
|
2009-12-22 11:27:19 -05:00
|
|
|
token(:COMMENT, comment.gsub(COMMENT_CLEANER, '').split(MULTILINER))
|
|
|
|
token("\n", "\n")
|
2009-12-17 22:13:29 -05:00
|
|
|
@i += comment.length
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-24 17:51:53 -05:00
|
|
|
# Record tokens for indentation differing from the previous line.
|
2009-12-24 16:48:46 -05:00
|
|
|
def indent_token
|
|
|
|
return false unless indent = @chunk[INDENT, 1]
|
|
|
|
size = indent.size
|
2009-12-26 16:22:53 -05:00
|
|
|
return newline_token(indent) if size == @indent
|
2009-12-24 16:48:46 -05:00
|
|
|
if size > @indent
|
2009-12-26 13:49:11 -05:00
|
|
|
token(:INDENT, size - @indent)
|
2009-12-26 13:59:47 -05:00
|
|
|
@indents << size - @indent
|
2009-12-24 16:48:46 -05:00
|
|
|
@indent = size
|
|
|
|
else
|
2009-12-26 13:59:47 -05:00
|
|
|
outdent_token(@indent - size)
|
2009-12-24 16:48:46 -05:00
|
|
|
end
|
2009-12-24 18:05:14 -05:00
|
|
|
@line += 1
|
2009-12-24 16:48:46 -05:00
|
|
|
@i += (size + 1)
|
|
|
|
end
|
|
|
|
|
2009-12-26 13:59:47 -05:00
|
|
|
def outdent_token(move_out)
|
|
|
|
while move_out > 0
|
|
|
|
last_indent = @indents.pop
|
|
|
|
token(:OUTDENT, last_indent)
|
|
|
|
move_out -= last_indent
|
|
|
|
end
|
2009-12-28 21:02:40 -05:00
|
|
|
# token("\n", "\n")
|
2009-12-26 13:59:47 -05:00
|
|
|
@indent = @indents.last || 0
|
|
|
|
end
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Matches and consumes non-meaningful whitespace.
|
2009-12-17 22:13:29 -05:00
|
|
|
def whitespace_token
|
|
|
|
return false unless whitespace = @chunk[WHITESPACE, 1]
|
|
|
|
@i += whitespace.length
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-26 13:49:11 -05:00
|
|
|
# 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
|
|
|
|
token("\n", "\n") unless ["\n", "\\"].include?(last_value)
|
|
|
|
@tokens.pop if last_value == "\\"
|
|
|
|
@i += newlines.length
|
|
|
|
end
|
|
|
|
|
2009-12-17 22:13:29 -05:00
|
|
|
# We treat all other single characters as a token. Eg.: ( ) , . !
|
|
|
|
# Multi-character operators are also literal tokens, so that Racc can assign
|
2009-12-26 13:49:11 -05:00
|
|
|
# the proper order of operations.
|
2009-12-17 22:13:29 -05:00
|
|
|
def literal_token
|
|
|
|
value = @chunk[OPERATOR, 1]
|
|
|
|
tag_parameters if value && value.match(CODE)
|
|
|
|
value ||= @chunk[0,1]
|
|
|
|
skip_following_newlines if EXP_START.include?(value)
|
|
|
|
remove_leading_newlines if EXP_END.include?(value)
|
2009-12-25 16:21:17 -05:00
|
|
|
tag = ASSIGN.include?(value) ? :ASSIGN : value
|
|
|
|
token(tag, value)
|
2009-12-17 22:13:29 -05:00
|
|
|
@i += value.length
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|
|
|
|
|
2009-12-22 10:48:58 -05:00
|
|
|
# Add a token to the results, taking note of the line number, and
|
|
|
|
# immediately-preceding comment.
|
2009-12-17 22:13:29 -05:00
|
|
|
def token(tag, value)
|
2009-12-22 11:27:19 -05:00
|
|
|
@tokens << [tag, Value.new(value, @line)]
|
2009-12-17 22:13:29 -05:00
|
|
|
end
|
2009-12-17 09:29:49 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Peek at the previous token.
|
2009-12-17 22:13:29 -05:00
|
|
|
def last_value
|
|
|
|
@tokens.last && @tokens.last[1]
|
|
|
|
end
|
2009-12-17 09:29:49 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# A source of ambiguity in our grammar was parameter lists in function
|
|
|
|
# definitions (as opposed to argument lists in function calls). Tag
|
|
|
|
# parameter identifiers in order to avoid this.
|
2009-12-17 22:13:29 -05:00
|
|
|
def tag_parameters
|
|
|
|
index = 0
|
|
|
|
loop do
|
|
|
|
tok = @tokens[index -= 1]
|
2009-12-18 07:11:01 -05:00
|
|
|
return if !tok
|
2009-12-17 22:13:29 -05:00
|
|
|
next if tok[0] == ','
|
2009-12-18 07:11:01 -05:00
|
|
|
return if tok[0] != :IDENTIFIER
|
2009-12-17 22:13:29 -05:00
|
|
|
tok[0] = :PARAM
|
|
|
|
end
|
2009-12-13 20:29:44 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Consume and ignore newlines immediately after this point.
|
2009-12-17 22:13:29 -05:00
|
|
|
def skip_following_newlines
|
|
|
|
newlines = @code[(@i+1)..-1][NEWLINE, 1]
|
|
|
|
if newlines
|
|
|
|
@line += newlines.length
|
|
|
|
@i += newlines.length
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Discard newlines immediately before this point.
|
2009-12-17 22:13:29 -05:00
|
|
|
def remove_leading_newlines
|
|
|
|
@tokens.pop if last_value == "\n"
|
2009-12-17 09:29:49 -05:00
|
|
|
end
|
2009-12-16 20:48:37 -05:00
|
|
|
|
2009-12-24 16:48:46 -05:00
|
|
|
# Close up all remaining open blocks.
|
|
|
|
def close_indentation
|
2009-12-26 13:59:47 -05:00
|
|
|
outdent_token(@indent)
|
2009-12-24 16:48:46 -05:00
|
|
|
end
|
|
|
|
|
2009-12-26 16:22:53 -05:00
|
|
|
# Rewrite the token stream, looking one token ahead and behind.
|
2009-12-28 21:02:40 -05:00
|
|
|
def scan_tokens
|
2009-12-26 16:22:53 -05:00
|
|
|
i = 0
|
2009-12-28 21:02:40 -05:00
|
|
|
while i < @tokens.length
|
2009-12-26 16:22:53 -05:00
|
|
|
yield(@tokens[i - 1], @tokens[i], @tokens[i + 1], i)
|
|
|
|
i += 1
|
|
|
|
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
|
2009-12-28 21:02:40 -05:00
|
|
|
scan_tokens do |prev, token, post, i|
|
2009-12-29 08:52:26 -05:00
|
|
|
if 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
|
2009-12-26 16:22:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# We'd like to support syntax like this:
|
|
|
|
# el.click(event =>
|
|
|
|
# el.hide())
|
|
|
|
# In order to accomplish this, move outdents that follow closing parens
|
2009-12-28 21:02:40 -05:00
|
|
|
# inwards, safely. The steps to accomplish this are:
|
|
|
|
#
|
|
|
|
# 1. Check that parentheses are balanced and in order.
|
|
|
|
# 2. Check that indent/outdents are balanced and in order.
|
|
|
|
# 3. Rewrite the stream with a stack: if you see an '(' or INDENT, add it
|
|
|
|
# to the stack. If you see an ')' or OUTDENT, pop the stack and replace
|
|
|
|
# it with the inverse of what we've just popped.
|
|
|
|
#
|
2009-12-26 16:22:53 -05:00
|
|
|
def rewrite_closing_parens
|
2009-12-28 21:02:40 -05:00
|
|
|
stack = []
|
|
|
|
scan_tokens do |prev, token, post, i|
|
2009-12-28 21:07:47 -05:00
|
|
|
stack.push(token) if [:INDENT, '('].include?(token[0])
|
|
|
|
if [:OUTDENT, ')'].include?(token[0])
|
|
|
|
reciprocal = stack.pop
|
|
|
|
if reciprocal[0] == :INDENT
|
|
|
|
@tokens[i] = [:OUTDENT, Value.new(reciprocal[1], token[1].line)]
|
|
|
|
else
|
|
|
|
@tokens[i] = [')', Value.new(')', token[1].line)]
|
|
|
|
end
|
|
|
|
end
|
2009-12-28 21:02:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_balance(*pairs)
|
|
|
|
levels = Hash.new(0)
|
|
|
|
scan_tokens do |prev, token, post, i|
|
|
|
|
pairs.each do |pair|
|
|
|
|
open, close = *pair
|
|
|
|
levels[open] += 1 if token[0] == open
|
|
|
|
levels[open] -= 1 if token[0] == close
|
|
|
|
raise ParseError.new(token[0], token[1], nil) if levels[open] < 0
|
2009-12-26 16:22:53 -05:00
|
|
|
end
|
|
|
|
end
|
2009-12-28 21:02:40 -05:00
|
|
|
unclosed = levels.detect {|k, v| v > 0 }
|
|
|
|
raise SyntaxError, "unclosed '#{unclosed[0]}'" if unclosed
|
2009-12-26 16:22:53 -05:00
|
|
|
end
|
|
|
|
|
2009-12-16 20:48:37 -05:00
|
|
|
end
|
|
|
|
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|