jvm.py 70 KB

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