gdscript.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 <djrmuv@gmail.com> 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. "setget",
  138. "signal",
  139. "tool",
  140. "const",
  141. "enum",
  142. "export",
  143. "onready",
  144. "static",
  145. "var",
  146. "break",
  147. "continue",
  148. "if",
  149. "elif",
  150. "else",
  151. "for",
  152. "pass",
  153. "return",
  154. "match",
  155. "while",
  156. "remote",
  157. "master",
  158. "puppet",
  159. "remotesync",
  160. "mastersync",
  161. "puppetsync",
  162. ),
  163. suffix=r"\b",
  164. ),
  165. Keyword,
  166. ),
  167. ],
  168. "builtins": [
  169. (
  170. words(
  171. (
  172. "Color8",
  173. "ColorN",
  174. "abs",
  175. "acos",
  176. "asin",
  177. "assert",
  178. "atan",
  179. "atan2",
  180. "bytes2var",
  181. "ceil",
  182. "char",
  183. "clamp",
  184. "convert",
  185. "cos",
  186. "cosh",
  187. "db2linear",
  188. "decimals",
  189. "dectime",
  190. "deg2rad",
  191. "dict2inst",
  192. "ease",
  193. "exp",
  194. "floor",
  195. "fmod",
  196. "fposmod",
  197. "funcref",
  198. "hash",
  199. "inst2dict",
  200. "instance_from_id",
  201. "is_inf",
  202. "is_nan",
  203. "lerp",
  204. "linear2db",
  205. "load",
  206. "log",
  207. "max",
  208. "min",
  209. "nearest_po2",
  210. "pow",
  211. "preload",
  212. "print",
  213. "print_stack",
  214. "printerr",
  215. "printraw",
  216. "prints",
  217. "printt",
  218. "rad2deg",
  219. "rand_range",
  220. "rand_seed",
  221. "randf",
  222. "randi",
  223. "randomize",
  224. "range",
  225. "round",
  226. "seed",
  227. "sign",
  228. "sin",
  229. "sinh",
  230. "sqrt",
  231. "stepify",
  232. "str",
  233. "str2var",
  234. "tan",
  235. "tan",
  236. "tanh",
  237. "type_exist",
  238. "typeof",
  239. "var2bytes",
  240. "var2str",
  241. "weakref",
  242. "yield",
  243. ),
  244. prefix=r"(?<!\.)",
  245. suffix=r"\b",
  246. ),
  247. Name.Builtin,
  248. ),
  249. (r"((?<!\.)(self|false|true)|(PI|TAU|NAN|INF)" r")\b", Name.Builtin.Pseudo),
  250. (
  251. words(
  252. (
  253. "bool",
  254. "int",
  255. "float",
  256. "String",
  257. "NodePath",
  258. "Vector2",
  259. "Rect2",
  260. "Transform2D",
  261. "Vector3",
  262. "Rect3",
  263. "Plane",
  264. "Quat",
  265. "Basis",
  266. "Transform",
  267. "Color",
  268. "RID",
  269. "Object",
  270. "NodePath",
  271. "Dictionary",
  272. "Array",
  273. "PoolByteArray",
  274. "PoolIntArray",
  275. "PoolRealArray",
  276. "PoolStringArray",
  277. "PoolVector2Array",
  278. "PoolVector3Array",
  279. "PoolColorArray",
  280. "null",
  281. ),
  282. prefix=r"(?<!\.)",
  283. suffix=r"\b",
  284. ),
  285. Name.Builtin.Type,
  286. ),
  287. ],
  288. "numbers": [
  289. (r"(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?", Number.Float),
  290. (r"\d+[eE][+-]?[0-9]+j?", Number.Float),
  291. (r"0[xX][a-fA-F0-9]+", Number.Hex),
  292. (r"\d+j?", Number.Integer),
  293. ],
  294. "name": [(r"[a-zA-Z_]\w*", Name)],
  295. "funcname": [(r"[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop")],
  296. "classname": [(r"[a-zA-Z_]\w*", Name.Class, "#pop")],
  297. "stringescape": [
  298. (
  299. r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  300. r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})",
  301. String.Escape,
  302. )
  303. ],
  304. "strings-single": innerstring_rules(String.Single),
  305. "strings-double": innerstring_rules(String.Double),
  306. "dqs": [
  307. (r'"', String.Double, "#pop"),
  308. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  309. include("strings-double"),
  310. ],
  311. "sqs": [
  312. (r"'", String.Single, "#pop"),
  313. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  314. include("strings-single"),
  315. ],
  316. "tdqs": [
  317. (r'"""', String.Double, "#pop"),
  318. include("strings-double"),
  319. (r"\n", String.Double),
  320. ],
  321. "tsqs": [
  322. (r"'''", String.Single, "#pop"),
  323. include("strings-single"),
  324. (r"\n", String.Single),
  325. ],
  326. }
  327. def setup(sphinx):
  328. sphinx.add_lexer("gdscript", GDScriptLexer)
  329. return {
  330. "parallel_read_safe": True,
  331. "parallel_write_safe": True,
  332. }