Skip to content

Commit ec15cb8

Browse files
Merge pull request #15 from Calman102/master
Added support for Python and MATLAB code
2 parents ac9c514 + 5e3d81f commit ec15cb8

File tree

4 files changed

+277
-0
lines changed

4 files changed

+277
-0
lines changed

ColorCode.Core/Common/LanguageId.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ public static class LanguageId
2828
public const string Haskell = "haskell";
2929
public const string Markdown = "markdown";
3030
public const string Fortran = "fortran";
31+
public const string Python = "python";
32+
public const string MatLab = "matlab";
3133
}
3234
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using ColorCode.Common;
7+
8+
namespace ColorCode.Compilation.Languages
9+
{
10+
public class MatLab : ILanguage
11+
{
12+
public string Id
13+
{
14+
get { return LanguageId.MatLab; }
15+
}
16+
17+
public string Name
18+
{
19+
get { return "MATLAB"; }
20+
}
21+
22+
public string CssClassName
23+
{
24+
get { return "matlab"; }
25+
}
26+
27+
public string FirstLinePattern
28+
{
29+
get
30+
{
31+
return null;
32+
}
33+
}
34+
35+
public IList<LanguageRule> Rules
36+
{
37+
get
38+
{
39+
return new List<LanguageRule>
40+
{
41+
// regular comments
42+
new LanguageRule(
43+
@"(%.*)\r?",
44+
new Dictionary<int, string>
45+
{
46+
{ 0, ScopeName.Comment },
47+
}),
48+
49+
// regular strings
50+
new LanguageRule(
51+
@"(?<!\.)('[^\n]*?')",
52+
new Dictionary<int, string>
53+
{
54+
{ 0, ScopeName.String },
55+
}),
56+
new LanguageRule(
57+
@"""[^\n]*?""",
58+
new Dictionary<int, string>
59+
{
60+
{ 0, ScopeName.String },
61+
}),
62+
63+
// keywords
64+
new LanguageRule(
65+
@"(?i)\b(break|case|catch|continue|else|elseif|end|for|function|global|if|otherwise|persistent|return|switch|try|while)\b",
66+
new Dictionary<int, string>
67+
{
68+
{ 1, ScopeName.Keyword },
69+
}),
70+
71+
// line continuation
72+
new LanguageRule(
73+
@"\.\.\.",
74+
new Dictionary<int, string>
75+
{
76+
{ 0, ScopeName.Continuation },
77+
}),
78+
79+
// numbers
80+
new LanguageRule(
81+
@"\b([0-9.]|[0-9.]+(e-*)(?=[0-9]))+?\b",
82+
new Dictionary<int, string>
83+
{
84+
{ 0, ScopeName.Number },
85+
}),
86+
};
87+
}
88+
}
89+
90+
public bool HasAlias(string lang)
91+
{
92+
switch (lang.ToLower())
93+
{
94+
case "m":
95+
return true;
96+
97+
case "mat":
98+
return true;
99+
100+
case "matlab":
101+
return true;
102+
103+
default:
104+
return false;
105+
}
106+
}
107+
108+
public override string ToString()
109+
{
110+
return Name;
111+
}
112+
}
113+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using ColorCode.Common;
7+
8+
namespace ColorCode.Compilation.Languages
9+
{
10+
public class Python : ILanguage
11+
{
12+
public string Id
13+
{
14+
get { return LanguageId.Python; }
15+
}
16+
17+
public string Name
18+
{
19+
get { return "Python"; }
20+
}
21+
22+
public string CssClassName
23+
{
24+
get { return "python"; }
25+
}
26+
27+
public string FirstLinePattern
28+
{
29+
get
30+
{
31+
return null;
32+
}
33+
}
34+
35+
public IList<LanguageRule> Rules
36+
{
37+
get
38+
{
39+
return new List<LanguageRule>
40+
{
41+
// docstring comments
42+
new LanguageRule(
43+
@"(?<=:\s*)(""{3})([^""]+)(""{3})",
44+
new Dictionary<int, string>
45+
{
46+
{ 0, ScopeName.Comment },
47+
}),
48+
new LanguageRule(
49+
@"(?<=:\s*)('{3})([^']+)('{3})",
50+
new Dictionary<int, string>
51+
{
52+
{ 0, ScopeName.Comment },
53+
}),
54+
55+
// regular comments
56+
new LanguageRule(
57+
@"(#.*)\r?",
58+
new Dictionary<int, string>
59+
{
60+
{ 0, ScopeName.Comment },
61+
}),
62+
63+
// multi-line strings
64+
new LanguageRule(
65+
@"(?<==\s*f*b*r*u*)(""{3})([^""]+)(""{3})",
66+
new Dictionary<int, string>
67+
{
68+
{ 0, ScopeName.String },
69+
}),
70+
new LanguageRule(
71+
@"(?<==\s*f*b*r*u*)('{3})([^']+)('{3})",
72+
new Dictionary<int, string>
73+
{
74+
{ 0, ScopeName.String },
75+
}),
76+
77+
// regular strings
78+
new LanguageRule(
79+
@"'[^\n]*?'",
80+
new Dictionary<int, string>
81+
{
82+
{ 0, ScopeName.String },
83+
}),
84+
new LanguageRule(
85+
@"""[^\n]*?""",
86+
new Dictionary<int, string>
87+
{
88+
{ 0, ScopeName.String },
89+
}),
90+
91+
// keywords
92+
new LanguageRule(
93+
@"(?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|" +
94+
@"nonlocal|while|assert|del|global|not|with|async|elif|if|or|yield|self)\b",
95+
new Dictionary<int, string>
96+
{
97+
{ 1, ScopeName.Keyword },
98+
}),
99+
100+
// intrinsic functions
101+
new LanguageRule(
102+
@"(?i)\b(abs|delattr|hash|memoryview|set|all|dict|help|min|setattr|any|dir|hex|next|slice|ascii|divmod|id|object|sorted|bin|enumerate" +
103+
"|input|oct|staticmethod|bool|eval|int|open|str|breakpoint|exec|isinstance|ord|sum|bytearray|filter|issubclass|pow|super|bytes|float" +
104+
"|iter|print|tuple|callable|format|len|property|type|chr|frozenset|list|range|vars|classmethod|getattr|locals|repr|zip|compile|globals" +
105+
@"|map|reversed|__import__|complex|hasattr|max|round)\b",
106+
new Dictionary<int, string>
107+
{
108+
{ 1, ScopeName.Intrinsic },
109+
}),
110+
111+
// numbers
112+
new LanguageRule(
113+
@"\b([0-9.]|[0-9.]+(e-*)(?=[0-9]))+?\b",
114+
new Dictionary<int, string>
115+
{
116+
{ 0, ScopeName.Number },
117+
}),
118+
};
119+
}
120+
}
121+
122+
public bool HasAlias(string lang)
123+
{
124+
switch (lang.ToLower())
125+
{
126+
case "py":
127+
return true;
128+
129+
case "python":
130+
return true;
131+
132+
default:
133+
return false;
134+
}
135+
}
136+
137+
public override string ToString()
138+
{
139+
return Name;
140+
}
141+
}
142+
}

ColorCode.Core/Languages.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ static Languages()
4949
Load<Haskell>();
5050
Load<Markdown>();
5151
Load<Fortran>();
52+
Load<Python>();
53+
Load<MatLab>();
5254
}
5355

5456
/// <summary>
@@ -257,6 +259,24 @@ public static ILanguage Fortran
257259
get { return LanguageRepository.FindById(LanguageId.Fortran); }
258260
}
259261

262+
/// <summary>
263+
/// Language support for Python.
264+
/// </summary>
265+
/// <value>Language support for Python.</value>
266+
public static ILanguage Python
267+
{
268+
get { return LanguageRepository.FindById(LanguageId.Python); }
269+
}
270+
271+
/// <summary>
272+
/// Language support for MATLAB.
273+
/// </summary>
274+
/// <value>Language support for MATLAB.</value>
275+
public static ILanguage MATLAB
276+
{
277+
get { return LanguageRepository.FindById(LanguageId.MatLab); }
278+
}
279+
260280
/// <summary>
261281
/// Finds a loaded language by the specified identifier.
262282
/// </summary>

0 commit comments

Comments
 (0)