diff --git a/lib/coffee_script/lexer.js b/lib/coffee_script/lexer.js index 998f59d6..7458240e 100644 --- a/lib/coffee_script/lexer.js +++ b/lib/coffee_script/lexer.js @@ -1,5 +1,5 @@ (function(){ - var ACCESSORS, ASSIGNMENT, BEFORE_WHEN, CALLABLE, CODE, COMMENT, COMMENT_CLEANER, HEREDOC, HEREDOC_INDENT, IDENTIFIER, JS, JS_CLEANER, KEYWORDS, LAST_DENT, LAST_DENTS, MULTILINER, MULTI_DENT, NOT_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX, Rewriter, STRING, STRING_NEWLINES, WHITESPACE, lex; + var ACCESSORS, ASSIGNMENT, BEFORE_WHEN, CALLABLE, CODE, COMMENT, COMMENT_CLEANER, HEREDOC, HEREDOC_INDENT, IDENTIFIER, JS, JS_CLEANER, KEYWORDS, LAST_DENT, LAST_DENTS, MULTILINER, MULTI_DENT, NOT_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX, RESERVED, Rewriter, STRING, STRING_NEWLINES, WHITESPACE, lex; if ((typeof process !== "undefined" && process !== null)) { Rewriter = require('./rewriter').Rewriter; } else { @@ -13,6 +13,9 @@ // Constants ============================================================ // The list of keywords passed verbatim to the parser. KEYWORDS = ["if", "else", "then", "unless", "true", "false", "yes", "no", "on", "off", "and", "or", "is", "isnt", "not", "new", "return", "try", "catch", "finally", "throw", "break", "continue", "for", "in", "of", "by", "where", "while", "delete", "instanceof", "typeof", "switch", "when", "super", "extends"]; + // The list of keywords that are reserved by JavaScript, but not used, and aren't + // used by CoffeeScript. Using these will throw an error at compile time. + RESERVED = ["case", "default", "do", "function", "var", "void", "with", "class", "const", "let", "debugger", "enum", "export", "import", "native"]; // Token matching regexes. (keep the IDENTIFIER regex in sync with AssignNode.) IDENTIFIER = /^([a-zA-Z$_](\w|\$)*)/; NUMBER = /^(\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?)))\b/i; @@ -121,6 +124,9 @@ if (KEYWORDS.indexOf(id) >= 0 && !(ACCESSORS.indexOf(this.tag()) >= 0)) { tag = id.toUpperCase(); } + if (RESERVED.indexOf(id) >= 0) { + throw new Error('SyntaxError: Reserved word "' + id + '" on line ' + this.line); + } if (tag === 'WHEN' && BEFORE_WHEN.indexOf(this.tag()) >= 0) { tag = 'LEADING_WHEN'; } diff --git a/src/lexer.coffee b/src/lexer.coffee index 9bc3cb39..d826fba1 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -25,6 +25,13 @@ KEYWORDS: [ "super", "extends" ] +# The list of keywords that are reserved by JavaScript, but not used, and aren't +# used by CoffeeScript. Using these will throw an error at compile time. +RESERVED: [ + "case", "default", "do", "function", "var", "void", "with", "class" + "const", "let", "debugger", "enum", "export", "import", "native" +] + # Token matching regexes. (keep the IDENTIFIER regex in sync with AssignNode.) IDENTIFIER : /^([a-zA-Z$_](\w|\$)*)/ NUMBER : /^(\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?)))\b/i @@ -110,6 +117,7 @@ lex::identifier_token: -> @tag(1, 'PROPERTY_ACCESS') tag: 'IDENTIFIER' tag: id.toUpperCase() if KEYWORDS.indexOf(id) >= 0 and not (ACCESSORS.indexOf(@tag()) >= 0) + throw new Error('SyntaxError: Reserved word "' + id + '" on line ' + @line) if RESERVED.indexOf(id) >= 0 tag: 'LEADING_WHEN' if tag is 'WHEN' and BEFORE_WHEN.indexOf(@tag()) >= 0 @token(tag, id) @i += id.length