jvm.py 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  1. """
  2. pygments.lexers.jvm
  3. ~~~~~~~~~~~~~~~~~~~
  4. Pygments lexers for JVM languages.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \
  10. this, combined, default, words
  11. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  12. Number, Punctuation, Whitespace
  13. from pygments.util import shebang_matches
  14. from pygments import unistring as uni
  15. __all__ = ['JavaLexer', 'ScalaLexer', 'GosuLexer', 'GosuTemplateLexer',
  16. 'GroovyLexer', 'IokeLexer', 'ClojureLexer', 'ClojureScriptLexer',
  17. 'KotlinLexer', 'XtendLexer', 'AspectJLexer', 'CeylonLexer',
  18. 'PigLexer', 'GoloLexer', 'JasminLexer', 'SarlLexer']
  19. class JavaLexer(RegexLexer):
  20. """
  21. For Java source code.
  22. """
  23. name = 'Java'
  24. url = 'https://www.oracle.com/technetwork/java/'
  25. aliases = ['java']
  26. filenames = ['*.java']
  27. mimetypes = ['text/x-java']
  28. version_added = ''
  29. flags = re.MULTILINE | re.DOTALL
  30. tokens = {
  31. 'root': [
  32. (r'(^\s*)((?:(?:public|private|protected|static|strictfp)(?:\s+))*)(record)\b',
  33. bygroups(Whitespace, using(this), Keyword.Declaration), 'class'),
  34. (r'[^\S\n]+', Whitespace),
  35. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  36. (r'/\*.*?\*/', Comment.Multiline),
  37. # keywords: go before method names to avoid lexing "throw new XYZ"
  38. # as a method signature
  39. (r'(assert|break|case|catch|continue|default|do|else|finally|for|'
  40. r'if|goto|instanceof|new|return|switch|this|throw|try|while)\b',
  41. Keyword),
  42. # method names
  43. (r'((?:(?:[^\W\d]|\$)[\w.\[\]$<>]*\s+)+?)' # return arguments
  44. r'((?:[^\W\d]|\$)[\w$]*)' # method name
  45. r'(\s*)(\()', # signature start
  46. bygroups(using(this), Name.Function, Whitespace, Punctuation)),
  47. (r'@[^\W\d][\w.]*', Name.Decorator),
  48. (r'(abstract|const|enum|extends|final|implements|native|private|'
  49. r'protected|public|sealed|static|strictfp|super|synchronized|throws|'
  50. r'transient|volatile|yield)\b', Keyword.Declaration),
  51. (r'(boolean|byte|char|double|float|int|long|short|void)\b',
  52. Keyword.Type),
  53. (r'(package)(\s+)', bygroups(Keyword.Namespace, Whitespace), 'import'),
  54. (r'(true|false|null)\b', Keyword.Constant),
  55. (r'(class|interface)\b', Keyword.Declaration, 'class'),
  56. (r'(var)(\s+)', bygroups(Keyword.Declaration, Whitespace), 'var'),
  57. (r'(import(?:\s+static)?)(\s+)', bygroups(Keyword.Namespace, Whitespace),
  58. 'import'),
  59. (r'"""\n', String, 'multiline_string'),
  60. (r'"', String, 'string'),
  61. (r"'\\.'|'[^\\]'|'\\u[0-9a-fA-F]{4}'", String.Char),
  62. (r'(\.)((?:[^\W\d]|\$)[\w$]*)', bygroups(Punctuation,
  63. Name.Attribute)),
  64. (r'^(\s*)(default)(:)', bygroups(Whitespace, Keyword, Punctuation)),
  65. (r'^(\s*)((?:[^\W\d]|\$)[\w$]*)(:)', bygroups(Whitespace, Name.Label,
  66. Punctuation)),
  67. (r'([^\W\d]|\$)[\w$]*', Name),
  68. (r'([0-9][0-9_]*\.([0-9][0-9_]*)?|'
  69. r'\.[0-9][0-9_]*)'
  70. r'([eE][+\-]?[0-9][0-9_]*)?[fFdD]?|'
  71. r'[0-9][eE][+\-]?[0-9][0-9_]*[fFdD]?|'
  72. r'[0-9]([eE][+\-]?[0-9][0-9_]*)?[fFdD]|'
  73. r'0[xX]([0-9a-fA-F][0-9a-fA-F_]*\.?|'
  74. r'([0-9a-fA-F][0-9a-fA-F_]*)?\.[0-9a-fA-F][0-9a-fA-F_]*)'
  75. r'[pP][+\-]?[0-9][0-9_]*[fFdD]?', Number.Float),
  76. (r'0[xX][0-9a-fA-F][0-9a-fA-F_]*[lL]?', Number.Hex),
  77. (r'0[bB][01][01_]*[lL]?', Number.Bin),
  78. (r'0[0-7_]+[lL]?', Number.Oct),
  79. (r'0|[1-9][0-9_]*[lL]?', Number.Integer),
  80. (r'[~^*!%&\[\]<>|+=/?-]', Operator),
  81. (r'[{}();:.,]', Punctuation),
  82. (r'\n', Whitespace)
  83. ],
  84. 'class': [
  85. (r'\s+', Text),
  86. (r'([^\W\d]|\$)[\w$]*', Name.Class, '#pop')
  87. ],
  88. 'var': [
  89. (r'([^\W\d]|\$)[\w$]*', Name, '#pop')
  90. ],
  91. 'import': [
  92. (r'[\w.]+\*?', Name.Namespace, '#pop')
  93. ],
  94. 'multiline_string': [
  95. (r'"""', String, '#pop'),
  96. (r'"', String),
  97. include('string')
  98. ],
  99. 'string': [
  100. (r'[^\\"]+', String),
  101. (r'\\\\', String), # Escaped backslash
  102. (r'\\"', String), # Escaped quote
  103. (r'\\', String), # Bare backslash
  104. (r'"', String, '#pop'), # Closing quote
  105. ],
  106. }
  107. class AspectJLexer(JavaLexer):
  108. """
  109. For AspectJ source code.
  110. """
  111. name = 'AspectJ'
  112. url = 'http://www.eclipse.org/aspectj/'
  113. aliases = ['aspectj']
  114. filenames = ['*.aj']
  115. mimetypes = ['text/x-aspectj']
  116. version_added = '1.6'
  117. aj_keywords = {
  118. 'aspect', 'pointcut', 'privileged', 'call', 'execution',
  119. 'initialization', 'preinitialization', 'handler', 'get', 'set',
  120. 'staticinitialization', 'target', 'args', 'within', 'withincode',
  121. 'cflow', 'cflowbelow', 'annotation', 'before', 'after', 'around',
  122. 'proceed', 'throwing', 'returning', 'adviceexecution', 'declare',
  123. 'parents', 'warning', 'error', 'soft', 'precedence', 'thisJoinPoint',
  124. 'thisJoinPointStaticPart', 'thisEnclosingJoinPointStaticPart',
  125. 'issingleton', 'perthis', 'pertarget', 'percflow', 'percflowbelow',
  126. 'pertypewithin', 'lock', 'unlock', 'thisAspectInstance'
  127. }
  128. aj_inter_type = {'parents:', 'warning:', 'error:', 'soft:', 'precedence:'}
  129. aj_inter_type_annotation = {'@type', '@method', '@constructor', '@field'}
  130. def get_tokens_unprocessed(self, text):
  131. for index, token, value in JavaLexer.get_tokens_unprocessed(self, text):
  132. if token is Name and value in self.aj_keywords:
  133. yield index, Keyword, value
  134. elif token is Name.Label and value in self.aj_inter_type:
  135. yield index, Keyword, value[:-1]
  136. yield index, Operator, value[-1]
  137. elif token is Name.Decorator and value in self.aj_inter_type_annotation:
  138. yield index, Keyword, value
  139. else:
  140. yield index, token, value
  141. class ScalaLexer(RegexLexer):
  142. """
  143. For Scala source code.
  144. """
  145. name = 'Scala'
  146. url = 'http://www.scala-lang.org'
  147. aliases = ['scala']
  148. filenames = ['*.scala']
  149. mimetypes = ['text/x-scala']
  150. version_added = ''
  151. flags = re.MULTILINE | re.DOTALL
  152. opchar = '[!#%&*\\-\\/:?@^' + uni.combine('Sm', 'So') + ']'
  153. letter = '[_\\$' + uni.combine('Ll', 'Lu', 'Lo', 'Nl', 'Lt') + ']'
  154. upperLetter = '[' + uni.combine('Lu', 'Lt') + ']'
  155. letterOrDigit = f'(?:{letter}|[0-9])'
  156. letterOrDigitNoDollarSign = '(?:{}|[0-9])'.format(letter.replace('\\$', ''))
  157. alphaId = f'{letter}+'
  158. simpleInterpolatedVariable = f'{letter}{letterOrDigitNoDollarSign}*'
  159. idrest = f'{letter}{letterOrDigit}*(?:(?<=_){opchar}+)?'
  160. idUpper = f'{upperLetter}{letterOrDigit}*(?:(?<=_){opchar}+)?'
  161. plainid = f'(?:{idrest}|{opchar}+)'
  162. backQuotedId = r'`[^`]+`'
  163. anyId = rf'(?:{plainid}|{backQuotedId})'
  164. notStartOfComment = r'(?!//|/\*)'
  165. endOfLineMaybeWithComment = r'(?=\s*(//|$))'
  166. keywords = (
  167. 'new', 'return', 'throw', 'classOf', 'isInstanceOf', 'asInstanceOf',
  168. 'else', 'if', 'then', 'do', 'while', 'for', 'yield', 'match', 'case',
  169. 'catch', 'finally', 'try'
  170. )
  171. operators = (
  172. '<%', '=:=', '<:<', '<%<', '>:', '<:', '=', '==', '!=', '<=', '>=',
  173. '<>', '<', '>', '<-', '←', '->', '→', '=>', '⇒', '?', '@', '|', '-',
  174. '+', '*', '%', '~', '\\'
  175. )
  176. storage_modifiers = (
  177. 'private', 'protected', 'synchronized', '@volatile', 'abstract',
  178. 'final', 'lazy', 'sealed', 'implicit', 'override', '@transient',
  179. '@native'
  180. )
  181. tokens = {
  182. 'root': [
  183. include('whitespace'),
  184. include('comments'),
  185. include('script-header'),
  186. include('imports'),
  187. include('exports'),
  188. include('storage-modifiers'),
  189. include('annotations'),
  190. include('using'),
  191. include('declarations'),
  192. include('inheritance'),
  193. include('extension'),
  194. include('end'),
  195. include('constants'),
  196. include('strings'),
  197. include('symbols'),
  198. include('singleton-type'),
  199. include('inline'),
  200. include('quoted'),
  201. include('keywords'),
  202. include('operators'),
  203. include('punctuation'),
  204. include('names'),
  205. ],
  206. # Includes:
  207. 'whitespace': [
  208. (r'\s+', Whitespace),
  209. ],
  210. 'comments': [
  211. (r'//.*?\n', Comment.Single),
  212. (r'/\*', Comment.Multiline, 'comment'),
  213. ],
  214. 'script-header': [
  215. (r'^#!([^\n]*)$', Comment.Hashbang),
  216. ],
  217. 'imports': [
  218. (r'\b(import)(\s+)', bygroups(Keyword, Whitespace), 'import-path'),
  219. ],
  220. 'exports': [
  221. (r'\b(export)(\s+)(given)(\s+)',
  222. bygroups(Keyword, Whitespace, Keyword, Whitespace), 'export-path'),
  223. (r'\b(export)(\s+)', bygroups(Keyword, Whitespace), 'export-path'),
  224. ],
  225. 'storage-modifiers': [
  226. (words(storage_modifiers, prefix=r'\b', suffix=r'\b'), Keyword),
  227. # Only highlight soft modifiers if they are eventually followed by
  228. # the correct keyword. Note that soft modifiers can be followed by a
  229. # sequence of regular modifiers; [a-z\s]* skips those, and we just
  230. # check that the soft modifier is applied to a supported statement.
  231. (r'\b(transparent|opaque|infix|open|inline)\b(?=[a-z\s]*\b'
  232. r'(def|val|var|given|type|class|trait|object|enum)\b)', Keyword),
  233. ],
  234. 'annotations': [
  235. (rf'@{idrest}', Name.Decorator),
  236. ],
  237. 'using': [
  238. # using is a soft keyword, can only be used in the first position of
  239. # a parameter or argument list.
  240. (r'(\()(\s*)(using)(\s)', bygroups(Punctuation, Whitespace, Keyword, Whitespace)),
  241. ],
  242. 'declarations': [
  243. (rf'\b(def)\b(\s*){notStartOfComment}({anyId})?',
  244. bygroups(Keyword, Whitespace, Name.Function)),
  245. (rf'\b(trait)\b(\s*){notStartOfComment}({anyId})?',
  246. bygroups(Keyword, Whitespace, Name.Class)),
  247. (rf'\b(?:(case)(\s+))?(class|object|enum)\b(\s*){notStartOfComment}({anyId})?',
  248. bygroups(Keyword, Whitespace, Keyword, Whitespace, Name.Class)),
  249. (rf'(?<!\.)\b(type)\b(\s*){notStartOfComment}({anyId})?',
  250. bygroups(Keyword, Whitespace, Name.Class)),
  251. (r'\b(val|var)\b', Keyword.Declaration),
  252. (rf'\b(package)(\s+)(object)\b(\s*){notStartOfComment}({anyId})?',
  253. bygroups(Keyword, Whitespace, Keyword, Whitespace, Name.Namespace)),
  254. (r'\b(package)(\s+)', bygroups(Keyword, Whitespace), 'package'),
  255. (rf'\b(given)\b(\s*)({idUpper})',
  256. bygroups(Keyword, Whitespace, Name.Class)),
  257. (rf'\b(given)\b(\s*)({anyId})?',
  258. bygroups(Keyword, Whitespace, Name)),
  259. ],
  260. 'inheritance': [
  261. (r'\b(extends|with|derives)\b(\s*)'
  262. rf'({idUpper}|{backQuotedId}|(?=\([^\)]+=>)|(?={plainid})|(?="))?',
  263. bygroups(Keyword, Whitespace, Name.Class)),
  264. ],
  265. 'extension': [
  266. (r'\b(extension)(\s+)(?=[\[\(])', bygroups(Keyword, Whitespace)),
  267. ],
  268. 'end': [
  269. # end is a soft keyword, should only be highlighted in certain cases
  270. (r'\b(end)(\s+)(if|while|for|match|new|extension|val|var)\b',
  271. bygroups(Keyword, Whitespace, Keyword)),
  272. (rf'\b(end)(\s+)({idUpper}){endOfLineMaybeWithComment}',
  273. bygroups(Keyword, Whitespace, Name.Class)),
  274. (rf'\b(end)(\s+)({backQuotedId}|{plainid})?{endOfLineMaybeWithComment}',
  275. bygroups(Keyword, Whitespace, Name.Namespace)),
  276. ],
  277. 'punctuation': [
  278. (r'[{}()\[\];,.]', Punctuation),
  279. (r'(?<!:):(?!:)', Punctuation),
  280. ],
  281. 'keywords': [
  282. (words(keywords, prefix=r'\b', suffix=r'\b'), Keyword),
  283. ],
  284. 'operators': [
  285. (rf'({opchar}{{2,}})(\s+)', bygroups(Operator, Whitespace)),
  286. (r'/(?![/*])', Operator),
  287. (words(operators), Operator),
  288. (rf'(?<!{opchar})(!|&&|\|\|)(?!{opchar})', Operator),
  289. ],
  290. 'constants': [
  291. (r'\b(this|super)\b', Name.Builtin.Pseudo),
  292. (r'(true|false|null)\b', Keyword.Constant),
  293. (r'0[xX][0-9a-fA-F_]*', Number.Hex),
  294. (r'([0-9][0-9_]*\.[0-9][0-9_]*|\.[0-9][0-9_]*)'
  295. r'([eE][+-]?[0-9][0-9_]*)?[fFdD]?', Number.Float),
  296. (r'[0-9]+([eE][+-]?[0-9]+)?[fFdD]', Number.Float),
  297. (r'[0-9]+([eE][+-]?[0-9]+)[fFdD]?', Number.Float),
  298. (r'[0-9]+[lL]', Number.Integer.Long),
  299. (r'[0-9]+', Number.Integer),
  300. (r'""".*?"""(?!")', String),
  301. (r'"(\\\\|\\"|[^"])*"', String),
  302. (r"(')(\\.)(')", bygroups(String.Char, String.Escape, String.Char)),
  303. (r"'[^\\]'|'\\u[0-9a-fA-F]{4}'", String.Char),
  304. ],
  305. "strings": [
  306. (r'[fs]"""', String, 'interpolated-string-triple'),
  307. (r'[fs]"', String, 'interpolated-string'),
  308. (r'raw"(\\\\|\\"|[^"])*"', String),
  309. ],
  310. 'symbols': [
  311. (rf"('{plainid})(?!')", String.Symbol),
  312. ],
  313. 'singleton-type': [
  314. (r'(\.)(type)\b', bygroups(Punctuation, Keyword)),
  315. ],
  316. 'inline': [
  317. # inline is a soft modifier, only highlighted if followed by if,
  318. # match or parameters.
  319. (rf'\b(inline)(?=\s+({plainid}|{backQuotedId})\s*:)',
  320. Keyword),
  321. (r'\b(inline)\b(?=(?:.(?!\b(?:val|def|given)\b))*\b(if|match)\b)',
  322. Keyword),
  323. ],
  324. 'quoted': [
  325. # '{...} or ${...}
  326. (r"['$]\{(?!')", Punctuation),
  327. # '[...]
  328. (r"'\[(?!')", Punctuation),
  329. ],
  330. 'names': [
  331. (idUpper, Name.Class),
  332. (anyId, Name),
  333. ],
  334. # States
  335. 'comment': [
  336. (r'[^/*]+', Comment.Multiline),
  337. (r'/\*', Comment.Multiline, '#push'),
  338. (r'\*/', Comment.Multiline, '#pop'),
  339. (r'[*/]', Comment.Multiline),
  340. ],
  341. 'import-path': [
  342. (r'(?<=[\n;:])', Text, '#pop'),
  343. include('comments'),
  344. (r'\b(given)\b', Keyword),
  345. include('qualified-name'),
  346. (r'\{', Punctuation, 'import-path-curly-brace'),
  347. ],
  348. 'import-path-curly-brace': [
  349. include('whitespace'),
  350. include('comments'),
  351. (r'\b(given)\b', Keyword),
  352. (r'=>', Operator),
  353. (r'\}', Punctuation, '#pop'),
  354. (r',', Punctuation),
  355. (r'[\[\]]', Punctuation),
  356. include('qualified-name'),
  357. ],
  358. 'export-path': [
  359. (r'(?<=[\n;:])', Text, '#pop'),
  360. include('comments'),
  361. include('qualified-name'),
  362. (r'\{', Punctuation, 'export-path-curly-brace'),
  363. ],
  364. 'export-path-curly-brace': [
  365. include('whitespace'),
  366. include('comments'),
  367. (r'=>', Operator),
  368. (r'\}', Punctuation, '#pop'),
  369. (r',', Punctuation),
  370. include('qualified-name'),
  371. ],
  372. 'package': [
  373. (r'(?<=[\n;])', Text, '#pop'),
  374. (r':', Punctuation, '#pop'),
  375. include('comments'),
  376. include('qualified-name'),
  377. ],
  378. 'interpolated-string-triple': [
  379. (r'"""(?!")', String, '#pop'),
  380. (r'"', String),
  381. include('interpolated-string-common'),
  382. ],
  383. 'interpolated-string': [
  384. (r'"', String, '#pop'),
  385. include('interpolated-string-common'),
  386. ],
  387. 'interpolated-string-brace': [
  388. (r'\}', String.Interpol, '#pop'),
  389. (r'\{', Punctuation, 'interpolated-string-nested-brace'),
  390. include('root'),
  391. ],
  392. 'interpolated-string-nested-brace': [
  393. (r'\{', Punctuation, '#push'),
  394. (r'\}', Punctuation, '#pop'),
  395. include('root'),
  396. ],
  397. # Helpers
  398. 'qualified-name': [
  399. (idUpper, Name.Class),
  400. (rf'({anyId})(\.)', bygroups(Name.Namespace, Punctuation)),
  401. (r'\.', Punctuation),
  402. (anyId, Name),
  403. (r'[^\S\n]+', Whitespace),
  404. ],
  405. 'interpolated-string-common': [
  406. (r'[^"$\\]+', String),
  407. (r'\$\$', String.Escape),
  408. (rf'(\$)({simpleInterpolatedVariable})',
  409. bygroups(String.Interpol, Name)),
  410. (r'\$\{', String.Interpol, 'interpolated-string-brace'),
  411. (r'\\.', String),
  412. ],
  413. }
  414. class GosuLexer(RegexLexer):
  415. """
  416. For Gosu source code.
  417. """
  418. name = 'Gosu'
  419. aliases = ['gosu']
  420. filenames = ['*.gs', '*.gsx', '*.gsp', '*.vark']
  421. mimetypes = ['text/x-gosu']
  422. url = 'https://gosu-lang.github.io'
  423. version_added = '1.5'
  424. flags = re.MULTILINE | re.DOTALL
  425. tokens = {
  426. 'root': [
  427. # method names
  428. (r'^(\s*(?:[a-zA-Z_][\w.\[\]]*\s+)+?)' # modifiers etc.
  429. r'([a-zA-Z_]\w*)' # method name
  430. r'(\s*)(\()', # signature start
  431. bygroups(using(this), Name.Function, Whitespace, Operator)),
  432. (r'[^\S\n]+', Whitespace),
  433. (r'//.*?\n', Comment.Single),
  434. (r'/\*.*?\*/', Comment.Multiline),
  435. (r'@[a-zA-Z_][\w.]*', Name.Decorator),
  436. (r'(in|as|typeof|statictypeof|typeis|typeas|if|else|foreach|for|'
  437. r'index|while|do|continue|break|return|try|catch|finally|this|'
  438. r'throw|new|switch|case|default|eval|super|outer|classpath|'
  439. r'using)\b', Keyword),
  440. (r'(var|delegate|construct|function|private|internal|protected|'
  441. r'public|abstract|override|final|static|extends|transient|'
  442. r'implements|represents|readonly)\b', Keyword.Declaration),
  443. (r'(property)(\s+)(get|set)?', bygroups(Keyword.Declaration, Whitespace, Keyword.Declaration)),
  444. (r'(boolean|byte|char|double|float|int|long|short|void|block)\b',
  445. Keyword.Type),
  446. (r'(package)(\s+)', bygroups(Keyword.Namespace, Whitespace)),
  447. (r'(true|false|null|NaN|Infinity)\b', Keyword.Constant),
  448. (r'(class|interface|enhancement|enum)(\s+)([a-zA-Z_]\w*)',
  449. bygroups(Keyword.Declaration, Whitespace, Name.Class)),
  450. (r'(uses)(\s+)([\w.]+\*?)',
  451. bygroups(Keyword.Namespace, Whitespace, Name.Namespace)),
  452. (r'"', String, 'string'),
  453. (r'(\??[.#])([a-zA-Z_]\w*)',
  454. bygroups(Operator, Name.Attribute)),
  455. (r'(:)([a-zA-Z_]\w*)',
  456. bygroups(Operator, Name.Attribute)),
  457. (r'[a-zA-Z_$]\w*', Name),
  458. (r'and|or|not|[\\~^*!%&\[\](){}<>|+=:;,./?-]', Operator),
  459. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  460. (r'[0-9]+', Number.Integer),
  461. (r'\n', Whitespace)
  462. ],
  463. 'templateText': [
  464. (r'(\\<)|(\\\$)', String),
  465. (r'(<%@\s+)(extends|params)',
  466. bygroups(Operator, Name.Decorator), 'stringTemplate'),
  467. (r'<%!--.*?--%>', Comment.Multiline),
  468. (r'(<%)|(<%=)', Operator, 'stringTemplate'),
  469. (r'\$\{', Operator, 'stringTemplateShorthand'),
  470. (r'.', String)
  471. ],
  472. 'string': [
  473. (r'"', String, '#pop'),
  474. include('templateText')
  475. ],
  476. 'stringTemplate': [
  477. (r'"', String, 'string'),
  478. (r'%>', Operator, '#pop'),
  479. include('root')
  480. ],
  481. 'stringTemplateShorthand': [
  482. (r'"', String, 'string'),
  483. (r'\{', Operator, 'stringTemplateShorthand'),
  484. (r'\}', Operator, '#pop'),
  485. include('root')
  486. ],
  487. }
  488. class GosuTemplateLexer(Lexer):
  489. """
  490. For Gosu templates.
  491. """
  492. name = 'Gosu Template'
  493. aliases = ['gst']
  494. filenames = ['*.gst']
  495. mimetypes = ['text/x-gosu-template']
  496. url = 'https://gosu-lang.github.io'
  497. version_added = '1.5'
  498. def get_tokens_unprocessed(self, text):
  499. lexer = GosuLexer()
  500. stack = ['templateText']
  501. yield from lexer.get_tokens_unprocessed(text, stack)
  502. class GroovyLexer(RegexLexer):
  503. """
  504. For Groovy source code.
  505. """
  506. name = 'Groovy'
  507. url = 'https://groovy-lang.org/'
  508. aliases = ['groovy']
  509. filenames = ['*.groovy','*.gradle']
  510. mimetypes = ['text/x-groovy']
  511. version_added = '1.5'
  512. flags = re.MULTILINE | re.DOTALL
  513. tokens = {
  514. 'root': [
  515. # Groovy allows a file to start with a shebang
  516. (r'#!(.*?)$', Comment.Preproc, 'base'),
  517. default('base'),
  518. ],
  519. 'base': [
  520. (r'[^\S\n]+', Whitespace),
  521. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  522. (r'/\*.*?\*/', Comment.Multiline),
  523. # keywords: go before method names to avoid lexing "throw new XYZ"
  524. # as a method signature
  525. (r'(assert|break|case|catch|continue|default|do|else|finally|for|'
  526. r'if|goto|instanceof|new|return|switch|this|throw|try|while|in|as)\b',
  527. Keyword),
  528. # method names
  529. (r'^(\s*(?:[a-zA-Z_][\w.\[\]]*\s+)+?)' # return arguments
  530. r'('
  531. r'[a-zA-Z_]\w*' # method name
  532. r'|"(?:\\\\|\\[^\\]|[^"\\])*"' # or double-quoted method name
  533. r"|'(?:\\\\|\\[^\\]|[^'\\])*'" # or single-quoted method name
  534. r')'
  535. r'(\s*)(\()', # signature start
  536. bygroups(using(this), Name.Function, Whitespace, Operator)),
  537. (r'@[a-zA-Z_][\w.]*', Name.Decorator),
  538. (r'(abstract|const|enum|extends|final|implements|native|private|'
  539. r'protected|public|static|strictfp|super|synchronized|throws|'
  540. r'transient|volatile)\b', Keyword.Declaration),
  541. (r'(def|boolean|byte|char|double|float|int|long|short|void)\b',
  542. Keyword.Type),
  543. (r'(package)(\s+)', bygroups(Keyword.Namespace, Whitespace)),
  544. (r'(true|false|null)\b', Keyword.Constant),
  545. (r'(class|interface)(\s+)', bygroups(Keyword.Declaration, Whitespace),
  546. 'class'),
  547. (r'(import)(\s+)', bygroups(Keyword.Namespace, Whitespace), 'import'),
  548. (r'""".*?"""', String.Double),
  549. (r"'''.*?'''", String.Single),
  550. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  551. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  552. (r'\$/((?!/\$).)*/\$', String),
  553. (r'/(\\\\|\\[^\\]|[^/\\])*/', String),
  554. (r"'\\.'|'[^\\]'|'\\u[0-9a-fA-F]{4}'", String.Char),
  555. (r'(\.)([a-zA-Z_]\w*)', bygroups(Operator, Name.Attribute)),
  556. (r'[a-zA-Z_]\w*:', Name.Label),
  557. (r'[a-zA-Z_$]\w*', Name),
  558. (r'[~^*!%&\[\](){}<>|+=:;,./?-]', Operator),
  559. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  560. (r'0x[0-9a-fA-F]+', Number.Hex),
  561. (r'[0-9]+L?', Number.Integer),
  562. (r'\n', Whitespace)
  563. ],
  564. 'class': [
  565. (r'[a-zA-Z_]\w*', Name.Class, '#pop')
  566. ],
  567. 'import': [
  568. (r'[\w.]+\*?', Name.Namespace, '#pop')
  569. ],
  570. }
  571. def analyse_text(text):
  572. return shebang_matches(text, r'groovy')
  573. class IokeLexer(RegexLexer):
  574. """
  575. For Ioke (a strongly typed, dynamic,
  576. prototype based programming language) source.
  577. """
  578. name = 'Ioke'
  579. url = 'https://ioke.org/'
  580. filenames = ['*.ik']
  581. aliases = ['ioke', 'ik']
  582. mimetypes = ['text/x-iokesrc']
  583. version_added = '1.4'
  584. tokens = {
  585. 'interpolatableText': [
  586. (r'(\\b|\\e|\\t|\\n|\\f|\\r|\\"|\\\\|\\#|\\\Z|\\u[0-9a-fA-F]{1,4}'
  587. r'|\\[0-3]?[0-7]?[0-7])', String.Escape),
  588. (r'#\{', Punctuation, 'textInterpolationRoot')
  589. ],
  590. 'text': [
  591. (r'(?<!\\)"', String, '#pop'),
  592. include('interpolatableText'),
  593. (r'[^"]', String)
  594. ],
  595. 'documentation': [
  596. (r'(?<!\\)"', String.Doc, '#pop'),
  597. include('interpolatableText'),
  598. (r'[^"]', String.Doc)
  599. ],
  600. 'textInterpolationRoot': [
  601. (r'\}', Punctuation, '#pop'),
  602. include('root')
  603. ],
  604. 'slashRegexp': [
  605. (r'(?<!\\)/[im-psux]*', String.Regex, '#pop'),
  606. include('interpolatableText'),
  607. (r'\\/', String.Regex),
  608. (r'[^/]', String.Regex)
  609. ],
  610. 'squareRegexp': [
  611. (r'(?<!\\)][im-psux]*', String.Regex, '#pop'),
  612. include('interpolatableText'),
  613. (r'\\]', String.Regex),
  614. (r'[^\]]', String.Regex)
  615. ],
  616. 'squareText': [
  617. (r'(?<!\\)]', String, '#pop'),
  618. include('interpolatableText'),
  619. (r'[^\]]', String)
  620. ],
  621. 'root': [
  622. (r'\n', Whitespace),
  623. (r'\s+', Whitespace),
  624. # Comments
  625. (r';(.*?)\n', Comment),
  626. (r'\A#!(.*?)\n', Comment),
  627. # Regexps
  628. (r'#/', String.Regex, 'slashRegexp'),
  629. (r'#r\[', String.Regex, 'squareRegexp'),
  630. # Symbols
  631. (r':[\w!:?]+', String.Symbol),
  632. (r'[\w!:?]+:(?![\w!?])', String.Other),
  633. (r':"(\\\\|\\[^\\]|[^"\\])*"', String.Symbol),
  634. # Documentation
  635. (r'((?<=fn\()|(?<=fnx\()|(?<=method\()|(?<=macro\()|(?<=lecro\()'
  636. r'|(?<=syntax\()|(?<=dmacro\()|(?<=dlecro\()|(?<=dlecrox\()'
  637. r'|(?<=dsyntax\())(\s*)"', String.Doc, 'documentation'),
  638. # Text
  639. (r'"', String, 'text'),
  640. (r'#\[', String, 'squareText'),
  641. # Mimic
  642. (r'\w[\w!:?]+(?=\s*=.*mimic\s)', Name.Entity),
  643. # Assignment
  644. (r'[a-zA-Z_][\w!:?]*(?=[\s]*[+*/-]?=[^=].*($|\.))',
  645. Name.Variable),
  646. # keywords
  647. (r'(break|cond|continue|do|ensure|for|for:dict|for:set|if|let|'
  648. r'loop|p:for|p:for:dict|p:for:set|return|unless|until|while|'
  649. r'with)(?![\w!:?])', Keyword.Reserved),
  650. # Origin
  651. (r'(eval|mimic|print|println)(?![\w!:?])', Keyword),
  652. # Base
  653. (r'(cell\?|cellNames|cellOwner\?|cellOwner|cells|cell|'
  654. r'documentation|hash|identity|mimic|removeCell\!|undefineCell\!)'
  655. r'(?![\w!:?])', Keyword),
  656. # Ground
  657. (r'(stackTraceAsText)(?![\w!:?])', Keyword),
  658. # DefaultBehaviour Literals
  659. (r'(dict|list|message|set)(?![\w!:?])', Keyword.Reserved),
  660. # DefaultBehaviour Case
  661. (r'(case|case:and|case:else|case:nand|case:nor|case:not|case:or|'
  662. r'case:otherwise|case:xor)(?![\w!:?])', Keyword.Reserved),
  663. # DefaultBehaviour Reflection
  664. (r'(asText|become\!|derive|freeze\!|frozen\?|in\?|is\?|kind\?|'
  665. r'mimic\!|mimics|mimics\?|prependMimic\!|removeAllMimics\!|'
  666. r'removeMimic\!|same\?|send|thaw\!|uniqueHexId)'
  667. r'(?![\w!:?])', Keyword),
  668. # DefaultBehaviour Aspects
  669. (r'(after|around|before)(?![\w!:?])', Keyword.Reserved),
  670. # DefaultBehaviour
  671. (r'(kind|cellDescriptionDict|cellSummary|genSym|inspect|notice)'
  672. r'(?![\w!:?])', Keyword),
  673. (r'(use|destructuring)', Keyword.Reserved),
  674. # DefaultBehavior BaseBehavior
  675. (r'(cell\?|cellOwner\?|cellOwner|cellNames|cells|cell|'
  676. r'documentation|identity|removeCell!|undefineCell)'
  677. r'(?![\w!:?])', Keyword),
  678. # DefaultBehavior Internal
  679. (r'(internal:compositeRegexp|internal:concatenateText|'
  680. r'internal:createDecimal|internal:createNumber|'
  681. r'internal:createRegexp|internal:createText)'
  682. r'(?![\w!:?])', Keyword.Reserved),
  683. # DefaultBehaviour Conditions
  684. (r'(availableRestarts|bind|error\!|findRestart|handle|'
  685. r'invokeRestart|rescue|restart|signal\!|warn\!)'
  686. r'(?![\w!:?])', Keyword.Reserved),
  687. # constants
  688. (r'(nil|false|true)(?![\w!:?])', Name.Constant),
  689. # names
  690. (r'(Arity|Base|Call|Condition|DateTime|Aspects|Pointcut|'
  691. r'Assignment|BaseBehavior|Boolean|Case|AndCombiner|Else|'
  692. r'NAndCombiner|NOrCombiner|NotCombiner|OrCombiner|XOrCombiner|'
  693. r'Conditions|Definitions|FlowControl|Internal|Literals|'
  694. r'Reflection|DefaultMacro|DefaultMethod|DefaultSyntax|Dict|'
  695. r'FileSystem|Ground|Handler|Hook|IO|IokeGround|Struct|'
  696. r'LexicalBlock|LexicalMacro|List|Message|Method|Mixins|'
  697. r'NativeMethod|Number|Origin|Pair|Range|Reflector|Regexp Match|'
  698. r'Regexp|Rescue|Restart|Runtime|Sequence|Set|Symbol|'
  699. r'System|Text|Tuple)(?![\w!:?])', Name.Builtin),
  700. # functions
  701. ('(generateMatchMethod|aliasMethod|\u03bb|\u028E|fnx|fn|method|'
  702. 'dmacro|dlecro|syntax|macro|dlecrox|lecrox|lecro|syntax)'
  703. '(?![\\w!:?])', Name.Function),
  704. # Numbers
  705. (r'-?0[xX][0-9a-fA-F]+', Number.Hex),
  706. (r'-?(\d+\.?\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),
  707. (r'-?\d+', Number.Integer),
  708. (r'#\(', Punctuation),
  709. # Operators
  710. (r'(&&>>|\|\|>>|\*\*>>|:::|::|\.\.\.|===|\*\*>|\*\*=|&&>|&&=|'
  711. r'\|\|>|\|\|=|\->>|\+>>|!>>|<>>>|<>>|&>>|%>>|#>>|@>>|/>>|\*>>|'
  712. r'\?>>|\|>>|\^>>|~>>|\$>>|=>>|<<=|>>=|<=>|<\->|=~|!~|=>|\+\+|'
  713. r'\-\-|<=|>=|==|!=|&&|\.\.|\+=|\-=|\*=|\/=|%=|&=|\^=|\|=|<\-|'
  714. r'\+>|!>|<>|&>|%>|#>|\@>|\/>|\*>|\?>|\|>|\^>|~>|\$>|<\->|\->|'
  715. r'<<|>>|\*\*|\?\||\?&|\|\||>|<|\*|\/|%|\+|\-|&|\^|\||=|\$|!|~|'
  716. r'\?|#|\u2260|\u2218|\u2208|\u2209)', Operator),
  717. (r'(and|nand|or|xor|nor|return|import)(?![\w!?])',
  718. Operator),
  719. # Punctuation
  720. (r'(\`\`|\`|\'\'|\'|\.|\,|@@|@|\[|\]|\(|\)|\{|\})', Punctuation),
  721. # kinds
  722. (r'[A-Z][\w!:?]*', Name.Class),
  723. # default cellnames
  724. (r'[a-z_][\w!:?]*', Name)
  725. ]
  726. }
  727. class ClojureLexer(RegexLexer):
  728. """
  729. Lexer for Clojure source code.
  730. """
  731. name = 'Clojure'
  732. url = 'http://clojure.org/'
  733. aliases = ['clojure', 'clj']
  734. filenames = ['*.clj', '*.cljc']
  735. mimetypes = ['text/x-clojure', 'application/x-clojure']
  736. version_added = '0.11'
  737. special_forms = (
  738. '.', 'def', 'do', 'fn', 'if', 'let', 'new', 'quote', 'var', 'loop'
  739. )
  740. # It's safe to consider 'ns' a declaration thing because it defines a new
  741. # namespace.
  742. declarations = (
  743. 'def-', 'defn', 'defn-', 'defmacro', 'defmulti', 'defmethod',
  744. 'defstruct', 'defonce', 'declare', 'definline', 'definterface',
  745. 'defprotocol', 'defrecord', 'deftype', 'defproject', 'ns'
  746. )
  747. builtins = (
  748. '*', '+', '-', '->', '/', '<', '<=', '=', '==', '>', '>=', '..',
  749. 'accessor', 'agent', 'agent-errors', 'aget', 'alength', 'all-ns',
  750. 'alter', 'and', 'append-child', 'apply', 'array-map', 'aset',
  751. 'aset-boolean', 'aset-byte', 'aset-char', 'aset-double', 'aset-float',
  752. 'aset-int', 'aset-long', 'aset-short', 'assert', 'assoc', 'await',
  753. 'await-for', 'bean', 'binding', 'bit-and', 'bit-not', 'bit-or',
  754. 'bit-shift-left', 'bit-shift-right', 'bit-xor', 'boolean', 'branch?',
  755. 'butlast', 'byte', 'cast', 'char', 'children', 'class',
  756. 'clear-agent-errors', 'comment', 'commute', 'comp', 'comparator',
  757. 'complement', 'concat', 'conj', 'cons', 'constantly', 'cond', 'if-not',
  758. 'construct-proxy', 'contains?', 'count', 'create-ns', 'create-struct',
  759. 'cycle', 'dec', 'deref', 'difference', 'disj', 'dissoc', 'distinct',
  760. 'doall', 'doc', 'dorun', 'doseq', 'dosync', 'dotimes', 'doto',
  761. 'double', 'down', 'drop', 'drop-while', 'edit', 'end?', 'ensure',
  762. 'eval', 'every?', 'false?', 'ffirst', 'file-seq', 'filter', 'find',
  763. 'find-doc', 'find-ns', 'find-var', 'first', 'float', 'flush', 'for',
  764. 'fnseq', 'frest', 'gensym', 'get-proxy-class', 'get',
  765. 'hash-map', 'hash-set', 'identical?', 'identity', 'if-let', 'import',
  766. 'in-ns', 'inc', 'index', 'insert-child', 'insert-left', 'insert-right',
  767. 'inspect-table', 'inspect-tree', 'instance?', 'int', 'interleave',
  768. 'intersection', 'into', 'into-array', 'iterate', 'join', 'key', 'keys',
  769. 'keyword', 'keyword?', 'last', 'lazy-cat', 'lazy-cons', 'left',
  770. 'lefts', 'line-seq', 'list*', 'list', 'load', 'load-file',
  771. 'locking', 'long', 'loop', 'macroexpand', 'macroexpand-1',
  772. 'make-array', 'make-node', 'map', 'map-invert', 'map?', 'mapcat',
  773. 'max', 'max-key', 'memfn', 'merge', 'merge-with', 'meta', 'min',
  774. 'min-key', 'name', 'namespace', 'neg?', 'new', 'newline', 'next',
  775. 'nil?', 'node', 'not', 'not-any?', 'not-every?', 'not=', 'ns-imports',
  776. 'ns-interns', 'ns-map', 'ns-name', 'ns-publics', 'ns-refers',
  777. 'ns-resolve', 'ns-unmap', 'nth', 'nthrest', 'or', 'parse', 'partial',
  778. 'path', 'peek', 'pop', 'pos?', 'pr', 'pr-str', 'print', 'print-str',
  779. 'println', 'println-str', 'prn', 'prn-str', 'project', 'proxy',
  780. 'proxy-mappings', 'quot', 'rand', 'rand-int', 'range', 're-find',
  781. 're-groups', 're-matcher', 're-matches', 're-pattern', 're-seq',
  782. 'read', 'read-line', 'reduce', 'ref', 'ref-set', 'refer', 'rem',
  783. 'remove', 'remove-method', 'remove-ns', 'rename', 'rename-keys',
  784. 'repeat', 'replace', 'replicate', 'resolve', 'rest', 'resultset-seq',
  785. 'reverse', 'rfirst', 'right', 'rights', 'root', 'rrest', 'rseq',
  786. 'second', 'select', 'select-keys', 'send', 'send-off', 'seq',
  787. 'seq-zip', 'seq?', 'set', 'short', 'slurp', 'some', 'sort',
  788. 'sort-by', 'sorted-map', 'sorted-map-by', 'sorted-set',
  789. 'special-symbol?', 'split-at', 'split-with', 'str', 'string?',
  790. 'struct', 'struct-map', 'subs', 'subvec', 'symbol', 'symbol?',
  791. 'sync', 'take', 'take-nth', 'take-while', 'test', 'time', 'to-array',
  792. 'to-array-2d', 'tree-seq', 'true?', 'union', 'up', 'update-proxy',
  793. 'val', 'vals', 'var-get', 'var-set', 'var?', 'vector', 'vector-zip',
  794. 'vector?', 'when', 'when-first', 'when-let', 'when-not',
  795. 'with-local-vars', 'with-meta', 'with-open', 'with-out-str',
  796. 'xml-seq', 'xml-zip', 'zero?', 'zipmap', 'zipper')
  797. # valid names for identifiers
  798. # well, names can only not consist fully of numbers
  799. # but this should be good enough for now
  800. # TODO / should divide keywords/symbols into namespace/rest
  801. # but that's hard, so just pretend / is part of the name
  802. valid_name = r'(?!#)[\w!$%*+<=>?/.#|-]+'
  803. tokens = {
  804. 'root': [
  805. # the comments - always starting with semicolon
  806. # and going to the end of the line
  807. (r';.*$', Comment.Single),
  808. # whitespaces - usually not relevant
  809. (r',+', Text),
  810. (r'\s+', Whitespace),
  811. # numbers
  812. (r'-?\d+\.\d+', Number.Float),
  813. (r'-?\d+/\d+', Number),
  814. (r'-?\d+', Number.Integer),
  815. (r'0x-?[abcdef\d]+', Number.Hex),
  816. # strings, symbols and characters
  817. (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
  818. (r"'" + valid_name, String.Symbol),
  819. (r"\\(.|[a-z]+)", String.Char),
  820. # keywords
  821. (r'::?#?' + valid_name, String.Symbol),
  822. # special operators
  823. (r'~@|[`\'#^~&@]', Operator),
  824. # highlight the special forms
  825. (words(special_forms, suffix=' '), Keyword),
  826. # Technically, only the special forms are 'keywords'. The problem
  827. # is that only treating them as keywords means that things like
  828. # 'defn' and 'ns' need to be highlighted as builtins. This is ugly
  829. # and weird for most styles. So, as a compromise we're going to
  830. # highlight them as Keyword.Declarations.
  831. (words(declarations, suffix=' '), Keyword.Declaration),
  832. # highlight the builtins
  833. (words(builtins, suffix=' '), Name.Builtin),
  834. # the remaining functions
  835. (r'(?<=\()' + valid_name, Name.Function),
  836. # find the remaining variables
  837. (valid_name, Name.Variable),
  838. # Clojure accepts vector notation
  839. (r'(\[|\])', Punctuation),
  840. # Clojure accepts map notation
  841. (r'(\{|\})', Punctuation),
  842. # the famous parentheses!
  843. (r'(\(|\))', Punctuation),
  844. ],
  845. }
  846. class ClojureScriptLexer(ClojureLexer):
  847. """
  848. Lexer for ClojureScript source code.
  849. """
  850. name = 'ClojureScript'
  851. url = 'http://clojure.org/clojurescript'
  852. aliases = ['clojurescript', 'cljs']
  853. filenames = ['*.cljs']
  854. mimetypes = ['text/x-clojurescript', 'application/x-clojurescript']
  855. version_added = '2.0'
  856. class TeaLangLexer(RegexLexer):
  857. """
  858. For Tea source code. Only used within a
  859. TeaTemplateLexer.
  860. .. versionadded:: 1.5
  861. """
  862. flags = re.MULTILINE | re.DOTALL
  863. tokens = {
  864. 'root': [
  865. # method names
  866. (r'^(\s*(?:[a-zA-Z_][\w\.\[\]]*\s+)+?)' # return arguments
  867. r'([a-zA-Z_]\w*)' # method name
  868. r'(\s*)(\()', # signature start
  869. bygroups(using(this), Name.Function, Whitespace, Operator)),
  870. (r'[^\S\n]+', Whitespace),
  871. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  872. (r'/\*.*?\*/', Comment.Multiline),
  873. (r'@[a-zA-Z_][\w\.]*', Name.Decorator),
  874. (r'(and|break|else|foreach|if|in|not|or|reverse)\b',
  875. Keyword),
  876. (r'(as|call|define)\b', Keyword.Declaration),
  877. (r'(true|false|null)\b', Keyword.Constant),
  878. (r'(template)(\s+)', bygroups(Keyword.Declaration, Whitespace), 'template'),
  879. (r'(import)(\s+)', bygroups(Keyword.Namespace, Whitespace), 'import'),
  880. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  881. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  882. (r'(\.)([a-zA-Z_]\w*)', bygroups(Operator, Name.Attribute)),
  883. (r'[a-zA-Z_]\w*:', Name.Label),
  884. (r'[a-zA-Z_\$]\w*', Name),
  885. (r'(isa|[.]{3}|[.]{2}|[=#!<>+-/%&;,.\*\\\(\)\[\]\{\}])', Operator),
  886. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  887. (r'0x[0-9a-fA-F]+', Number.Hex),
  888. (r'[0-9]+L?', Number.Integer),
  889. (r'\n', Whitespace)
  890. ],
  891. 'template': [
  892. (r'[a-zA-Z_]\w*', Name.Class, '#pop')
  893. ],
  894. 'import': [
  895. (r'[\w.]+\*?', Name.Namespace, '#pop')
  896. ],
  897. }
  898. class CeylonLexer(RegexLexer):
  899. """
  900. For Ceylon source code.
  901. """
  902. name = 'Ceylon'
  903. url = 'http://ceylon-lang.org/'
  904. aliases = ['ceylon']
  905. filenames = ['*.ceylon']
  906. mimetypes = ['text/x-ceylon']
  907. version_added = '1.6'
  908. flags = re.MULTILINE | re.DOTALL
  909. #: optional Comment or Whitespace
  910. _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
  911. tokens = {
  912. 'root': [
  913. # method names
  914. (r'^(\s*(?:[a-zA-Z_][\w.\[\]]*\s+)+?)' # return arguments
  915. r'([a-zA-Z_]\w*)' # method name
  916. r'(\s*)(\()', # signature start
  917. bygroups(using(this), Name.Function, Whitespace, Operator)),
  918. (r'[^\S\n]+', Whitespace),
  919. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  920. (r'/\*', Comment.Multiline, 'comment'),
  921. (r'(shared|abstract|formal|default|actual|variable|deprecated|small|'
  922. r'late|literal|doc|by|see|throws|optional|license|tagged|final|native|'
  923. r'annotation|sealed)\b', Name.Decorator),
  924. (r'(break|case|catch|continue|else|finally|for|in|'
  925. r'if|return|switch|this|throw|try|while|is|exists|dynamic|'
  926. r'nonempty|then|outer|assert|let)\b', Keyword),
  927. (r'(abstracts|extends|satisfies|'
  928. r'super|given|of|out|assign)\b', Keyword.Declaration),
  929. (r'(function|value|void|new)\b',
  930. Keyword.Type),
  931. (r'(assembly|module|package)(\s+)', bygroups(Keyword.Namespace, Whitespace)),
  932. (r'(true|false|null)\b', Keyword.Constant),
  933. (r'(class|interface|object|alias)(\s+)',
  934. bygroups(Keyword.Declaration, Whitespace), 'class'),
  935. (r'(import)(\s+)', bygroups(Keyword.Namespace, Whitespace), 'import'),
  936. (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
  937. (r"'\\.'|'[^\\]'|'\\\{#[0-9a-fA-F]{4}\}'", String.Char),
  938. (r'(\.)([a-z_]\w*)',
  939. bygroups(Operator, Name.Attribute)),
  940. (r'[a-zA-Z_]\w*:', Name.Label),
  941. (r'[a-zA-Z_]\w*', Name),
  942. (r'[~^*!%&\[\](){}<>|+=:;,./?-]', Operator),
  943. (r'\d{1,3}(_\d{3})+\.\d{1,3}(_\d{3})+[kMGTPmunpf]?', Number.Float),
  944. (r'\d{1,3}(_\d{3})+\.[0-9]+([eE][+-]?[0-9]+)?[kMGTPmunpf]?',
  945. Number.Float),
  946. (r'[0-9][0-9]*\.\d{1,3}(_\d{3})+[kMGTPmunpf]?', Number.Float),
  947. (r'[0-9][0-9]*\.[0-9]+([eE][+-]?[0-9]+)?[kMGTPmunpf]?',
  948. Number.Float),
  949. (r'#([0-9a-fA-F]{4})(_[0-9a-fA-F]{4})+', Number.Hex),
  950. (r'#[0-9a-fA-F]+', Number.Hex),
  951. (r'\$([01]{4})(_[01]{4})+', Number.Bin),
  952. (r'\$[01]+', Number.Bin),
  953. (r'\d{1,3}(_\d{3})+[kMGTP]?', Number.Integer),
  954. (r'[0-9]+[kMGTP]?', Number.Integer),
  955. (r'\n', Whitespace)
  956. ],
  957. 'class': [
  958. (r'[A-Za-z_]\w*', Name.Class, '#pop')
  959. ],
  960. 'import': [
  961. (r'[a-z][\w.]*',
  962. Name.Namespace, '#pop')
  963. ],
  964. 'comment': [
  965. (r'[^*/]', Comment.Multiline),
  966. (r'/\*', Comment.Multiline, '#push'),
  967. (r'\*/', Comment.Multiline, '#pop'),
  968. (r'[*/]', Comment.Multiline)
  969. ],
  970. }
  971. class KotlinLexer(RegexLexer):
  972. """
  973. For Kotlin source code.
  974. """
  975. name = 'Kotlin'
  976. url = 'http://kotlinlang.org/'
  977. aliases = ['kotlin']
  978. filenames = ['*.kt', '*.kts']
  979. mimetypes = ['text/x-kotlin']
  980. version_added = '1.5'
  981. flags = re.MULTILINE | re.DOTALL
  982. kt_name = ('@?[_' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl') + ']' +
  983. '[' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl', 'Nd', 'Pc', 'Cf',
  984. 'Mn', 'Mc') + ']*')
  985. kt_space_name = ('@?[_' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl') + ']' +
  986. '[' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl', 'Nd', 'Pc', 'Cf',
  987. 'Mn', 'Mc', 'Zs')
  988. + r'\'~!%^&*()+=|\[\]:;,.<>/\?-]*')
  989. kt_id = '(' + kt_name + '|`' + kt_space_name + '`)'
  990. modifiers = (r'actual|abstract|annotation|companion|const|crossinline|'
  991. r'data|enum|expect|external|final|infix|inline|inner|'
  992. r'internal|lateinit|noinline|open|operator|override|private|'
  993. r'protected|public|sealed|suspend|tailrec|value')
  994. tokens = {
  995. 'root': [
  996. # Whitespaces
  997. (r'[^\S\n]+', Whitespace),
  998. (r'\s+', Whitespace),
  999. (r'\\$', String.Escape), # line continuation
  1000. (r'\n', Whitespace),
  1001. # Comments
  1002. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  1003. (r'^(#!/.+?)(\n)', bygroups(Comment.Single, Whitespace)), # shebang for kotlin scripts
  1004. (r'/[*].*?[*]/', Comment.Multiline),
  1005. # Keywords
  1006. (r'as\?', Keyword),
  1007. (r'(as|break|by|catch|constructor|continue|do|dynamic|else|finally|'
  1008. r'get|for|if|init|[!]*in|[!]*is|out|reified|return|set|super|this|'
  1009. r'throw|try|typealias|typeof|vararg|when|where|while)\b', Keyword),
  1010. (r'it\b', Name.Builtin),
  1011. # Built-in types
  1012. (words(('Boolean?', 'Byte?', 'Char?', 'Double?', 'Float?',
  1013. 'Int?', 'Long?', 'Short?', 'String?', 'Any?', 'Unit?')), Keyword.Type),
  1014. (words(('Boolean', 'Byte', 'Char', 'Double', 'Float',
  1015. 'Int', 'Long', 'Short', 'String', 'Any', 'Unit'), suffix=r'\b'), Keyword.Type),
  1016. # Constants
  1017. (r'(true|false|null)\b', Keyword.Constant),
  1018. # Imports
  1019. (r'(package|import)(\s+)(\S+)', bygroups(Keyword, Whitespace, Name.Namespace)),
  1020. # Dot access
  1021. (r'(\?\.)((?:[^\W\d]|\$)[\w$]*)', bygroups(Operator, Name.Attribute)),
  1022. (r'(\.)((?:[^\W\d]|\$)[\w$]*)', bygroups(Punctuation, Name.Attribute)),
  1023. # Annotations
  1024. (r'@[^\W\d][\w.]*', Name.Decorator),
  1025. # Labels
  1026. (r'[^\W\d][\w.]+@', Name.Decorator),
  1027. # Object expression
  1028. (r'(object)(\s+)(:)(\s+)', bygroups(Keyword, Whitespace, Punctuation, Whitespace), 'class'),
  1029. # Types
  1030. (r'((?:(?:' + modifiers + r'|fun)\s+)*)(class|interface|object)(\s+)',
  1031. bygroups(using(this, state='modifiers'), Keyword.Declaration, Whitespace), 'class'),
  1032. # Variables
  1033. (r'(var|val)(\s+)(\()', bygroups(Keyword.Declaration, Whitespace, Punctuation),
  1034. 'destructuring_assignment'),
  1035. (r'((?:(?:' + modifiers + r')\s+)*)(var|val)(\s+)',
  1036. bygroups(using(this, state='modifiers'), Keyword.Declaration, Whitespace), 'variable'),
  1037. # Functions
  1038. (r'((?:(?:' + modifiers + r')\s+)*)(fun)(\s+)',
  1039. bygroups(using(this, state='modifiers'), Keyword.Declaration, Whitespace), 'function'),
  1040. # Operators
  1041. (r'::|!!|\?[:.]', Operator),
  1042. (r'[~^*!%&\[\]<>|+=/?-]', Operator),
  1043. # Punctuation
  1044. (r'[{}();:.,]', Punctuation),
  1045. # Strings
  1046. (r'"""', String, 'multiline_string'),
  1047. (r'"', String, 'string'),
  1048. (r"'\\.'|'[^\\]'", String.Char),
  1049. # Numbers
  1050. (r"[0-9](\.[0-9]*)?([eE][+-][0-9]+)?[flFL]?|"
  1051. r"0[xX][0-9a-fA-F]+[Ll]?", Number),
  1052. # Identifiers
  1053. (r'' + kt_id + r'((\?[^.])?)', Name) # additionally handle nullable types
  1054. ],
  1055. 'class': [
  1056. (kt_id, Name.Class, '#pop')
  1057. ],
  1058. 'variable': [
  1059. (kt_id, Name.Variable, '#pop')
  1060. ],
  1061. 'destructuring_assignment': [
  1062. (r',', Punctuation),
  1063. (r'\s+', Whitespace),
  1064. (kt_id, Name.Variable),
  1065. (r'(:)(\s+)(' + kt_id + ')', bygroups(Punctuation, Whitespace, Name)),
  1066. (r'<', Operator, 'generic'),
  1067. (r'\)', Punctuation, '#pop')
  1068. ],
  1069. 'function': [
  1070. (r'<', Operator, 'generic'),
  1071. (r'' + kt_id + r'(\.)' + kt_id, bygroups(Name, Punctuation, Name.Function), '#pop'),
  1072. (kt_id, Name.Function, '#pop')
  1073. ],
  1074. 'generic': [
  1075. (r'(>)(\s*)', bygroups(Operator, Whitespace), '#pop'),
  1076. (r':', Punctuation),
  1077. (r'(reified|out|in)\b', Keyword),
  1078. (r',', Punctuation),
  1079. (r'\s+', Whitespace),
  1080. (kt_id, Name)
  1081. ],
  1082. 'modifiers': [
  1083. (r'\w+', Keyword.Declaration),
  1084. (r'\s+', Whitespace),
  1085. default('#pop')
  1086. ],
  1087. 'string': [
  1088. (r'"', String, '#pop'),
  1089. include('string_common')
  1090. ],
  1091. 'multiline_string': [
  1092. (r'"""', String, '#pop'),
  1093. (r'"', String),
  1094. include('string_common')
  1095. ],
  1096. 'string_common': [
  1097. (r'\\\\', String), # escaped backslash
  1098. (r'\\"', String), # escaped quote
  1099. (r'\\', String), # bare backslash
  1100. (r'\$\{', String.Interpol, 'interpolation'),
  1101. (r'(\$)(\w+)', bygroups(String.Interpol, Name)),
  1102. (r'[^\\"$]+', String)
  1103. ],
  1104. 'interpolation': [
  1105. (r'"', String),
  1106. (r'\$\{', String.Interpol, 'interpolation'),
  1107. (r'\{', Punctuation, 'scope'),
  1108. (r'\}', String.Interpol, '#pop'),
  1109. include('root')
  1110. ],
  1111. 'scope': [
  1112. (r'\{', Punctuation, 'scope'),
  1113. (r'\}', Punctuation, '#pop'),
  1114. include('root')
  1115. ]
  1116. }
  1117. class XtendLexer(RegexLexer):
  1118. """
  1119. For Xtend source code.
  1120. """
  1121. name = 'Xtend'
  1122. url = 'https://www.eclipse.org/xtend/'
  1123. aliases = ['xtend']
  1124. filenames = ['*.xtend']
  1125. mimetypes = ['text/x-xtend']
  1126. version_added = '1.6'
  1127. flags = re.MULTILINE | re.DOTALL
  1128. tokens = {
  1129. 'root': [
  1130. # method names
  1131. (r'^(\s*(?:[a-zA-Z_][\w.\[\]]*\s+)+?)' # return arguments
  1132. r'([a-zA-Z_$][\w$]*)' # method name
  1133. r'(\s*)(\()', # signature start
  1134. bygroups(using(this), Name.Function, Whitespace, Operator)),
  1135. (r'[^\S\n]+', Whitespace),
  1136. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  1137. (r'/\*.*?\*/', Comment.Multiline),
  1138. (r'@[a-zA-Z_][\w.]*', Name.Decorator),
  1139. (r'(assert|break|case|catch|continue|default|do|else|finally|for|'
  1140. r'if|goto|instanceof|new|return|switch|this|throw|try|while|IF|'
  1141. r'ELSE|ELSEIF|ENDIF|FOR|ENDFOR|SEPARATOR|BEFORE|AFTER)\b',
  1142. Keyword),
  1143. (r'(def|abstract|const|enum|extends|final|implements|native|private|'
  1144. r'protected|public|static|strictfp|super|synchronized|throws|'
  1145. r'transient|volatile|val|var)\b', Keyword.Declaration),
  1146. (r'(boolean|byte|char|double|float|int|long|short|void)\b',
  1147. Keyword.Type),
  1148. (r'(package)(\s+)', bygroups(Keyword.Namespace, Whitespace)),
  1149. (r'(true|false|null)\b', Keyword.Constant),
  1150. (r'(class|interface)(\s+)', bygroups(Keyword.Declaration, Whitespace),
  1151. 'class'),
  1152. (r'(import)(\s+)', bygroups(Keyword.Namespace, Whitespace), 'import'),
  1153. (r"(''')", String, 'template'),
  1154. (r'(\u00BB)', String, 'template'),
  1155. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  1156. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  1157. (r'[a-zA-Z_]\w*:', Name.Label),
  1158. (r'[a-zA-Z_$]\w*', Name),
  1159. (r'[~^*!%&\[\](){}<>\|+=:;,./?-]', Operator),
  1160. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  1161. (r'0x[0-9a-fA-F]+', Number.Hex),
  1162. (r'[0-9]+L?', Number.Integer),
  1163. (r'\n', Whitespace)
  1164. ],
  1165. 'class': [
  1166. (r'[a-zA-Z_]\w*', Name.Class, '#pop')
  1167. ],
  1168. 'import': [
  1169. (r'[\w.]+\*?', Name.Namespace, '#pop')
  1170. ],
  1171. 'template': [
  1172. (r"'''", String, '#pop'),
  1173. (r'\u00AB', String, '#pop'),
  1174. (r'.', String)
  1175. ],
  1176. }
  1177. class PigLexer(RegexLexer):
  1178. """
  1179. For Pig Latin source code.
  1180. """
  1181. name = 'Pig'
  1182. url = 'https://pig.apache.org/'
  1183. aliases = ['pig']
  1184. filenames = ['*.pig']
  1185. mimetypes = ['text/x-pig']
  1186. version_added = '2.0'
  1187. flags = re.MULTILINE | re.IGNORECASE
  1188. tokens = {
  1189. 'root': [
  1190. (r'\s+', Whitespace),
  1191. (r'--.*', Comment),
  1192. (r'/\*[\w\W]*?\*/', Comment.Multiline),
  1193. (r'\\$', String.Escape),
  1194. (r'\\', Text),
  1195. (r'\'(?:\\[ntbrf\\\']|\\u[0-9a-f]{4}|[^\'\\\n\r])*\'', String),
  1196. include('keywords'),
  1197. include('types'),
  1198. include('builtins'),
  1199. include('punct'),
  1200. include('operators'),
  1201. (r'[0-9]*\.[0-9]+(e[0-9]+)?[fd]?', Number.Float),
  1202. (r'0x[0-9a-f]+', Number.Hex),
  1203. (r'[0-9]+L?', Number.Integer),
  1204. (r'\n', Whitespace),
  1205. (r'([a-z_]\w*)(\s*)(\()',
  1206. bygroups(Name.Function, Whitespace, Punctuation)),
  1207. (r'[()#:]', Text),
  1208. (r'[^(:#\'")\s]+', Text),
  1209. (r'\S+\s+', Text) # TODO: make tests pass without \s+
  1210. ],
  1211. 'keywords': [
  1212. (r'(assert|and|any|all|arrange|as|asc|bag|by|cache|CASE|cat|cd|cp|'
  1213. r'%declare|%default|define|dense|desc|describe|distinct|du|dump|'
  1214. r'eval|exex|explain|filter|flatten|foreach|full|generate|group|'
  1215. r'help|if|illustrate|import|inner|input|into|is|join|kill|left|'
  1216. r'limit|load|ls|map|matches|mkdir|mv|not|null|onschema|or|order|'
  1217. r'outer|output|parallel|pig|pwd|quit|register|returns|right|rm|'
  1218. r'rmf|rollup|run|sample|set|ship|split|stderr|stdin|stdout|store|'
  1219. r'stream|through|union|using|void)\b', Keyword)
  1220. ],
  1221. 'builtins': [
  1222. (r'(AVG|BinStorage|cogroup|CONCAT|copyFromLocal|copyToLocal|COUNT|'
  1223. r'cross|DIFF|MAX|MIN|PigDump|PigStorage|SIZE|SUM|TextLoader|'
  1224. r'TOKENIZE)\b', Name.Builtin)
  1225. ],
  1226. 'types': [
  1227. (r'(bytearray|BIGINTEGER|BIGDECIMAL|chararray|datetime|double|float|'
  1228. r'int|long|tuple)\b', Keyword.Type)
  1229. ],
  1230. 'punct': [
  1231. (r'[;(){}\[\]]', Punctuation),
  1232. ],
  1233. 'operators': [
  1234. (r'[#=,./%+\-?]', Operator),
  1235. (r'(eq|gt|lt|gte|lte|neq|matches)\b', Operator),
  1236. (r'(==|<=|<|>=|>|!=)', Operator),
  1237. ],
  1238. }
  1239. class GoloLexer(RegexLexer):
  1240. """
  1241. For Golo source code.
  1242. """
  1243. name = 'Golo'
  1244. url = 'http://golo-lang.org/'
  1245. filenames = ['*.golo']
  1246. aliases = ['golo']
  1247. version_added = '2.0'
  1248. tokens = {
  1249. 'root': [
  1250. (r'[^\S\n]+', Whitespace),
  1251. (r'#.*$', Comment),
  1252. (r'(\^|\.\.\.|:|\?:|->|==|!=|=|\+|\*|%|/|<=|<|>=|>|=|\.)',
  1253. Operator),
  1254. (r'(?<=[^-])(-)(?=[^-])', Operator),
  1255. (r'(?<=[^`])(is|isnt|and|or|not|oftype|in|orIfNull)\b', Operator.Word),
  1256. (r'[]{}|(),[]', Punctuation),
  1257. (r'(module|import)(\s+)',
  1258. bygroups(Keyword.Namespace, Whitespace),
  1259. 'modname'),
  1260. (r'\b([a-zA-Z_][\w$.]*)(::)', bygroups(Name.Namespace, Punctuation)),
  1261. (r'\b([a-zA-Z_][\w$]*(?:\.[a-zA-Z_][\w$]*)+)\b', Name.Namespace),
  1262. (r'(let|var)(\s+)',
  1263. bygroups(Keyword.Declaration, Whitespace),
  1264. 'varname'),
  1265. (r'(struct)(\s+)',
  1266. bygroups(Keyword.Declaration, Whitespace),
  1267. 'structname'),
  1268. (r'(function)(\s+)',
  1269. bygroups(Keyword.Declaration, Whitespace),
  1270. 'funcname'),
  1271. (r'(null|true|false)\b', Keyword.Constant),
  1272. (r'(augment|pimp'
  1273. r'|if|else|case|match|return'
  1274. r'|case|when|then|otherwise'
  1275. r'|while|for|foreach'
  1276. r'|try|catch|finally|throw'
  1277. r'|local'
  1278. r'|continue|break)\b', Keyword),
  1279. (r'(map|array|list|set|vector|tuple)(\[)',
  1280. bygroups(Name.Builtin, Punctuation)),
  1281. (r'(print|println|readln|raise|fun'
  1282. r'|asInterfaceInstance)\b', Name.Builtin),
  1283. (r'(`?[a-zA-Z_][\w$]*)(\()',
  1284. bygroups(Name.Function, Punctuation)),
  1285. (r'-?[\d_]*\.[\d_]*([eE][+-]?\d[\d_]*)?F?', Number.Float),
  1286. (r'0[0-7]+j?', Number.Oct),
  1287. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  1288. (r'-?\d[\d_]*L', Number.Integer.Long),
  1289. (r'-?\d[\d_]*', Number.Integer),
  1290. (r'`?[a-zA-Z_][\w$]*', Name),
  1291. (r'@[a-zA-Z_][\w$.]*', Name.Decorator),
  1292. (r'"""', String, combined('stringescape', 'triplestring')),
  1293. (r'"', String, combined('stringescape', 'doublestring')),
  1294. (r"'", String, combined('stringescape', 'singlestring')),
  1295. (r'----((.|\n)*?)----', String.Doc)
  1296. ],
  1297. 'funcname': [
  1298. (r'`?[a-zA-Z_][\w$]*', Name.Function, '#pop'),
  1299. ],
  1300. 'modname': [
  1301. (r'[a-zA-Z_][\w$.]*\*?', Name.Namespace, '#pop')
  1302. ],
  1303. 'structname': [
  1304. (r'`?[\w.]+\*?', Name.Class, '#pop')
  1305. ],
  1306. 'varname': [
  1307. (r'`?[a-zA-Z_][\w$]*', Name.Variable, '#pop'),
  1308. ],
  1309. 'string': [
  1310. (r'[^\\\'"\n]+', String),
  1311. (r'[\'"\\]', String)
  1312. ],
  1313. 'stringescape': [
  1314. (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  1315. r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
  1316. ],
  1317. 'triplestring': [
  1318. (r'"""', String, '#pop'),
  1319. include('string'),
  1320. (r'\n', String),
  1321. ],
  1322. 'doublestring': [
  1323. (r'"', String.Double, '#pop'),
  1324. include('string'),
  1325. ],
  1326. 'singlestring': [
  1327. (r"'", String, '#pop'),
  1328. include('string'),
  1329. ],
  1330. 'operators': [
  1331. (r'[#=,./%+\-?]', Operator),
  1332. (r'(eq|gt|lt|gte|lte|neq|matches)\b', Operator),
  1333. (r'(==|<=|<|>=|>|!=)', Operator),
  1334. ],
  1335. }
  1336. class JasminLexer(RegexLexer):
  1337. """
  1338. For Jasmin assembly code.
  1339. """
  1340. name = 'Jasmin'
  1341. url = 'http://jasmin.sourceforge.net/'
  1342. aliases = ['jasmin', 'jasminxt']
  1343. filenames = ['*.j']
  1344. version_added = '2.0'
  1345. _whitespace = r' \n\t\r'
  1346. _ws = rf'(?:[{_whitespace}]+)'
  1347. _separator = rf'{_whitespace}:='
  1348. _break = rf'(?=[{_separator}]|$)'
  1349. _name = rf'[^{_separator}]+'
  1350. _unqualified_name = rf'(?:[^{_separator}.;\[/]+)'
  1351. tokens = {
  1352. 'default': [
  1353. (r'\n', Whitespace, '#pop'),
  1354. (r"'", String.Single, ('#pop', 'quote')),
  1355. (r'"', String.Double, 'string'),
  1356. (r'=', Punctuation),
  1357. (r':', Punctuation, 'label'),
  1358. (_ws, Whitespace),
  1359. (r';.*', Comment.Single),
  1360. (rf'(\$[-+])?0x-?[\da-fA-F]+{_break}', Number.Hex),
  1361. (rf'(\$[-+]|\+)?-?\d+{_break}', Number.Integer),
  1362. (r'-?(\d+\.\d*|\.\d+)([eE][-+]?\d+)?[fFdD]?'
  1363. rf'[\x00-\x08\x0b\x0c\x0e-\x1f]*{_break}', Number.Float),
  1364. (rf'\${_name}', Name.Variable),
  1365. # Directives
  1366. (rf'\.annotation{_break}', Keyword.Reserved, 'annotation'),
  1367. (r'(\.attribute|\.bytecode|\.debug|\.deprecated|\.enclosing|'
  1368. r'\.interface|\.line|\.signature|\.source|\.stack|\.var|abstract|'
  1369. r'annotation|bridge|class|default|enum|field|final|fpstrict|'
  1370. r'interface|native|private|protected|public|signature|static|'
  1371. rf'synchronized|synthetic|transient|varargs|volatile){_break}',
  1372. Keyword.Reserved),
  1373. (rf'\.catch{_break}', Keyword.Reserved, 'caught-exception'),
  1374. (r'(\.class|\.implements|\.inner|\.super|inner|invisible|'
  1375. rf'invisibleparam|outer|visible|visibleparam){_break}',
  1376. Keyword.Reserved, 'class/convert-dots'),
  1377. (rf'\.field{_break}', Keyword.Reserved,
  1378. ('descriptor/convert-dots', 'field')),
  1379. (rf'(\.end|\.limit|use){_break}', Keyword.Reserved,
  1380. 'no-verification'),
  1381. (rf'\.method{_break}', Keyword.Reserved, 'method'),
  1382. (rf'\.set{_break}', Keyword.Reserved, 'var'),
  1383. (rf'\.throws{_break}', Keyword.Reserved, 'exception'),
  1384. (rf'(from|offset|to|using){_break}', Keyword.Reserved, 'label'),
  1385. (rf'is{_break}', Keyword.Reserved,
  1386. ('descriptor/convert-dots', 'var')),
  1387. (rf'(locals|stack){_break}', Keyword.Reserved, 'verification'),
  1388. (rf'method{_break}', Keyword.Reserved, 'enclosing-method'),
  1389. # Instructions
  1390. (words((
  1391. 'aaload', 'aastore', 'aconst_null', 'aload', 'aload_0', 'aload_1', 'aload_2',
  1392. 'aload_3', 'aload_w', 'areturn', 'arraylength', 'astore', 'astore_0', 'astore_1',
  1393. 'astore_2', 'astore_3', 'astore_w', 'athrow', 'baload', 'bastore', 'bipush',
  1394. 'breakpoint', 'caload', 'castore', 'd2f', 'd2i', 'd2l', 'dadd', 'daload', 'dastore',
  1395. 'dcmpg', 'dcmpl', 'dconst_0', 'dconst_1', 'ddiv', 'dload', 'dload_0', 'dload_1',
  1396. 'dload_2', 'dload_3', 'dload_w', 'dmul', 'dneg', 'drem', 'dreturn', 'dstore', 'dstore_0',
  1397. 'dstore_1', 'dstore_2', 'dstore_3', 'dstore_w', 'dsub', 'dup', 'dup2', 'dup2_x1',
  1398. 'dup2_x2', 'dup_x1', 'dup_x2', 'f2d', 'f2i', 'f2l', 'fadd', 'faload', 'fastore', 'fcmpg',
  1399. 'fcmpl', 'fconst_0', 'fconst_1', 'fconst_2', 'fdiv', 'fload', 'fload_0', 'fload_1',
  1400. 'fload_2', 'fload_3', 'fload_w', 'fmul', 'fneg', 'frem', 'freturn', 'fstore', 'fstore_0',
  1401. 'fstore_1', 'fstore_2', 'fstore_3', 'fstore_w', 'fsub', 'i2b', 'i2c', 'i2d', 'i2f', 'i2l',
  1402. 'i2s', 'iadd', 'iaload', 'iand', 'iastore', 'iconst_0', 'iconst_1', 'iconst_2',
  1403. 'iconst_3', 'iconst_4', 'iconst_5', 'iconst_m1', 'idiv', 'iinc', 'iinc_w', 'iload',
  1404. 'iload_0', 'iload_1', 'iload_2', 'iload_3', 'iload_w', 'imul', 'ineg', 'int2byte',
  1405. 'int2char', 'int2short', 'ior', 'irem', 'ireturn', 'ishl', 'ishr', 'istore', 'istore_0',
  1406. 'istore_1', 'istore_2', 'istore_3', 'istore_w', 'isub', 'iushr', 'ixor', 'l2d', 'l2f',
  1407. 'l2i', 'ladd', 'laload', 'land', 'lastore', 'lcmp', 'lconst_0', 'lconst_1', 'ldc2_w',
  1408. 'ldiv', 'lload', 'lload_0', 'lload_1', 'lload_2', 'lload_3', 'lload_w', 'lmul', 'lneg',
  1409. 'lookupswitch', 'lor', 'lrem', 'lreturn', 'lshl', 'lshr', 'lstore', 'lstore_0',
  1410. 'lstore_1', 'lstore_2', 'lstore_3', 'lstore_w', 'lsub', 'lushr', 'lxor',
  1411. 'monitorenter', 'monitorexit', 'nop', 'pop', 'pop2', 'ret', 'ret_w', 'return', 'saload',
  1412. 'sastore', 'sipush', 'swap'), suffix=_break), Keyword.Reserved),
  1413. (rf'(anewarray|checkcast|instanceof|ldc|ldc_w|new){_break}',
  1414. Keyword.Reserved, 'class/no-dots'),
  1415. (r'invoke(dynamic|interface|nonvirtual|special|'
  1416. rf'static|virtual){_break}', Keyword.Reserved,
  1417. 'invocation'),
  1418. (rf'(getfield|putfield){_break}', Keyword.Reserved,
  1419. ('descriptor/no-dots', 'field')),
  1420. (rf'(getstatic|putstatic){_break}', Keyword.Reserved,
  1421. ('descriptor/no-dots', 'static')),
  1422. (words((
  1423. 'goto', 'goto_w', 'if_acmpeq', 'if_acmpne', 'if_icmpeq',
  1424. 'if_icmpge', 'if_icmpgt', 'if_icmple', 'if_icmplt', 'if_icmpne',
  1425. 'ifeq', 'ifge', 'ifgt', 'ifle', 'iflt', 'ifne', 'ifnonnull',
  1426. 'ifnull', 'jsr', 'jsr_w'), suffix=_break),
  1427. Keyword.Reserved, 'label'),
  1428. (rf'(multianewarray|newarray){_break}', Keyword.Reserved,
  1429. 'descriptor/convert-dots'),
  1430. (rf'tableswitch{_break}', Keyword.Reserved, 'table')
  1431. ],
  1432. 'quote': [
  1433. (r"'", String.Single, '#pop'),
  1434. (r'\\u[\da-fA-F]{4}', String.Escape),
  1435. (r"[^'\\]+", String.Single)
  1436. ],
  1437. 'string': [
  1438. (r'"', String.Double, '#pop'),
  1439. (r'\\([nrtfb"\'\\]|u[\da-fA-F]{4}|[0-3]?[0-7]{1,2})',
  1440. String.Escape),
  1441. (r'[^"\\]+', String.Double)
  1442. ],
  1443. 'root': [
  1444. (r'\n+', Whitespace),
  1445. (r"'", String.Single, 'quote'),
  1446. include('default'),
  1447. (rf'({_name})([ \t\r]*)(:)',
  1448. bygroups(Name.Label, Whitespace, Punctuation)),
  1449. (_name, String.Other)
  1450. ],
  1451. 'annotation': [
  1452. (r'\n', Whitespace, ('#pop', 'annotation-body')),
  1453. (rf'default{_break}', Keyword.Reserved,
  1454. ('#pop', 'annotation-default')),
  1455. include('default')
  1456. ],
  1457. 'annotation-body': [
  1458. (r'\n+', Whitespace),
  1459. (rf'\.end{_break}', Keyword.Reserved, '#pop'),
  1460. include('default'),
  1461. (_name, String.Other, ('annotation-items', 'descriptor/no-dots'))
  1462. ],
  1463. 'annotation-default': [
  1464. (r'\n+', Whitespace),
  1465. (rf'\.end{_break}', Keyword.Reserved, '#pop'),
  1466. include('default'),
  1467. default(('annotation-items', 'descriptor/no-dots'))
  1468. ],
  1469. 'annotation-items': [
  1470. (r"'", String.Single, 'quote'),
  1471. include('default'),
  1472. (_name, String.Other)
  1473. ],
  1474. 'caught-exception': [
  1475. (rf'all{_break}', Keyword, '#pop'),
  1476. include('exception')
  1477. ],
  1478. 'class/convert-dots': [
  1479. include('default'),
  1480. (rf'(L)((?:{_unqualified_name}[/.])*)({_name})(;)',
  1481. bygroups(Keyword.Type, Name.Namespace, Name.Class, Punctuation),
  1482. '#pop'),
  1483. (rf'((?:{_unqualified_name}[/.])*)({_name})',
  1484. bygroups(Name.Namespace, Name.Class), '#pop')
  1485. ],
  1486. 'class/no-dots': [
  1487. include('default'),
  1488. (r'\[+', Punctuation, ('#pop', 'descriptor/no-dots')),
  1489. (rf'(L)((?:{_unqualified_name}/)*)({_name})(;)',
  1490. bygroups(Keyword.Type, Name.Namespace, Name.Class, Punctuation),
  1491. '#pop'),
  1492. (rf'((?:{_unqualified_name}/)*)({_name})',
  1493. bygroups(Name.Namespace, Name.Class), '#pop')
  1494. ],
  1495. 'descriptor/convert-dots': [
  1496. include('default'),
  1497. (r'\[+', Punctuation),
  1498. (rf'(L)((?:{_unqualified_name}[/.])*)({_name}?)(;)',
  1499. bygroups(Keyword.Type, Name.Namespace, Name.Class, Punctuation),
  1500. '#pop'),
  1501. (rf'[^{_separator}\[)L]+', Keyword.Type, '#pop'),
  1502. default('#pop')
  1503. ],
  1504. 'descriptor/no-dots': [
  1505. include('default'),
  1506. (r'\[+', Punctuation),
  1507. (rf'(L)((?:{_unqualified_name}/)*)({_name})(;)',
  1508. bygroups(Keyword.Type, Name.Namespace, Name.Class, Punctuation),
  1509. '#pop'),
  1510. (rf'[^{_separator}\[)L]+', Keyword.Type, '#pop'),
  1511. default('#pop')
  1512. ],
  1513. 'descriptors/convert-dots': [
  1514. (r'\)', Punctuation, '#pop'),
  1515. default('descriptor/convert-dots')
  1516. ],
  1517. 'enclosing-method': [
  1518. (_ws, Whitespace),
  1519. (rf'(?=[^{_separator}]*\()', Text, ('#pop', 'invocation')),
  1520. default(('#pop', 'class/convert-dots'))
  1521. ],
  1522. 'exception': [
  1523. include('default'),
  1524. (rf'((?:{_unqualified_name}[/.])*)({_name})',
  1525. bygroups(Name.Namespace, Name.Exception), '#pop')
  1526. ],
  1527. 'field': [
  1528. (rf'static{_break}', Keyword.Reserved, ('#pop', 'static')),
  1529. include('default'),
  1530. (rf'((?:{_unqualified_name}[/.](?=[^{_separator}]*[/.]))*)({_unqualified_name}[/.])?({_name})',
  1531. bygroups(Name.Namespace, Name.Class, Name.Variable.Instance),
  1532. '#pop')
  1533. ],
  1534. 'invocation': [
  1535. include('default'),
  1536. (rf'((?:{_unqualified_name}[/.](?=[^{_separator}(]*[/.]))*)({_unqualified_name}[/.])?({_name})(\()',
  1537. bygroups(Name.Namespace, Name.Class, Name.Function, Punctuation),
  1538. ('#pop', 'descriptor/convert-dots', 'descriptors/convert-dots',
  1539. 'descriptor/convert-dots'))
  1540. ],
  1541. 'label': [
  1542. include('default'),
  1543. (_name, Name.Label, '#pop')
  1544. ],
  1545. 'method': [
  1546. include('default'),
  1547. (rf'({_name})(\()', bygroups(Name.Function, Punctuation),
  1548. ('#pop', 'descriptor/convert-dots', 'descriptors/convert-dots',
  1549. 'descriptor/convert-dots'))
  1550. ],
  1551. 'no-verification': [
  1552. (rf'(locals|method|stack){_break}', Keyword.Reserved, '#pop'),
  1553. include('default')
  1554. ],
  1555. 'static': [
  1556. include('default'),
  1557. (rf'((?:{_unqualified_name}[/.](?=[^{_separator}]*[/.]))*)({_unqualified_name}[/.])?({_name})',
  1558. bygroups(Name.Namespace, Name.Class, Name.Variable.Class), '#pop')
  1559. ],
  1560. 'table': [
  1561. (r'\n+', Whitespace),
  1562. (rf'default{_break}', Keyword.Reserved, '#pop'),
  1563. include('default'),
  1564. (_name, Name.Label)
  1565. ],
  1566. 'var': [
  1567. include('default'),
  1568. (_name, Name.Variable, '#pop')
  1569. ],
  1570. 'verification': [
  1571. include('default'),
  1572. (rf'(Double|Float|Integer|Long|Null|Top|UninitializedThis){_break}', Keyword, '#pop'),
  1573. (rf'Object{_break}', Keyword, ('#pop', 'class/no-dots')),
  1574. (rf'Uninitialized{_break}', Keyword, ('#pop', 'label'))
  1575. ]
  1576. }
  1577. def analyse_text(text):
  1578. score = 0
  1579. if re.search(r'^\s*\.class\s', text, re.MULTILINE):
  1580. score += 0.5
  1581. if re.search(r'^\s*[a-z]+_[a-z]+\b', text, re.MULTILINE):
  1582. score += 0.3
  1583. if re.search(r'^\s*\.(attribute|bytecode|debug|deprecated|enclosing|'
  1584. r'inner|interface|limit|set|signature|stack)\b', text,
  1585. re.MULTILINE):
  1586. score += 0.6
  1587. return min(score, 1.0)
  1588. class SarlLexer(RegexLexer):
  1589. """
  1590. For SARL source code.
  1591. """
  1592. name = 'SARL'
  1593. url = 'http://www.sarl.io'
  1594. aliases = ['sarl']
  1595. filenames = ['*.sarl']
  1596. mimetypes = ['text/x-sarl']
  1597. version_added = '2.4'
  1598. flags = re.MULTILINE | re.DOTALL
  1599. tokens = {
  1600. 'root': [
  1601. # method names
  1602. (r'^(\s*(?:[a-zA-Z_][\w.\[\]]*\s+)+?)' # return arguments
  1603. r'([a-zA-Z_$][\w$]*)' # method name
  1604. r'(\s*)(\()', # signature start
  1605. bygroups(using(this), Name.Function, Whitespace, Operator)),
  1606. (r'[^\S\n]+', Whitespace),
  1607. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  1608. (r'/\*.*?\*/', Comment.Multiline),
  1609. (r'@[a-zA-Z_][\w.]*', Name.Decorator),
  1610. (r'(as|break|case|catch|default|do|else|extends|extension|finally|'
  1611. r'fires|for|if|implements|instanceof|new|on|requires|return|super|'
  1612. r'switch|throw|throws|try|typeof|uses|while|with)\b',
  1613. Keyword),
  1614. (r'(abstract|def|dispatch|final|native|override|private|protected|'
  1615. r'public|static|strictfp|synchronized|transient|val|var|volatile)\b',
  1616. Keyword.Declaration),
  1617. (r'(boolean|byte|char|double|float|int|long|short|void)\b',
  1618. Keyword.Type),
  1619. (r'(package)(\s+)', bygroups(Keyword.Namespace, Whitespace)),
  1620. (r'(false|it|null|occurrence|this|true|void)\b', Keyword.Constant),
  1621. (r'(agent|annotation|artifact|behavior|capacity|class|enum|event|'
  1622. r'interface|skill|space)(\s+)', bygroups(Keyword.Declaration, Whitespace),
  1623. 'class'),
  1624. (r'(import)(\s+)', bygroups(Keyword.Namespace, Whitespace), 'import'),
  1625. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  1626. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  1627. (r'[a-zA-Z_]\w*:', Name.Label),
  1628. (r'[a-zA-Z_$]\w*', Name),
  1629. (r'[~^*!%&\[\](){}<>\|+=:;,./?-]', Operator),
  1630. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  1631. (r'0x[0-9a-fA-F]+', Number.Hex),
  1632. (r'[0-9]+L?', Number.Integer),
  1633. (r'\n', Whitespace)
  1634. ],
  1635. 'class': [
  1636. (r'[a-zA-Z_]\w*', Name.Class, '#pop')
  1637. ],
  1638. 'import': [
  1639. (r'[\w.]+\*?', Name.Namespace, '#pop')
  1640. ],
  1641. }