gdscript.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. (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. "bytes2var",
  176. "bytes2var_with_objects",
  177. "ceil",
  178. "clamp",
  179. "clampf",
  180. "clampi",
  181. "cos",
  182. "cosh",
  183. "cubic_interpolate",
  184. "db2linear",
  185. "deg2rad",
  186. "ease",
  187. "error_string",
  188. "exp",
  189. "floor",
  190. "fmod",
  191. "fposmod",
  192. "hash",
  193. "instance_from_id",
  194. "inverse_lerp",
  195. "is_equal_approx",
  196. "is_inf",
  197. "is_instance_id_valid",
  198. "is_instance_valid",
  199. "is_nan",
  200. "is_zero_approx",
  201. "lerp",
  202. "lerp_angle",
  203. "linear2db",
  204. "log",
  205. "max",
  206. "maxf",
  207. "maxi",
  208. "min",
  209. "minf",
  210. "mini",
  211. "move_toward",
  212. "nearest_po2",
  213. "pingpong",
  214. "posmod",
  215. "pow",
  216. "print",
  217. "print_verbose",
  218. "printerr",
  219. "printraw",
  220. "prints",
  221. "printt",
  222. "push_error",
  223. "push_warning",
  224. "rad2deg",
  225. "rand_from_seed",
  226. "randf",
  227. "randf_range",
  228. "randfn",
  229. "randi",
  230. "randi_range",
  231. "randomize",
  232. "range_lerp",
  233. "range_step_decimals",
  234. "rid_allocate_id",
  235. "rid_from_int64",
  236. "round",
  237. "seed",
  238. "sign",
  239. "signf",
  240. "signi",
  241. "sin",
  242. "sinh",
  243. "smoothstep",
  244. "snapped",
  245. "sqrt",
  246. "step_decimals",
  247. "str",
  248. "str2var",
  249. "tan",
  250. "tanh",
  251. "typeof",
  252. "var2bytes",
  253. "var2bytes_with_objects",
  254. "var2str",
  255. "weakref",
  256. "wrapf",
  257. "wrapi",
  258. # modules/gdscript/doc_classes/@GDScript.xml
  259. "Color8",
  260. "assert",
  261. "char",
  262. "convert",
  263. "dict2inst",
  264. "get_stack",
  265. "inst2dict",
  266. "len",
  267. "load",
  268. "preload",
  269. "print_debug",
  270. "print_stack",
  271. "range",
  272. "str",
  273. "type_exists",
  274. ),
  275. prefix=r"(?<!\.)",
  276. suffix=r"\b",
  277. ),
  278. Name.Builtin,
  279. ),
  280. (r"((?<!\.)(self|super|false|true)|(PI|TAU|NAN|INF)" r")\b", Name.Builtin.Pseudo),
  281. (
  282. words(
  283. (
  284. "bool",
  285. "int",
  286. "float",
  287. "String",
  288. "StringName",
  289. "NodePath",
  290. "Vector2",
  291. "Vector2i",
  292. "Rect2",
  293. "Rect2i",
  294. "Transform2D",
  295. "Vector3",
  296. "Vector3i",
  297. "AABB",
  298. "Plane",
  299. "Quaternion",
  300. "Basis",
  301. "Transform3D",
  302. "Color",
  303. "RID",
  304. "Object",
  305. "NodePath",
  306. "Dictionary",
  307. "Array",
  308. "PackedByteArray",
  309. "PackedInt32Array",
  310. "PackedInt64Array",
  311. "PackedFloat32Array",
  312. "PackedFloat64Array",
  313. "PackedStringArray",
  314. "PackedVector2Array",
  315. "PackedVector2iArray",
  316. "PackedVector3Array",
  317. "PackedVector3iArray",
  318. "PackedColorArray",
  319. "null",
  320. ),
  321. prefix=r"(?<!\.)",
  322. suffix=r"\b",
  323. ),
  324. Name.Builtin.Type,
  325. ),
  326. ],
  327. "decorators": [
  328. (
  329. words(
  330. (
  331. "@export",
  332. "@export_color_no_alpha",
  333. "@export_dir",
  334. "@export_enum",
  335. "@export_exp_easing",
  336. "@export_file",
  337. "@export_flags",
  338. "@export_flags_2d_navigation",
  339. "@export_flags_2d_physics",
  340. "@export_flags_2d_render",
  341. "@export_flags_3d_navigation",
  342. "@export_flags_3d_physics",
  343. "@export_flags_3d_render",
  344. "@export_global_dir",
  345. "@export_global_file",
  346. "@export_multiline",
  347. "@export_node_path",
  348. "@export_placeholder",
  349. "@export_range",
  350. "@icon",
  351. "@onready",
  352. "@rpc",
  353. "@tool",
  354. "@warning_ignore",
  355. ),
  356. prefix=r"(?<!\.)",
  357. suffix=r"\b",
  358. ),
  359. Name.Decorator,
  360. ),
  361. ],
  362. "numbers": [
  363. (r"(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?", Number.Float),
  364. (r"\d+[eE][+-]?[0-9]+j?", Number.Float),
  365. (r"0x[a-fA-F0-9]+", Number.Hex),
  366. (r"0b[01]+", Number.Bin),
  367. (r"\d+j?", Number.Integer),
  368. ],
  369. "name": [(r"@?[a-zA-Z_]\w*", Name)],
  370. "funcname": [(r"[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop")],
  371. "classname": [(r"[a-zA-Z_]\w*", Name.Class, "#pop")],
  372. "stringescape": [
  373. (
  374. r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  375. r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})",
  376. String.Escape,
  377. )
  378. ],
  379. "strings-single": innerstring_rules(String.Single),
  380. "strings-double": innerstring_rules(String.Double),
  381. "dqs": [
  382. (r'"', String.Double, "#pop"),
  383. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  384. include("strings-double"),
  385. ],
  386. "sqs": [
  387. (r"'", String.Single, "#pop"),
  388. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  389. include("strings-single"),
  390. ],
  391. "tdqs": [
  392. (r'"""', String.Double, "#pop"),
  393. include("strings-double"),
  394. (r"\n", String.Double),
  395. ],
  396. "tsqs": [
  397. (r"'''", String.Single, "#pop"),
  398. include("strings-single"),
  399. (r"\n", String.Single),
  400. ],
  401. }
  402. def setup(sphinx):
  403. sphinx.add_lexer("gdscript", GDScriptLexer)
  404. return {
  405. "parallel_read_safe": True,
  406. "parallel_write_safe": True,
  407. }