jvm.py 71 KB

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