gdscript.py 14 KB

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