diff --git a/_includes/runners/code.html b/_includes/runners/code.html index b5c94d5f7b..707d42bdf0 100644 --- a/_includes/runners/code.html +++ b/_includes/runners/code.html @@ -60,9 +60,13 @@ // AP CSP Pseudocode syntax highlighting mode for CodeMirror 5 if (typeof CodeMirror !== 'undefined' && !CodeMirror.modes['pseudocode']) { CodeMirror.defineMode('pseudocode', function() { - const KW = new Set(['IF','ELSE','REPEAT','TIMES','UNTIL','FOR','EACH','IN','PROCEDURE','RETURN','AND','OR','NOT','MOD']); - const BI = new Set(['DISPLAY','INPUT','APPEND','INSERT','REMOVE','LENGTH']); - const BOOL= new Set(['TRUE','FALSE']); + const KW = new Set(['IF','ELSE','REPEAT','TIMES','UNTIL','FOR','EACH','IN','PROCEDURE','RETURN','AND','OR','NOT','MOD']); + const BI = new Set(['DISPLAY','INPUT','APPEND','INSERT','REMOVE','LENGTH']); + const BOOL = new Set(['TRUE','FALSE']); + // Robot commands folded into existing token types for colour consistency + const BI_EXTRA = new Set(['MOVE_FORWARD','ROTATE_LEFT','ROTATE_RIGHT','CAN_MOVE','RENDER']); + const ROBOT_BLUE = new Set(['FROM','TILEMAPS','IMPORT','SPAWN']); + const ROBOT_YELLOW = new Set(['CHARACTER','GOAL']); return { token: function(stream) { if (stream.eatSpace()) return null; @@ -70,11 +74,11 @@ // Comments if (stream.match('//')) { stream.skipToEnd(); return 'comment'; } - // Strings — handles curly and straight quotes + // Strings — handles curly (“ ”) and straight quotes const q = stream.peek(); - if (q === '"' || q === '“' || q === '”' || q === "'") { + if (q === '"' || q === '\u201c' || q === '\u201d' || q === "'") { stream.next(); - const close = (q === '“') ? '”' : q; + const close = (q === '\u201c') ? '\u201d' : q; while (!stream.eol() && stream.peek() !== close) stream.next(); if (!stream.eol()) stream.next(); return 'string'; @@ -83,12 +87,14 @@ // Numbers if (stream.match(/^-?\d+(\.\d*)?/)) return 'number'; - // Words: keywords / builtins / booleans / identifiers + // Words: robot commands take priority, then pseudocode keywords if (stream.match(/^[a-zA-Z_][a-zA-Z0-9_]*/)) { - const w = stream.current().toUpperCase(); - if (KW.has(w)) return 'keyword'; - if (BI.has(w)) return 'builtin'; - if (BOOL.has(w)) return 'atom'; + const w = stream.current(); + if (KW.has(w)) return 'keyword'; + if (BI.has(w) || BI_EXTRA.has(w)) return 'builtin'; + if (BOOL.has(w)) return 'atom'; + if (ROBOT_BLUE.has(w)) return 'attribute'; + if (ROBOT_YELLOW.has(w)) return 'property'; return 'variable'; } @@ -108,7 +114,7 @@