2009-12-13 17:07:16 -05:00
|
|
|
class Lexer
|
|
|
|
|
2009-12-15 08:53:21 -05:00
|
|
|
KEYWORDS = ["if", "else", "then", "unless",
|
2009-12-13 17:07:16 -05:00
|
|
|
"true", "false", "null",
|
2009-12-13 18:37:29 -05:00
|
|
|
"and", "or", "is", "aint", "not",
|
2009-12-14 10:00:31 -05:00
|
|
|
"new", "return",
|
2009-12-14 23:11:28 -05:00
|
|
|
"try", "catch", "finally", "throw",
|
2009-12-15 00:27:34 -05:00
|
|
|
"break", "continue",
|
2009-12-15 22:30:27 -05:00
|
|
|
"for", "in", "while",
|
2009-12-17 21:14:36 -05:00
|
|
|
"switch", "case",
|
2009-12-17 21:21:07 -05:00
|
|
|
"super",
|
|
|
|
"delete"]
|
2009-12-13 17:07:16 -05:00
|
|
|
|
|
|
|
IDENTIFIER = /\A([a-zA-Z$_]\w*)/
|
2009-12-17 20:59:19 -05:00
|
|
|
NUMBER = /\A\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?))\b/i
|
2009-12-17 08:26:20 -05:00
|
|
|
STRING = /\A("(.*?)[^\\]"|'(.*?)[^\\]')/m
|
2009-12-15 09:11:27 -05:00
|
|
|
JS = /\A(`(.*?)`)/
|
2009-12-13 17:07:16 -05:00
|
|
|
OPERATOR = /\A([+\*&|\/\-%=<>]+)/
|
|
|
|
WHITESPACE = /\A([ \t\r]+)/
|
|
|
|
NEWLINE = /\A([\r\n]+)/
|
|
|
|
COMMENT = /\A(#[^\r\n]*)/
|
|
|
|
CODE = /\A(=>)/
|
2009-12-17 08:29:19 -05:00
|
|
|
REGEX = /\A(\/(.*?)[^\\]\/[imgy]{0,4})/
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-15 09:11:27 -05:00
|
|
|
JS_CLEANER = /(\A`|`\Z)/
|
2009-12-17 08:23:07 -05:00
|
|
|
MULTILINER = /[\r\n]/
|
2009-12-15 09:11:27 -05:00
|
|
|
|
2009-12-16 20:48:37 -05:00
|
|
|
EXP_START = ['{', '(', '[']
|
|
|
|
EXP_END = ['}', ')', ']']
|
|
|
|
|
2009-12-13 17:07:16 -05:00
|
|
|
# This is how to implement a very simple scanner.
|
|
|
|
# Scan one caracter at the time until you find something to parse.
|
|
|
|
def tokenize(code)
|
|
|
|
@code = code.chomp # Cleanup code by remove extra line breaks
|
|
|
|
@i = 0 # Current character position we're parsing
|
2009-12-17 09:29:49 -05:00
|
|
|
@line = 1 # The current line.
|
2009-12-13 17:07:16 -05:00
|
|
|
@tokens = [] # Collection of all parsed tokens in the form [:TOKEN_TYPE, value]
|
|
|
|
while @i < @code.length
|
|
|
|
@chunk = @code[@i..-1]
|
|
|
|
extract_next_token
|
|
|
|
end
|
|
|
|
@tokens
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_next_token
|
|
|
|
return if identifier_token
|
|
|
|
return if number_token
|
|
|
|
return if string_token
|
2009-12-15 09:11:27 -05:00
|
|
|
return if js_token
|
2009-12-13 18:37:29 -05:00
|
|
|
return if regex_token
|
2009-12-13 17:07:16 -05:00
|
|
|
return if remove_comment
|
|
|
|
return if whitespace_token
|
|
|
|
return literal_token
|
|
|
|
end
|
|
|
|
|
|
|
|
# Matching if, print, method names, etc.
|
|
|
|
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-17 09:29:49 -05:00
|
|
|
@tokens[-1][0] = :PROPERTY_ACCESS if tag == :IDENTIFIER && last_value == '.'
|
|
|
|
token(tag, identifier)
|
2009-12-13 17:07:16 -05:00
|
|
|
@i += identifier.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def number_token
|
|
|
|
return false unless number = @chunk[NUMBER, 1]
|
2009-12-17 20:59:19 -05:00
|
|
|
token(:NUMBER, number)
|
2009-12-13 17:07:16 -05:00
|
|
|
@i += number.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def string_token
|
|
|
|
return false unless string = @chunk[STRING, 1]
|
2009-12-17 09:29:49 -05:00
|
|
|
escaped = string.gsub(MULTILINER) do |match|
|
|
|
|
@line += 1
|
|
|
|
"\\\n"
|
|
|
|
end
|
|
|
|
token(:STRING, escaped)
|
2009-12-13 20:29:44 -05:00
|
|
|
@i += string.length
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|
|
|
|
|
2009-12-15 09:11:27 -05:00
|
|
|
def js_token
|
|
|
|
return false unless script = @chunk[JS, 1]
|
2009-12-17 09:29:49 -05:00
|
|
|
token(:JS, script.gsub(JS_CLEANER, ''))
|
2009-12-15 09:11:27 -05:00
|
|
|
@i += script.length
|
|
|
|
end
|
|
|
|
|
2009-12-13 18:37:29 -05:00
|
|
|
def regex_token
|
|
|
|
return false unless regex = @chunk[REGEX, 1]
|
2009-12-17 09:29:49 -05:00
|
|
|
token(:REGEX, regex)
|
2009-12-13 18:37:29 -05:00
|
|
|
@i += regex.length
|
|
|
|
end
|
|
|
|
|
2009-12-13 17:07:16 -05:00
|
|
|
def remove_comment
|
|
|
|
return false unless comment = @chunk[COMMENT, 1]
|
|
|
|
@i += comment.length
|
|
|
|
end
|
|
|
|
|
|
|
|
# Ignore whitespace
|
|
|
|
def whitespace_token
|
|
|
|
return false unless whitespace = @chunk[WHITESPACE, 1]
|
|
|
|
@i += whitespace.length
|
|
|
|
end
|
|
|
|
|
|
|
|
# We treat all other single characters as a token. Eg.: ( ) , . !
|
|
|
|
# Multi-character operators are also literal tokens, so that Racc can assign
|
|
|
|
# the proper order of operations. Multiple newlines get merged.
|
|
|
|
def literal_token
|
|
|
|
value = @chunk[NEWLINE, 1]
|
|
|
|
if value
|
2009-12-17 09:29:49 -05:00
|
|
|
@line += value.length
|
|
|
|
token("\n", "\n") unless last_value == "\n"
|
2009-12-13 17:07:16 -05:00
|
|
|
return @i += value.length
|
|
|
|
end
|
|
|
|
value = @chunk[OPERATOR, 1]
|
2009-12-13 20:29:44 -05:00
|
|
|
tag_parameters if value && value.match(CODE)
|
2009-12-13 17:07:16 -05:00
|
|
|
value ||= @chunk[0,1]
|
2009-12-16 20:48:37 -05:00
|
|
|
skip_following_newlines if EXP_START.include?(value)
|
|
|
|
remove_leading_newlines if EXP_END.include?(value)
|
2009-12-17 09:29:49 -05:00
|
|
|
token(value, value)
|
2009-12-13 17:07:16 -05:00
|
|
|
@i += value.length
|
|
|
|
end
|
|
|
|
|
2009-12-17 09:29:49 -05:00
|
|
|
def token(tag, value)
|
2009-12-17 09:37:42 -05:00
|
|
|
@tokens << [tag, Value.new(value, @line)]
|
2009-12-17 09:29:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def last_value
|
|
|
|
@tokens.last && @tokens.last[1]
|
|
|
|
end
|
|
|
|
|
2009-12-13 20:29:44 -05:00
|
|
|
# The main source of ambiguity in our grammar was Parameter lists (as opposed
|
|
|
|
# to argument lists in method calls). Tag parameter identifiers to avoid this.
|
|
|
|
def tag_parameters
|
|
|
|
index = 0
|
|
|
|
loop do
|
|
|
|
tok = @tokens[index -= 1]
|
|
|
|
next if tok[0] == ','
|
|
|
|
return if tok[0] != :IDENTIFIER
|
|
|
|
tok[0] = :PARAM
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-16 20:48:37 -05:00
|
|
|
def skip_following_newlines
|
|
|
|
newlines = @code[(@i+1)..-1][NEWLINE, 1]
|
2009-12-17 09:29:49 -05:00
|
|
|
if newlines
|
|
|
|
@line += newlines.length
|
|
|
|
@i += newlines.length
|
|
|
|
end
|
2009-12-16 20:48:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_leading_newlines
|
2009-12-17 09:29:49 -05:00
|
|
|
@tokens.pop if last_value == "\n"
|
2009-12-16 20:48:37 -05:00
|
|
|
end
|
|
|
|
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|