gdscript.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. (r"!=|==|<<|>>|&&|\+=|-=|\*=|/=|%=|&=|\|=|\|\||[-~+/*%=<>&^.!|]", Operator),
  80. include("keywords"),
  81. (r"(func)((?:\s|\\\s)+)", bygroups(Keyword, Text), "funcname"),
  82. (r"(class)((?:\s|\\\s)+)", bygroups(Keyword, Text), "classname"),
  83. include("builtins"),
  84. (
  85. '([rR]|[uUbB][rR]|[rR][uUbB])(""")',
  86. bygroups(String.Affix, String.Double),
  87. "tdqs",
  88. ),
  89. (
  90. "([rR]|[uUbB][rR]|[rR][uUbB])(''')",
  91. bygroups(String.Affix, String.Single),
  92. "tsqs",
  93. ),
  94. (
  95. '([rR]|[uUbB][rR]|[rR][uUbB])(")',
  96. bygroups(String.Affix, String.Double),
  97. "dqs",
  98. ),
  99. (
  100. "([rR]|[uUbB][rR]|[rR][uUbB])(')",
  101. bygroups(String.Affix, String.Single),
  102. "sqs",
  103. ),
  104. (
  105. '([uUbB]?)(""")',
  106. bygroups(String.Affix, String.Double),
  107. combined("stringescape", "tdqs"),
  108. ),
  109. (
  110. "([uUbB]?)(''')",
  111. bygroups(String.Affix, String.Single),
  112. combined("stringescape", "tsqs"),
  113. ),
  114. (
  115. '([uUbB]?)(")',
  116. bygroups(String.Affix, String.Double),
  117. combined("stringescape", "dqs"),
  118. ),
  119. (
  120. "([uUbB]?)(')",
  121. bygroups(String.Affix, String.Single),
  122. combined("stringescape", "sqs"),
  123. ),
  124. include("name"),
  125. include("numbers"),
  126. ],
  127. "keywords": [
  128. (
  129. words(
  130. (
  131. "do",
  132. "var",
  133. "const",
  134. "extends",
  135. "export",
  136. "onready",
  137. "tool",
  138. "static",
  139. "setget",
  140. "signal",
  141. "breakpoint",
  142. "switch",
  143. "case",
  144. "assert",
  145. "break",
  146. "continue",
  147. "elif",
  148. "else",
  149. "for",
  150. "if",
  151. "pass",
  152. "return",
  153. "while",
  154. ),
  155. suffix=r"\b",
  156. ),
  157. Keyword,
  158. ),
  159. ],
  160. "builtins": [
  161. (
  162. words(
  163. (
  164. "Color8",
  165. "abs",
  166. "acos",
  167. "asin",
  168. "assert",
  169. "atan",
  170. "atan2",
  171. "bytes2var",
  172. "ceil",
  173. "clamp",
  174. "convert",
  175. "cos",
  176. "cosh",
  177. "db2linear",
  178. "decimals",
  179. "dectime",
  180. "deg2rad",
  181. "dict2inst",
  182. "ease",
  183. "exp",
  184. "floor",
  185. "fmod",
  186. "fposmod",
  187. "funcref",
  188. "hash",
  189. "inst2dict",
  190. "instance_from_id",
  191. "is_inf",
  192. "is_nan",
  193. "lerp",
  194. "linear2db",
  195. "load",
  196. "log",
  197. "max",
  198. "min",
  199. "nearest_po2",
  200. "pow",
  201. "preload",
  202. "print",
  203. "print_stack",
  204. "printerr",
  205. "printraw",
  206. "prints",
  207. "printt",
  208. "rad2deg",
  209. "rand_range",
  210. "rand_seed",
  211. "randf",
  212. "randi",
  213. "randomize",
  214. "range",
  215. "round",
  216. "seed",
  217. "sign",
  218. "sin",
  219. "sinh",
  220. "sqrt",
  221. "stepify",
  222. "str",
  223. "str2var",
  224. "tan",
  225. "tan",
  226. "tanh",
  227. "type_exist",
  228. "typeof",
  229. "var2bytes",
  230. "var2str",
  231. "weakref",
  232. "yield",
  233. ),
  234. prefix=r"(?<!\.)",
  235. suffix=r"\b",
  236. ),
  237. Name.Builtin,
  238. ),
  239. (r"(?<!\.)(self|false|true" r")\b", Name.Builtin.Pseudo),
  240. (
  241. words(
  242. (
  243. "null",
  244. "bool",
  245. "int",
  246. "float",
  247. "String",
  248. "Vector2",
  249. "Vector3",
  250. "Matrix32",
  251. "Array",
  252. "ByteArray",
  253. "IntArray",
  254. "FloatArray",
  255. "StringArray",
  256. "Vector2Array",
  257. "Vector3Array",
  258. "ColorArray",
  259. "Plane",
  260. "Quat",
  261. "AABB",
  262. "Matrix3",
  263. "Transform",
  264. "Color",
  265. "Image",
  266. "NodePath",
  267. "RID",
  268. "Object",
  269. "InputEvent",
  270. "Rect2",
  271. ),
  272. prefix=r"(?<!\.)",
  273. suffix=r"\b",
  274. ),
  275. Name.Builtin.Type,
  276. ),
  277. ],
  278. "numbers": [
  279. (r"(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?", Number.Float),
  280. (r"\d+[eE][+-]?[0-9]+j?", Number.Float),
  281. (r"0[xX][a-fA-F0-9]+", Number.Hex),
  282. (r"\d+j?", Number.Integer),
  283. ],
  284. "name": [("[a-zA-Z_]\w*", Name),],
  285. "funcname": [("[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop"),],
  286. "classname": [("[a-zA-Z_]\w*", Name.Class, "#pop")],
  287. "stringescape": [
  288. (
  289. r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  290. r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})",
  291. String.Escape,
  292. )
  293. ],
  294. "strings-single": innerstring_rules(String.Single),
  295. "strings-double": innerstring_rules(String.Double),
  296. "dqs": [
  297. (r'"', String.Double, "#pop"),
  298. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  299. include("strings-double"),
  300. ],
  301. "sqs": [
  302. (r"'", String.Single, "#pop"),
  303. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  304. include("strings-single"),
  305. ],
  306. "tdqs": [
  307. (r'"""', String.Double, "#pop"),
  308. include("strings-double"),
  309. (r"\n", String.Double),
  310. ],
  311. "tsqs": [
  312. (r"'''", String.Single, "#pop"),
  313. include("strings-single"),
  314. (r"\n", String.Single),
  315. ],
  316. }