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