From 4b4649db36e67480ea03e3ca016f39f72200a55c Mon Sep 17 00:00:00 2001 From: Sami Ibrahim Date: Mon, 18 Dec 2023 21:36:23 +0300 Subject: [PATCH 1/3] feat: add c language --- src/basic-languages/c/c.contribution.ts | 24 + src/basic-languages/c/c.test.ts | 805 ++++++++++++++++++++++++ src/basic-languages/c/c.ts | 262 ++++++++ 3 files changed, 1091 insertions(+) create mode 100644 src/basic-languages/c/c.contribution.ts create mode 100644 src/basic-languages/c/c.test.ts create mode 100644 src/basic-languages/c/c.ts diff --git a/src/basic-languages/c/c.contribution.ts b/src/basic-languages/c/c.contribution.ts new file mode 100644 index 0000000000..5ea39dcdbb --- /dev/null +++ b/src/basic-languages/c/c.contribution.ts @@ -0,0 +1,24 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { registerLanguage } from '../_.contribution'; + +declare var AMD: any; +declare var require: any; + +registerLanguage({ + id: 'c', + extensions: ['.c', '.h'], + aliases: ['C', 'c'], + loader: () => { + if (AMD) { + return new Promise((resolve, reject) => { + require(['vs/basic-languages/c/c'], resolve, reject); + }); + } else { + return import('./c'); + } + } +}); diff --git a/src/basic-languages/c/c.test.ts b/src/basic-languages/c/c.test.ts new file mode 100644 index 0000000000..6f2c04bc73 --- /dev/null +++ b/src/basic-languages/c/c.test.ts @@ -0,0 +1,805 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { testTokenization } from '../test/testRunner'; + +testTokenization('c', [ + // Keywords + [ + { + line: 'int _tmain(int argc, _TCHAR* argv[])', + tokens: [ + { startIndex: 0, type: 'keyword.int.c' }, + { startIndex: 3, type: '' }, + { startIndex: 4, type: 'identifier.c' }, + { startIndex: 10, type: 'delimiter.parenthesis.c' }, + { startIndex: 11, type: 'keyword.int.c' }, + { startIndex: 14, type: '' }, + { startIndex: 15, type: 'identifier.c' }, + { startIndex: 19, type: 'delimiter.c' }, + { startIndex: 20, type: '' }, + { startIndex: 21, type: 'identifier.c' }, + { startIndex: 27, type: 'delimiter.c' }, + { startIndex: 28, type: '' }, + { startIndex: 29, type: 'identifier.c' }, + { startIndex: 33, type: 'delimiter.square.c' }, + { startIndex: 35, type: 'delimiter.parenthesis.c' } + ] + } + ], + + // Comments - single line + [ + { + line: '//', + tokens: [{ startIndex: 0, type: 'comment.c' }] + } + ], + + [ + { + line: ' // a comment', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 4, type: 'comment.c' } + ] + } + ], + + [ + { + line: '// a comment', + tokens: [{ startIndex: 0, type: 'comment.c' }] + } + ], + + [ + { + line: '//sticky comment', + tokens: [{ startIndex: 0, type: 'comment.c' }] + } + ], + + [ + { + line: '/almost a comment', + tokens: [ + { startIndex: 0, type: 'delimiter.c' }, + { startIndex: 1, type: 'identifier.c' }, + { startIndex: 7, type: '' }, + { startIndex: 8, type: 'identifier.c' }, + { startIndex: 9, type: '' }, + { startIndex: 10, type: 'identifier.c' } + ] + } + ], + + [ + { + line: '/* //*/ a', + tokens: [ + { startIndex: 0, type: 'comment.c' }, + { startIndex: 7, type: '' }, + { startIndex: 8, type: 'identifier.c' } + ] + } + ], + + [ + { + line: '1 / 2; /* comment', + tokens: [ + { startIndex: 0, type: 'number.c' }, + { startIndex: 1, type: '' }, + { startIndex: 2, type: 'delimiter.c' }, + { startIndex: 3, type: '' }, + { startIndex: 4, type: 'number.c' }, + { startIndex: 5, type: 'delimiter.c' }, + { startIndex: 6, type: '' }, + { startIndex: 7, type: 'comment.c' } + ] + } + ], + + [ + { + line: 'int x = 1; // my comment // is a nice one', + tokens: [ + { startIndex: 0, type: 'keyword.int.c' }, + { startIndex: 3, type: '' }, + { startIndex: 4, type: 'identifier.c' }, + { startIndex: 5, type: '' }, + { startIndex: 6, type: 'delimiter.c' }, + { startIndex: 7, type: '' }, + { startIndex: 8, type: 'number.c' }, + { startIndex: 9, type: 'delimiter.c' }, + { startIndex: 10, type: '' }, + { startIndex: 11, type: 'comment.c' } + ] + } + ], + + // Comments - range comment, single line + [ + { + line: '/* a simple comment */', + tokens: [{ startIndex: 0, type: 'comment.c' }] + } + ], + + [ + { + line: 'int x = /* a simple comment */ 1;', + tokens: [ + { startIndex: 0, type: 'keyword.int.c' }, + { startIndex: 3, type: '' }, + { startIndex: 4, type: 'identifier.c' }, + { startIndex: 5, type: '' }, + { startIndex: 6, type: 'delimiter.c' }, + { startIndex: 7, type: '' }, + { startIndex: 8, type: 'comment.c' }, + { startIndex: 30, type: '' }, + { startIndex: 31, type: 'number.c' }, + { startIndex: 32, type: 'delimiter.c' } + ] + } + ], + + [ + { + line: 'int x = /* comment */ 1; */', + tokens: [ + { startIndex: 0, type: 'keyword.int.c' }, + { startIndex: 3, type: '' }, + { startIndex: 4, type: 'identifier.c' }, + { startIndex: 5, type: '' }, + { startIndex: 6, type: 'delimiter.c' }, + { startIndex: 7, type: '' }, + { startIndex: 8, type: 'comment.c' }, + { startIndex: 21, type: '' }, + { startIndex: 22, type: 'number.c' }, + { startIndex: 23, type: 'delimiter.c' }, + { startIndex: 24, type: '' } + ] + } + ], + + [ + { + line: 'x = /**/;', + tokens: [ + { startIndex: 0, type: 'identifier.c' }, + { startIndex: 1, type: '' }, + { startIndex: 2, type: 'delimiter.c' }, + { startIndex: 3, type: '' }, + { startIndex: 4, type: 'comment.c' }, + { startIndex: 8, type: 'delimiter.c' } + ] + } + ], + + [ + { + line: 'x = /*/;', + tokens: [ + { startIndex: 0, type: 'identifier.c' }, + { startIndex: 1, type: '' }, + { startIndex: 2, type: 'delimiter.c' }, + { startIndex: 3, type: '' }, + { startIndex: 4, type: 'comment.c' } + ] + } + ], + + // Numbers + [ + { + line: '0', + tokens: [{ startIndex: 0, type: 'number.c' }] + } + ], + + [ + { + line: '12l', + tokens: [{ startIndex: 0, type: 'number.c' }] + } + ], + + [ + { + line: '34U', + tokens: [{ startIndex: 0, type: 'number.c' }] + } + ], + + [ + { + line: '55LL', + tokens: [{ startIndex: 0, type: 'number.c' }] + } + ], + + [ + { + line: '34ul', + tokens: [{ startIndex: 0, type: 'number.c' }] + } + ], + + [ + { + line: '55llU', + tokens: [{ startIndex: 0, type: 'number.c' }] + } + ], + + [ + { + line: "5'5llU", + tokens: [{ startIndex: 0, type: 'number.c' }] + } + ], + + [ + { + line: "100'000'000", + tokens: [{ startIndex: 0, type: 'number.c' }] + } + ], + + [ + { + line: "0x100'aafllU", + tokens: [{ startIndex: 0, type: 'number.hex.c' }] + } + ], + + [ + { + line: "0342'325", + tokens: [{ startIndex: 0, type: 'number.octal.c' }] + } + ], + + [ + { + line: '0x123', + tokens: [{ startIndex: 0, type: 'number.hex.c' }] + } + ], + + [ + { + line: '23.5', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '23.5e3', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '23.5E3', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '23.5F', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '23.5f', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '1.72E3F', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '1.72E3f', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '1.72e3F', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '1.72e3f', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '23.5L', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '23.5l', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '1.72E3L', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '1.72E3l', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '1.72e3L', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '1.72e3l', + tokens: [{ startIndex: 0, type: 'number.float.c' }] + } + ], + + [ + { + line: '0+0', + tokens: [ + { startIndex: 0, type: 'number.c' }, + { startIndex: 1, type: 'delimiter.c' }, + { startIndex: 2, type: 'number.c' } + ] + } + ], + + [ + { + line: '100+10', + tokens: [ + { startIndex: 0, type: 'number.c' }, + { startIndex: 3, type: 'delimiter.c' }, + { startIndex: 4, type: 'number.c' } + ] + } + ], + + [ + { + line: '0 + 0', + tokens: [ + { startIndex: 0, type: 'number.c' }, + { startIndex: 1, type: '' }, + { startIndex: 2, type: 'delimiter.c' }, + { startIndex: 3, type: '' }, + { startIndex: 4, type: 'number.c' } + ] + } + ], + + // Monarch Generated + [ + { + line: '#include', + tokens: [ + { startIndex: 0, type: 'keyword.directive.include.c' }, + { startIndex: 8, type: 'keyword.directive.include.begin.c' }, + { startIndex: 9, type: 'string.include.identifier.c' }, + { startIndex: 17, type: 'keyword.directive.include.end.c' } + ] + }, + { + line: '#include "/path/to/my/file.h"', + tokens: [ + { startIndex: 0, type: 'keyword.directive.include.c' }, + { startIndex: 8, type: '' }, + { startIndex: 9, type: 'keyword.directive.include.begin.c' }, + { startIndex: 10, type: 'string.include.identifier.c' }, + { startIndex: 28, type: 'keyword.directive.include.end.c' } + ] + }, + { + line: '', + tokens: [] + }, + { + line: '#ifdef VAR', + tokens: [ + { startIndex: 0, type: 'keyword.directive.c' }, + { startIndex: 6, type: '' }, + { startIndex: 7, type: 'identifier.c' } + ] + }, + { + line: '#define SUM(A,B) (A) + (B)', + tokens: [ + { startIndex: 0, type: 'keyword.directive.c' }, + { startIndex: 7, type: '' }, + { startIndex: 8, type: 'identifier.c' }, + { startIndex: 11, type: 'delimiter.parenthesis.c' }, + { startIndex: 12, type: 'identifier.c' }, + { startIndex: 13, type: 'delimiter.c' }, + { startIndex: 14, type: 'identifier.c' }, + { startIndex: 15, type: 'delimiter.parenthesis.c' }, + { startIndex: 16, type: '' }, + { startIndex: 17, type: 'delimiter.parenthesis.c' }, + { startIndex: 18, type: 'identifier.c' }, + { startIndex: 19, type: 'delimiter.parenthesis.c' }, + { startIndex: 20, type: '' }, + { startIndex: 21, type: 'delimiter.c' }, + { startIndex: 22, type: '' }, + { startIndex: 23, type: 'delimiter.parenthesis.c' }, + { startIndex: 24, type: 'identifier.c' }, + { startIndex: 25, type: 'delimiter.parenthesis.c' } + ] + }, + { + line: '', + tokens: [] + }, + { + line: 'int main(int argc, char** argv)', + tokens: [ + { startIndex: 0, type: 'keyword.int.c' }, + { startIndex: 3, type: '' }, + { startIndex: 4, type: 'identifier.c' }, + { startIndex: 8, type: 'delimiter.parenthesis.c' }, + { startIndex: 9, type: 'keyword.int.c' }, + { startIndex: 12, type: '' }, + { startIndex: 13, type: 'identifier.c' }, + { startIndex: 17, type: 'delimiter.c' }, + { startIndex: 18, type: '' }, + { startIndex: 19, type: 'keyword.char.c' }, + { startIndex: 23, type: '' }, + { startIndex: 26, type: 'identifier.c' }, + { startIndex: 30, type: 'delimiter.parenthesis.c' } + ] + }, + { + line: '{', + tokens: [{ startIndex: 0, type: 'delimiter.curly.c' }] + }, + { + line: ' return 0;', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 1, type: 'keyword.return.c' }, + { startIndex: 7, type: '' }, + { startIndex: 8, type: 'number.c' }, + { startIndex: 9, type: 'delimiter.c' } + ] + }, + { + line: '}', + tokens: [{ startIndex: 0, type: 'delimiter.curly.c' }] + }, + { + line: '', + tokens: [] + }, + { + line: '{', + tokens: [{ startIndex: 0, type: 'delimiter.curly.c' }] + }, + { + line: ' {', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 1, type: 'delimiter.curly.c' } + ] + }, + { + line: ' ', + tokens: [{ startIndex: 0, type: '' }] + }, + { + line: ' static T field;', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 2, type: 'keyword.static.c' }, + { startIndex: 8, type: '' }, + { startIndex: 9, type: 'identifier.c' }, + { startIndex: 10, type: '' }, + { startIndex: 11, type: 'identifier.c' }, + { startIndex: 16, type: 'delimiter.c' } + ] + }, + { + line: ' ', + tokens: [{ startIndex: 0, type: '' }] + }, + { + line: ' ', + tokens: [{ startIndex: 0, type: '' }] + }, + { + line: ' foo method() const override', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 2, type: 'identifier.c' }, + { startIndex: 5, type: '' }, + { startIndex: 6, type: 'identifier.c' }, + { startIndex: 12, type: 'delimiter.parenthesis.c' }, + { startIndex: 14, type: '' }, + { startIndex: 15, type: 'keyword.const.c' }, + { startIndex: 20, type: '' }, + { startIndex: 21, type: 'keyword.override.c' } + ] + }, + { + line: ' {', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 2, type: 'delimiter.curly.c' } + ] + }, + { + line: ' ', + tokens: [{ startIndex: 0, type: '' }] + }, + { + line: ' if (s.field) {', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 3, type: 'keyword.if.c' }, + { startIndex: 5, type: '' }, + { startIndex: 6, type: 'delimiter.parenthesis.c' }, + { startIndex: 7, type: 'identifier.c' }, + { startIndex: 8, type: 'delimiter.c' }, + { startIndex: 9, type: 'identifier.c' }, + { startIndex: 14, type: 'delimiter.parenthesis.c' }, + { startIndex: 15, type: '' }, + { startIndex: 16, type: 'delimiter.curly.c' } + ] + }, + { + line: ' for(const auto & b : s.field) {', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 4, type: 'keyword.for.c' }, + { startIndex: 7, type: 'delimiter.parenthesis.c' }, + { startIndex: 8, type: 'keyword.const.c' }, + { startIndex: 13, type: '' }, + { startIndex: 14, type: 'keyword.auto.c' }, + { startIndex: 18, type: '' }, + { startIndex: 19, type: 'delimiter.c' }, + { startIndex: 20, type: '' }, + { startIndex: 21, type: 'identifier.c' }, + { startIndex: 22, type: '' }, + { startIndex: 23, type: 'delimiter.c' }, + { startIndex: 24, type: '' }, + { startIndex: 25, type: 'identifier.c' }, + { startIndex: 26, type: 'delimiter.c' }, + { startIndex: 27, type: 'identifier.c' }, + { startIndex: 32, type: 'delimiter.parenthesis.c' }, + { startIndex: 33, type: '' }, + { startIndex: 34, type: 'delimiter.curly.c' } + ] + }, + { + line: ' break;', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 5, type: 'keyword.break.c' }, + { startIndex: 10, type: 'delimiter.c' } + ] + }, + { + line: ' }', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 4, type: 'delimiter.curly.c' } + ] + }, + { + line: ' }', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 3, type: 'delimiter.curly.c' } + ] + }, + { + line: ' }', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 2, type: 'delimiter.curly.c' } + ] + }, + { + line: ' ', + tokens: [{ startIndex: 0, type: '' }] + }, + { + line: ' std::string s = "hello wordld\\n";', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 2, type: 'identifier.c' }, + { startIndex: 5, type: '' }, + { startIndex: 7, type: 'identifier.c' }, + { startIndex: 13, type: '' }, + { startIndex: 14, type: 'identifier.c' }, + { startIndex: 15, type: '' }, + { startIndex: 16, type: 'delimiter.c' }, + { startIndex: 17, type: '' }, + { startIndex: 18, type: 'string.c' }, + { startIndex: 31, type: 'string.escape.c' }, + { startIndex: 33, type: 'string.c' }, + { startIndex: 34, type: 'delimiter.c' } + ] + }, + { + line: ' ', + tokens: [{ startIndex: 0, type: '' }] + }, + { + line: " int number = 123'123'123Ull;", + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 2, type: 'keyword.int.c' }, + { startIndex: 5, type: '' }, + { startIndex: 6, type: 'identifier.c' }, + { startIndex: 12, type: '' }, + { startIndex: 13, type: 'delimiter.c' }, + { startIndex: 14, type: '' }, + { startIndex: 15, type: 'number.c' }, + { startIndex: 29, type: 'delimiter.c' } + ] + }, + { + line: ' }', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 1, type: 'delimiter.curly.c' } + ] + }, + { + line: '}', + tokens: [{ startIndex: 0, type: 'delimiter.curly.c' }] + }, + { + line: '', + tokens: [] + }, + { + line: '#endif', + tokens: [{ startIndex: 0, type: 'keyword.directive.c' }] + }, + { + line: '# ifdef VAR', + tokens: [ + { startIndex: 0, type: 'keyword.directive.c' }, + { startIndex: 10, type: '' }, + { startIndex: 11, type: 'identifier.c' } + ] + }, + { + line: '# define SUM(A,B) (A) + (B)', + tokens: [ + { startIndex: 0, type: 'keyword.directive.c' }, + { startIndex: 8, type: '' }, + { startIndex: 9, type: 'identifier.c' }, + { startIndex: 12, type: 'delimiter.parenthesis.c' }, + { startIndex: 13, type: 'identifier.c' }, + { startIndex: 14, type: 'delimiter.c' }, + { startIndex: 15, type: 'identifier.c' }, + { startIndex: 16, type: 'delimiter.parenthesis.c' }, + { startIndex: 17, type: '' }, + { startIndex: 18, type: 'delimiter.parenthesis.c' }, + { startIndex: 19, type: 'identifier.c' }, + { startIndex: 20, type: 'delimiter.parenthesis.c' }, + { startIndex: 21, type: '' }, + { startIndex: 22, type: 'delimiter.c' }, + { startIndex: 23, type: '' }, + { startIndex: 24, type: 'delimiter.parenthesis.c' }, + { startIndex: 25, type: 'identifier.c' }, + { startIndex: 26, type: 'delimiter.parenthesis.c' } + ] + } + ], + + // https://github.com/microsoft/monaco-editor/issues/1951 + [ + { + line: ' // This is a comment, not a string', + tokens: [ + { startIndex: 0, type: '' }, + { startIndex: 4, type: 'comment.c' } + ] + } + ], + + // Preprocessor directives with whitespace inamongst the characters, + // and crucially checking with whitespace before the initial #. + [ + { + line: ' # if defined(SOMETHING)', + tokens: [ + { startIndex: 0, type: 'keyword.directive.c' }, + { startIndex: 5, type: '' }, + { startIndex: 6, type: 'identifier.c' }, + { startIndex: 13, type: 'delimiter.parenthesis.c' }, + { startIndex: 14, type: 'identifier.c' }, + { startIndex: 23, type: 'delimiter.parenthesis.c' } + ] + }, + { + line: ' #include ', + tokens: [ + { startIndex: 0, type: 'keyword.directive.include.c' }, + { startIndex: 16, type: '' }, + { startIndex: 17, type: 'keyword.directive.include.begin.c' }, + { startIndex: 18, type: 'string.include.identifier.c' }, + { startIndex: 22, type: 'keyword.directive.include.end.c' } + ] + }, + { + line: ' # include ', + tokens: [ + { startIndex: 0, type: 'keyword.directive.include.c' }, + { startIndex: 16, type: '' }, + { startIndex: 17, type: 'keyword.directive.include.begin.c' }, + { startIndex: 18, type: 'string.include.identifier.c' }, + { startIndex: 22, type: 'keyword.directive.include.end.c' } + ] + } + ], + + [ + // microsoft/monaco-editor#2497 : comment continuation highlighting + { + line: '// this is a comment \\', + tokens: [{ startIndex: 0, type: 'comment.c' }] + }, + { + line: 'this is still a comment', + tokens: [{ startIndex: 0, type: 'comment.c' }] + }, + { + line: 'int x = 1;', + tokens: [ + { startIndex: 0, type: 'keyword.int.c' }, + { startIndex: 3, type: '' }, + { startIndex: 4, type: 'identifier.c' }, + { startIndex: 5, type: '' }, + { startIndex: 6, type: 'delimiter.c' }, + { startIndex: 7, type: '' }, + { startIndex: 8, type: 'number.c' }, + { startIndex: 9, type: 'delimiter.c' } + ] + } + ] +]); diff --git a/src/basic-languages/c/c.ts b/src/basic-languages/c/c.ts new file mode 100644 index 0000000000..60aab05778 --- /dev/null +++ b/src/basic-languages/c/c.ts @@ -0,0 +1,262 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import type { languages } from '../../fillers/monaco-editor-core'; + +export const conf: languages.LanguageConfiguration = { + comments: { + lineComment: '//', + blockComment: ['/*', '*/'] + }, + brackets: [ + ['{', '}'], + ['[', ']'], + ['(', ')'] + ], + autoClosingPairs: [ + { open: '[', close: ']' }, + { open: '{', close: '}' }, + { open: '(', close: ')' }, + { open: "'", close: "'", notIn: ['string', 'comment'] }, + { open: '"', close: '"', notIn: ['string'] } + ], + surroundingPairs: [ + { open: '{', close: '}' }, + { open: '[', close: ']' }, + { open: '(', close: ')' }, + { open: '"', close: '"' }, + { open: "'", close: "'" } + ] +}; + +export const language = { + defaultToken: '', + tokenPostfix: '.c', + + brackets: [ + { token: 'delimiter.curly', open: '{', close: '}' }, + { token: 'delimiter.parenthesis', open: '(', close: ')' }, + { token: 'delimiter.square', open: '[', close: ']' }, + { token: 'delimiter.angle', open: '<', close: '>' } + ], + + keywords: [ + 'auto', + 'bool', + 'break', + 'case', + 'char', + 'const', + 'constexpr', + 'continue', + 'cpu', + 'default', + 'do', + 'double', + 'else', + 'enum', + 'extern', + 'false', + 'float', + 'for', + 'goto', + 'if', + 'int', + 'long', + 'nullptr', + 'override', + 'register', + 'restrict', + 'return', + 'short', + 'signed', + 'sizeof', + 'static', + 'static_assert', + 'struct', + 'switch', + 'thread_local', + 'true', + 'typedef', + 'unsigned', + 'void', + 'volatile', + 'while', + 'inline', + 'alignof', + 'asm', + 'fortran', + 'pragma', + 'typeof', + 'typeof__unequal', + + '_asm', // reserved word with one underscores + '_Alignof', + '_Pragma', + + '__abstract' // reserved word with two underscores + ], + + operators: [ + '=', + '>', + '<', + '!', + '~', + '?', + ':', + '==', + '<=', + '>=', + '!=', + '&&', + '||', + '++', + '--', + '+', + '-', + '*', + '/', + '&', + '|', + '^', + '%', + '<<', + '>>', + '+=', + '-=', + '*=', + '/=', + '&=', + '|=', + '^=', + '%=', + '<<=', + '>>=' + ], + + // we include these common regular expressions + symbols: /[=>\[\]]/, '@brackets'], + [ + /@symbols/, + { + cases: { + '@operators': 'delimiter', + '@default': '' + } + } + ], + + // numbers + [/\d*\d+[eE]([\-+]?\d+)?(@floatsuffix)/, 'number.float'], + [/\d*\.\d+([eE][\-+]?\d+)?(@floatsuffix)/, 'number.float'], + [/0[xX][0-9a-fA-F']*[0-9a-fA-F](@integersuffix)/, 'number.hex'], + [/0[0-7']*[0-7](@integersuffix)/, 'number.octal'], + [/0[bB][0-1']*[0-1](@integersuffix)/, 'number.binary'], + [/\d[\d']*\d(@integersuffix)/, 'number'], + [/\d(@integersuffix)/, 'number'], + + // delimiter: after number because of .\d floats + [/[;,.]/, 'delimiter'], + + // strings + [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string + [/"/, 'string', '@string'], + + // characters + [/'[^\\']'/, 'string'], + [/(')(@escapes)(')/, ['string', 'string.escape', 'string']], + [/'/, 'string.invalid'] + ], + + whitespace: [ + [/[ \t\r\n]+/, ''], + [/\/\*\*(?!\/)/, 'comment.doc', '@doccomment'], + [/\/\*/, 'comment', '@comment'], + [/\/\/.*\\$/, 'comment', '@linecomment'], + [/\/\/.*$/, 'comment'] + ], + + comment: [ + [/[^\/*]+/, 'comment'], + [/\*\//, 'comment', '@pop'], + [/[\/*]/, 'comment'] + ], + + //For use with continuous line comments + linecomment: [ + [/.*[^\\]$/, 'comment', '@pop'], + [/[^]+/, 'comment'] + ], + + //Identical copy of comment above, except for the addition of .doc + doccomment: [ + [/[^\/*]+/, 'comment.doc'], + [/\*\//, 'comment.doc', '@pop'], + [/[\/*]/, 'comment.doc'] + ], + + string: [ + [/[^\\"]+/, 'string'], + [/@escapes/, 'string.escape'], + [/\\./, 'string.escape.invalid'], + [/"/, 'string', '@pop'] + ], + + include: [ + [ + /(\s*)(<)([^<>]*)(>)/, + [ + '', + 'keyword.directive.include.begin', + 'string.include.identifier', + { token: 'keyword.directive.include.end', next: '@pop' } + ] + ], + [ + /(\s*)(")([^"]*)(")/, + [ + '', + 'keyword.directive.include.begin', + 'string.include.identifier', + { token: 'keyword.directive.include.end', next: '@pop' } + ] + ] + ] + } +}; From ba1ad63f7881088d3c329665ac4b1f3b4937a3b3 Mon Sep 17 00:00:00 2001 From: Sami Ibrahim Date: Mon, 18 Dec 2023 21:36:44 +0300 Subject: [PATCH 2/3] feat: register c language --- src/basic-languages/monaco.contribution.ts | 1 + website/index/samples/sample.c.txt | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 website/index/samples/sample.c.txt diff --git a/src/basic-languages/monaco.contribution.ts b/src/basic-languages/monaco.contribution.ts index b6c07a545a..83ce638c6b 100644 --- a/src/basic-languages/monaco.contribution.ts +++ b/src/basic-languages/monaco.contribution.ts @@ -12,6 +12,7 @@ import './cameligo/cameligo.contribution'; import './clojure/clojure.contribution'; import './coffee/coffee.contribution'; import './cpp/cpp.contribution'; +import './c/c.contribution'; import './csharp/csharp.contribution'; import './csp/csp.contribution'; import './css/css.contribution'; diff --git a/website/index/samples/sample.c.txt b/website/index/samples/sample.c.txt new file mode 100644 index 0000000000..e577d91244 --- /dev/null +++ b/website/index/samples/sample.c.txt @@ -0,0 +1,7 @@ +#include +#include + +int main() { + int class = 4; + printf("Hello world, the secres passowrd is %d\n", class); +} From e9d5ef666a95c97f3685b1e43d3c88b34d237ded Mon Sep 17 00:00:00 2001 From: Sami Ibrahim Date: Mon, 18 Dec 2023 21:36:57 +0300 Subject: [PATCH 3/3] feat: remvoe c from cpp --- src/basic-languages/cpp/cpp.contribution.ts | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/basic-languages/cpp/cpp.contribution.ts b/src/basic-languages/cpp/cpp.contribution.ts index 51a874d1d5..8be0ce0d0f 100644 --- a/src/basic-languages/cpp/cpp.contribution.ts +++ b/src/basic-languages/cpp/cpp.contribution.ts @@ -8,20 +8,6 @@ import { registerLanguage } from '../_.contribution'; declare var AMD: any; declare var require: any; -registerLanguage({ - id: 'c', - extensions: ['.c', '.h'], - aliases: ['C', 'c'], - loader: () => { - if (AMD) { - return new Promise((resolve, reject) => { - require(['vs/basic-languages/cpp/cpp'], resolve, reject); - }); - } else { - return import('./cpp'); - } - } -}); registerLanguage({ id: 'cpp', extensions: ['.cpp', '.cc', '.cxx', '.hpp', '.hh', '.hxx'],