kuin.py 10 KB

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