urbi.py 5.6 KB

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