|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import re |
| 4 | + |
| 5 | +from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, this, do_insertions |
| 6 | +from pygments.token import * |
| 7 | + |
| 8 | +__all__ = ['SwiftLexer', 'SwiftConsoleLexer'] |
| 9 | + |
| 10 | + |
| 11 | +class SwiftLexer(RegexLexer): |
| 12 | + name = 'Swift' |
| 13 | + aliases = ['swift'] |
| 14 | + filenames = ['*.swift'] |
| 15 | + |
| 16 | + flags = re.MULTILINE | re.DOTALL |
| 17 | + |
| 18 | + _isa = r'([a-zA-Z_][a-zA-Z0-9_]*)(\s+)(:)(\s+)([A-Z0-9_][a-zA-Z0-9_]*)' |
| 19 | + _isa_comma = r'([a-zA-Z_][a-zA-Z0-9_]*)(\s+)(:)(\s+)([A-Z0-9_][a-zA-Z0-9_]*)(,\s?)' |
| 20 | + _name = r'[a-zA-Z_][a-zA-Z0-9_?]*' |
| 21 | + |
| 22 | + tokens = { |
| 23 | + |
| 24 | + 'root' : [ |
| 25 | + (r'^', Punctuation, 'root2'), |
| 26 | + ], |
| 27 | + |
| 28 | + 'root2' : [ |
| 29 | + (r'\n', Text, '#pop'), |
| 30 | + |
| 31 | + (r'//.*?\n', Comment.Single, '#pop'), |
| 32 | + |
| 33 | + (r'\bvar\s', Keyword.Declaration, 'var-decl'), |
| 34 | + (r'\bfor\s', Keyword.Reserved, 'for-loop'), |
| 35 | + (r'\b(func|constructor|destructor)\b\s?', Keyword.Declaration, 'func-decl'), |
| 36 | + (r'(\bset\b)(\s?)(\()', bygroups(Keyword.Declaration, Whitespace, Punctuation), 'arg-list'), |
| 37 | + (r'(set|get)(:)', bygroups(Keyword.Reserved, Punctuation)), |
| 38 | + (r'\b(this|This)\b', Name.Builtin.Pseudo), |
| 39 | + (r'\bid\b', Name.Builtin), |
| 40 | + |
| 41 | + (r'\b(class|struct|protocol|extension)\s', Keyword.Declaration, 'class-decl'), |
| 42 | + |
| 43 | + (r'(\b[A-Z][a-zA-Z0-9_]*\s?)(\()', bygroups(Name.Constant, Punctuation), 'type-cast'), |
| 44 | + |
| 45 | + (r'"', String, 'string'), |
| 46 | + |
| 47 | + (r'(\bnew\b\s?)', Keyword.Reserved, 'class-name'), |
| 48 | + (r'\b(true|false)\b', Keyword.Reserved), |
| 49 | + (r'\b(if|else)\s', Keyword.Reserved), |
| 50 | + (r'\b(return|break)\b', Keyword.Reserved), |
| 51 | + |
| 52 | + (r'[\^\*!%&<>+=/?-]|\.{2}', Operator), |
| 53 | + (r'\$([0-9]+)', Name.Variable), #Tokens |
| 54 | + (r'[\[\]\(\)\{\}\|:;,.#]', Punctuation), |
| 55 | + (r'[0-9]+\.[0-9]+', Number.Float), |
| 56 | + (r'0x[0-9a-fA-F]+', Number.Hex), |
| 57 | + (r'[0-9]+', Number.Integer), |
| 58 | + (r'\s', Whitespace), |
| 59 | + |
| 60 | + (r'\(', Punctuation, 'tuple'), |
| 61 | + |
| 62 | + include('name'), |
| 63 | + |
| 64 | + ], |
| 65 | + |
| 66 | + 'isa' : [ |
| 67 | + (_isa, bygroups(Name, Whitespace, Punctuation, Whitespace, Name.Constant)), |
| 68 | + ], |
| 69 | + |
| 70 | + 'class-isa' : [ |
| 71 | + (_isa, bygroups(Name.Class, Whitespace, Punctuation, Whitespace, Name.Constant)), |
| 72 | + ], |
| 73 | + |
| 74 | + 'var-isa' : [ |
| 75 | + (_isa, bygroups(Name.Variable, Whitespace, Punctuation, Whitespace, Name.Constant)), |
| 76 | + ], |
| 77 | + |
| 78 | + 'var-isa-pop' : [ |
| 79 | + (_isa, bygroups(Name.Variable, Whitespace, Punctuation, Whitespace, Name.Constant), '#pop'), |
| 80 | + ], |
| 81 | + |
| 82 | + 'var-isa-comma' : [ |
| 83 | + (_isa_comma, bygroups(Name.Variable, Whitespace, Punctuation, Whitespace, Name.Constant, Punctuation)), |
| 84 | + ], |
| 85 | + |
| 86 | + 'var-name' : [ |
| 87 | + (r'[a-zA-Z_][a-zA-Z0-9_?]*', Name.Variable), |
| 88 | + ], |
| 89 | + |
| 90 | + 'tuple' : [ |
| 91 | + (r'\(', Punctuation, 'in-tuple'), |
| 92 | + ], |
| 93 | + |
| 94 | + 'in-tuple' : [ |
| 95 | + (r'\)', Punctuation, '#pop'), |
| 96 | + include('class-name'), |
| 97 | + include('name'), |
| 98 | + include('isa'), |
| 99 | + include('root2'), |
| 100 | + ], |
| 101 | + |
| 102 | + 'name' : [ |
| 103 | + (_name, Name), |
| 104 | + ], |
| 105 | + |
| 106 | + 'generic-type' : [ |
| 107 | + (r'>', Punctuation, '#pop'), |
| 108 | + include('class-name'), |
| 109 | + include('isa'), |
| 110 | + include('root2'), |
| 111 | + ], |
| 112 | + |
| 113 | + 'class-name' : [ |
| 114 | + (r'[A-Z][a-zA-Z0-9_?]*', Name.Constant), |
| 115 | + (r'(\[)([0-9]+)(\])', bygroups(Operator, Number.Integer, Operator)), |
| 116 | + (r'<', Punctuation, 'generic-type'), |
| 117 | + (r'\(', Punctuation, 'type-cast'), |
| 118 | + (r'\)', Punctuation, '#pop'), |
| 119 | + ], |
| 120 | + |
| 121 | + 'label' : [ |
| 122 | + (r'[a-zA-Z_][a-zA-Z0-9_]*:(?=\s*\n)', Name.Label), |
| 123 | + ], |
| 124 | + |
| 125 | + 'ws-pop' : [ |
| 126 | + (r'\s?[\s\n]', Whitespace, '#pop'), |
| 127 | + ], |
| 128 | + |
| 129 | + 'var-decl' : [ |
| 130 | + (r'(\[)([\w\s,]*)(\])(\s+)', bygroups(Punctuation, Name.Attribute, Punctuation, Whitespace)), |
| 131 | + include('tuple'), |
| 132 | + include('var-isa-comma'), |
| 133 | + include('var-isa-pop'), |
| 134 | + include('var-name'), |
| 135 | + (r',\s+', Punctuation, 'var-decl'), |
| 136 | + include('ws-pop'), |
| 137 | + ], |
| 138 | + |
| 139 | + 'for-loop' : [ |
| 140 | + (r'\sin\s', Keyword.Reserved), |
| 141 | + include('isa'), |
| 142 | + include('name'), |
| 143 | + include('ws-pop'), |
| 144 | + include('root2'), |
| 145 | + ], |
| 146 | + |
| 147 | + 'func-decl' : [ |
| 148 | + (r'(\[)([\w\s,]*)(\])(\s+)', bygroups(Punctuation, Name.Attribute, Punctuation, Whitespace)), |
| 149 | + (r'\s?\breturn\b', Keyword.Reserved, 'root2'), |
| 150 | + (r'\s?\w', Name.Function), |
| 151 | + (r'<', Punctuation, 'generic-type'), |
| 152 | + (r'\(\s?', Punctuation, 'arg-list'), |
| 153 | + (r'\s?->\s?', Operator, 'return-type'), |
| 154 | + (r'\s?\{', Punctuation, '#pop'), |
| 155 | + ], |
| 156 | + |
| 157 | + 'return-type' : [ |
| 158 | + include('tuple'), |
| 159 | + include('class-name'), |
| 160 | + (r'\bid\b', Name.Builtin), |
| 161 | + (r'\s?\{', Punctuation, '#pop'), |
| 162 | + (r'\s?\)', Punctuation), |
| 163 | + ], |
| 164 | + |
| 165 | + 'class-decl' : [ |
| 166 | + (r'\{', Punctuation, '#pop'), |
| 167 | + (r'(\[)([\w\s,]*)(\])(\s+)', bygroups(Punctuation, Name.Attribute, Punctuation, Whitespace)), |
| 168 | + include('class-isa'), |
| 169 | + (r'[A-Z][a-zA-Z0-9_?]*', Name.Class), |
| 170 | + (r'\s', Whitespace), |
| 171 | + (r'<', Punctuation, 'generic-type'), |
| 172 | + ], |
| 173 | + |
| 174 | + 'arg-list' : [ |
| 175 | + (r',\s?', Punctuation), |
| 176 | + (r'\)', Punctuation, '#pop'), |
| 177 | + include('isa'), |
| 178 | + (r'\s?->\s?', Operator, 'return-type'), |
| 179 | + include('root2'), |
| 180 | + ], |
| 181 | + |
| 182 | + 'type-cast' : [ |
| 183 | + (r'\)', Punctuation, '#pop'), |
| 184 | + include('root2'), |
| 185 | + ], |
| 186 | + |
| 187 | + 'in-interpolated' : [ |
| 188 | + ('\)', String.Interpol, '#pop'), |
| 189 | + include('root2'), |
| 190 | + ], |
| 191 | + |
| 192 | + 'string' : [ |
| 193 | + (r'"', String, '#pop'), |
| 194 | + (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape), |
| 195 | + (r'\\\(', String.Interpol, 'in-interpolated'), |
| 196 | + (r'[^\\"]+', String), |
| 197 | + (r'\\', String), |
| 198 | + ], |
| 199 | + } |
| 200 | + |
| 201 | + |
| 202 | +class SwiftConsoleLexer(RegexLexer): |
| 203 | + name = 'SwiftConsole' |
| 204 | + aliases = ['swift-console'] |
| 205 | + filenames = ['*.swiftc'] |
| 206 | + |
| 207 | + flags = re.MULTILINE | re.DOTALL |
| 208 | + |
| 209 | + _isa = r'([a-zA-Z_][a-zA-Z0-9_]*)(\s+)(:)(\s+)([A-Z0-9_][a-zA-Z0-9_]*)' |
| 210 | + _isa_comma = r'([a-zA-Z_][a-zA-Z0-9_]*)(\s+)(:)(\s+)([A-Z0-9_][a-zA-Z0-9_]*)(,\s?)' |
| 211 | + _name = r'[a-zA-Z_][a-zA-Z0-9_?]*' |
| 212 | + |
| 213 | + tokens = SwiftLexer.tokens.copy() |
| 214 | + tokens['root'] = [ |
| 215 | + (r'Welcome to swift. Type \':help\' for assistance.', Generic.Prompt), |
| 216 | + (r'(\(swift\) | )', Generic.Prompt, 'root2'), |
| 217 | + (r'\(swift\)', Generic.Prompt), |
| 218 | + (r' ', Generic.Prompt), |
| 219 | + (r'//.*?\n', Generic.Output), |
| 220 | + (r'<REPL Buffer>:[0-9]*:[0-9]*:.*?\n', Generic.Heading), |
| 221 | + (r'~*?\^\s?~*?\n', Generic.Heading), |
| 222 | + (r'.*?\n', Generic.Output), |
| 223 | + ] |
0 commit comments