gdscript.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. 'and', 'in', 'not', 'or', 'as', 'breakpoint', 'class', 'class_name',
  79. 'extends', 'is', 'func', 'setget', 'signal', 'tool', 'const',
  80. 'enum', 'export', 'onready', 'static', 'var', 'break', 'continue',
  81. 'if', 'elif', 'else', 'for', 'pass', 'return', 'match', 'while',
  82. 'remote', 'master', 'puppet', 'remotesync', 'mastersync',
  83. 'puppetsync'), suffix=r'\b'),
  84. Keyword),
  85. ],
  86. 'builtins': [
  87. (words((
  88. 'Color8', 'ColorN', 'abs', 'acos', 'asin', 'assert', 'atan', 'atan2',
  89. 'bytes2var', 'ceil', 'char', 'clamp', 'convert', 'cos', 'cosh',
  90. 'db2linear', 'decimals', 'dectime', 'deg2rad', 'dict2inst',
  91. 'ease', 'exp', 'floor', 'fmod', 'fposmod', 'funcref', 'hash',
  92. 'inst2dict', 'instance_from_id', 'is_inf', 'is_nan', 'lerp',
  93. 'linear2db', 'load', 'log', 'max', 'min', 'nearest_po2', 'pow',
  94. 'preload', 'print', 'print_stack', 'printerr', 'printraw',
  95. 'prints', 'printt', 'rad2deg', 'rand_range', 'rand_seed',
  96. 'randf', 'randi', 'randomize', 'range', 'round', 'seed', 'sign',
  97. 'sin', 'sinh', 'sqrt', 'stepify', 'str', 'str2var', 'tan',
  98. 'tan', 'tanh', 'type_exist', 'typeof', 'var2bytes', 'var2str',
  99. 'weakref', 'yield'),
  100. prefix=r'(?<!\.)', suffix=r'\b'),
  101. Name.Builtin),
  102. (r'((?<!\.)(self|false|true)|(PI|TAU|NAN|INF)'
  103. r')\b', Name.Builtin.Pseudo),
  104. (words((
  105. 'bool', 'int', 'float', 'String', 'NodePath'
  106. 'Vector2', 'Rect2', 'Transform2D',
  107. 'Vector3', 'Rect3', 'Plane', 'Quat', 'Basis', 'Transform',
  108. 'Color', "RID", 'Object', 'NodePath', 'Dictionary',
  109. 'Array', 'PoolByteArray', 'PoolIntArray', 'PoolRealArray',
  110. 'PoolStringArray', 'PoolVector2Array', 'PoolVector3Array', 'PoolColorArray',
  111. 'null',
  112. ), prefix=r'(?<!\.)', suffix=r'\b'), Name.Builtin.Type),
  113. ],
  114. 'numbers': [
  115. (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?', Number.Float),
  116. (r'\d+[eE][+-]?[0-9]+j?', Number.Float),
  117. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  118. (r'\d+j?', Number.Integer)
  119. ],
  120. 'name': [
  121. ('[a-zA-Z_]\w*', Name),
  122. ],
  123. 'funcname': [
  124. ('[a-zA-Z_]\w*', Name.Function, '#pop'),
  125. default('#pop'),
  126. ],
  127. 'classname': [
  128. ('[a-zA-Z_]\w*', Name.Class, '#pop')
  129. ],
  130. 'stringescape': [
  131. (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  132. r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
  133. ],
  134. 'strings-single': innerstring_rules(String.Single),
  135. 'strings-double': innerstring_rules(String.Double),
  136. 'dqs': [
  137. (r'"', String.Double, '#pop'),
  138. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  139. include('strings-double')
  140. ],
  141. 'sqs': [
  142. (r"'", String.Single, '#pop'),
  143. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  144. include('strings-single')
  145. ],
  146. 'tdqs': [
  147. (r'"""', String.Double, '#pop'),
  148. include('strings-double'),
  149. (r'\n', String.Double)
  150. ],
  151. 'tsqs': [
  152. (r"'''", String.Single, '#pop'),
  153. include('strings-single'),
  154. (r'\n', String.Single)
  155. ],
  156. }
  157. def setup(sphinx):
  158. sphinx.add_lexer('gdscript', GDScriptLexer())