Skip to content

Commit

Permalink
Merge pull request #15 from Calman102/master
Browse files Browse the repository at this point in the history
Added support for Python and MATLAB code
  • Loading branch information
michael-hawker authored Sep 30, 2022
2 parents ac9c514 + 5e3d81f commit ec15cb8
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 0 deletions.
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";
}
}
113 changes: 113 additions & 0 deletions ColorCode.Core/Compilation/Languages/MatLab.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// 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;

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;
}
}
}
142 changes: 142 additions & 0 deletions ColorCode.Core/Compilation/Languages/Python.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// 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;

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

0 comments on commit ec15cb8

Please sign in to comment.