Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
repl: support syntax highlighting#53571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // Some patterns inspired by https://github.com/speed-highlight/core/blob/249ad8fe9bf3eb951eb263b5b3c35887bbbbb2eb/src/languages/js.js | ||
| 'use strict'; | ||
| const { | ||
| RegExpPrototypeExec, | ||
| StringPrototypeSlice, | ||
| } = primordials; | ||
| const { styleText, inspect } = require('util'); | ||
| const matchers = [ | ||
| { | ||
| // Single line comment or multi line comment | ||
| color: 'gray', | ||
| patterns: [ | ||
| /\/\/[^\n]*|\/\*[\s\S]*?\*\//g, | ||
| ], | ||
| }, | ||
| { | ||
| // String | ||
| color: inspect.styles.string, | ||
| patterns: [ | ||
| // 'string' or "string" | ||
| /(["'])(\\[^]|(?!\1)[^\r\n\\])*\1?/g, | ||
| // `string` | ||
| /`((?!`)[^]|\\[^])*`?/g, | ||
| ], | ||
| }, | ||
| { | ||
| // Keywords | ||
| color: inspect.styles.special, | ||
| patterns: [ | ||
| /=>|\b(as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|export|extends|finally|for|from|function|if|import|in|instanceof|let|new|of|return|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/g, | ||
| ], | ||
| }, | ||
| { | ||
| // RegExp | ||
| color: inspect.styles.regexp, | ||
| patterns: [ | ||
| // eslint-disable-next-line node-core/no-unescaped-regexp-dot | ||
| /\/(?:[^/\\\r\n]|\\.)*\/[dgimsuy]*/g, | ||
| ], | ||
| }, | ||
| { | ||
| // Number | ||
| color: inspect.styles.number, | ||
| patterns: [ | ||
| /(\.e?|\b)\d(e-|[\d.oxa-fA-F_])*(\.|\b)/g, | ||
| /\bNaN\b/g, | ||
| ], | ||
| }, | ||
| { | ||
| // Undefined | ||
| color: inspect.styles.undefined, | ||
| patterns: [ | ||
| /\bundefined\b/g, | ||
| ], | ||
| }, | ||
| { | ||
| // Null | ||
| color: inspect.styles.null, | ||
| patterns: [ | ||
| /\bnull\b/g, | ||
| ], | ||
| }, | ||
| { | ||
| // Boolean | ||
| color: inspect.styles.boolean, | ||
| patterns: [ | ||
| /\b(true|false)\b/g, | ||
| ], | ||
| }, | ||
| { | ||
| // Operator | ||
| color: inspect.styles.special, | ||
| patterns: [ | ||
| /[/*+:?&|%^~=!,<>.^-]+/g, | ||
| ], | ||
| }, | ||
| { | ||
| // Identifier | ||
| color: null, | ||
| patterns: [ | ||
| /\b[A-Z][\w_]*\b/g, | ||
| ], | ||
| }, | ||
| { | ||
| // Function Declaration | ||
| color: inspect.styles.special, | ||
| patterns: [ | ||
| // eslint-disable-next-line no-useless-escape | ||
| /[a-zA-Z$_][\w$_]*(?=\s*((\?\.)?\s*\(|=\s*(\(?[\w,{}\[\])]+\)? =>|function\b)))/g, | ||
| ], | ||
| }, | ||
| ]; | ||
| function highlight(code) { | ||
| let tokens = ''; | ||
| for (let index = 0; index < code.length;) { | ||
| let match = null; | ||
| let matchIndex = code.length; | ||
| let matchType = null; | ||
| for (const { color, patterns } of matchers) { | ||
| for (const pattern of patterns) { | ||
| pattern.lastIndex = index; | ||
| const result = RegExpPrototypeExec(pattern, code); | ||
| if (result && result.index < matchIndex) { | ||
| match = result[0]; | ||
| matchIndex = result.index; | ||
| matchType = color; | ||
| } | ||
| } | ||
| } | ||
| if (matchIndex > index) { | ||
| tokens += StringPrototypeSlice(code, index, matchIndex); | ||
| } | ||
| if (match) { | ||
| tokens += matchType ? styleText(matchType, match) : match; | ||
| index = matchIndex + match.length; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| return tokens; | ||
| } | ||
| module.exports = highlight; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -80,70 +80,67 @@ repl.inputStream.run([ | ||
| 'const r = 5;', | ||
| ]); | ||
| /* eslint-disable @stylistic/js/max-len */ | ||
| const testCases = [{ | ||
| input: 'foo', | ||
| preview: [ | ||
| 'foo\r', | ||
| '\x1B[1G\x1B[0Jrepl > f\x1B[9G\x1B[1G\x1B[0Jrepl > fo\x1B[10G\x1B[1G\x1B[0Jrepl > foo\x1B[11G\r', | ||
| '\x1B[36m[Function: foo]\x1B[39m', | ||
| ] | ||
| }, { | ||
| input: 'r', | ||
| preview: [ | ||
| 'r\r', | ||
| '\x1B[33m5\x1B[39m', | ||
| ] | ||
| }, { | ||
| input: 'koo', | ||
| preview: [ | ||
| 'koo\r', | ||
| '\x1B[1G\x1B[0Jrepl > k\x1B[9G\x1B[1G\x1B[0Jrepl > ko\x1B[10G\x1B[1G\x1B[0Jrepl > koo\x1B[11G\r', | ||
| '\x1B[36m[Function: koo]\x1B[39m', | ||
| ] | ||
| }, { | ||
| input: 'a', | ||
| preview: ['a\r'] // No "undefined" preview. | ||
| noPreview: 'repl > ', // No "undefined" output. | ||
| preview: ['\x1B[1G\x1B[0Jrepl > a\x1B[9G\r'] // No "undefined" preview. | ||
| }, { | ||
| input: " { b: 1 }['b'] === 1", | ||
| preview: [ | ||
| " { b: 1 }['b'] === 1\r", | ||
| "\x1B[1G\x1B[0Jrepl > \x1B[9G\x1B[1G\x1B[0Jrepl > {\x1B[10G\x1B[1G\x1B[0Jrepl > { \x1B[11G\x1B[1G\x1B[0Jrepl > { b\x1B[12G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m\x1B[12G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[13G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m\x1B[12G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m \x1B[12G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }\x1B[12G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[13G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'\x1B[39m\x1B[13G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b\x1B[39m\x1B[14G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m\x1B[13G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m]\x1B[13G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[13G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[36m=\x1B[39m\x1B[14G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[36m==\x1B[39m\x1B[14G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[36m===\x1B[39m\x1B[15G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[36m===\x1B[39m \x1B[14G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[36m===\x1B[39m \x1B[33m1\x1B[39m\x1B[14G\r", | ||
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example | ||
| '\x1B[33mtrue\x1B[39m', | ||
| ] | ||
| }, { | ||
| input: "{ b: 1 }['b'] === 1;", | ||
| preview: [ | ||
| "{ b: 1 }['b'] === 1;\r", | ||
| "\x1B[1G\x1B[0Jrepl > {\x1B[9G\x1B[1G\x1B[0Jrepl > { \x1B[10G\x1B[1G\x1B[0Jrepl > { b\x1B[11G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m\x1B[11G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[12G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m\x1B[11G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m \x1B[11G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }\x1B[11G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[12G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'\x1B[39m\x1B[12G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b\x1B[39m\x1B[13G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m\x1B[12G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m]\x1B[12G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[12G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[36m=\x1B[39m\x1B[13G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[36m==\x1B[39m\x1B[13G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[36m===\x1B[39m\x1B[14G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[36m===\x1B[39m \x1B[13G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[36m===\x1B[39m \x1B[33m1\x1B[39m\x1B[13G\x1B[1G\x1B[0Jrepl > { b\x1B[36m:\x1B[39m \x1B[33m1\x1B[39m }[\x1B[32m'b'\x1B[39m] \x1B[36m===\x1B[39m \x1B[33m1\x1B[39m;\x1B[13G\r", | ||
| '\x1B[33mfalse\x1B[39m', | ||
| ] | ||
| }, { | ||
| input: '{ a: true }', | ||
| preview: [ | ||
| '{ a: true }\r', | ||
| '\x1B[1G\x1B[0Jrepl > {\x1B[9G\x1B[1G\x1B[0Jrepl > { \x1B[10G\x1B[1G\x1B[0Jrepl > { a\x1B[11G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m\x1B[11G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[12G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m t\x1B[11G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m tr\x1B[11G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m tru\x1B[11G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[33mtrue\x1B[39m\x1B[12G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[33mtrue\x1B[39m \x1B[12G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[33mtrue\x1B[39m }\x1B[13G\r', | ||
| '{ a: \x1B[33mtrue\x1B[39m }', | ||
| ] | ||
| }, { | ||
| input: '{ a: true };', | ||
| preview: [ | ||
| '{ a: true };\r', | ||
| '\x1B[1G\x1B[0Jrepl > {\x1B[9G\x1B[1G\x1B[0Jrepl > { \x1B[10G\x1B[1G\x1B[0Jrepl > { a\x1B[11G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m\x1B[11G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[12G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m t\x1B[11G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m tr\x1B[11G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m tru\x1B[11G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[33mtrue\x1B[39m\x1B[12G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[33mtrue\x1B[39m \x1B[12G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[33mtrue\x1B[39m }\x1B[13G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[33mtrue\x1B[39m };\x1B[12G\r', | ||
| '\x1B[33mtrue\x1B[39m', | ||
| ] | ||
| }, { | ||
| input: ' \t { a: true};', | ||
| preview: [ | ||
| ' { a: true};\r', | ||
| '\x1B[1G\x1B[0Jrepl > \x1B[9G\x1B[1G\x1B[0Jrepl > \x1B[10G\x1B[1G\x1B[0Jrepl > {\x1B[11G\x1B[1G\x1B[0Jrepl > { \x1B[12G\x1B[1G\x1B[0Jrepl > { a\x1B[13G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m\x1B[13G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[14G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m t\x1B[13G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m tr\x1B[13G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m tru\x1B[13G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[33mtrue\x1B[39m\x1B[14G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[33mtrue\x1B[39m}\x1B[14G\x1B[1G\x1B[0Jrepl > { a\x1B[36m:\x1B[39m \x1B[33mtrue\x1B[39m};\x1B[15G\r', | ||
| '\x1B[33mtrue\x1B[39m', | ||
| ] | ||
| }, { | ||
| input: '1n + 2n', | ||
| preview: [ | ||
| '1n + 2n\r', | ||
| '\x1B[1G\x1B[0Jrepl > \x1B[33m1\x1B[39m\x1B[8G\x1B[1G\x1B[0Jrepl > 1n\x1B[10G\x1B[1G\x1B[0Jrepl > 1n \x1B[11G\x1B[1G\x1B[0Jrepl > 1n \x1B[36m+\x1B[39m\x1B[11G\x1B[1G\x1B[0Jrepl > 1n \x1B[36m+\x1B[39m \x1B[12G\x1B[1G\x1B[0Jrepl > 1n \x1B[36m+\x1B[39m \x1B[33m2\x1B[39m\x1B[11G\x1B[1G\x1B[0Jrepl > 1n \x1B[36m+\x1B[39m 2n\x1B[11G\r', | ||
| '\x1B[33m3n\x1B[39m', | ||
| ] | ||
| }, { | ||
| input: '{};1', | ||
| preview: [ | ||
| '{};1\r', | ||
| '\x1B[1G\x1B[0Jrepl > {\x1B[9G\x1B[1G\x1B[0Jrepl > {}\x1B[10G\x1B[1G\x1B[0Jrepl > {};\x1B[11G\x1B[1G\x1B[0Jrepl > {};\x1B[33m1\x1B[39m\x1B[11G\r', | ||
| '\x1B[33m1\x1B[39m', | ||
| ], | ||
| ] | ||
| }]; | ||
| /* eslint-enable @stylistic/js/max-len */ | ||
| async function runTest() { | ||
| for (const { input, preview } of testCases) { | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.