|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.urbi |
|
4 ~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for UrbiScript language. |
|
7 |
|
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 import re |
|
13 |
|
14 from pygments.lexer import ExtendedRegexLexer, words |
|
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
|
16 Number, Punctuation |
|
17 |
|
18 __all__ = ['UrbiscriptLexer'] |
|
19 |
|
20 |
|
21 class UrbiscriptLexer(ExtendedRegexLexer): |
|
22 """ |
|
23 For UrbiScript source code. |
|
24 |
|
25 .. versionadded:: 1.5 |
|
26 """ |
|
27 |
|
28 name = 'UrbiScript' |
|
29 aliases = ['urbiscript'] |
|
30 filenames = ['*.u'] |
|
31 mimetypes = ['application/x-urbiscript'] |
|
32 |
|
33 flags = re.DOTALL |
|
34 |
|
35 # TODO |
|
36 # - handle Experimental and deprecated tags with specific tokens |
|
37 # - handle Angles and Durations with specific tokens |
|
38 |
|
39 def blob_callback(lexer, match, ctx): |
|
40 text_before_blob = match.group(1) |
|
41 blob_start = match.group(2) |
|
42 blob_size_str = match.group(3) |
|
43 blob_size = int(blob_size_str) |
|
44 yield match.start(), String, text_before_blob |
|
45 ctx.pos += len(text_before_blob) |
|
46 |
|
47 # if blob size doesn't match blob format (example : "\B(2)(aaa)") |
|
48 # yield blob as a string |
|
49 if ctx.text[match.end() + blob_size] != ")": |
|
50 result = "\\B(" + blob_size_str + ")(" |
|
51 yield match.start(), String, result |
|
52 ctx.pos += len(result) |
|
53 return |
|
54 |
|
55 # if blob is well formated, yield as Escape |
|
56 blob_text = blob_start + ctx.text[match.end():match.end()+blob_size] + ")" |
|
57 yield match.start(), String.Escape, blob_text |
|
58 ctx.pos = match.end() + blob_size + 1 # +1 is the ending ")" |
|
59 |
|
60 tokens = { |
|
61 'root': [ |
|
62 (r'\s+', Text), |
|
63 # comments |
|
64 (r'//.*?\n', Comment), |
|
65 (r'/\*', Comment.Multiline, 'comment'), |
|
66 (r'(every|for|loop|while)(?:;|&|\||,)', Keyword), |
|
67 (words(( |
|
68 'assert', 'at', 'break', 'case', 'catch', 'closure', 'compl', |
|
69 'continue', 'default', 'else', 'enum', 'every', 'external', |
|
70 'finally', 'for', 'freezeif', 'if', 'new', 'onleave', 'return', |
|
71 'stopif', 'switch', 'this', 'throw', 'timeout', 'try', |
|
72 'waituntil', 'whenever', 'while'), suffix=r'\b'), |
|
73 Keyword), |
|
74 (words(( |
|
75 'asm', 'auto', 'bool', 'char', 'const_cast', 'delete', 'double', |
|
76 'dynamic_cast', 'explicit', 'export', 'extern', 'float', 'friend', |
|
77 'goto', 'inline', 'int', 'long', 'mutable', 'namespace', 'register', |
|
78 'reinterpret_cast', 'short', 'signed', 'sizeof', 'static_cast', |
|
79 'struct', 'template', 'typedef', 'typeid', 'typename', 'union', |
|
80 'unsigned', 'using', 'virtual', 'volatile', 'wchar_t'), suffix=r'\b'), |
|
81 Keyword.Reserved), |
|
82 # deprecated keywords, use a meaningfull token when available |
|
83 (r'(emit|foreach|internal|loopn|static)\b', Keyword), |
|
84 # ignored keywords, use a meaningfull token when available |
|
85 (r'(private|protected|public)\b', Keyword), |
|
86 (r'(var|do|const|function|class)\b', Keyword.Declaration), |
|
87 (r'(true|false|nil|void)\b', Keyword.Constant), |
|
88 (words(( |
|
89 'Barrier', 'Binary', 'Boolean', 'CallMessage', 'Channel', 'Code', |
|
90 'Comparable', 'Container', 'Control', 'Date', 'Dictionary', 'Directory', |
|
91 'Duration', 'Enumeration', 'Event', 'Exception', 'Executable', 'File', |
|
92 'Finalizable', 'Float', 'FormatInfo', 'Formatter', 'Global', 'Group', |
|
93 'Hash', 'InputStream', 'IoService', 'Job', 'Kernel', 'Lazy', 'List', |
|
94 'Loadable', 'Lobby', 'Location', 'Logger', 'Math', 'Mutex', 'nil', |
|
95 'Object', 'Orderable', 'OutputStream', 'Pair', 'Path', 'Pattern', |
|
96 'Position', 'Primitive', 'Process', 'Profile', 'PseudoLazy', 'PubSub', |
|
97 'RangeIterable', 'Regexp', 'Semaphore', 'Server', 'Singleton', 'Socket', |
|
98 'StackFrame', 'Stream', 'String', 'System', 'Tag', 'Timeout', |
|
99 'Traceable', 'TrajectoryGenerator', 'Triplet', 'Tuple', 'UObject', |
|
100 'UValue', 'UVar'), suffix=r'\b'), |
|
101 Name.Builtin), |
|
102 (r'(?:this)\b', Name.Builtin.Pseudo), |
|
103 # don't match single | and & |
|
104 (r'(?:[-=+*%/<>~^:]+|\.&?|\|\||&&)', Operator), |
|
105 (r'(?:and_eq|and|bitand|bitor|in|not|not_eq|or_eq|or|xor_eq|xor)\b', |
|
106 Operator.Word), |
|
107 (r'[{}\[\]()]+', Punctuation), |
|
108 (r'(?:;|\||,|&|\?|!)+', Punctuation), |
|
109 (r'[$a-zA-Z_]\w*', Name.Other), |
|
110 (r'0x[0-9a-fA-F]+', Number.Hex), |
|
111 # Float, Integer, Angle and Duration |
|
112 (r'(?:[0-9]+(?:(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)?' |
|
113 r'((?:rad|deg|grad)|(?:ms|s|min|h|d))?)\b', Number.Float), |
|
114 # handle binary blob in strings |
|
115 (r'"', String.Double, "string.double"), |
|
116 (r"'", String.Single, "string.single"), |
|
117 ], |
|
118 'string.double': [ |
|
119 (r'((?:\\\\|\\"|[^"])*?)(\\B\((\d+)\)\()', blob_callback), |
|
120 (r'(\\\\|\\"|[^"])*?"', String.Double, '#pop'), |
|
121 ], |
|
122 'string.single': [ |
|
123 (r"((?:\\\\|\\'|[^'])*?)(\\B\((\d+)\)\()", blob_callback), |
|
124 (r"(\\\\|\\'|[^'])*?'", String.Single, '#pop'), |
|
125 ], |
|
126 # from http://pygments.org/docs/lexerdevelopment/#changing-states |
|
127 'comment': [ |
|
128 (r'[^*/]', Comment.Multiline), |
|
129 (r'/\*', Comment.Multiline, '#push'), |
|
130 (r'\*/', Comment.Multiline, '#pop'), |
|
131 (r'[*/]', Comment.Multiline), |
|
132 ] |
|
133 } |