gdscript.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. "when",
  169. "while",
  170. ),
  171. suffix=r"\b",
  172. ),
  173. # Custom control flow class used to give control flow keywords a different color,
  174. # like in the Godot editor.
  175. Keyword.ControlFlow,
  176. ),
  177. ],
  178. "builtins": [
  179. (
  180. words(
  181. (
  182. # doc/classes/@GlobalScope.xml
  183. "abs",
  184. "absf",
  185. "absi",
  186. "acos",
  187. "asin",
  188. "atan",
  189. "atan2",
  190. "bezier_derivative",
  191. "bezier_interpolate",
  192. "bytes_to_var",
  193. "bytes_to_var_with_objects",
  194. "ceil",
  195. "ceilf",
  196. "ceili",
  197. "clamp",
  198. "clampf",
  199. "clampi",
  200. "cos",
  201. "cosh",
  202. "cubic_interpolate",
  203. "cubic_interpolate_angle",
  204. "cubic_interpolate_angle_in_time",
  205. "cubic_interpolate_in_time",
  206. "db_to_linear",
  207. "deg_to_rad",
  208. "ease",
  209. "error_string",
  210. "exp",
  211. "floor",
  212. "floorf",
  213. "floori",
  214. "fmod",
  215. "fposmod",
  216. "hash",
  217. "instance_from_id",
  218. "inverse_lerp",
  219. "is_equal_approx",
  220. "is_finite",
  221. "is_inf",
  222. "is_instance_id_valid",
  223. "is_instance_valid",
  224. "is_nan",
  225. "is_zero_approx",
  226. "lerp",
  227. "lerp_angle",
  228. "lerpf",
  229. "linear_to_db",
  230. "log",
  231. "max",
  232. "maxf",
  233. "maxi",
  234. "min",
  235. "minf",
  236. "mini",
  237. "move_toward",
  238. "nearest_po2",
  239. "pingpong",
  240. "posmod",
  241. "pow",
  242. "print",
  243. "print_rich",
  244. "print_verbose",
  245. "printerr",
  246. "printraw",
  247. "prints",
  248. "printt",
  249. "push_error",
  250. "push_warning",
  251. "rad_to_deg",
  252. "rand_from_seed",
  253. "randf",
  254. "randf_range",
  255. "randfn",
  256. "randi",
  257. "randi_range",
  258. "randomize",
  259. "remap",
  260. "rid_allocate_id",
  261. "rid_from_int64",
  262. "round",
  263. "roundf",
  264. "roundi",
  265. "seed",
  266. "sign",
  267. "signf",
  268. "signi",
  269. "sin",
  270. "sinh",
  271. "smoothstep",
  272. "snapped",
  273. "snappedf",
  274. "snappedi",
  275. "sqrt",
  276. "step_decimals",
  277. "str",
  278. "str_to_var",
  279. "tan",
  280. "tanh",
  281. "typeof",
  282. "var_to_bytes",
  283. "var_to_bytes_with_objects",
  284. "var_to_str",
  285. "weakref",
  286. "wrap",
  287. "wrapf",
  288. "wrapi",
  289. # modules/gdscript/doc_classes/@GDScript.xml
  290. "Color8",
  291. "assert",
  292. "char",
  293. "convert",
  294. "dict_to_inst",
  295. "get_stack",
  296. "inst_to_dict",
  297. "len",
  298. "load",
  299. "preload",
  300. "print_debug",
  301. "print_stack",
  302. "range",
  303. "str",
  304. "type_exists",
  305. ),
  306. prefix=r"(?<!\.)",
  307. suffix=r"\b",
  308. ),
  309. Name.Builtin,
  310. ),
  311. (r"((?<!\.)(self|super|false|true)|(PI|TAU|NAN|INF)" r")\b", Name.Builtin.Pseudo),
  312. (
  313. words(
  314. (
  315. "bool",
  316. "int",
  317. "float",
  318. "String",
  319. "StringName",
  320. "NodePath",
  321. "Vector2",
  322. "Vector2i",
  323. "Rect2",
  324. "Rect2i",
  325. "Transform2D",
  326. "Vector3",
  327. "Vector3i",
  328. "AABB",
  329. "Plane",
  330. "Quaternion",
  331. "Vector4",
  332. "Vector4i",
  333. "Basis",
  334. "Transform3D",
  335. "Color",
  336. "RID",
  337. "Object",
  338. "Dictionary",
  339. "Array",
  340. "PackedByteArray",
  341. "PackedInt32Array",
  342. "PackedInt64Array",
  343. "PackedFloat32Array",
  344. "PackedFloat64Array",
  345. "PackedStringArray",
  346. "PackedVector2Array",
  347. "PackedVector3Array",
  348. "PackedVector4Array",
  349. "PackedColorArray",
  350. "null",
  351. "void",
  352. ),
  353. prefix=r"(?<!\.)",
  354. suffix=r"\b",
  355. ),
  356. Name.Builtin.Type,
  357. ),
  358. ],
  359. "decorators": [
  360. (
  361. words(
  362. (
  363. "@export",
  364. "@export_category",
  365. "@export_color_no_alpha",
  366. "@export_dir",
  367. "@export_enum",
  368. "@export_exp_easing",
  369. "@export_file",
  370. "@export_flags",
  371. "@export_flags_2d_navigation",
  372. "@export_flags_2d_physics",
  373. "@export_flags_2d_render",
  374. "@export_flags_3d_navigation",
  375. "@export_flags_3d_physics",
  376. "@export_flags_3d_render",
  377. "@export_global_dir",
  378. "@export_global_file",
  379. "@export_group",
  380. "@export_multiline",
  381. "@export_node_path",
  382. "@export_placeholder",
  383. "@export_range",
  384. "@export_subgroup",
  385. "@icon",
  386. "@onready",
  387. "@rpc",
  388. "@tool",
  389. "@warning_ignore",
  390. ),
  391. prefix=r"(?<!\.)",
  392. suffix=r"\b",
  393. ),
  394. Name.Decorator,
  395. ),
  396. ],
  397. "numbers": [
  398. (
  399. r"(-)?((\d|(?<=\d)_)+\.(\d|(?<=\d)_)*|(\d|(?<=\d)_)*\.(\d|(?<=\d)_)+)([eE][+-]?(\d|(?<=\d)_)+)?j?",
  400. Number.Float,
  401. ),
  402. (r"(-)?(\d|(?<=\d)_)+[eE][+-]?(\d|(?<=\d)_)+j?", Number.Float),
  403. (r"(-)?0[xX]([a-fA-F0-9]|(?<=[a-fA-F0-9])_)+", Number.Hex),
  404. (r"(-)?0[bB]([01]|(?<=[01])_)+", Number.Bin),
  405. (r"(-)?(\d|(?<=\d)_)+j?", Number.Integer),
  406. ],
  407. "name": [(r"@?[a-zA-Z_]\w*", Name)],
  408. "funcname": [(r"[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop")],
  409. "classname": [(r"[a-zA-Z_]\w*", Name.Class, "#pop")],
  410. "stringescape": [
  411. (
  412. r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  413. r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})",
  414. String.Escape,
  415. )
  416. ],
  417. "strings-single": innerstring_rules(String.Single),
  418. "strings-double": innerstring_rules(String.Double),
  419. "dqs": [
  420. (r'"', String.Double, "#pop"),
  421. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  422. include("strings-double"),
  423. ],
  424. "sqs": [
  425. (r"'", String.Single, "#pop"),
  426. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  427. include("strings-single"),
  428. ],
  429. "tdqs": [
  430. (r'"""', String.Double, "#pop"),
  431. include("strings-double"),
  432. (r"\n", Whitespace),
  433. ],
  434. "tsqs": [
  435. (r"'''", String.Single, "#pop"),
  436. include("strings-single"),
  437. (r"\n", Whitespace),
  438. ],
  439. }
  440. def setup(sphinx):
  441. sphinx.add_lexer("gdscript", GDScriptLexer)
  442. return {
  443. "parallel_read_safe": True,
  444. "parallel_write_safe": True,
  445. }