kuin.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. """
  2. pygments.lexers.kuin
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the Kuin language.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, include, using, this, bygroups, words
  9. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation, Whitespace
  11. __all__ = ['KuinLexer']
  12. class KuinLexer(RegexLexer):
  13. """
  14. For Kuin source code.
  15. """
  16. name = 'Kuin'
  17. url = 'https://github.com/kuina/Kuin'
  18. aliases = ['kuin']
  19. filenames = ['*.kn']
  20. version_added = '2.9'
  21. tokens = {
  22. 'root': [
  23. include('statement'),
  24. ],
  25. 'statement': [
  26. # Whitespace / Comment
  27. include('whitespace'),
  28. # Block-statement
  29. (r'(\+?)([ \t]*)(\*?)([ \t]*)(\bfunc)([ \t]+(?:\n\s*\|)*[ \t]*)([a-zA-Z_][0-9a-zA-Z_]*)',
  30. bygroups(Keyword,Whitespace, Keyword, Whitespace, Keyword,
  31. using(this), Name.Function), 'func_'),
  32. (r'\b(class)([ \t]+(?:\n\s*\|)*[ \t]*)([a-zA-Z_][0-9a-zA-Z_]*)',
  33. bygroups(Keyword, using(this), Name.Class), 'class_'),
  34. (r'\b(enum)([ \t]+(?:\n\s*\|)*[ \t]*)([a-zA-Z_][0-9a-zA-Z_]*)',
  35. bygroups(Keyword, using(this), Name.Constant), 'enum_'),
  36. (r'\b(block)\b(?:([ \t]+(?:\n\s*\|)*[ \t]*)([a-zA-Z_][0-9a-zA-Z_]*))?',
  37. bygroups(Keyword, using(this), Name.Other), 'block_'),
  38. (r'\b(ifdef)\b(?:([ \t]+(?:\n\s*\|)*[ \t]*)([a-zA-Z_][0-9a-zA-Z_]*))?',
  39. bygroups(Keyword, using(this), Name.Other), 'ifdef_'),
  40. (r'\b(if)\b(?:([ \t]+(?:\n\s*\|)*[ \t]*)([a-zA-Z_][0-9a-zA-Z_]*))?',
  41. bygroups(Keyword, using(this), Name.Other), 'if_'),
  42. (r'\b(switch)\b(?:([ \t]+(?:\n\s*\|)*[ \t]*)([a-zA-Z_][0-9a-zA-Z_]*))?',
  43. bygroups(Keyword, using(this), Name.Other), 'switch_'),
  44. (r'\b(while)\b(?:([ \t]+(?:\n\s*\|)*[ \t]*)([a-zA-Z_][0-9a-zA-Z_]*))?',
  45. bygroups(Keyword, using(this), Name.Other), 'while_'),
  46. (r'\b(for)\b(?:([ \t]+(?:\n\s*\|)*[ \t]*)([a-zA-Z_][0-9a-zA-Z_]*))?',
  47. bygroups(Keyword, using(this), Name.Other), 'for_'),
  48. (r'\b(foreach)\b(?:([ \t]+(?:\n\s*\|)*[ \t]*)([a-zA-Z_][0-9a-zA-Z_]*))?',
  49. bygroups(Keyword, using(this), Name.Other), 'foreach_'),
  50. (r'\b(try)\b(?:([ \t]+(?:\n\s*\|)*[ \t]*)([a-zA-Z_][0-9a-zA-Z_]*))?',
  51. bygroups(Keyword, using(this), Name.Other), 'try_'),
  52. # Line-statement
  53. (r'\b(do)\b', Keyword, 'do'),
  54. (r'(\+?[ \t]*\bvar)\b', Keyword, 'var'),
  55. (r'\b(const)\b', Keyword, 'const'),
  56. (r'\b(ret)\b', Keyword, 'ret'),
  57. (r'\b(throw)\b', Keyword, 'throw'),
  58. (r'\b(alias)\b', Keyword, 'alias'),
  59. (r'\b(assert)\b', Keyword, 'assert'),
  60. (r'\|', Text, 'continued_line'),
  61. (r'[ \t]*\n', Whitespace),
  62. ],
  63. # Whitespace / Comment
  64. 'whitespace': [
  65. (r'^([ \t]*)(;.*)', bygroups(Comment.Single, Whitespace)),
  66. (r'[ \t]+(?![; \t])', Whitespace),
  67. (r'\{', Comment.Multiline, 'multiline_comment'),
  68. ],
  69. 'multiline_comment': [
  70. (r'\{', Comment.Multiline, 'multiline_comment'),
  71. (r'(?:\s*;.*|[^{}\n]+)', Comment.Multiline),
  72. (r'\n', Comment.Multiline),
  73. (r'\}', Comment.Multiline, '#pop'),
  74. ],
  75. # Block-statement
  76. 'func_': [
  77. include('expr'),
  78. (r'\n', Whitespace, 'func'),
  79. ],
  80. 'func': [
  81. (r'\b(end)([ \t]+(?:\n\s*\|)*[ \t]*)(func)\b',
  82. bygroups(Keyword, using(this), Keyword), '#pop:2'),
  83. include('statement'),
  84. ],
  85. 'class_': [
  86. include('expr'),
  87. (r'\n', Whitespace, 'class'),
  88. ],
  89. 'class': [
  90. (r'\b(end)([ \t]+(?:\n\s*\|)*[ \t]*)(class)\b',
  91. bygroups(Keyword, using(this), Keyword), '#pop:2'),
  92. include('statement'),
  93. ],
  94. 'enum_': [
  95. include('expr'),
  96. (r'\n', Whitespace, 'enum'),
  97. ],
  98. 'enum': [
  99. (r'\b(end)([ \t]+(?:\n\s*\|)*[ \t]*)(enum)\b',
  100. bygroups(Keyword, using(this), Keyword), '#pop:2'),
  101. include('expr'),
  102. (r'\n', Whitespace),
  103. ],
  104. 'block_': [
  105. include('expr'),
  106. (r'\n', Whitespace, 'block'),
  107. ],
  108. 'block': [
  109. (r'\b(end)([ \t]+(?:\n\s*\|)*[ \t]*)(block)\b',
  110. bygroups(Keyword, using(this), Keyword), '#pop:2'),
  111. include('statement'),
  112. include('break'),
  113. include('skip'),
  114. ],
  115. 'ifdef_': [
  116. include('expr'),
  117. (r'\n', Whitespace, 'ifdef'),
  118. ],
  119. 'ifdef': [
  120. (r'\b(end)([ \t]+(?:\n\s*\|)*[ \t]*)(ifdef)\b',
  121. bygroups(Keyword, using(this), Keyword), '#pop:2'),
  122. (words(('rls', 'dbg'), prefix=r'\b', suffix=r'\b'),
  123. Keyword.Constant, 'ifdef_sp'),
  124. include('statement'),
  125. include('break'),
  126. include('skip'),
  127. ],
  128. 'ifdef_sp': [
  129. include('expr'),
  130. (r'\n', Whitespace, '#pop'),
  131. ],
  132. 'if_': [
  133. include('expr'),
  134. (r'\n', Whitespace, 'if'),
  135. ],
  136. 'if': [
  137. (r'\b(end)([ \t]+(?:\n\s*\|)*[ \t]*)(if)\b',
  138. bygroups(Keyword, using(this), Keyword), '#pop:2'),
  139. (words(('elif', 'else'), prefix=r'\b', suffix=r'\b'), Keyword, 'if_sp'),
  140. include('statement'),
  141. include('break'),
  142. include('skip'),
  143. ],
  144. 'if_sp': [
  145. include('expr'),
  146. (r'\n', Whitespace, '#pop'),
  147. ],
  148. 'switch_': [
  149. include('expr'),
  150. (r'\n', Whitespace, 'switch'),
  151. ],
  152. 'switch': [
  153. (r'\b(end)([ \t]+(?:\n\s*\|)*[ \t]*)(switch)\b',
  154. bygroups(Keyword, using(this), Keyword), '#pop:2'),
  155. (words(('case', 'default', 'to'), prefix=r'\b', suffix=r'\b'),
  156. Keyword, 'switch_sp'),
  157. include('statement'),
  158. include('break'),
  159. include('skip'),
  160. ],
  161. 'switch_sp': [
  162. include('expr'),
  163. (r'\n', Whitespace, '#pop'),
  164. ],
  165. 'while_': [
  166. include('expr'),
  167. (r'\n', Whitespace, 'while'),
  168. ],
  169. 'while': [
  170. (r'\b(end)([ \t]+(?:\n\s*\|)*[ \t]*)(while)\b',
  171. bygroups(Keyword, using(this), Keyword), '#pop:2'),
  172. include('statement'),
  173. include('break'),
  174. include('skip'),
  175. ],
  176. 'for_': [
  177. include('expr'),
  178. (r'\n', Whitespace, 'for'),
  179. ],
  180. 'for': [
  181. (r'\b(end)([ \t]+(?:\n\s*\|)*[ \t]*)(for)\b',
  182. bygroups(Keyword, using(this), Keyword), '#pop:2'),
  183. include('statement'),
  184. include('break'),
  185. include('skip'),
  186. ],
  187. 'foreach_': [
  188. include('expr'),
  189. (r'\n', Whitespace, 'foreach'),
  190. ],
  191. 'foreach': [
  192. (r'\b(end)([ \t]+(?:\n\s*\|)*[ \t]*)(foreach)\b',
  193. bygroups(Keyword, using(this), Keyword), '#pop:2'),
  194. include('statement'),
  195. include('break'),
  196. include('skip'),
  197. ],
  198. 'try_': [
  199. include('expr'),
  200. (r'\n', Whitespace, 'try'),
  201. ],
  202. 'try': [
  203. (r'\b(end)([ \t]+(?:\n\s*\|)*[ \t]*)(try)\b',
  204. bygroups(Keyword, using(this), Keyword), '#pop:2'),
  205. (words(('catch', 'finally', 'to'), prefix=r'\b', suffix=r'\b'),
  206. Keyword, 'try_sp'),
  207. include('statement'),
  208. include('break'),
  209. include('skip'),
  210. ],
  211. 'try_sp': [
  212. include('expr'),
  213. (r'\n', Whitespace, '#pop'),
  214. ],
  215. # Line-statement
  216. 'break': [
  217. (r'\b(break)\b([ \t]+)([a-zA-Z_][0-9a-zA-Z_]*)',
  218. bygroups(Keyword, using(this), Name.Other)),
  219. ],
  220. 'skip': [
  221. (r'\b(skip)\b([ \t]+)([a-zA-Z_][0-9a-zA-Z_]*)',
  222. bygroups(Keyword, using(this), Name.Other)),
  223. ],
  224. 'alias': [
  225. include('expr'),
  226. (r'\n', Whitespace, '#pop'),
  227. ],
  228. 'assert': [
  229. include('expr'),
  230. (r'\n', Whitespace, '#pop'),
  231. ],
  232. 'const': [
  233. include('expr'),
  234. (r'\n', Whitespace, '#pop'),
  235. ],
  236. 'do': [
  237. include('expr'),
  238. (r'\n', Whitespace, '#pop'),
  239. ],
  240. 'ret': [
  241. include('expr'),
  242. (r'\n', Whitespace, '#pop'),
  243. ],
  244. 'throw': [
  245. include('expr'),
  246. (r'\n', Whitespace, '#pop'),
  247. ],
  248. 'var': [
  249. include('expr'),
  250. (r'\n', Whitespace, '#pop'),
  251. ],
  252. 'continued_line': [
  253. include('expr'),
  254. (r'\n', Whitespace, '#pop'),
  255. ],
  256. 'expr': [
  257. # Whitespace / Comment
  258. include('whitespace'),
  259. # Punctuation
  260. (r'\(', Punctuation,),
  261. (r'\)', Punctuation,),
  262. (r'\[', Punctuation,),
  263. (r'\]', Punctuation,),
  264. (r',', Punctuation),
  265. # Keyword
  266. (words((
  267. 'true', 'false', 'null', 'inf'
  268. ), prefix=r'\b', suffix=r'\b'), Keyword.Constant),
  269. (words((
  270. 'me'
  271. ), prefix=r'\b', suffix=r'\b'), Keyword),
  272. (words((
  273. 'bit16', 'bit32', 'bit64', 'bit8', 'bool',
  274. 'char', 'class', 'dict', 'enum', 'float', 'func',
  275. 'int', 'list', 'queue', 'stack'
  276. ), prefix=r'\b', suffix=r'\b'), Keyword.Type),
  277. # Number
  278. (r'\b[0-9]\.[0-9]+(?!\.)(:?e[\+-][0-9]+)?\b', Number.Float),
  279. (r'\b2#[01]+(?:b(?:8|16|32|64))?\b', Number.Bin),
  280. (r'\b8#[0-7]+(?:b(?:8|16|32|64))?\b', Number.Oct),
  281. (r'\b16#[0-9A-F]+(?:b(?:8|16|32|64))?\b', Number.Hex),
  282. (r'\b[0-9]+(?:b(?:8|16|32|64))?\b', Number.Decimal),
  283. # String / Char
  284. (r'"', String.Double, 'string'),
  285. (r"'(?:\\.|.)+?'", String.Char),
  286. # Operator
  287. (r'(?:\.|\$(?:>|<)?)', Operator),
  288. (r'(?:\^)', Operator),
  289. (r'(?:\+|-|!|##?)', Operator),
  290. (r'(?:\*|/|%)', Operator),
  291. (r'(?:~)', Operator),
  292. (r'(?:(?:=|<>)(?:&|\$)?|<=?|>=?)', Operator),
  293. (r'(?:&)', Operator),
  294. (r'(?:\|)', Operator),
  295. (r'(?:\?)', Operator),
  296. (r'(?::(?::|\+|-|\*|/|%|\^|~)?)', Operator),
  297. # Identifier
  298. (r"\b([a-zA-Z_][0-9a-zA-Z_]*)(?=@)\b", Name),
  299. (r"(@)?\b([a-zA-Z_][0-9a-zA-Z_]*)\b",
  300. bygroups(Name.Other, Name.Variable)),
  301. ],
  302. # String
  303. 'string': [
  304. (r'(?:\\[^{\n]|[^"\\])+', String.Double),
  305. (r'\\\{', String.Double, 'toStrInString'),
  306. (r'"', String.Double, '#pop'),
  307. ],
  308. 'toStrInString': [
  309. include('expr'),
  310. (r'\}', String.Double, '#pop'),
  311. ],
  312. }