gdscript.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. "when",
  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. ),
  350. prefix=r"(?<!\.)",
  351. suffix=r"\b",
  352. ),
  353. Name.Builtin.Type,
  354. ),
  355. ],
  356. "decorators": [
  357. (
  358. words(
  359. (
  360. "@export",
  361. "@export_category",
  362. "@export_color_no_alpha",
  363. "@export_dir",
  364. "@export_enum",
  365. "@export_exp_easing",
  366. "@export_file",
  367. "@export_flags",
  368. "@export_flags_2d_navigation",
  369. "@export_flags_2d_physics",
  370. "@export_flags_2d_render",
  371. "@export_flags_3d_navigation",
  372. "@export_flags_3d_physics",
  373. "@export_flags_3d_render",
  374. "@export_global_dir",
  375. "@export_global_file",
  376. "@export_group",
  377. "@export_multiline",
  378. "@export_node_path",
  379. "@export_placeholder",
  380. "@export_range",
  381. "@export_subgroup",
  382. "@icon",
  383. "@onready",
  384. "@rpc",
  385. "@tool",
  386. "@warning_ignore",
  387. ),
  388. prefix=r"(?<!\.)",
  389. suffix=r"\b",
  390. ),
  391. Name.Decorator,
  392. ),
  393. ],
  394. "numbers": [
  395. (r"(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?", Number.Float),
  396. (r"\d+[eE][+-]?[0-9]+j?", Number.Float),
  397. (r"0x[a-fA-F0-9]+", Number.Hex),
  398. (r"0b[01]+", Number.Bin),
  399. (r"\d+j?", Number.Integer),
  400. ],
  401. "name": [(r"@?[a-zA-Z_]\w*", Name)],
  402. "funcname": [(r"[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop")],
  403. "classname": [(r"[a-zA-Z_]\w*", Name.Class, "#pop")],
  404. "stringescape": [
  405. (
  406. r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  407. r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})",
  408. String.Escape,
  409. )
  410. ],
  411. "strings-single": innerstring_rules(String.Single),
  412. "strings-double": innerstring_rules(String.Double),
  413. "dqs": [
  414. (r'"', String.Double, "#pop"),
  415. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  416. include("strings-double"),
  417. ],
  418. "sqs": [
  419. (r"'", String.Single, "#pop"),
  420. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  421. include("strings-single"),
  422. ],
  423. "tdqs": [
  424. (r'"""', String.Double, "#pop"),
  425. include("strings-double"),
  426. (r"\n", String.Double),
  427. ],
  428. "tsqs": [
  429. (r"'''", String.Single, "#pop"),
  430. include("strings-single"),
  431. (r"\n", String.Single),
  432. ],
  433. }
  434. def setup(sphinx):
  435. sphinx.add_lexer("gdscript", GDScriptLexer)
  436. return {
  437. "parallel_read_safe": True,
  438. "parallel_write_safe": True,
  439. }