gdscript.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 (
  12. RegexLexer,
  13. include,
  14. bygroups,
  15. default,
  16. words,
  17. combined,
  18. )
  19. from pygments.token import (
  20. Text,
  21. Comment,
  22. Operator,
  23. Keyword,
  24. Name,
  25. String,
  26. Number,
  27. Punctuation,
  28. )
  29. __all__ = ["GDScriptLexer"]
  30. line_re = re.compile(".*?\n")
  31. class GDScriptLexer(RegexLexer):
  32. """
  33. For `GDScript source code <https://www.godotengine.org>`_.
  34. """
  35. name = "GDScript"
  36. aliases = ["gdscript", "gd"]
  37. filenames = ["*.gd"]
  38. mimetypes = ["text/x-gdscript", "application/x-gdscript"]
  39. def innerstring_rules(ttype):
  40. return [
  41. # the old style '%s' % (...) string formatting
  42. (
  43. r"%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?"
  44. "[hlL]?[E-GXc-giorsux%]",
  45. String.Interpol,
  46. ),
  47. # backslashes, quotes and formatting signs must be parsed one at a time
  48. (r'[^\\\'"%\n]+', ttype),
  49. (r'[\'"\\]', ttype),
  50. # unhandled string formatting sign
  51. (r"%", ttype),
  52. # newlines are an error (use "nl" state)
  53. ]
  54. tokens = {
  55. "root": [
  56. (r"\n", Text),
  57. (
  58. r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")',
  59. bygroups(Text, String.Affix, String.Doc),
  60. ),
  61. (
  62. r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')",
  63. bygroups(Text, String.Affix, String.Doc),
  64. ),
  65. (r"[^\S\n]+", Text),
  66. (r"#.*$", Comment.Single),
  67. (r"[]{}:(),;[]", Punctuation),
  68. (r"\\\n", Text),
  69. (r"\\", Text),
  70. (r"(in|and|or|not)\b", Operator.Word),
  71. (
  72. r"!=|==|<<|>>|&&|\+=|-=|\*=|/=|%=|&=|\|=|\|\||[-~+/*%=<>&^.!|$]",
  73. Operator,
  74. ),
  75. include("keywords"),
  76. (r"(func)((?:\s|\\\s)+)", bygroups(Keyword, Text), "funcname"),
  77. (r"(class)((?:\s|\\\s)+)", bygroups(Keyword, Text), "classname"),
  78. include("builtins"),
  79. (
  80. '([rR]|[uUbB][rR]|[rR][uUbB])(""")',
  81. bygroups(String.Affix, String.Double),
  82. "tdqs",
  83. ),
  84. (
  85. "([rR]|[uUbB][rR]|[rR][uUbB])(''')",
  86. bygroups(String.Affix, String.Single),
  87. "tsqs",
  88. ),
  89. (
  90. '([rR]|[uUbB][rR]|[rR][uUbB])(")',
  91. bygroups(String.Affix, String.Double),
  92. "dqs",
  93. ),
  94. (
  95. "([rR]|[uUbB][rR]|[rR][uUbB])(')",
  96. bygroups(String.Affix, String.Single),
  97. "sqs",
  98. ),
  99. (
  100. '([uUbB]?)(""")',
  101. bygroups(String.Affix, String.Double),
  102. combined("stringescape", "tdqs"),
  103. ),
  104. (
  105. "([uUbB]?)(''')",
  106. bygroups(String.Affix, String.Single),
  107. combined("stringescape", "tsqs"),
  108. ),
  109. (
  110. '([uUbB]?)(")',
  111. bygroups(String.Affix, String.Double),
  112. combined("stringescape", "dqs"),
  113. ),
  114. (
  115. "([uUbB]?)(')",
  116. bygroups(String.Affix, String.Single),
  117. combined("stringescape", "sqs"),
  118. ),
  119. include("name"),
  120. include("numbers"),
  121. ],
  122. "keywords": [
  123. (
  124. words(
  125. (
  126. "and",
  127. "in",
  128. "not",
  129. "or",
  130. "as",
  131. "breakpoint",
  132. "class",
  133. "class_name",
  134. "extends",
  135. "is",
  136. "func",
  137. "signal",
  138. "const",
  139. "enum",
  140. "static",
  141. "var",
  142. "break",
  143. "continue",
  144. "if",
  145. "elif",
  146. "else",
  147. "for",
  148. "pass",
  149. "return",
  150. "match",
  151. "while",
  152. ),
  153. suffix=r"\b",
  154. ),
  155. Keyword,
  156. ),
  157. ],
  158. "builtins": [
  159. (
  160. words(
  161. (
  162. "Color8",
  163. "ColorN",
  164. "abs",
  165. "acos",
  166. "asin",
  167. "assert",
  168. "atan",
  169. "atan2",
  170. "await",
  171. "bytes2var",
  172. "ceil",
  173. "char",
  174. "clamp",
  175. "convert",
  176. "cos",
  177. "cosh",
  178. "db2linear",
  179. "decimals",
  180. "dectime",
  181. "deg2rad",
  182. "dict2inst",
  183. "ease",
  184. "exp",
  185. "floor",
  186. "fmod",
  187. "fposmod",
  188. "funcref",
  189. "hash",
  190. "inst2dict",
  191. "instance_from_id",
  192. "is_inf",
  193. "is_nan",
  194. "lerp",
  195. "linear2db",
  196. "load",
  197. "log",
  198. "max",
  199. "min",
  200. "nearest_po2",
  201. "pow",
  202. "preload",
  203. "print",
  204. "print_stack",
  205. "printerr",
  206. "printraw",
  207. "prints",
  208. "printt",
  209. "rad2deg",
  210. "rand_range",
  211. "rand_seed",
  212. "randf",
  213. "randi",
  214. "randomize",
  215. "range",
  216. "round",
  217. "seed",
  218. "sign",
  219. "sin",
  220. "sinh",
  221. "sqrt",
  222. "stepify",
  223. "str",
  224. "str2var",
  225. "tan",
  226. "tan",
  227. "tanh",
  228. "type_exist",
  229. "typeof",
  230. "var2bytes",
  231. "var2str",
  232. "weakref",
  233. ),
  234. prefix=r"(?<!\.)",
  235. suffix=r"\b",
  236. ),
  237. Name.Builtin,
  238. ),
  239. (r"((?<!\.)(self|super|false|true)|(PI|TAU|NAN|INF)" r")\b", Name.Builtin.Pseudo),
  240. (
  241. words(
  242. (
  243. "bool",
  244. "int",
  245. "float",
  246. "String",
  247. "NodePath",
  248. "Vector2",
  249. "Rect2",
  250. "Transform2D",
  251. "Vector3",
  252. "Rect3",
  253. "Plane",
  254. "Quat",
  255. "Basis",
  256. "Transform3D",
  257. "Color",
  258. "RID",
  259. "Object",
  260. "NodePath",
  261. "Dictionary",
  262. "Array",
  263. "PackedByteArray",
  264. "PackedInt32Array",
  265. "PackedInt64Array",
  266. "PackedFloat32Array",
  267. "PackedFloat64Array",
  268. "PackedStringArray",
  269. "PackedVector2Array",
  270. "PackedVector3Array",
  271. "PackedColorArray",
  272. "null",
  273. ),
  274. prefix=r"(?<!\.)",
  275. suffix=r"\b",
  276. ),
  277. Name.Builtin.Type,
  278. ),
  279. ],
  280. "numbers": [
  281. (r"(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?", Number.Float),
  282. (r"\d+[eE][+-]?[0-9]+j?", Number.Float),
  283. (r"0[xX][a-fA-F0-9]+", Number.Hex),
  284. (r"\d+j?", Number.Integer),
  285. ],
  286. "name": [(r"@?[a-zA-Z_]\w*", Name)],
  287. "funcname": [(r"[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop")],
  288. "classname": [(r"[a-zA-Z_]\w*", Name.Class, "#pop")],
  289. "stringescape": [
  290. (
  291. r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  292. r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})",
  293. String.Escape,
  294. )
  295. ],
  296. "strings-single": innerstring_rules(String.Single),
  297. "strings-double": innerstring_rules(String.Double),
  298. "dqs": [
  299. (r'"', String.Double, "#pop"),
  300. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  301. include("strings-double"),
  302. ],
  303. "sqs": [
  304. (r"'", String.Single, "#pop"),
  305. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  306. include("strings-single"),
  307. ],
  308. "tdqs": [
  309. (r'"""', String.Double, "#pop"),
  310. include("strings-double"),
  311. (r"\n", String.Double),
  312. ],
  313. "tsqs": [
  314. (r"'''", String.Single, "#pop"),
  315. include("strings-single"),
  316. (r"\n", String.Single),
  317. ],
  318. }
  319. def setup(sphinx):
  320. sphinx.add_lexer("gdscript", GDScriptLexer)
  321. return {
  322. "parallel_read_safe": True,
  323. "parallel_write_safe": True,
  324. }