mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-17 19:11:22 -05:00
adding the list of javascript reserved words as syntax errors in CoffeeScript, so that no one uses them accidentally.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
(function(){
|
(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)) {
|
if ((typeof process !== "undefined" && process !== null)) {
|
||||||
Rewriter = require('./rewriter').Rewriter;
|
Rewriter = require('./rewriter').Rewriter;
|
||||||
} else {
|
} else {
|
||||||
@@ -13,6 +13,9 @@
|
|||||||
// Constants ============================================================
|
// Constants ============================================================
|
||||||
// The list of keywords passed verbatim to the parser.
|
// 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"];
|
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.)
|
// Token matching regexes. (keep the IDENTIFIER regex in sync with AssignNode.)
|
||||||
IDENTIFIER = /^([a-zA-Z$_](\w|\$)*)/;
|
IDENTIFIER = /^([a-zA-Z$_](\w|\$)*)/;
|
||||||
NUMBER = /^(\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?)))\b/i;
|
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)) {
|
if (KEYWORDS.indexOf(id) >= 0 && !(ACCESSORS.indexOf(this.tag()) >= 0)) {
|
||||||
tag = id.toUpperCase();
|
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) {
|
if (tag === 'WHEN' && BEFORE_WHEN.indexOf(this.tag()) >= 0) {
|
||||||
tag = 'LEADING_WHEN';
|
tag = 'LEADING_WHEN';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,13 @@ KEYWORDS: [
|
|||||||
"super", "extends"
|
"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.)
|
# Token matching regexes. (keep the IDENTIFIER regex in sync with AssignNode.)
|
||||||
IDENTIFIER : /^([a-zA-Z$_](\w|\$)*)/
|
IDENTIFIER : /^([a-zA-Z$_](\w|\$)*)/
|
||||||
NUMBER : /^(\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?)))\b/i
|
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(1, 'PROPERTY_ACCESS')
|
||||||
tag: 'IDENTIFIER'
|
tag: 'IDENTIFIER'
|
||||||
tag: id.toUpperCase() if KEYWORDS.indexOf(id) >= 0 and not (ACCESSORS.indexOf(@tag()) >= 0)
|
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
|
tag: 'LEADING_WHEN' if tag is 'WHEN' and BEFORE_WHEN.indexOf(@tag()) >= 0
|
||||||
@token(tag, id)
|
@token(tag, id)
|
||||||
@i += id.length
|
@i += id.length
|
||||||
|
|||||||
Reference in New Issue
Block a user