From f29f6c54cadfce8a8469787424bc028d212c8957 Mon Sep 17 00:00:00 2001 From: Callum Gray Date: Thu, 10 Dec 2020 22:38:16 +1000 Subject: [PATCH 1/2] added support for Python and MATLAB code --- ColorCode.Core/Common/LanguageId.cs | 2 + .../Compilation/Languages/MatLab.cs | 111 ++++++++++++++ .../Compilation/Languages/Python.cs | 140 ++++++++++++++++++ ColorCode.Core/Languages.cs | 20 +++ 4 files changed, 273 insertions(+) create mode 100644 ColorCode.Core/Compilation/Languages/MatLab.cs create mode 100644 ColorCode.Core/Compilation/Languages/Python.cs diff --git a/ColorCode.Core/Common/LanguageId.cs b/ColorCode.Core/Common/LanguageId.cs index 20c1685..5bbbd83 100644 --- a/ColorCode.Core/Common/LanguageId.cs +++ b/ColorCode.Core/Common/LanguageId.cs @@ -26,5 +26,7 @@ public static class LanguageId public const string Haskell = "haskell"; public const string Markdown = "markdown"; public const string Fortran = "fortran"; + public const string Python = "python"; + public const string MatLab = "matlab"; } } \ No newline at end of file diff --git a/ColorCode.Core/Compilation/Languages/MatLab.cs b/ColorCode.Core/Compilation/Languages/MatLab.cs new file mode 100644 index 0000000..751db1f --- /dev/null +++ b/ColorCode.Core/Compilation/Languages/MatLab.cs @@ -0,0 +1,111 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. + +using System.Collections.Generic; +using ColorCode.Common; + +namespace ColorCode.Compilation.Languages +{ + public class MatLab : ILanguage + { + public string Id + { + get { return LanguageId.MatLab; } + } + + public string Name + { + get { return "MATLAB"; } + } + + public string CssClassName + { + get { return "matlab"; } + } + + public string FirstLinePattern + { + get + { + return null; + } + } + + public IList Rules + { + get + { + return new List + { + // regular comments + new LanguageRule( + @"(%.*)\r?", + new Dictionary + { + { 0, ScopeName.Comment }, + }), + + // regular strings + new LanguageRule( + @"(? + { + { 0, ScopeName.String }, + }), + new LanguageRule( + @"""[^\n]*?""", + new Dictionary + { + { 0, ScopeName.String }, + }), + + // keywords + new LanguageRule( + @"(?i)\b(break|case|catch|continue|else|elseif|end|for|function|global|if|otherwise|persistent|return|switch|try|while)\b", + new Dictionary + { + { 1, ScopeName.Keyword }, + }), + + // line continuation + new LanguageRule( + @"\.\.\.", + new Dictionary + { + { 0, ScopeName.Continuation }, + }), + + // numbers + new LanguageRule( + @"\b([0-9.]|[0-9.]+(e-*)(?=[0-9]))+?\b", + new Dictionary + { + { 0, ScopeName.Number }, + }), + }; + } + } + + public bool HasAlias(string lang) + { + switch (lang.ToLower()) + { + case "m": + return true; + + case "mat": + return true; + + case "matlab": + return true; + + default: + return false; + } + } + + public override string ToString() + { + return Name; + } + } +} \ No newline at end of file diff --git a/ColorCode.Core/Compilation/Languages/Python.cs b/ColorCode.Core/Compilation/Languages/Python.cs new file mode 100644 index 0000000..67ca1cf --- /dev/null +++ b/ColorCode.Core/Compilation/Languages/Python.cs @@ -0,0 +1,140 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. + +using System.Collections.Generic; +using ColorCode.Common; + +namespace ColorCode.Compilation.Languages +{ + public class Python : ILanguage + { + public string Id + { + get { return LanguageId.Python; } + } + + public string Name + { + get { return "Python"; } + } + + public string CssClassName + { + get { return "python"; } + } + + public string FirstLinePattern + { + get + { + return null; + } + } + + public IList Rules + { + get + { + return new List + { + // docstring comments + new LanguageRule( + @"(?<=:\s*)(""{3})([^""]+)(""{3})", + new Dictionary + { + { 0, ScopeName.Comment }, + }), + new LanguageRule( + @"(?<=:\s*)('{3})([^']+)('{3})", + new Dictionary + { + { 0, ScopeName.Comment }, + }), + + // regular comments + new LanguageRule( + @"(#.*)\r?", + new Dictionary + { + { 0, ScopeName.Comment }, + }), + + // multi-line strings + new LanguageRule( + @"(?<==\s*f*b*r*u*)(""{3})([^""]+)(""{3})", + new Dictionary + { + { 0, ScopeName.String }, + }), + new LanguageRule( + @"(?<==\s*f*b*r*u*)('{3})([^']+)('{3})", + new Dictionary + { + { 0, ScopeName.String }, + }), + + // regular strings + new LanguageRule( + @"'[^\n]*?'", + new Dictionary + { + { 0, ScopeName.String }, + }), + new LanguageRule( + @"""[^\n]*?""", + new Dictionary + { + { 0, ScopeName.String }, + }), + + // keywords + new LanguageRule( + @"(?i)\b(False|await|else|import|pass|None|break|except|in|raise|True|class|finally|is|return|and|continue|for|lambda|try|as|def|from|" + + @"nonlocal|while|assert|del|global|not|with|async|elif|if|or|yield|self)\b", + new Dictionary + { + { 1, ScopeName.Keyword }, + }), + + // intrinsic functions + new LanguageRule( + @"(?i)\b(abs|delattr|hash|memoryview|set|all|dict|help|min|setattr|any|dir|hex|next|slice|ascii|divmod|id|object|sorted|bin|enumerate" + + "|input|oct|staticmethod|bool|eval|int|open|str|breakpoint|exec|isinstance|ord|sum|bytearray|filter|issubclass|pow|super|bytes|float" + + "|iter|print|tuple|callable|format|len|property|type|chr|frozenset|list|range|vars|classmethod|getattr|locals|repr|zip|compile|globals" + + @"|map|reversed|__import__|complex|hasattr|max|round)\b", + new Dictionary + { + { 1, ScopeName.Intrinsic }, + }), + + // numbers + new LanguageRule( + @"\b([0-9.]|[0-9.]+(e-*)(?=[0-9]))+?\b", + new Dictionary + { + { 0, ScopeName.Number }, + }), + }; + } + } + + public bool HasAlias(string lang) + { + switch (lang.ToLower()) + { + case "py": + return true; + + case "python": + return true; + + default: + return false; + } + } + + public override string ToString() + { + return Name; + } + } +} \ No newline at end of file diff --git a/ColorCode.Core/Languages.cs b/ColorCode.Core/Languages.cs index f3abadb..d9f6f0d 100644 --- a/ColorCode.Core/Languages.cs +++ b/ColorCode.Core/Languages.cs @@ -47,6 +47,8 @@ static Languages() Load(); Load(); Load(); + Load(); + Load(); } /// @@ -255,6 +257,24 @@ public static ILanguage Fortran get { return LanguageRepository.FindById(LanguageId.Fortran); } } + /// + /// Language support for Python. + /// + /// Language support for Python. + public static ILanguage Python + { + get { return LanguageRepository.FindById(LanguageId.Python); } + } + + /// + /// Language support for MATLAB. + /// + /// Language support for MATLAB. + public static ILanguage MATLAB + { + get { return LanguageRepository.FindById(LanguageId.MatLab); } + } + /// /// Finds a loaded language by the specified identifier. /// From 5e3d81f71d64652763003502ad419120b6e35d1f Mon Sep 17 00:00:00 2001 From: "Michael Hawker MSFT (XAML Llama)" <24302614+michael-hawker@users.noreply.github.com> Date: Fri, 30 Sep 2022 13:05:06 -0700 Subject: [PATCH 2/2] Update license files to align with repo No legal ramifications here as CLA signed. --- ColorCode.Core/Compilation/Languages/MatLab.cs | 4 +++- ColorCode.Core/Compilation/Languages/Python.cs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ColorCode.Core/Compilation/Languages/MatLab.cs b/ColorCode.Core/Compilation/Languages/MatLab.cs index 751db1f..4e9b1a4 100644 --- a/ColorCode.Core/Compilation/Languages/MatLab.cs +++ b/ColorCode.Core/Compilation/Languages/MatLab.cs @@ -1,4 +1,6 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. using System.Collections.Generic; using ColorCode.Common; diff --git a/ColorCode.Core/Compilation/Languages/Python.cs b/ColorCode.Core/Compilation/Languages/Python.cs index 67ca1cf..aa082b8 100644 --- a/ColorCode.Core/Compilation/Languages/Python.cs +++ b/ColorCode.Core/Compilation/Languages/Python.cs @@ -1,4 +1,6 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. using System.Collections.Generic; using ColorCode.Common;