From 8c2bed4819a7892b55b02179e1ce3fdc8cccb31e Mon Sep 17 00:00:00 2001 From: Matt Godbolt Date: Fri, 25 Jun 2021 17:23:51 -0500 Subject: [PATCH] Updates to better support cppx-blue With @hsutter --- examples/cppx_blue/Declarations.blue | 40 ++++++++++++++++++++++++ examples/cppx_blue/default.blue | 10 ++---- lib/languages.js | 2 +- static/modes/cppx-blue-mode.js | 46 ++++++++++++++++++++++++++++ static/panes/editor.js | 1 + static/themes.js | 20 ++++++++++-- 6 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 examples/cppx_blue/Declarations.blue create mode 100644 static/modes/cppx-blue-mode.js diff --git a/examples/cppx_blue/Declarations.blue b/examples/cppx_blue/Declarations.blue new file mode 100644 index 000000000..fe23d62ce --- /dev/null +++ b/examples/cppx_blue/Declarations.blue @@ -0,0 +1,40 @@ +// Every declaration of a variable, function, or type is of the following form: +// name : kind = definition +// where the components mean (and most are optional): +// name is the name being declared +// kind is the kind of entity being declared +// definition can be any expression, including a block expression + +// For example, these are declaration statements (and definitions): + +widget: type = int; // define a type + +x: widget = 42; // a named variable + +shape: type = { x: int; y: int; } // a named type + +add: (x:_, y:_) = { return x+y; } // a named generic function + +// add: (x, y) = x+y; // same, using defaults + +// In expression scope, omit name to declare an unnamed entity. For example, these are declaration expressions: +:widget = 42; // an unnamed (temporary) object expression + +:type = { x: int; y: int; } // an unnamed type expression + +//:(x, y) = x+y // an unnamed (lambda) function expression + +// Note These : expressions have very low precedence, so a trailing ; is not needed when lambdas are passed as arguments. +// A local variable declaration statement may omit kind or definition. +// If kind is omitted, it is deduced as if specified _. +// If = definition is omitted, name must be defined by initialization before use (see §3.4.1). For example: + +y : int = 42; // y is an int with initial value 42 + +z : = 42; // z, the same, “_” is implicit default + +w := 42; // same, stylistic convenience + +// s: string; // declares s, unconstructed + +// t = f(); // constructs t, definite first use diff --git a/examples/cppx_blue/default.blue b/examples/cppx_blue/default.blue index 46a3d94ba..c1a962e03 100644 --- a/examples/cppx_blue/default.blue +++ b/examples/cppx_blue/default.blue @@ -1,8 +1,4 @@ -// Type your code here, or load an example. -int square(int num) { - return num * num; +// Type your code here, or load an example +square: (x: int) -> int = { + return x * x; } - -int main() { - return square(2); -} \ No newline at end of file diff --git a/lib/languages.js b/lib/languages.js index e77c61961..bbaca8da3 100644 --- a/lib/languages.js +++ b/lib/languages.js @@ -77,7 +77,7 @@ export const languages = { }, cppx_blue: { name: 'Cppx-Blue', - monaco: 'cppp', + monaco: 'cppx-blue', extensions: ['.blue', '.cpp', '.cxx', '.h', '.hpp', '.hxx', '.c'], alias: [], }, diff --git a/static/modes/cppx-blue-mode.js b/static/modes/cppx-blue-mode.js new file mode 100644 index 000000000..2db4cd6d6 --- /dev/null +++ b/static/modes/cppx-blue-mode.js @@ -0,0 +1,46 @@ +// Copyright (c) 2018, Compiler Explorer Authors +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +'use strict'; +var $ = require('jquery'); +var monaco = require('monaco-editor'); +var cpp = require('monaco-editor/esm/vs/basic-languages/cpp/cpp'); +var cppp = require('./cppp-mode'); + +function definition() { + var cppx_blue = $.extend(true, {}, cppp); // deep copy + cppx_blue.tokenPostfix = '.cppx-blue'; + + // add the 'type' keyword + cppx_blue.keywords.push('type'); + + // pick up 'identifier:' as a definition. + cppx_blue.tokenizer.root.unshift([/[a-zA-Z_]\w*\s*:/, 'identifier.definition']); + + return cppx_blue; +} + +monaco.languages.register({id: 'cppx-blue'}); +monaco.languages.setLanguageConfiguration('cppx-blue', cpp.conf); +monaco.languages.setMonarchTokensProvider('cppx-blue', definition()); diff --git a/static/panes/editor.js b/static/panes/editor.js index 832f95a0f..18a9cbdac 100644 --- a/static/panes/editor.js +++ b/static/panes/editor.js @@ -39,6 +39,7 @@ var monacoConfig = require('../monaco-config'); var TomSelect = require('tom-select'); require('../modes/cppp-mode'); require('../modes/cppx-gold-mode'); +require('../modes/cppx-blue-mode'); require('../modes/d-mode'); require('../modes/ispc-mode'); require('../modes/llvm-ir-mode'); diff --git a/static/themes.js b/static/themes.js index e254d467a..b35e18c38 100644 --- a/static/themes.js +++ b/static/themes.js @@ -32,17 +32,33 @@ var themes = { id: 'default', name: 'Light', 'main-color': '#f2f2f2', - monaco: 'vs', // Optional field + monaco: 'ce', }, dark: { path: 'dark', id: 'dark', name: 'Dark', 'main-color': '#333333', - monaco: 'vs-dark', + monaco: 'ce-dark', }, }; +monaco.editor.defineTheme('ce', { + base: 'vs', + inherit: true, + rules: [ + { token: 'identifier.definition.cppx-blue', foreground: '004400', fontStyle: 'bold' }, + ], +}); + +monaco.editor.defineTheme('ce-dark', { + base: 'vs-dark', + inherit: true, + rules: [ + { token: 'identifier.definition.cppx-blue', foreground: 'bbccbb', fontStyle: 'bold' }, + ], +}); + function Themer(eventHub, initialSettings) { this.currentTheme = null; this.eventHub = eventHub;