jvm.py 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  1. """
  2. pygments.lexers.jvm
  3. ~~~~~~~~~~~~~~~~~~~
  4. Pygments lexers for JVM languages.
  5. :copyright: Copyright 2006-2023 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. flags = re.MULTILINE | re.DOTALL
  29. tokens = {
  30. 'root': [
  31. (r'(^\s*)((?:(?:public|private|protected|static|strictfp)(?:\s+))*)(record)\b',
  32. bygroups(Whitespace, using(this), Keyword.Declaration), 'class'),
  33. (r'[^\S\n]+', Whitespace),
  34. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  35. (r'/\*.*?\*/', Comment.Multiline),
  36. # keywords: go before method names to avoid lexing "throw new XYZ"
  37. # as a method signature
  38. (r'(assert|break|case|catch|continue|default|do|else|finally|for|'
  39. r'if|goto|instanceof|new|return|switch|this|throw|try|while)\b',
  40. Keyword),
  41. # method names
  42. (r'((?:(?:[^\W\d]|\$)[\w.\[\]$<>]*\s+)+?)' # return arguments
  43. r'((?:[^\W\d]|\$)[\w$]*)' # method name
  44. r'(\s*)(\()', # signature start
  45. bygroups(using(this), Name.Function, Whitespace, Punctuation)),
  46. (r'@[^\W\d][\w.]*', Name.Decorator),
  47. (r'(abstract|const|enum|extends|final|implements|native|private|'
  48. r'protected|public|sealed|static|strictfp|super|synchronized|throws|'
  49. r'transient|volatile|yield)\b', Keyword.Declaration),
  50. (r'(boolean|byte|char|double|float|int|long|short|void)\b',
  51. Keyword.Type),
  52. (r'(package)(\s+)', bygroups(Keyword.Namespace, Whitespace), 'import'),
  53. (r'(true|false|null)\b', Keyword.Constant),
  54. (r'(class|interface)\b', Keyword.Declaration, 'class'),
  55. (r'(var)(\s+)', bygroups(Keyword.Declaration, Whitespace), 'var'),
  56. (r'(import(?:\s+static)?)(\s+)', bygroups(Keyword.Namespace, Whitespace),
  57. 'import'),
  58. (r'"""\n', String, 'multiline_string'),
  59. (r'"', String, 'string'),
  60. (r"'\\.'|'[^\\]'|'\\u[0-9a-fA-F]{4}'", String.Char),
  61. (r'(\.)((?:[^\W\d]|\$)[\w$]*)', bygroups(Punctuation,
  62. Name.Attribute)),
  63. (r'^(\s*)(default)(:)', bygroups(Whitespace, Keyword, Punctuation)),
  64. (r'^(\s*)((?:[^\W\d]|\$)[\w$]*)(:)', bygroups(Whitespace, Name.Label,
  65. Punctuation)),
  66. (r'([^\W\d]|\$)[\w$]*', Name),
  67. (r'([0-9][0-9_]*\.([0-9][0-9_]*)?|'
  68. r'\.[0-9][0-9_]*)'
  69. r'([eE][+\-]?[0-9][0-9_]*)?[fFdD]?|'
  70. r'[0-9][eE][+\-]?[0-9][0-9_]*[fFdD]?|'
  71. r'[0-9]([eE][+\-]?[0-9][0-9_]*)?[fFdD]|'
  72. r'0[xX]([0-9a-fA-F][0-9a-fA-F_]*\.?|'
  73. r'([0-9a-fA-F][0-9a-fA-F_]*)?\.[0-9a-fA-F][0-9a-fA-F_]*)'
  74. r'[pP][+\-]?[0-9][0-9_]*[fFdD]?', Number.Float),
  75. (r'0[xX][0-9a-fA-F][0-9a-fA-F_]*[lL]?', Number.Hex),
  76. (r'0[bB][01][01_]*[lL]?', Number.Bin),
  77. (r'0[0-7_]+[lL]?', Number.Oct),
  78. (r'0|[1-9][0-9_]*[lL]?', Number.Integer),
  79. (r'[~^*!%&\[\]<>|+=/?-]', Operator),
  80. (r'[{}();:.,]', Punctuation),
  81. (r'\n', Whitespace)
  82. ],
  83. 'class': [
  84. (r'\s+', Text),
  85. (r'([^\W\d]|\$)[\w$]*', Name.Class, '#pop')
  86. ],
  87. 'var': [
  88. (r'([^\W\d]|\$)[\w$]*', Name, '#pop')
  89. ],
  90. 'import': [
  91. (r'[\w.]+\*?', Name.Namespace, '#pop')
  92. ],
  93. 'multiline_string': [
  94. (r'"""', String, '#pop'),
  95. (r'"', String),
  96. include('string')
  97. ],
  98. 'string': [
  99. (r'[^\\"]+', String),
  100. (r'\\\\', String), # Escaped backslash
  101. (r'\\"', String), # Escaped quote
  102. (r'\\', String), # Bare backslash
  103. (r'"', String, '#pop'), # Closing quote
  104. ],
  105. }
  106. class AspectJLexer(JavaLexer):
  107. """
  108. For AspectJ source code.
  109. .. versionadded:: 1.6
  110. """
  111. name = 'AspectJ'
  112. url = 'http://www.eclipse.org/aspectj/'
  113. aliases = ['aspectj']
  114. filenames = ['*.aj']
  115. mimetypes = ['text/x-aspectj']
  116. aj_keywords = {
  117. 'aspect', 'pointcut', 'privileged', 'call', 'execution',
  118. 'initialization', 'preinitialization', 'handler', 'get', 'set',
  119. 'staticinitialization', 'target', 'args', 'within', 'withincode',
  120. 'cflow', 'cflowbelow', 'annotation', 'before', 'after', 'around',
  121. 'proceed', 'throwing', 'returning', 'adviceexecution', 'declare',
  122. 'parents', 'warning', 'error', 'soft', 'precedence', 'thisJoinPoint',
  123. 'thisJoinPointStaticPart', 'thisEnclosingJoinPointStaticPart',
  124. 'issingleton', 'perthis', 'pertarget', 'percflow', 'percflowbelow',
  125. 'pertypewithin', 'lock', 'unlock', 'thisAspectInstance'
  126. }
  127. aj_inter_type = {'parents:', 'warning:', 'error:', 'soft:', 'precedence:'}
  128. aj_inter_type_annotation = {'@type', '@method', '@constructor', '@field'}
  129. def get_tokens_unprocessed(self, text):
  130. for index, token, value in JavaLexer.get_tokens_unprocessed(self, text):
  131. if token is Name and value in self.aj_keywords:
  132. yield index, Keyword, value
  133. elif token is Name.Label and value in self.aj_inter_type:
  134. yield index, Keyword, value[:-1]
  135. yield index, Operator, value[-1]
  136. elif token is Name.Decorator and value in self.aj_inter_type_annotation:
  137. yield index, Keyword, value
  138. else:
  139. yield index, token, value
  140. class ScalaLexer(RegexLexer):
  141. """
  142. For Scala source code.
  143. """
  144. name = 'Scala'
  145. url = 'http://www.scala-lang.org'
  146. aliases = ['scala']
  147. filenames = ['*.scala']
  148. mimetypes = ['text/x-scala']
  149. flags = re.MULTILINE | re.DOTALL
  150. opchar = '[!#%&*\\-\\/:?@^' + uni.combine('Sm', 'So') + ']'
  151. letter = '[_\\$' + uni.combine('Ll', 'Lu', 'Lo', 'Nl', 'Lt') + ']'
  152. upperLetter = '[' + uni.combine('Lu', 'Lt') + ']'
  153. letterOrDigit = '(?:%s|[0-9])' % letter
  154. letterOrDigitNoDollarSign = '(?:%s|[0-9])' % letter.replace('\\$', '')
  155. alphaId = '%s+' % letter
  156. simpleInterpolatedVariable = '%s%s*' % (letter, letterOrDigitNoDollarSign)
  157. idrest = '%s%s*(?:(?<=_)%s+)?' % (letter, letterOrDigit, opchar)
  158. idUpper = '%s%s*(?:(?<=_)%s+)?' % (upperLetter, letterOrDigit, opchar)
  159. plainid = '(?:%s|%s+)' % (idrest, opchar)
  160. backQuotedId = r'`[^`]+`'
  161. anyId = r'(?:%s|%s)' % (plainid, backQuotedId)
  162. notStartOfComment = r'(?!//|/\*)'
  163. endOfLineMaybeWithComment = r'(?=\s*(//|$))'
  164. keywords = (
  165. 'new', 'return', 'throw', 'classOf', 'isInstanceOf', 'asInstanceOf',
  166. 'else', 'if', 'then', 'do', 'while', 'for', 'yield', 'match', 'case',
  167. 'catch', 'finally', 'try'
  168. )
  169. operators = (
  170. '<%', '=:=', '<:<', '<%<', '>:', '<:', '=', '==', '!=', '<=', '>=',
  171. '<>', '<', '>', '<-', '←', '->', '→', '=>', '⇒', '?', '@', '|', '-',
  172. '+', '*', '%', '~', '\\'
  173. )
  174. storage_modifiers = (
  175. 'private', 'protected', 'synchronized', '@volatile', 'abstract',
  176. 'final', 'lazy', 'sealed', 'implicit', 'override', '@transient',
  177. '@native'
  178. )
  179. tokens = {
  180. 'root': [
  181. include('whitespace'),
  182. include('comments'),
  183. include('script-header'),
  184. include('imports'),
  185. include('exports'),
  186. include('storage-modifiers'),
  187. include('annotations'),
  188. include('using'),
  189. include('declarations'),
  190. include('inheritance'),
  191. include('extension'),
  192. include('end'),
  193. include('constants'),
  194. include('strings'),
  195. include('symbols'),
  196. include('singleton-type'),
  197. include('inline'),
  198. include('quoted'),
  199. include('keywords'),
  200. include('operators'),
  201. include('punctuation'),
  202. include('names'),
  203. ],
  204. # Includes:
  205. 'whitespace': [
  206. (r'\s+', Whitespace),
  207. ],
  208. 'comments': [
  209. (r'//.*?\n', Comment.Single),
  210. (r'/\*', Comment.Multiline, 'comment'),
  211. ],
  212. 'script-header': [
  213. (r'^#!([^\n]*)$', Comment.Hashbang),
  214. ],
  215. 'imports': [
  216. (r'\b(import)(\s+)', bygroups(Keyword, Whitespace), 'import-path'),
  217. ],
  218. 'exports': [
  219. (r'\b(export)(\s+)(given)(\s+)',
  220. bygroups(Keyword, Whitespace, Keyword, Whitespace), 'export-path'),
  221. (r'\b(export)(\s+)', bygroups(Keyword, Whitespace), 'export-path'),
  222. ],
  223. 'storage-modifiers': [
  224. (words(storage_modifiers, prefix=r'\b', suffix=r'\b'), Keyword),
  225. # Only highlight soft modifiers if they are eventually followed by
  226. # the correct keyword. Note that soft modifiers can be followed by a
  227. # sequence of regular modifiers; [a-z\s]* skips those, and we just
  228. # check that the soft modifier is applied to a supported statement.
  229. (r'\b(transparent|opaque|infix|open|inline)\b(?=[a-z\s]*\b'
  230. r'(def|val|var|given|type|class|trait|object|enum)\b)', Keyword),
  231. ],
  232. 'annotations': [
  233. (r'@%s' % idrest, Name.Decorator),
  234. ],
  235. 'using': [
  236. # using is a soft keyword, can only be used in the first position of
  237. # a parameter or argument list.
  238. (r'(\()(\s*)(using)(\s)', bygroups(Punctuation, Whitespace, Keyword, Whitespace)),
  239. ],
  240. 'declarations': [
  241. (r'\b(def)\b(\s*)%s(%s)?' % (notStartOfComment, anyId),
  242. bygroups(Keyword, Whitespace, Name.Function)),
  243. (r'\b(trait)\b(\s*)%s(%s)?' % (notStartOfComment, anyId),
  244. bygroups(Keyword, Whitespace, Name.Class)),
  245. (r'\b(?:(case)(\s+))?(class|object|enum)\b(\s*)%s(%s)?' %
  246. (notStartOfComment, anyId),
  247. bygroups(Keyword, Whitespace, Keyword, Whitespace, Name.Class)),
  248. (r'(?<!\.)\b(type)\b(\s*)%s(%s)?' % (notStartOfComment, anyId),
  249. bygroups(Keyword, Whitespace, Name.Class)),
  250. (r'\b(val|var)\b', Keyword.Declaration),
  251. (r'\b(package)(\s+)(object)\b(\s*)%s(%s)?' %
  252. (notStartOfComment, anyId),
  253. bygroups(Keyword, Whitespace, Keyword, Whitespace, Name.Namespace)),
  254. (r'\b(package)(\s+)', bygroups(Keyword, Whitespace), 'package'),
  255. (r'\b(given)\b(\s*)(%s)' % idUpper,
  256. bygroups(Keyword, Whitespace, Name.Class)),
  257. (r'\b(given)\b(\s*)(%s)?' % anyId,
  258. bygroups(Keyword, Whitespace, Name)),
  259. ],
  260. 'inheritance': [
  261. (r'\b(extends|with|derives)\b(\s*)'
  262. r'(%s|%s|(?=\([^\)]+=>)|(?=%s)|(?="))?' %
  263. (idUpper, backQuotedId, plainid),
  264. bygroups(Keyword, Whitespace, Name.Class)),
  265. ],
  266. 'extension': [
  267. (r'\b(extension)(\s+)(?=[\[\(])', bygroups(Keyword, Whitespace)),
  268. ],
  269. 'end': [
  270. # end is a soft keyword, should only be highlighted in certain cases
  271. (r'\b(end)(\s+)(if|while|for|match|new|extension|val|var)\b',
  272. bygroups(Keyword, Whitespace, Keyword)),
  273. (r'\b(end)(\s+)(%s)%s' % (idUpper, endOfLineMaybeWithComment),
  274. bygroups(Keyword, Whitespace, Name.Class)),
  275. (r'\b(end)(\s+)(%s|%s)?%s' %
  276. (backQuotedId, plainid, endOfLineMaybeWithComment),
  277. bygroups(Keyword, Whitespace, Name.Namespace)),
  278. ],
  279. 'punctuation': [
  280. (r'[{}()\[\];,.]', Punctuation),
  281. (r'(?<!:):(?!:)', Punctuation),
  282. ],
  283. 'keywords': [
  284. (words(keywords, prefix=r'\b', suffix=r'\b'), Keyword),
  285. ],
  286. 'operators': [
  287. (r'(%s{2,})(\s+)' % opchar, bygroups(Operator, Whitespace)),
  288. (r'/(?![/*])', Operator),
  289. (words(operators), Operator),
  290. (r'(?<!%s)(!|&&|\|\|)(?!%s)' % (opchar, opchar), Operator),
  291. ],
  292. 'constants': [
  293. (r'\b(this|super)\b', Name.Builtin.Pseudo),
  294. (r'(true|false|null)\b', Keyword.Constant),
  295. (r'0[xX][0-9a-fA-F_]*', Number.Hex),
  296. (r'([0-9][0-9_]*\.[0-9][0-9_]*|\.[0-9][0-9_]*)'
  297. r'([eE][+-]?[0-9][0-9_]*)?[fFdD]?', Number.Float),
  298. (r'[0-9]+([eE][+-]?[0-9]+)?[fFdD]', Number.Float),
  299. (r'[0-9]+([eE][+-]?[0-9]+)[fFdD]?', Number.Float),
  300. (r'[0-9]+[lL]', Number.Integer.Long),
  301. (r'[0-9]+', Number.Integer),
  302. (r'""".*?"""(?!")', String),
  303. (r'"(\\\\|\\"|[^"])*"', String),
  304. (r"(')(\\.)(')", bygroups(String.Char, String.Escape, String.Char)),
  305. (r"'[^\\]'|'\\u[0-9a-fA-F]{4}'", String.Char),
  306. ],
  307. "strings": [
  308. (r'[fs]"""', String, 'interpolated-string-triple'),
  309. (r'[fs]"', String, 'interpolated-string'),
  310. (r'raw"(\\\\|\\"|[^"])*"', String),
  311. ],
  312. 'symbols': [
  313. (r"('%s)(?!')" % plainid, String.Symbol),
  314. ],
  315. 'singleton-type': [
  316. (r'(\.)(type)\b', bygroups(Punctuation, Keyword)),
  317. ],
  318. 'inline': [
  319. # inline is a soft modifier, only highlighted if followed by if,
  320. # match or parameters.
  321. (r'\b(inline)(?=\s+(%s|%s)\s*:)' % (plainid, backQuotedId),
  322. Keyword),
  323. (r'\b(inline)\b(?=(?:.(?!\b(?:val|def|given)\b))*\b(if|match)\b)',
  324. Keyword),
  325. ],
  326. 'quoted': [
  327. # '{...} or ${...}
  328. (r"['$]\{(?!')", Punctuation),
  329. # '[...]
  330. (r"'\[(?!')", Punctuation),
  331. ],
  332. 'names': [
  333. (idUpper, Name.Class),
  334. (anyId, Name),
  335. ],
  336. # States
  337. 'comment': [
  338. (r'[^/*]+', Comment.Multiline),
  339. (r'/\*', Comment.Multiline, '#push'),
  340. (r'\*/', Comment.Multiline, '#pop'),
  341. (r'[*/]', Comment.Multiline),
  342. ],
  343. 'import-path': [
  344. (r'(?<=[\n;:])', Text, '#pop'),
  345. include('comments'),
  346. (r'\b(given)\b', Keyword),
  347. include('qualified-name'),
  348. (r'\{', Punctuation, 'import-path-curly-brace'),
  349. ],
  350. 'import-path-curly-brace': [
  351. include('whitespace'),
  352. include('comments'),
  353. (r'\b(given)\b', Keyword),
  354. (r'=>', Operator),
  355. (r'\}', Punctuation, '#pop'),
  356. (r',', Punctuation),
  357. (r'[\[\]]', Punctuation),
  358. include('qualified-name'),
  359. ],
  360. 'export-path': [
  361. (r'(?<=[\n;:])', Text, '#pop'),
  362. include('comments'),
  363. include('qualified-name'),
  364. (r'\{', Punctuation, 'export-path-curly-brace'),
  365. ],
  366. 'export-path-curly-brace': [
  367. include('whitespace'),
  368. include('comments'),
  369. (r'=>', Operator),
  370. (r'\}', Punctuation, '#pop'),
  371. (r',', Punctuation),
  372. include('qualified-name'),
  373. ],
  374. 'package': [
  375. (r'(?<=[\n;])', Text, '#pop'),
  376. (r':', Punctuation, '#pop'),
  377. include('comments'),
  378. include('qualified-name'),
  379. ],
  380. 'interpolated-string-triple': [
  381. (r'"""(?!")', String, '#pop'),
  382. (r'"', String),
  383. include('interpolated-string-common'),
  384. ],
  385. 'interpolated-string': [
  386. (r'"', String, '#pop'),
  387. include('interpolated-string-common'),
  388. ],
  389. 'interpolated-string-brace': [
  390. (r'\}', String.Interpol, '#pop'),
  391. (r'\{', Punctuation, 'interpolated-string-nested-brace'),
  392. include('root'),
  393. ],
  394. 'interpolated-string-nested-brace': [
  395. (r'\{', Punctuation, '#push'),
  396. (r'\}', Punctuation, '#pop'),
  397. include('root'),
  398. ],
  399. # Helpers
  400. 'qualified-name': [
  401. (idUpper, Name.Class),
  402. (r'(%s)(\.)' % anyId, bygroups(Name.Namespace, Punctuation)),
  403. (r'\.', Punctuation),
  404. (anyId, Name),
  405. (r'[^\S\n]+', Whitespace),
  406. ],
  407. 'interpolated-string-common': [
  408. (r'[^"$\\]+', String),
  409. (r'\$\$', String.Escape),
  410. (r'(\$)(%s)' % simpleInterpolatedVariable,
  411. bygroups(String.Interpol, Name)),
  412. (r'\$\{', String.Interpol, 'interpolated-string-brace'),
  413. (r'\\.', String),
  414. ],
  415. }
  416. class GosuLexer(RegexLexer):
  417. """
  418. For Gosu source code.
  419. .. versionadded:: 1.5
  420. """
  421. name = 'Gosu'
  422. aliases = ['gosu']
  423. filenames = ['*.gs', '*.gsx', '*.gsp', '*.vark']
  424. mimetypes = ['text/x-gosu']
  425. flags = re.MULTILINE | re.DOTALL
  426. tokens = {
  427. 'root': [
  428. # method names
  429. (r'^(\s*(?:[a-zA-Z_][\w.\[\]]*\s+)+?)' # modifiers etc.
  430. r'([a-zA-Z_]\w*)' # method name
  431. r'(\s*)(\()', # signature start
  432. bygroups(using(this), Name.Function, Whitespace, Operator)),
  433. (r'[^\S\n]+', Whitespace),
  434. (r'//.*?\n', Comment.Single),
  435. (r'/\*.*?\*/', Comment.Multiline),
  436. (r'@[a-zA-Z_][\w.]*', Name.Decorator),
  437. (r'(in|as|typeof|statictypeof|typeis|typeas|if|else|foreach|for|'
  438. r'index|while|do|continue|break|return|try|catch|finally|this|'
  439. r'throw|new|switch|case|default|eval|super|outer|classpath|'
  440. r'using)\b', Keyword),
  441. (r'(var|delegate|construct|function|private|internal|protected|'
  442. r'public|abstract|override|final|static|extends|transient|'
  443. r'implements|represents|readonly)\b', Keyword.Declaration),
  444. (r'(property)(\s+)(get|set)?', bygroups(Keyword.Declaration, Whitespace, Keyword.Declaration)),
  445. (r'(boolean|byte|char|double|float|int|long|short|void|block)\b',
  446. Keyword.Type),
  447. (r'(package)(\s+)', bygroups(Keyword.Namespace, Whitespace)),
  448. (r'(true|false|null|NaN|Infinity)\b', Keyword.Constant),
  449. (r'(class|interface|enhancement|enum)(\s+)([a-zA-Z_]\w*)',
  450. bygroups(Keyword.Declaration, Whitespace, Name.Class)),
  451. (r'(uses)(\s+)([\w.]+\*?)',
  452. bygroups(Keyword.Namespace, Whitespace, Name.Namespace)),
  453. (r'"', String, 'string'),
  454. (r'(\??[.#])([a-zA-Z_]\w*)',
  455. bygroups(Operator, Name.Attribute)),
  456. (r'(:)([a-zA-Z_]\w*)',
  457. bygroups(Operator, Name.Attribute)),
  458. (r'[a-zA-Z_$]\w*', Name),
  459. (r'and|or|not|[\\~^*!%&\[\](){}<>|+=:;,./?-]', Operator),
  460. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  461. (r'[0-9]+', Number.Integer),
  462. (r'\n', Whitespace)
  463. ],
  464. 'templateText': [
  465. (r'(\\<)|(\\\$)', String),
  466. (r'(<%@\s+)(extends|params)',
  467. bygroups(Operator, Name.Decorator), 'stringTemplate'),
  468. (r'<%!--.*?--%>', Comment.Multiline),
  469. (r'(<%)|(<%=)', Operator, 'stringTemplate'),
  470. (r'\$\{', Operator, 'stringTemplateShorthand'),
  471. (r'.', String)
  472. ],
  473. 'string': [
  474. (r'"', String, '#pop'),
  475. include('templateText')
  476. ],
  477. 'stringTemplate': [
  478. (r'"', String, 'string'),
  479. (r'%>', Operator, '#pop'),
  480. include('root')
  481. ],
  482. 'stringTemplateShorthand': [
  483. (r'"', String, 'string'),
  484. (r'\{', Operator, 'stringTemplateShorthand'),
  485. (r'\}', Operator, '#pop'),
  486. include('root')
  487. ],
  488. }
  489. class GosuTemplateLexer(Lexer):
  490. """
  491. For Gosu templates.
  492. .. versionadded:: 1.5
  493. """
  494. name = 'Gosu Template'
  495. aliases = ['gst']
  496. filenames = ['*.gst']
  497. mimetypes = ['text/x-gosu-template']
  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. .. versionadded:: 1.5
  506. """
  507. name = 'Groovy'
  508. url = 'https://groovy-lang.org/'
  509. aliases = ['groovy']
  510. filenames = ['*.groovy','*.gradle']
  511. mimetypes = ['text/x-groovy']
  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. .. versionadded:: 1.4
  578. """
  579. name = 'Ioke'
  580. url = 'https://ioke.org/'
  581. filenames = ['*.ik']
  582. aliases = ['ioke', 'ik']
  583. mimetypes = ['text/x-iokesrc']
  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. .. versionadded:: 0.11
  731. """
  732. name = 'Clojure'
  733. url = 'http://clojure.org/'
  734. aliases = ['clojure', 'clj']
  735. filenames = ['*.clj', '*.cljc']
  736. mimetypes = ['text/x-clojure', 'application/x-clojure']
  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. .. versionadded:: 2.0
  850. """
  851. name = 'ClojureScript'
  852. url = 'http://clojure.org/clojurescript'
  853. aliases = ['clojurescript', 'cljs']
  854. filenames = ['*.cljs']
  855. mimetypes = ['text/x-clojurescript', 'application/x-clojurescript']
  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. .. versionadded:: 1.6
  902. """
  903. name = 'Ceylon'
  904. url = 'http://ceylon-lang.org/'
  905. aliases = ['ceylon']
  906. filenames = ['*.ceylon']
  907. mimetypes = ['text/x-ceylon']
  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. .. versionadded:: 1.5
  975. """
  976. name = 'Kotlin'
  977. url = 'http://kotlinlang.org/'
  978. aliases = ['kotlin']
  979. filenames = ['*.kt', '*.kts']
  980. mimetypes = ['text/x-kotlin']
  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. .. versionadded:: 1.6
  1121. """
  1122. name = 'Xtend'
  1123. url = 'https://www.eclipse.org/xtend/'
  1124. aliases = ['xtend']
  1125. filenames = ['*.xtend']
  1126. mimetypes = ['text/x-xtend']
  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)\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. .. versionadded:: 2.0
  1181. """
  1182. name = 'Pig'
  1183. url = 'https://pig.apache.org/'
  1184. aliases = ['pig']
  1185. filenames = ['*.pig']
  1186. mimetypes = ['text/x-pig']
  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. .. versionadded:: 2.0
  1243. """
  1244. name = 'Golo'
  1245. url = 'http://golo-lang.org/'
  1246. filenames = ['*.golo']
  1247. aliases = ['golo']
  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. .. versionadded:: 2.0
  1340. """
  1341. name = 'Jasmin'
  1342. url = 'http://jasmin.sourceforge.net/'
  1343. aliases = ['jasmin', 'jasminxt']
  1344. filenames = ['*.j']
  1345. _whitespace = r' \n\t\r'
  1346. _ws = r'(?:[%s]+)' % _whitespace
  1347. _separator = r'%s:=' % _whitespace
  1348. _break = r'(?=[%s]|$)' % _separator
  1349. _name = r'[^%s]+' % _separator
  1350. _unqualified_name = r'(?:[^%s.;\[/]+)' % _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. (r'(\$[-+])?0x-?[\da-fA-F]+%s' % _break, Number.Hex),
  1361. (r'(\$[-+]|\+)?-?\d+%s' % _break, Number.Integer),
  1362. (r'-?(\d+\.\d*|\.\d+)([eE][-+]?\d+)?[fFdD]?'
  1363. r'[\x00-\x08\x0b\x0c\x0e-\x1f]*%s' % _break, Number.Float),
  1364. (r'\$%s' % _name, Name.Variable),
  1365. # Directives
  1366. (r'\.annotation%s' % _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. r'synchronized|synthetic|transient|varargs|volatile)%s' % _break,
  1372. Keyword.Reserved),
  1373. (r'\.catch%s' % _break, Keyword.Reserved, 'caught-exception'),
  1374. (r'(\.class|\.implements|\.inner|\.super|inner|invisible|'
  1375. r'invisibleparam|outer|visible|visibleparam)%s' % _break,
  1376. Keyword.Reserved, 'class/convert-dots'),
  1377. (r'\.field%s' % _break, Keyword.Reserved,
  1378. ('descriptor/convert-dots', 'field')),
  1379. (r'(\.end|\.limit|use)%s' % _break, Keyword.Reserved,
  1380. 'no-verification'),
  1381. (r'\.method%s' % _break, Keyword.Reserved, 'method'),
  1382. (r'\.set%s' % _break, Keyword.Reserved, 'var'),
  1383. (r'\.throws%s' % _break, Keyword.Reserved, 'exception'),
  1384. (r'(from|offset|to|using)%s' % _break, Keyword.Reserved, 'label'),
  1385. (r'is%s' % _break, Keyword.Reserved,
  1386. ('descriptor/convert-dots', 'var')),
  1387. (r'(locals|stack)%s' % _break, Keyword.Reserved, 'verification'),
  1388. (r'method%s' % _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. (r'(anewarray|checkcast|instanceof|ldc|ldc_w|new)%s' % _break,
  1414. Keyword.Reserved, 'class/no-dots'),
  1415. (r'invoke(dynamic|interface|nonvirtual|special|'
  1416. r'static|virtual)%s' % _break, Keyword.Reserved,
  1417. 'invocation'),
  1418. (r'(getfield|putfield)%s' % _break, Keyword.Reserved,
  1419. ('descriptor/no-dots', 'field')),
  1420. (r'(getstatic|putstatic)%s' % _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. (r'(multianewarray|newarray)%s' % _break, Keyword.Reserved,
  1429. 'descriptor/convert-dots'),
  1430. (r'tableswitch%s' % _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. (r'(%s)([ \t\r]*)(:)' % _name,
  1448. bygroups(Name.Label, Whitespace, Punctuation)),
  1449. (_name, String.Other)
  1450. ],
  1451. 'annotation': [
  1452. (r'\n', Whitespace, ('#pop', 'annotation-body')),
  1453. (r'default%s' % _break, Keyword.Reserved,
  1454. ('#pop', 'annotation-default')),
  1455. include('default')
  1456. ],
  1457. 'annotation-body': [
  1458. (r'\n+', Whitespace),
  1459. (r'\.end%s' % _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. (r'\.end%s' % _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. (r'all%s' % _break, Keyword, '#pop'),
  1476. include('exception')
  1477. ],
  1478. 'class/convert-dots': [
  1479. include('default'),
  1480. (r'(L)((?:%s[/.])*)(%s)(;)' % (_unqualified_name, _name),
  1481. bygroups(Keyword.Type, Name.Namespace, Name.Class, Punctuation),
  1482. '#pop'),
  1483. (r'((?:%s[/.])*)(%s)' % (_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. (r'(L)((?:%s/)*)(%s)(;)' % (_unqualified_name, _name),
  1490. bygroups(Keyword.Type, Name.Namespace, Name.Class, Punctuation),
  1491. '#pop'),
  1492. (r'((?:%s/)*)(%s)' % (_unqualified_name, _name),
  1493. bygroups(Name.Namespace, Name.Class), '#pop')
  1494. ],
  1495. 'descriptor/convert-dots': [
  1496. include('default'),
  1497. (r'\[+', Punctuation),
  1498. (r'(L)((?:%s[/.])*)(%s?)(;)' % (_unqualified_name, _name),
  1499. bygroups(Keyword.Type, Name.Namespace, Name.Class, Punctuation),
  1500. '#pop'),
  1501. (r'[^%s\[)L]+' % _separator, Keyword.Type, '#pop'),
  1502. default('#pop')
  1503. ],
  1504. 'descriptor/no-dots': [
  1505. include('default'),
  1506. (r'\[+', Punctuation),
  1507. (r'(L)((?:%s/)*)(%s)(;)' % (_unqualified_name, _name),
  1508. bygroups(Keyword.Type, Name.Namespace, Name.Class, Punctuation),
  1509. '#pop'),
  1510. (r'[^%s\[)L]+' % _separator, 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. (r'(?=[^%s]*\()' % _separator, Text, ('#pop', 'invocation')),
  1520. default(('#pop', 'class/convert-dots'))
  1521. ],
  1522. 'exception': [
  1523. include('default'),
  1524. (r'((?:%s[/.])*)(%s)' % (_unqualified_name, _name),
  1525. bygroups(Name.Namespace, Name.Exception), '#pop')
  1526. ],
  1527. 'field': [
  1528. (r'static%s' % _break, Keyword.Reserved, ('#pop', 'static')),
  1529. include('default'),
  1530. (r'((?:%s[/.](?=[^%s]*[/.]))*)(%s[/.])?(%s)' %
  1531. (_unqualified_name, _separator, _unqualified_name, _name),
  1532. bygroups(Name.Namespace, Name.Class, Name.Variable.Instance),
  1533. '#pop')
  1534. ],
  1535. 'invocation': [
  1536. include('default'),
  1537. (r'((?:%s[/.](?=[^%s(]*[/.]))*)(%s[/.])?(%s)(\()' %
  1538. (_unqualified_name, _separator, _unqualified_name, _name),
  1539. bygroups(Name.Namespace, Name.Class, Name.Function, Punctuation),
  1540. ('#pop', 'descriptor/convert-dots', 'descriptors/convert-dots',
  1541. 'descriptor/convert-dots'))
  1542. ],
  1543. 'label': [
  1544. include('default'),
  1545. (_name, Name.Label, '#pop')
  1546. ],
  1547. 'method': [
  1548. include('default'),
  1549. (r'(%s)(\()' % _name, bygroups(Name.Function, Punctuation),
  1550. ('#pop', 'descriptor/convert-dots', 'descriptors/convert-dots',
  1551. 'descriptor/convert-dots'))
  1552. ],
  1553. 'no-verification': [
  1554. (r'(locals|method|stack)%s' % _break, Keyword.Reserved, '#pop'),
  1555. include('default')
  1556. ],
  1557. 'static': [
  1558. include('default'),
  1559. (r'((?:%s[/.](?=[^%s]*[/.]))*)(%s[/.])?(%s)' %
  1560. (_unqualified_name, _separator, _unqualified_name, _name),
  1561. bygroups(Name.Namespace, Name.Class, Name.Variable.Class), '#pop')
  1562. ],
  1563. 'table': [
  1564. (r'\n+', Whitespace),
  1565. (r'default%s' % _break, Keyword.Reserved, '#pop'),
  1566. include('default'),
  1567. (_name, Name.Label)
  1568. ],
  1569. 'var': [
  1570. include('default'),
  1571. (_name, Name.Variable, '#pop')
  1572. ],
  1573. 'verification': [
  1574. include('default'),
  1575. (r'(Double|Float|Integer|Long|Null|Top|UninitializedThis)%s' %
  1576. _break, Keyword, '#pop'),
  1577. (r'Object%s' % _break, Keyword, ('#pop', 'class/no-dots')),
  1578. (r'Uninitialized%s' % _break, Keyword, ('#pop', 'label'))
  1579. ]
  1580. }
  1581. def analyse_text(text):
  1582. score = 0
  1583. if re.search(r'^\s*\.class\s', text, re.MULTILINE):
  1584. score += 0.5
  1585. if re.search(r'^\s*[a-z]+_[a-z]+\b', text, re.MULTILINE):
  1586. score += 0.3
  1587. if re.search(r'^\s*\.(attribute|bytecode|debug|deprecated|enclosing|'
  1588. r'inner|interface|limit|set|signature|stack)\b', text,
  1589. re.MULTILINE):
  1590. score += 0.6
  1591. return min(score, 1.0)
  1592. class SarlLexer(RegexLexer):
  1593. """
  1594. For SARL source code.
  1595. .. versionadded:: 2.4
  1596. """
  1597. name = 'SARL'
  1598. url = 'http://www.sarl.io'
  1599. aliases = ['sarl']
  1600. filenames = ['*.sarl']
  1601. mimetypes = ['text/x-sarl']
  1602. flags = re.MULTILINE | re.DOTALL
  1603. tokens = {
  1604. 'root': [
  1605. # method names
  1606. (r'^(\s*(?:[a-zA-Z_][\w.\[\]]*\s+)+?)' # return arguments
  1607. r'([a-zA-Z_$][\w$]*)' # method name
  1608. r'(\s*)(\()', # signature start
  1609. bygroups(using(this), Name.Function, Whitespace, Operator)),
  1610. (r'[^\S\n]+', Whitespace),
  1611. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  1612. (r'/\*.*?\*/', Comment.Multiline),
  1613. (r'@[a-zA-Z_][\w.]*', Name.Decorator),
  1614. (r'(as|break|case|catch|default|do|else|extends|extension|finally|'
  1615. r'fires|for|if|implements|instanceof|new|on|requires|return|super|'
  1616. r'switch|throw|throws|try|typeof|uses|while|with)\b',
  1617. Keyword),
  1618. (r'(abstract|def|dispatch|final|native|override|private|protected|'
  1619. r'public|static|strictfp|synchronized|transient|val|var|volatile)\b',
  1620. Keyword.Declaration),
  1621. (r'(boolean|byte|char|double|float|int|long|short|void)\b',
  1622. Keyword.Type),
  1623. (r'(package)(\s+)', bygroups(Keyword.Namespace, Whitespace)),
  1624. (r'(false|it|null|occurrence|this|true|void)\b', Keyword.Constant),
  1625. (r'(agent|annotation|artifact|behavior|capacity|class|enum|event|'
  1626. r'interface|skill|space)(\s+)', bygroups(Keyword.Declaration, Whitespace),
  1627. 'class'),
  1628. (r'(import)(\s+)', bygroups(Keyword.Namespace, Whitespace), 'import'),
  1629. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  1630. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  1631. (r'[a-zA-Z_]\w*:', Name.Label),
  1632. (r'[a-zA-Z_$]\w*', Name),
  1633. (r'[~^*!%&\[\](){}<>\|+=:;,./?-]', Operator),
  1634. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  1635. (r'0x[0-9a-fA-F]+', Number.Hex),
  1636. (r'[0-9]+L?', Number.Integer),
  1637. (r'\n', Whitespace)
  1638. ],
  1639. 'class': [
  1640. (r'[a-zA-Z_]\w*', Name.Class, '#pop')
  1641. ],
  1642. 'import': [
  1643. (r'[\w.]+\*?', Name.Namespace, '#pop')
  1644. ],
  1645. }