Skip to content

Commit 636ea4f

Browse files
dmaclachadrianheine
authored andcommitted
Add support for _[A-Z].* and __.* types to clike languages.
From C++ Standard Each name that contains a double underscore (__) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use. From C Standard All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use. Turns these all into "builtin".
1 parent 1cca2b1 commit 636ea4f

2 files changed

Lines changed: 45 additions & 8 deletions

File tree

mode/clike/clike.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
6565
numberStart = parserConfig.numberStart || /[\d\.]/,
6666
number = parserConfig.number || /^(?:0x[a-f\d]+|0b[01]+|(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?)(u|ll?|l|f)?/i,
6767
isOperatorChar = parserConfig.isOperatorChar || /[+\-*&%=<>!?|\/]/,
68-
isIdentifierChar = parserConfig.isIdentifierChar || /[\w\$_\xa1-\uffff]/;
68+
isIdentifierChar = parserConfig.isIdentifierChar || /[\w\$_\xa1-\uffff]/,
69+
// An optional function that takes a {string} token and returns true if it
70+
// should be treated as a builtin.
71+
isReservedIdentifier = parserConfig.isReservedIdentifier || false;
6972

7073
var curPunc, isDefKeyword;
7174

@@ -113,7 +116,8 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
113116
return "keyword";
114117
}
115118
if (contains(types, cur)) return "type";
116-
if (contains(builtin, cur)) {
119+
if (contains(builtin, cur)
120+
|| (isReservedIdentifier && isReservedIdentifier(cur))) {
117121
if (contains(blockKeywords, cur)) curPunc = "newstatement";
118122
return "builtin";
119123
}
@@ -286,6 +290,14 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
286290
return false;
287291
}
288292

293+
// For C and C++ (and ObjC): identifiers starting with __
294+
// or _ followed by a capital letter are reserved for the compiler.
295+
function cIsReservedIdentifier(token) {
296+
if (!token || token.length < 2) return false;
297+
if (token[0] != '_') return false;
298+
return (token[1] == '_') || (token[1] !== token[1].toLowerCase());
299+
}
300+
289301
function cpp14Literal(stream) {
290302
stream.eatWhile(/[\w\.']/);
291303
return "number";
@@ -368,14 +380,17 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
368380
def(["text/x-csrc", "text/x-c", "text/x-chdr"], {
369381
name: "clike",
370382
keywords: words(cKeywords),
371-
types: words(cTypes + " bool _Complex _Bool float_t double_t intptr_t intmax_t " +
372-
"int8_t int16_t int32_t int64_t uintptr_t uintmax_t uint8_t uint16_t " +
373-
"uint32_t uint64_t"),
383+
types: words(cTypes + " bool float_t double_t intptr_t intmax_t int8_t int16_t " +
384+
"int32_t int64_t uintptr_t uintmax_t uint8_t uint16_t uint32_t uint64_t"),
374385
blockKeywords: words("case do else for if switch while struct"),
375386
defKeywords: words("struct"),
376387
typeFirstDefinitions: true,
377388
atoms: words("NULL true false"),
378-
hooks: {"#": cppHook, "*": pointerHook},
389+
isReservedIdentifier: cIsReservedIdentifier,
390+
hooks: {
391+
"#": cppHook,
392+
"*": pointerHook,
393+
},
379394
modeProps: {fold: ["brace", "include"]}
380395
});
381396

@@ -393,6 +408,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
393408
atoms: words("true false NULL"),
394409
dontIndentStatements: /^template$/,
395410
isIdentifierChar: /[\w\$_~\xa1-\uffff]/,
411+
isReservedIdentifier: cIsReservedIdentifier,
396412
hooks: {
397413
"#": cppHook,
398414
"*": pointerHook,
@@ -720,10 +736,11 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
720736

721737
def("text/x-objectivec", {
722738
name: "clike",
723-
keywords: words(cKeywords + "inline restrict _Bool _Complex _Imaginary BOOL Class bycopy byref id IMP in " +
724-
"inout nil oneway out Protocol SEL self super atomic nonatomic retain copy readwrite readonly"),
739+
keywords: words(cKeywords + "inline restrict BOOL Class bycopy byref id IMP in inout nil oneway out " +
740+
"Protocol SEL self super atomic nonatomic retain copy readwrite readonly"),
725741
types: words(cTypes),
726742
atoms: words("YES NO NULL NILL ON OFF true false"),
743+
isReservedIdentifier: cIsReservedIdentifier,
727744
hooks: {
728745
"@": function(stream) {
729746
stream.eatWhile(/[\w\$]/);

mode/clike/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
"[type unsigned] [type int] [variable bar] [operator =] [number 8];",
4444
"[meta #include <baz> ][comment // comment]")
4545

46+
MT("c_underscores",
47+
"[builtin __FOO];",
48+
"[builtin _Complex];",
49+
"[builtin __aName];",
50+
"[variable _aName];");
4651

4752
var mode_cpp = CodeMirror.getMode({indentUnit: 2}, "text/x-c++src");
4853
function MTCPP(name) { test.mode(name, mode_cpp, Array.prototype.slice.call(arguments, 1)); }
@@ -57,6 +62,21 @@
5762
"[def Foo::Foo]() {}",
5863
"[def Foo::~Foo]() {}");
5964

65+
MTCPP("cpp_underscores",
66+
"[builtin __FOO];",
67+
"[builtin _Complex];",
68+
"[builtin __aName];",
69+
"[variable _aName];");
70+
71+
var mode_objc = CodeMirror.getMode({indentUnit: 2}, "text/x-objectivec");
72+
function MTOBJC(name) { test.mode(name, mode_objc, Array.prototype.slice.call(arguments, 1)); }
73+
74+
MTOBJC("objc_underscores",
75+
"[builtin __FOO];",
76+
"[builtin _Complex];",
77+
"[builtin __aName];",
78+
"[variable _aName];");
79+
6080
var mode_scala = CodeMirror.getMode({indentUnit: 2}, "text/x-scala");
6181
function MTSCALA(name) { test.mode("scala_" + name, mode_scala, Array.prototype.slice.call(arguments, 1)); }
6282
MTSCALA("nested_comments",

0 commit comments

Comments
 (0)