Skip to content

Commit f29f6c5

Browse files
committed
added support for Python and MATLAB code
1 parent 17e1b36 commit f29f6c5

4 files changed

Lines changed: 273 additions & 0 deletions

File tree

ColorCode.Core/Common/LanguageId.cs

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

ColorCode.Core/Languages.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ static Languages()
4747
Load<Haskell>();
4848
Load<Markdown>();
4949
Load<Fortran>();
50+
Load<Python>();
51+
Load<MatLab>();
5052
}
5153

5254
/// <summary>
@@ -255,6 +257,24 @@ public static ILanguage Fortran
255257
get { return LanguageRepository.FindById(LanguageId.Fortran); }
256258
}
257259

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

0 commit comments

Comments
 (0)