gdscript.py 15 KB

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