gdscript.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. "cartesian2polar",
  182. "ceil",
  183. "char",
  184. "clamp",
  185. "convert",
  186. "cos",
  187. "cosh",
  188. "db2linear",
  189. "decimals",
  190. "dectime",
  191. "deep_equal",
  192. "deg2rad",
  193. "dict2inst",
  194. "ease",
  195. "exp",
  196. "floor",
  197. "fmod",
  198. "fposmod",
  199. "funcref",
  200. "get_stack",
  201. "hash",
  202. "inst2dict",
  203. "instance_from_id",
  204. "inverse_lerp",
  205. "is_equal_approx",
  206. "is_inf",
  207. "is_instance_valid",
  208. "is_nan",
  209. "is_zero_approx",
  210. "len",
  211. "lerp",
  212. "lerp_angle",
  213. "linear2db",
  214. "load",
  215. "log",
  216. "max",
  217. "min",
  218. "move_toward",
  219. "nearest_po2",
  220. "ord",
  221. "parse_json",
  222. "polar2cartesian",
  223. "posmod",
  224. "pow",
  225. "preload",
  226. "print",
  227. "print_debug",
  228. "print_stack",
  229. "printerr",
  230. "printraw",
  231. "prints",
  232. "printt",
  233. "push_error",
  234. "push_warning",
  235. "rad2deg",
  236. "rand_range",
  237. "rand_seed",
  238. "randf",
  239. "randi",
  240. "randomize",
  241. "range",
  242. "range_lerp",
  243. "round",
  244. "seed",
  245. "sign",
  246. "sin",
  247. "sinh",
  248. "smoothstep",
  249. "sqrt",
  250. "step_decimals",
  251. "stepify",
  252. "str",
  253. "str2var",
  254. "tan",
  255. "tanh",
  256. "to_json",
  257. "type_exists",
  258. "typeof",
  259. "validate_json",
  260. "var2bytes",
  261. "var2str",
  262. "weakref",
  263. "wrapf",
  264. "wrapi",
  265. "yield",
  266. ),
  267. prefix=r"(?<!\.)",
  268. suffix=r"\b",
  269. ),
  270. Name.Builtin,
  271. ),
  272. (r"((?<!\.)(self|false|true)|(PI|TAU|NAN|INF)" r")\b", Name.Builtin.Pseudo),
  273. (
  274. words(
  275. (
  276. "bool",
  277. "int",
  278. "float",
  279. "String",
  280. "NodePath",
  281. "Vector2",
  282. "Rect2",
  283. "Transform2D",
  284. "Vector3",
  285. "Rect3",
  286. "Plane",
  287. "Quat",
  288. "Basis",
  289. "Transform",
  290. "Color",
  291. "RID",
  292. "Object",
  293. "NodePath",
  294. "Dictionary",
  295. "Array",
  296. "PoolByteArray",
  297. "PoolIntArray",
  298. "PoolRealArray",
  299. "PoolStringArray",
  300. "PoolVector2Array",
  301. "PoolVector3Array",
  302. "PoolColorArray",
  303. "null",
  304. ),
  305. prefix=r"(?<!\.)",
  306. suffix=r"\b",
  307. ),
  308. Name.Builtin.Type,
  309. ),
  310. ],
  311. "numbers": [
  312. (r"(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?", Number.Float),
  313. (r"\d+[eE][+-]?[0-9]+j?", Number.Float),
  314. (r"0x[a-fA-F0-9]+", Number.Hex),
  315. (r"0b[01]+", Number.Bin),
  316. (r"\d+j?", Number.Integer),
  317. ],
  318. "name": [(r"[a-zA-Z_]\w*", Name)],
  319. "funcname": [(r"[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop")],
  320. "classname": [(r"[a-zA-Z_]\w*", Name.Class, "#pop")],
  321. "stringescape": [
  322. (
  323. r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  324. r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})",
  325. String.Escape,
  326. )
  327. ],
  328. "strings-single": innerstring_rules(String.Single),
  329. "strings-double": innerstring_rules(String.Double),
  330. "dqs": [
  331. (r'"', String.Double, "#pop"),
  332. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  333. include("strings-double"),
  334. ],
  335. "sqs": [
  336. (r"'", String.Single, "#pop"),
  337. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  338. include("strings-single"),
  339. ],
  340. "tdqs": [
  341. (r'"""', String.Double, "#pop"),
  342. include("strings-double"),
  343. (r"\n", String.Double),
  344. ],
  345. "tsqs": [
  346. (r"'''", String.Single, "#pop"),
  347. include("strings-single"),
  348. (r"\n", String.Single),
  349. ],
  350. }
  351. def setup(sphinx):
  352. sphinx.add_lexer("gdscript", GDScriptLexer)
  353. return {
  354. "parallel_read_safe": True,
  355. "parallel_write_safe": True,
  356. }