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+ }
0 commit comments