gdscript.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. Lexer,
  13. RegexLexer,
  14. include,
  15. bygroups,
  16. using,
  17. default,
  18. words,
  19. combined,
  20. do_insertions,
  21. )
  22. from pygments.util import get_bool_opt, shebang_matches
  23. from pygments.token import (
  24. Text,
  25. Comment,
  26. Operator,
  27. Keyword,
  28. Name,
  29. String,
  30. Number,
  31. Punctuation,
  32. Generic,
  33. Other,
  34. Error,
  35. )
  36. from pygments import unistring as uni
  37. __all__ = ["GDScriptLexer"]
  38. line_re = re.compile(".*?\n")
  39. class GDScriptLexer(RegexLexer):
  40. """
  41. For `Godot source code <https://www.godotengine.org>`_ source code.
  42. """
  43. name = "GDScript"
  44. aliases = ["gdscript", "gd"]
  45. filenames = ["*.gd"]
  46. mimetypes = ["text/x-gdscript", "application/x-gdscript"]
  47. def innerstring_rules(ttype):
  48. return [
  49. # the old style '%s' % (...) string formatting
  50. (
  51. r"%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?"
  52. "[hlL]?[E-GXc-giorsux%]",
  53. String.Interpol,
  54. ),
  55. # backslashes, quotes and formatting signs must be parsed one at a time
  56. (r'[^\\\'"%\n]+', ttype),
  57. (r'[\'"\\]', ttype),
  58. # unhandled string formatting sign
  59. (r"%", ttype),
  60. # newlines are an error (use "nl" state)
  61. ]
  62. tokens = {
  63. "root": [
  64. (r"\n", Text),
  65. (
  66. r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")',
  67. bygroups(Text, String.Affix, String.Doc),
  68. ),
  69. (
  70. r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')",
  71. bygroups(Text, String.Affix, String.Doc),
  72. ),
  73. (r"[^\S\n]+", Text),
  74. (r"#.*$", Comment.Single),
  75. (r"[]{}:(),;[]", Punctuation),
  76. (r"\\\n", Text),
  77. (r"\\", Text),
  78. (r"(in|and|or|not)\b", Operator.Word),
  79. (
  80. r"!=|==|<<|>>|&&|\+=|-=|\*=|/=|%=|&=|\|=|\|\||[-~+/*%=<>&^.!|$]",
  81. Operator,
  82. ),
  83. include("keywords"),
  84. (r"(func)((?:\s|\\\s)+)", bygroups(Keyword, Text), "funcname"),
  85. (r"(class)((?:\s|\\\s)+)", bygroups(Keyword, Text), "classname"),
  86. include("builtins"),
  87. (
  88. '([rR]|[uUbB][rR]|[rR][uUbB])(""")',
  89. bygroups(String.Affix, String.Double),
  90. "tdqs",
  91. ),
  92. (
  93. "([rR]|[uUbB][rR]|[rR][uUbB])(''')",
  94. bygroups(String.Affix, String.Single),
  95. "tsqs",
  96. ),
  97. (
  98. '([rR]|[uUbB][rR]|[rR][uUbB])(")',
  99. bygroups(String.Affix, String.Double),
  100. "dqs",
  101. ),
  102. (
  103. "([rR]|[uUbB][rR]|[rR][uUbB])(')",
  104. bygroups(String.Affix, String.Single),
  105. "sqs",
  106. ),
  107. (
  108. '([uUbB]?)(""")',
  109. bygroups(String.Affix, String.Double),
  110. combined("stringescape", "tdqs"),
  111. ),
  112. (
  113. "([uUbB]?)(''')",
  114. bygroups(String.Affix, String.Single),
  115. combined("stringescape", "tsqs"),
  116. ),
  117. (
  118. '([uUbB]?)(")',
  119. bygroups(String.Affix, String.Double),
  120. combined("stringescape", "dqs"),
  121. ),
  122. (
  123. "([uUbB]?)(')",
  124. bygroups(String.Affix, String.Single),
  125. combined("stringescape", "sqs"),
  126. ),
  127. include("name"),
  128. include("numbers"),
  129. ],
  130. "keywords": [
  131. (
  132. words(
  133. (
  134. "and",
  135. "in",
  136. "not",
  137. "or",
  138. "as",
  139. "breakpoint",
  140. "class",
  141. "class_name",
  142. "extends",
  143. "is",
  144. "func",
  145. "setget",
  146. "signal",
  147. "tool",
  148. "const",
  149. "enum",
  150. "export",
  151. "onready",
  152. "static",
  153. "var",
  154. "break",
  155. "continue",
  156. "if",
  157. "elif",
  158. "else",
  159. "for",
  160. "pass",
  161. "return",
  162. "match",
  163. "while",
  164. "remote",
  165. "master",
  166. "puppet",
  167. "remotesync",
  168. "mastersync",
  169. "puppetsync",
  170. ),
  171. suffix=r"\b",
  172. ),
  173. Keyword,
  174. ),
  175. ],
  176. "builtins": [
  177. (
  178. words(
  179. (
  180. "Color8",
  181. "ColorN",
  182. "abs",
  183. "acos",
  184. "asin",
  185. "assert",
  186. "atan",
  187. "atan2",
  188. "bytes2var",
  189. "ceil",
  190. "char",
  191. "clamp",
  192. "convert",
  193. "cos",
  194. "cosh",
  195. "db2linear",
  196. "decimals",
  197. "dectime",
  198. "deg2rad",
  199. "dict2inst",
  200. "ease",
  201. "exp",
  202. "floor",
  203. "fmod",
  204. "fposmod",
  205. "funcref",
  206. "hash",
  207. "inst2dict",
  208. "instance_from_id",
  209. "is_inf",
  210. "is_nan",
  211. "lerp",
  212. "linear2db",
  213. "load",
  214. "log",
  215. "max",
  216. "min",
  217. "nearest_po2",
  218. "pow",
  219. "preload",
  220. "print",
  221. "print_stack",
  222. "printerr",
  223. "printraw",
  224. "prints",
  225. "printt",
  226. "rad2deg",
  227. "rand_range",
  228. "rand_seed",
  229. "randf",
  230. "randi",
  231. "randomize",
  232. "range",
  233. "round",
  234. "seed",
  235. "sign",
  236. "sin",
  237. "sinh",
  238. "sqrt",
  239. "stepify",
  240. "str",
  241. "str2var",
  242. "tan",
  243. "tan",
  244. "tanh",
  245. "type_exist",
  246. "typeof",
  247. "var2bytes",
  248. "var2str",
  249. "weakref",
  250. "yield",
  251. ),
  252. prefix=r"(?<!\.)",
  253. suffix=r"\b",
  254. ),
  255. Name.Builtin,
  256. ),
  257. (r"((?<!\.)(self|false|true)|(PI|TAU|NAN|INF)" r")\b", Name.Builtin.Pseudo),
  258. (
  259. words(
  260. (
  261. "bool",
  262. "int",
  263. "float",
  264. "String",
  265. "NodePath" "Vector2",
  266. "Rect2",
  267. "Transform2D",
  268. "Vector3",
  269. "Rect3",
  270. "Plane",
  271. "Quat",
  272. "Basis",
  273. "Transform",
  274. "Color",
  275. "RID",
  276. "Object",
  277. "NodePath",
  278. "Dictionary",
  279. "Array",
  280. "PoolByteArray",
  281. "PoolIntArray",
  282. "PoolRealArray",
  283. "PoolStringArray",
  284. "PoolVector2Array",
  285. "PoolVector3Array",
  286. "PoolColorArray",
  287. "null",
  288. ),
  289. prefix=r"(?<!\.)",
  290. suffix=r"\b",
  291. ),
  292. Name.Builtin.Type,
  293. ),
  294. ],
  295. "numbers": [
  296. (r"(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?", Number.Float),
  297. (r"\d+[eE][+-]?[0-9]+j?", Number.Float),
  298. (r"0[xX][a-fA-F0-9]+", Number.Hex),
  299. (r"\d+j?", Number.Integer),
  300. ],
  301. "name": [("[a-zA-Z_]\w*", Name),],
  302. "funcname": [("[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop"),],
  303. "classname": [("[a-zA-Z_]\w*", Name.Class, "#pop")],
  304. "stringescape": [
  305. (
  306. r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  307. r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})",
  308. String.Escape,
  309. )
  310. ],
  311. "strings-single": innerstring_rules(String.Single),
  312. "strings-double": innerstring_rules(String.Double),
  313. "dqs": [
  314. (r'"', String.Double, "#pop"),
  315. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  316. include("strings-double"),
  317. ],
  318. "sqs": [
  319. (r"'", String.Single, "#pop"),
  320. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  321. include("strings-single"),
  322. ],
  323. "tdqs": [
  324. (r'"""', String.Double, "#pop"),
  325. include("strings-double"),
  326. (r"\n", String.Double),
  327. ],
  328. "tsqs": [
  329. (r"'''", String.Single, "#pop"),
  330. include("strings-single"),
  331. (r"\n", String.Single),
  332. ],
  333. }
  334. def setup(sphinx):
  335. sphinx.add_lexer("gdscript", GDScriptLexer())