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