Skip to content
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

Added support for Python and MATLAB code #15

Merged
merged 3 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ColorCode.Core/Common/LanguageId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,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";
}
}
111 changes: 111 additions & 0 deletions ColorCode.Core/Compilation/Languages/MatLab.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
michael-hawker marked this conversation as resolved.
Show resolved Hide resolved

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<LanguageRule> Rules
{
get
{
return new List<LanguageRule>
{
// regular comments
new LanguageRule(
@"(%.*)\r?",
new Dictionary<int, string>
{
{ 0, ScopeName.Comment },
}),

// regular strings
new LanguageRule(
@"(?<!\.)('[^\n]*?')",
new Dictionary<int, string>
{
{ 0, ScopeName.String },
}),
new LanguageRule(
@"""[^\n]*?""",
new Dictionary<int, string>
{
{ 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<int, string>
{
{ 1, ScopeName.Keyword },
}),

// line continuation
new LanguageRule(
@"\.\.\.",
new Dictionary<int, string>
{
{ 0, ScopeName.Continuation },
}),

// numbers
new LanguageRule(
@"\b([0-9.]|[0-9.]+(e-*)(?=[0-9]))+?\b",
new Dictionary<int, string>
{
{ 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;
}
}
}
140 changes: 140 additions & 0 deletions ColorCode.Core/Compilation/Languages/Python.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
michael-hawker marked this conversation as resolved.
Show resolved Hide resolved

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<LanguageRule> Rules
{
get
{
return new List<LanguageRule>
{
// docstring comments
new LanguageRule(
@"(?<=:\s*)(""{3})([^""]+)(""{3})",
new Dictionary<int, string>
{
{ 0, ScopeName.Comment },
}),
new LanguageRule(
@"(?<=:\s*)('{3})([^']+)('{3})",
new Dictionary<int, string>
{
{ 0, ScopeName.Comment },
}),

// regular comments
new LanguageRule(
@"(#.*)\r?",
new Dictionary<int, string>
{
{ 0, ScopeName.Comment },
}),

// multi-line strings
new LanguageRule(
@"(?<==\s*f*b*r*u*)(""{3})([^""]+)(""{3})",
new Dictionary<int, string>
{
{ 0, ScopeName.String },
}),
new LanguageRule(
@"(?<==\s*f*b*r*u*)('{3})([^']+)('{3})",
new Dictionary<int, string>
{
{ 0, ScopeName.String },
}),

// regular strings
new LanguageRule(
@"'[^\n]*?'",
new Dictionary<int, string>
{
{ 0, ScopeName.String },
}),
new LanguageRule(
@"""[^\n]*?""",
new Dictionary<int, string>
{
{ 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<int, string>
{
{ 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<int, string>
{
{ 1, ScopeName.Intrinsic },
}),

// numbers
new LanguageRule(
@"\b([0-9.]|[0-9.]+(e-*)(?=[0-9]))+?\b",
new Dictionary<int, string>
{
{ 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;
}
}
}
20 changes: 20 additions & 0 deletions ColorCode.Core/Languages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ static Languages()
Load<Haskell>();
Load<Markdown>();
Load<Fortran>();
Load<Python>();
Load<MatLab>();
}

/// <summary>
Expand Down Expand Up @@ -257,6 +259,24 @@ public static ILanguage Fortran
get { return LanguageRepository.FindById(LanguageId.Fortran); }
}

/// <summary>
/// Language support for Python.
/// </summary>
/// <value>Language support for Python.</value>
public static ILanguage Python
{
get { return LanguageRepository.FindById(LanguageId.Python); }
}

/// <summary>
/// Language support for MATLAB.
/// </summary>
/// <value>Language support for MATLAB.</value>
public static ILanguage MATLAB
{
get { return LanguageRepository.FindById(LanguageId.MatLab); }
}

/// <summary>
/// Finds a loaded language by the specified identifier.
/// </summary>
Expand Down