gdscript.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. 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. "do",
  135. "var",
  136. "const",
  137. "extends",
  138. "is",
  139. "export",
  140. "onready",
  141. "tool",
  142. "static",
  143. "setget",
  144. "signal",
  145. "breakpoint",
  146. "switch",
  147. "case",
  148. "assert",
  149. "break",
  150. "continue",
  151. "elif",
  152. "else",
  153. "for",
  154. "if",
  155. "pass",
  156. "return",
  157. "while",
  158. "match",
  159. "master",
  160. "sync",
  161. "slave",
  162. "rpc",
  163. "enum",
  164. ),
  165. suffix=r"\b",
  166. ),
  167. Keyword,
  168. ),
  169. ],
  170. "builtins": [
  171. (
  172. words(
  173. (
  174. "Color8",
  175. "ColorN",
  176. "abs",
  177. "acos",
  178. "asin",
  179. "assert",
  180. "atan",
  181. "atan2",
  182. "bytes2var",
  183. "ceil",
  184. "char",
  185. "clamp",
  186. "convert",
  187. "cos",
  188. "cosh",
  189. "db2linear",
  190. "decimals",
  191. "dectime",
  192. "deg2rad",
  193. "dict2inst",
  194. "ease",
  195. "exp",
  196. "floor",
  197. "fmod",
  198. "fposmod",
  199. "funcref",
  200. "hash",
  201. "inst2dict",
  202. "instance_from_id",
  203. "is_inf",
  204. "is_nan",
  205. "lerp",
  206. "linear2db",
  207. "load",
  208. "log",
  209. "max",
  210. "min",
  211. "nearest_po2",
  212. "pow",
  213. "preload",
  214. "print",
  215. "print_stack",
  216. "printerr",
  217. "printraw",
  218. "prints",
  219. "printt",
  220. "rad2deg",
  221. "rand_range",
  222. "rand_seed",
  223. "randf",
  224. "randi",
  225. "randomize",
  226. "range",
  227. "round",
  228. "seed",
  229. "sign",
  230. "sin",
  231. "sinh",
  232. "sqrt",
  233. "stepify",
  234. "str",
  235. "str2var",
  236. "tan",
  237. "tan",
  238. "tanh",
  239. "type_exist",
  240. "typeof",
  241. "var2bytes",
  242. "var2str",
  243. "weakref",
  244. "yield",
  245. ),
  246. prefix=r"(?<!\.)",
  247. suffix=r"\b",
  248. ),
  249. Name.Builtin,
  250. ),
  251. (r"((?<!\.)(self|false|true)|(PI|NAN|INF)" r")\b", Name.Builtin.Pseudo),
  252. (
  253. words(
  254. (
  255. "bool",
  256. "int",
  257. "float",
  258. "String",
  259. "NodePath" "Vector2",
  260. "Rect2",
  261. "Transform2D",
  262. "Vector3",
  263. "Rect3",
  264. "Plane",
  265. "Quat",
  266. "Basis",
  267. "Transform",
  268. "Color",
  269. "RID",
  270. "Object",
  271. "NodePath",
  272. "Dictionary",
  273. "Array",
  274. "PoolByteArray",
  275. "PoolIntArray",
  276. "PoolRealArray",
  277. "PoolStringArray",
  278. "PoolVector2Array",
  279. "PoolVector3Array",
  280. "PoolColorArray",
  281. "null",
  282. ),
  283. prefix=r"(?<!\.)",
  284. suffix=r"\b",
  285. ),
  286. Name.Builtin.Type,
  287. ),
  288. ],
  289. "numbers": [
  290. (r"(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?", Number.Float),
  291. (r"\d+[eE][+-]?[0-9]+j?", Number.Float),
  292. (r"0[xX][a-fA-F0-9]+", Number.Hex),
  293. (r"\d+j?", Number.Integer),
  294. ],
  295. "name": [("[a-zA-Z_]\w*", Name),],
  296. "funcname": [("[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop"),],
  297. "classname": [("[a-zA-Z_]\w*", Name.Class, "#pop")],
  298. "stringescape": [
  299. (
  300. r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  301. r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})",
  302. String.Escape,
  303. )
  304. ],
  305. "strings-single": innerstring_rules(String.Single),
  306. "strings-double": innerstring_rules(String.Double),
  307. "dqs": [
  308. (r'"', String.Double, "#pop"),
  309. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  310. include("strings-double"),
  311. ],
  312. "sqs": [
  313. (r"'", String.Single, "#pop"),
  314. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  315. include("strings-single"),
  316. ],
  317. "tdqs": [
  318. (r'"""', String.Double, "#pop"),
  319. include("strings-double"),
  320. (r"\n", String.Double),
  321. ],
  322. "tsqs": [
  323. (r"'''", String.Single, "#pop"),
  324. include("strings-single"),
  325. (r"\n", String.Single),
  326. ],
  327. }
  328. def setup(sphinx):
  329. sphinx.add_lexer("gdscript", GDScriptLexer())