gdscript.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. Whitespace,
  29. )
  30. __all__ = ["GDScriptLexer"]
  31. line_re = re.compile(".*?\n")
  32. class GDScriptLexer(RegexLexer):
  33. """
  34. For `GDScript source code <https://www.godotengine.org>`_.
  35. """
  36. name = "GDScript"
  37. aliases = ["gdscript", "gd"]
  38. filenames = ["*.gd"]
  39. mimetypes = ["text/x-gdscript", "application/x-gdscript"]
  40. def innerstring_rules(ttype):
  41. return [
  42. # the old style '%s' % (...) string formatting
  43. (
  44. r"%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?"
  45. "[hlL]?[E-GXc-giorsux%]",
  46. String.Interpol,
  47. ),
  48. # backslashes, quotes and formatting signs must be parsed one at a time
  49. (r'[^\\\'"%\n]+', ttype),
  50. (r'[\'"\\]', ttype),
  51. # unhandled string formatting sign
  52. (r"%", ttype),
  53. # newlines are an error (use "nl" state)
  54. ]
  55. tokens = {
  56. "root": [
  57. (r"\n", Whitespace),
  58. (
  59. r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")',
  60. bygroups(Whitespace, String.Affix, String.Doc),
  61. ),
  62. (
  63. r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')",
  64. bygroups(Whitespace, String.Affix, String.Doc),
  65. ),
  66. (r"[^\S\n]+", Whitespace),
  67. (r"#.*$", Comment.Single),
  68. (r"[]{}:(),;[]", Punctuation),
  69. (r"(\\)(\n)", Whitespace),
  70. (r"\\", Text),
  71. (r"(in|and|or|not)\b", Operator.Word),
  72. (
  73. r"!=|==|<<|>>|&&|\+=|-=|\*=|/=|%=|&=|\|=|\|\||[-~+/*%=<>&^.!|$]",
  74. Operator,
  75. ),
  76. include("keywords"),
  77. include("control_flow_keywords"),
  78. (r"(func)((?:\s|\\\s)+)", bygroups(Keyword, Whitespace), "funcname"),
  79. (r"(class)((?:\s|\\\s)+)", bygroups(Keyword, Whitespace), "classname"),
  80. include("builtins"),
  81. include("decorators"),
  82. (
  83. '([rR]|[uUbB][rR]|[rR][uUbB])(""")',
  84. bygroups(String.Affix, String.Double),
  85. "tdqs",
  86. ),
  87. (
  88. "([rR]|[uUbB][rR]|[rR][uUbB])(''')",
  89. bygroups(String.Affix, String.Single),
  90. "tsqs",
  91. ),
  92. (
  93. '([rR]|[uUbB][rR]|[rR][uUbB])(")',
  94. bygroups(String.Affix, String.Double),
  95. "dqs",
  96. ),
  97. (
  98. "([rR]|[uUbB][rR]|[rR][uUbB])(')",
  99. bygroups(String.Affix, String.Single),
  100. "sqs",
  101. ),
  102. (
  103. '([uUbB]?)(""")',
  104. bygroups(String.Affix, String.Double),
  105. combined("stringescape", "tdqs"),
  106. ),
  107. (
  108. "([uUbB]?)(''')",
  109. bygroups(String.Affix, String.Single),
  110. combined("stringescape", "tsqs"),
  111. ),
  112. (
  113. '([uUbB]?)(")',
  114. bygroups(String.Affix, String.Double),
  115. combined("stringescape", "dqs"),
  116. ),
  117. (
  118. "([uUbB]?)(')",
  119. bygroups(String.Affix, String.Single),
  120. combined("stringescape", "sqs"),
  121. ),
  122. include("name"),
  123. include("numbers"),
  124. ],
  125. "keywords": [
  126. (
  127. words(
  128. (
  129. "and",
  130. "await",
  131. "in",
  132. "get",
  133. "set",
  134. "not",
  135. "or",
  136. "as",
  137. "breakpoint",
  138. "class",
  139. "class_name",
  140. "extends",
  141. "is",
  142. "func",
  143. "signal",
  144. "const",
  145. "enum",
  146. "static",
  147. "var",
  148. "super",
  149. ),
  150. suffix=r"\b",
  151. ),
  152. Keyword,
  153. ),
  154. ],
  155. "control_flow_keywords": [
  156. (
  157. words(
  158. (
  159. "break",
  160. "continue",
  161. "elif",
  162. "else",
  163. "if",
  164. "for",
  165. "match",
  166. "pass",
  167. "return",
  168. "while",
  169. ),
  170. suffix=r"\b",
  171. ),
  172. # Custom control flow class used to give control flow keywords a different color,
  173. # like in the Godot editor.
  174. Keyword.ControlFlow,
  175. ),
  176. ],
  177. "builtins": [
  178. (
  179. words(
  180. (
  181. # doc/classes/@GlobalScope.xml
  182. "abs",
  183. "absf",
  184. "absi",
  185. "acos",
  186. "asin",
  187. "atan",
  188. "atan2",
  189. "bezier_derivative",
  190. "bezier_interpolate",
  191. "bytes_to_var",
  192. "bytes_to_var_with_objects",
  193. "ceil",
  194. "ceilf",
  195. "ceili",
  196. "clamp",
  197. "clampf",
  198. "clampi",
  199. "cos",
  200. "cosh",
  201. "cubic_interpolate",
  202. "cubic_interpolate_angle",
  203. "cubic_interpolate_angle_in_time",
  204. "cubic_interpolate_in_time",
  205. "db_to_linear",
  206. "deg_to_rad",
  207. "ease",
  208. "error_string",
  209. "exp",
  210. "floor",
  211. "floorf",
  212. "floori",
  213. "fmod",
  214. "fposmod",
  215. "hash",
  216. "instance_from_id",
  217. "inverse_lerp",
  218. "is_equal_approx",
  219. "is_finite",
  220. "is_inf",
  221. "is_instance_id_valid",
  222. "is_instance_valid",
  223. "is_nan",
  224. "is_zero_approx",
  225. "lerp",
  226. "lerp_angle",
  227. "lerpf",
  228. "linear_to_db",
  229. "log",
  230. "max",
  231. "maxf",
  232. "maxi",
  233. "min",
  234. "minf",
  235. "mini",
  236. "move_toward",
  237. "nearest_po2",
  238. "pingpong",
  239. "posmod",
  240. "pow",
  241. "print",
  242. "print_rich",
  243. "print_verbose",
  244. "printerr",
  245. "printraw",
  246. "prints",
  247. "printt",
  248. "push_error",
  249. "push_warning",
  250. "rad_to_deg",
  251. "rand_from_seed",
  252. "randf",
  253. "randf_range",
  254. "randfn",
  255. "randi",
  256. "randi_range",
  257. "randomize",
  258. "remap",
  259. "rid_allocate_id",
  260. "rid_from_int64",
  261. "round",
  262. "roundf",
  263. "roundi",
  264. "seed",
  265. "sign",
  266. "signf",
  267. "signi",
  268. "sin",
  269. "sinh",
  270. "smoothstep",
  271. "snapped",
  272. "snappedf",
  273. "snappedi",
  274. "sqrt",
  275. "step_decimals",
  276. "str",
  277. "str_to_var",
  278. "tan",
  279. "tanh",
  280. "typeof",
  281. "var_to_bytes",
  282. "var_to_bytes_with_objects",
  283. "var_to_str",
  284. "weakref",
  285. "wrap",
  286. "wrapf",
  287. "wrapi",
  288. # modules/gdscript/doc_classes/@GDScript.xml
  289. "Color8",
  290. "assert",
  291. "char",
  292. "convert",
  293. "dict_to_inst",
  294. "get_stack",
  295. "inst_to_dict",
  296. "len",
  297. "load",
  298. "preload",
  299. "print_debug",
  300. "print_stack",
  301. "range",
  302. "str",
  303. "type_exists",
  304. ),
  305. prefix=r"(?<!\.)",
  306. suffix=r"\b",
  307. ),
  308. Name.Builtin,
  309. ),
  310. (r"((?<!\.)(self|super|false|true)|(PI|TAU|NAN|INF)" r")\b", Name.Builtin.Pseudo),
  311. (
  312. words(
  313. (
  314. "bool",
  315. "int",
  316. "float",
  317. "String",
  318. "StringName",
  319. "NodePath",
  320. "Vector2",
  321. "Vector2i",
  322. "Rect2",
  323. "Rect2i",
  324. "Transform2D",
  325. "Vector3",
  326. "Vector3i",
  327. "AABB",
  328. "Plane",
  329. "Quaternion",
  330. "Basis",
  331. "Transform3D",
  332. "Color",
  333. "RID",
  334. "Object",
  335. "Dictionary",
  336. "Array",
  337. "PackedByteArray",
  338. "PackedInt32Array",
  339. "PackedInt64Array",
  340. "PackedFloat32Array",
  341. "PackedFloat64Array",
  342. "PackedStringArray",
  343. "PackedVector2Array",
  344. "PackedVector2iArray",
  345. "PackedVector3Array",
  346. "PackedVector3iArray",
  347. "PackedColorArray",
  348. "null",
  349. "void",
  350. ),
  351. prefix=r"(?<!\.)",
  352. suffix=r"\b",
  353. ),
  354. Name.Builtin.Type,
  355. ),
  356. ],
  357. "decorators": [
  358. (
  359. words(
  360. (
  361. "@export",
  362. "@export_category",
  363. "@export_color_no_alpha",
  364. "@export_dir",
  365. "@export_enum",
  366. "@export_exp_easing",
  367. "@export_file",
  368. "@export_flags",
  369. "@export_flags_2d_navigation",
  370. "@export_flags_2d_physics",
  371. "@export_flags_2d_render",
  372. "@export_flags_3d_navigation",
  373. "@export_flags_3d_physics",
  374. "@export_flags_3d_render",
  375. "@export_global_dir",
  376. "@export_global_file",
  377. "@export_group",
  378. "@export_multiline",
  379. "@export_node_path",
  380. "@export_placeholder",
  381. "@export_range",
  382. "@export_subgroup",
  383. "@icon",
  384. "@onready",
  385. "@rpc",
  386. "@tool",
  387. "@warning_ignore",
  388. ),
  389. prefix=r"(?<!\.)",
  390. suffix=r"\b",
  391. ),
  392. Name.Decorator,
  393. ),
  394. ],
  395. "numbers": [
  396. (
  397. r"(-)?((\d|(?<=\d)_)+\.(\d|(?<=\d)_)*|(\d|(?<=\d)_)*\.(\d|(?<=\d)_)+)([eE][+-]?(\d|(?<=\d)_)+)?j?",
  398. Number.Float,
  399. ),
  400. (r"(-)?(\d|(?<=\d)_)+[eE][+-]?(\d|(?<=\d)_)+j?", Number.Float),
  401. (r"(-)?0[xX]([a-fA-F0-9]|(?<=[a-fA-F0-9])_)+", Number.Hex),
  402. (r"(-)?0[bB]([01]|(?<=[01])_)+", Number.Bin),
  403. (r"(-)?(\d|(?<=\d)_)+j?", Number.Integer),
  404. ],
  405. "name": [(r"@?[a-zA-Z_]\w*", Name)],
  406. "funcname": [(r"[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop")],
  407. "classname": [(r"[a-zA-Z_]\w*", Name.Class, "#pop")],
  408. "stringescape": [
  409. (
  410. r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  411. r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})",
  412. String.Escape,
  413. )
  414. ],
  415. "strings-single": innerstring_rules(String.Single),
  416. "strings-double": innerstring_rules(String.Double),
  417. "dqs": [
  418. (r'"', String.Double, "#pop"),
  419. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  420. include("strings-double"),
  421. ],
  422. "sqs": [
  423. (r"'", String.Single, "#pop"),
  424. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  425. include("strings-single"),
  426. ],
  427. "tdqs": [
  428. (r'"""', String.Double, "#pop"),
  429. include("strings-double"),
  430. (r"\n", Whitespace),
  431. ],
  432. "tsqs": [
  433. (r"'''", String.Single, "#pop"),
  434. include("strings-single"),
  435. (r"\n", Whitespace),
  436. ],
  437. }
  438. def setup(sphinx):
  439. sphinx.add_lexer("gdscript", GDScriptLexer)
  440. return {
  441. "parallel_read_safe": True,
  442. "parallel_write_safe": True,
  443. }