Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
urbi.py146 linesDownload Raw Back to lexers
1"""2    pygments.lexers.urbi3    ~~~~~~~~~~~~~~~~~~~~4 5    Lexers for UrbiScript language.6 7    :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.8    :license: BSD, see LICENSE for details.9"""10 11import re12 13from pygments.lexer import ExtendedRegexLexer, words14from pygments.token import Text, Comment, Operator, Keyword, Name, String, \15    Number, Punctuation16 17__all__ = ['UrbiscriptLexer']18 19 20class UrbiscriptLexer(ExtendedRegexLexer):21    """22    For UrbiScript source code.23    """24 25    name = 'UrbiScript'26    aliases = ['urbiscript']27    filenames = ['*.u']28    mimetypes = ['application/x-urbiscript']29    url = 'https://github.com/urbiforge/urbi'30    version_added = '1.5'31 32    flags = re.DOTALL33 34    # TODO35    # - handle Experimental and deprecated tags with specific tokens36    # - handle Angles and Durations with specific tokens37 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_blob44        ctx.pos += len(text_before_blob)45 46        # if blob size doesn't match blob format (example : "\B(2)(aaa)")47        # yield blob as a string48        if ctx.text[match.end() + blob_size] != ")":49            result = "\\B(" + blob_size_str + ")("50            yield match.start(), String, result51            ctx.pos += len(result)52            return53 54        # if blob is well formatted, yield as Escape55        blob_text = blob_start + ctx.text[match.end():match.end()+blob_size] + ")"56        yield match.start(), String.Escape, blob_text57        ctx.pos = match.end() + blob_size + 1  # +1 is the ending ")"58 59    tokens = {60        'root': [61            (r'\s+', Text),62            # comments63            (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 available82            (r'(emit|foreach|internal|loopn|static)\b', Keyword),83            # ignored keywords, use a meaningful token when available84            (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 Duration111            (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 strings114            (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-states126        'comment': [127            (r'[^*/]', Comment.Multiline),128            (r'/\*', Comment.Multiline, '#push'),129            (r'\*/', Comment.Multiline, '#pop'),130            (r'[*/]', Comment.Multiline),131        ]132    }133 134    def analyse_text(text):135        """This is fairly similar to C and others, but freezeif and136        waituntil are unique keywords."""137        result = 0138 139        if 'freezeif' in text:140            result += 0.05141 142        if 'waituntil' in text:143            result += 0.05144 145        return result146 
codekingpro/portable-devtools · Team Ai