gdscript.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.gdscript
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. Lexer for GDScript.
  6. :copyright: Copyright 2xxx by The Godot Engine Community
  7. :license: MIT.
  8. modified by Daniel J. Ramirez <[email protected]> based on the original python.py pygment
  9. """
  10. import re
  11. from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \
  12. default, words, combined, do_insertions
  13. from pygments.util import get_bool_opt, shebang_matches
  14. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  15. Number, Punctuation, Generic, Other, Error
  16. from pygments import unistring as uni
  17. __all__ = ['GDScriptLexer']
  18. line_re = re.compile('.*?\n')
  19. class GDScriptLexer(RegexLexer):
  20. """
  21. For `Godot source code <https://www.godotengine.org>`_ source code.
  22. """
  23. name = 'GDScript'
  24. aliases = ['gdscript', 'gd']
  25. filenames = ['*.gd']
  26. mimetypes = ['text/x-gdscript', 'application/x-gdscript']
  27. def innerstring_rules(ttype):
  28. return [
  29. # the old style '%s' % (...) string formatting
  30. (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
  31. '[hlL]?[E-GXc-giorsux%]', String.Interpol),
  32. # backslashes, quotes and formatting signs must be parsed one at a time
  33. (r'[^\\\'"%\n]+', ttype),
  34. (r'[\'"\\]', ttype),
  35. # unhandled string formatting sign
  36. (r'%', ttype),
  37. # newlines are an error (use "nl" state)
  38. ]
  39. tokens = {
  40. 'root': [
  41. (r'\n', Text),
  42. (r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")',
  43. bygroups(Text, String.Affix, String.Doc)),
  44. (r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')",
  45. bygroups(Text, String.Affix, String.Doc)),
  46. (r'[^\S\n]+', Text),
  47. (r'#.*$', Comment.Single),
  48. (r'[]{}:(),;[]', Punctuation),
  49. (r'\\\n', Text),
  50. (r'\\', Text),
  51. (r'(in|and|or|not)\b', Operator.Word),
  52. (r'!=|==|<<|>>|&&|\+=|-=|\*=|/=|%=|&=|\|=|\|\||[-~+/*%=<>&^.!|$]', Operator),
  53. include('keywords'),
  54. (r'(func)((?:\s|\\\s)+)', bygroups(Keyword, Text), 'funcname'),
  55. (r'(class)((?:\s|\\\s)+)', bygroups(Keyword, Text), 'classname'),
  56. include('builtins'),
  57. ('([rR]|[uUbB][rR]|[rR][uUbB])(""")',
  58. bygroups(String.Affix, String.Double), 'tdqs'),
  59. ("([rR]|[uUbB][rR]|[rR][uUbB])(''')",
  60. bygroups(String.Affix, String.Single), 'tsqs'),
  61. ('([rR]|[uUbB][rR]|[rR][uUbB])(")',
  62. bygroups(String.Affix, String.Double), 'dqs'),
  63. ("([rR]|[uUbB][rR]|[rR][uUbB])(')",
  64. bygroups(String.Affix, String.Single), 'sqs'),
  65. ('([uUbB]?)(""")', bygroups(String.Affix, String.Double),
  66. combined('stringescape', 'tdqs')),
  67. ("([uUbB]?)(''')", bygroups(String.Affix, String.Single),
  68. combined('stringescape', 'tsqs')),
  69. ('([uUbB]?)(")', bygroups(String.Affix, String.Double),
  70. combined('stringescape', 'dqs')),
  71. ("([uUbB]?)(')", bygroups(String.Affix, String.Single),
  72. combined('stringescape', 'sqs')),
  73. include('name'),
  74. include('numbers'),
  75. ],
  76. 'keywords': [
  77. (words((
  78. 'do', 'var', 'const', 'extends', 'is', 'export', 'onready', 'tool',
  79. 'static', 'setget', 'signal', 'breakpoint', 'switch', 'case',
  80. 'assert', 'break', 'continue', 'elif', 'else', 'for', 'if',
  81. 'pass', 'return', 'while', 'match', 'master', 'sync', 'slave', 'rpc', 'enum'), suffix=r'\b'),
  82. Keyword),
  83. ],
  84. 'builtins': [
  85. (words((
  86. 'Color8', 'ColorN', 'abs', 'acos', 'asin', 'assert', 'atan', 'atan2',
  87. 'bytes2var', 'ceil', 'char', 'clamp', 'convert', 'cos', 'cosh',
  88. 'db2linear', 'decimals', 'dectime', 'deg2rad', 'dict2inst',
  89. 'ease', 'exp', 'floor', 'fmod', 'fposmod', 'funcref', 'hash',
  90. 'inst2dict', 'instance_from_id', 'is_inf', 'is_nan', 'lerp',
  91. 'linear2db', 'load', 'log', 'max', 'min', 'nearest_po2', 'pow',
  92. 'preload', 'print', 'print_stack', 'printerr', 'printraw',
  93. 'prints', 'printt', 'rad2deg', 'rand_range', 'rand_seed',
  94. 'randf', 'randi', 'randomize', 'range', 'round', 'seed', 'sign',
  95. 'sin', 'sinh', 'sqrt', 'stepify', 'str', 'str2var', 'tan',
  96. 'tan', 'tanh', 'type_exist', 'typeof', 'var2bytes', 'var2str',
  97. 'weakref', 'yield'),
  98. prefix=r'(?<!\.)', suffix=r'\b'),
  99. Name.Builtin),
  100. (r'((?<!\.)(self|false|true)|(PI|NAN|INF)'
  101. r')\b', Name.Builtin.Pseudo),
  102. (words((
  103. 'bool', 'int', 'float', 'String', 'NodePath'
  104. 'Vector2', 'Rect2', 'Transform2D',
  105. 'Vector3', 'Rect3', 'Plane', 'Quat', 'Basis', 'Transform',
  106. 'Color', "RID", 'Object', 'NodePath', 'Dictionary',
  107. 'Array', 'PoolByteArray', 'PoolIntArray', 'PoolRealArray',
  108. 'PoolStringArray', 'PoolVector2Array', 'PoolVector3Array', 'PoolColorArray',
  109. 'null',
  110. ), prefix=r'(?<!\.)', suffix=r'\b'), Name.Builtin.Type),
  111. ],
  112. 'numbers': [
  113. (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?', Number.Float),
  114. (r'\d+[eE][+-]?[0-9]+j?', Number.Float),
  115. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  116. (r'\d+j?', Number.Integer)
  117. ],
  118. 'name': [
  119. ('[a-zA-Z_]\w*', Name),
  120. ],
  121. 'funcname': [
  122. ('[a-zA-Z_]\w*', Name.Function, '#pop'),
  123. default('#pop'),
  124. ],
  125. 'classname': [
  126. ('[a-zA-Z_]\w*', Name.Class, '#pop')
  127. ],
  128. 'stringescape': [
  129. (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  130. r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
  131. ],
  132. 'strings-single': innerstring_rules(String.Single),
  133. 'strings-double': innerstring_rules(String.Double),
  134. 'dqs': [
  135. (r'"', String.Double, '#pop'),
  136. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  137. include('strings-double')
  138. ],
  139. 'sqs': [
  140. (r"'", String.Single, '#pop'),
  141. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  142. include('strings-single')
  143. ],
  144. 'tdqs': [
  145. (r'"""', String.Double, '#pop'),
  146. include('strings-double'),
  147. (r'\n', String.Double)
  148. ],
  149. 'tsqs': [
  150. (r"'''", String.Single, '#pop'),
  151. include('strings-single'),
  152. (r'\n', String.Single)
  153. ],
  154. }