javascript.py 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.javascript
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for JavaScript and related languages.
  6. :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, include, bygroups, default, using, \
  11. this, words, combined
  12. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  13. Number, Punctuation, Other
  14. from pygments.util import get_bool_opt, iteritems
  15. import pygments.unistring as uni
  16. __all__ = ['JavascriptLexer', 'KalLexer', 'LiveScriptLexer', 'DartLexer',
  17. 'TypeScriptLexer', 'LassoLexer', 'ObjectiveJLexer',
  18. 'CoffeeScriptLexer', 'MaskLexer', 'EarlGreyLexer', 'JuttleLexer']
  19. JS_IDENT_START = ('(?:[$_' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl') +
  20. ']|\\\\u[a-fA-F0-9]{4})')
  21. JS_IDENT_PART = ('(?:[$' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl',
  22. 'Mn', 'Mc', 'Nd', 'Pc') +
  23. u'\u200c\u200d]|\\\\u[a-fA-F0-9]{4})')
  24. JS_IDENT = JS_IDENT_START + '(?:' + JS_IDENT_PART + ')*'
  25. class JavascriptLexer(RegexLexer):
  26. """
  27. For JavaScript source code.
  28. """
  29. name = 'JavaScript'
  30. aliases = ['js', 'javascript']
  31. filenames = ['*.js', '*.jsm']
  32. mimetypes = ['application/javascript', 'application/x-javascript',
  33. 'text/x-javascript', 'text/javascript']
  34. flags = re.DOTALL | re.UNICODE | re.MULTILINE
  35. tokens = {
  36. 'commentsandwhitespace': [
  37. (r'\s+', Text),
  38. (r'<!--', Comment),
  39. (r'//.*?\n', Comment.Single),
  40. (r'/\*.*?\*/', Comment.Multiline)
  41. ],
  42. 'slashstartsregex': [
  43. include('commentsandwhitespace'),
  44. (r'/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
  45. r'([gimuy]+\b|\B)', String.Regex, '#pop'),
  46. (r'(?=/)', Text, ('#pop', 'badregex')),
  47. default('#pop')
  48. ],
  49. 'badregex': [
  50. (r'\n', Text, '#pop')
  51. ],
  52. 'root': [
  53. (r'\A#! ?/.*?\n', Comment.Hashbang), # recognized by node.js
  54. (r'^(?=\s|/|<!--)', Text, 'slashstartsregex'),
  55. include('commentsandwhitespace'),
  56. (r'(\.\d+|[0-9]+\.[0-9]*)([eE][-+]?[0-9]+)?', Number.Float),
  57. (r'0[bB][01]+', Number.Bin),
  58. (r'0[oO][0-7]+', Number.Oct),
  59. (r'0[xX][0-9a-fA-F]+', Number.Hex),
  60. (r'[0-9]+', Number.Integer),
  61. (r'\.\.\.|=>', Punctuation),
  62. (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|'
  63. r'(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'),
  64. (r'[{(\[;,]', Punctuation, 'slashstartsregex'),
  65. (r'[})\].]', Punctuation),
  66. (r'(for|in|while|do|break|return|continue|switch|case|default|if|else|'
  67. r'throw|try|catch|finally|new|delete|typeof|instanceof|void|yield|'
  68. r'this|of)\b', Keyword, 'slashstartsregex'),
  69. (r'(var|let|with|function)\b', Keyword.Declaration, 'slashstartsregex'),
  70. (r'(abstract|boolean|byte|char|class|const|debugger|double|enum|export|'
  71. r'extends|final|float|goto|implements|import|int|interface|long|native|'
  72. r'package|private|protected|public|short|static|super|synchronized|throws|'
  73. r'transient|volatile)\b', Keyword.Reserved),
  74. (r'(true|false|null|NaN|Infinity|undefined)\b', Keyword.Constant),
  75. (r'(Array|Boolean|Date|Error|Function|Math|netscape|'
  76. r'Number|Object|Packages|RegExp|String|Promise|Proxy|sun|decodeURI|'
  77. r'decodeURIComponent|encodeURI|encodeURIComponent|'
  78. r'Error|eval|isFinite|isNaN|isSafeInteger|parseFloat|parseInt|'
  79. r'document|this|window)\b', Name.Builtin),
  80. (JS_IDENT, Name.Other),
  81. (r'"(\\\\|\\"|[^"])*"', String.Double),
  82. (r"'(\\\\|\\'|[^'])*'", String.Single),
  83. (r'`', String.Backtick, 'interp'),
  84. ],
  85. 'interp': [
  86. (r'`', String.Backtick, '#pop'),
  87. (r'\\\\', String.Backtick),
  88. (r'\\`', String.Backtick),
  89. (r'\$\{', String.Interpol, 'interp-inside'),
  90. (r'\$', String.Backtick),
  91. (r'[^`\\$]+', String.Backtick),
  92. ],
  93. 'interp-inside': [
  94. # TODO: should this include single-line comments and allow nesting strings?
  95. (r'\}', String.Interpol, '#pop'),
  96. include('root'),
  97. ],
  98. # (\\\\|\\`|[^`])*`', String.Backtick),
  99. }
  100. class KalLexer(RegexLexer):
  101. """
  102. For `Kal`_ source code.
  103. .. _Kal: http://rzimmerman.github.io/kal
  104. .. versionadded:: 2.0
  105. """
  106. name = 'Kal'
  107. aliases = ['kal']
  108. filenames = ['*.kal']
  109. mimetypes = ['text/kal', 'application/kal']
  110. flags = re.DOTALL
  111. tokens = {
  112. 'commentsandwhitespace': [
  113. (r'\s+', Text),
  114. (r'###[^#].*?###', Comment.Multiline),
  115. (r'#(?!##[^#]).*?\n', Comment.Single),
  116. ],
  117. 'functiondef': [
  118. (r'[$a-zA-Z_][\w$]*\s*', Name.Function, '#pop'),
  119. include('commentsandwhitespace'),
  120. ],
  121. 'classdef': [
  122. (r'\binherits\s+from\b', Keyword),
  123. (r'[$a-zA-Z_][\w$]*\s*\n', Name.Class, '#pop'),
  124. (r'[$a-zA-Z_][\w$]*\s*', Name.Class),
  125. include('commentsandwhitespace'),
  126. ],
  127. 'listcomprehension': [
  128. (r'\]', Punctuation, '#pop'),
  129. (r'\b(property|value)\b', Keyword),
  130. include('root'),
  131. ],
  132. 'waitfor': [
  133. (r'\n', Punctuation, '#pop'),
  134. (r'\bfrom\b', Keyword),
  135. include('root'),
  136. ],
  137. 'root': [
  138. include('commentsandwhitespace'),
  139. (r'/(?! )(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
  140. r'([gim]+\b|\B)', String.Regex),
  141. (r'\?|:|_(?=\n)|==?|!=|-(?!>)|[<>+*/-]=?',
  142. Operator),
  143. (r'\b(and|or|isnt|is|not|but|bitwise|mod|\^|xor|exists|'
  144. r'doesnt\s+exist)\b', Operator.Word),
  145. (r'(?:\([^()]+\))?\s*>', Name.Function),
  146. (r'[{(]', Punctuation),
  147. (r'\[', Punctuation, 'listcomprehension'),
  148. (r'[})\].,]', Punctuation),
  149. (r'\b(function|method|task)\b', Keyword.Declaration, 'functiondef'),
  150. (r'\bclass\b', Keyword.Declaration, 'classdef'),
  151. (r'\b(safe\s+)?wait\s+for\b', Keyword, 'waitfor'),
  152. (r'\b(me|this)(\.[$a-zA-Z_][\w.$]*)?\b', Name.Variable.Instance),
  153. (r'(?<![.$])(for(\s+(parallel|series))?|in|of|while|until|'
  154. r'break|return|continue|'
  155. r'when|if|unless|else|otherwise|except\s+when|'
  156. r'throw|raise|fail\s+with|try|catch|finally|new|delete|'
  157. r'typeof|instanceof|super|run\s+in\s+parallel|'
  158. r'inherits\s+from)\b', Keyword),
  159. (r'(?<![.$])(true|false|yes|no|on|off|null|nothing|none|'
  160. r'NaN|Infinity|undefined)\b',
  161. Keyword.Constant),
  162. (r'(Array|Boolean|Date|Error|Function|Math|netscape|'
  163. r'Number|Object|Packages|RegExp|String|sun|decodeURI|'
  164. r'decodeURIComponent|encodeURI|encodeURIComponent|'
  165. r'eval|isFinite|isNaN|isSafeInteger|parseFloat|parseInt|document|'
  166. r'window|'
  167. r'print)\b',
  168. Name.Builtin),
  169. (r'[$a-zA-Z_][\w.$]*\s*(:|[+\-*/]?\=)?\b', Name.Variable),
  170. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  171. (r'0x[0-9a-fA-F]+', Number.Hex),
  172. (r'[0-9]+', Number.Integer),
  173. ('"""', String, 'tdqs'),
  174. ("'''", String, 'tsqs'),
  175. ('"', String, 'dqs'),
  176. ("'", String, 'sqs'),
  177. ],
  178. 'strings': [
  179. (r'[^#\\\'"]+', String),
  180. # note that all kal strings are multi-line.
  181. # hashmarks, quotes and backslashes must be parsed one at a time
  182. ],
  183. 'interpoling_string': [
  184. (r'\}', String.Interpol, "#pop"),
  185. include('root')
  186. ],
  187. 'dqs': [
  188. (r'"', String, '#pop'),
  189. (r'\\.|\'', String), # double-quoted string don't need ' escapes
  190. (r'#\{', String.Interpol, "interpoling_string"),
  191. include('strings')
  192. ],
  193. 'sqs': [
  194. (r"'", String, '#pop'),
  195. (r'#|\\.|"', String), # single quoted strings don't need " escapses
  196. include('strings')
  197. ],
  198. 'tdqs': [
  199. (r'"""', String, '#pop'),
  200. (r'\\.|\'|"', String), # no need to escape quotes in triple-string
  201. (r'#\{', String.Interpol, "interpoling_string"),
  202. include('strings'),
  203. ],
  204. 'tsqs': [
  205. (r"'''", String, '#pop'),
  206. (r'#|\\.|\'|"', String), # no need to escape quotes in triple-strings
  207. include('strings')
  208. ],
  209. }
  210. class LiveScriptLexer(RegexLexer):
  211. """
  212. For `LiveScript`_ source code.
  213. .. _LiveScript: http://gkz.github.com/LiveScript/
  214. .. versionadded:: 1.6
  215. """
  216. name = 'LiveScript'
  217. aliases = ['live-script', 'livescript']
  218. filenames = ['*.ls']
  219. mimetypes = ['text/livescript']
  220. flags = re.DOTALL
  221. tokens = {
  222. 'commentsandwhitespace': [
  223. (r'\s+', Text),
  224. (r'/\*.*?\*/', Comment.Multiline),
  225. (r'#.*?\n', Comment.Single),
  226. ],
  227. 'multilineregex': [
  228. include('commentsandwhitespace'),
  229. (r'//([gim]+\b|\B)', String.Regex, '#pop'),
  230. (r'/', String.Regex),
  231. (r'[^/#]+', String.Regex)
  232. ],
  233. 'slashstartsregex': [
  234. include('commentsandwhitespace'),
  235. (r'//', String.Regex, ('#pop', 'multilineregex')),
  236. (r'/(?! )(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
  237. r'([gim]+\b|\B)', String.Regex, '#pop'),
  238. default('#pop'),
  239. ],
  240. 'root': [
  241. # this next expr leads to infinite loops root -> slashstartsregex
  242. # (r'^(?=\s|/|<!--)', Text, 'slashstartsregex'),
  243. include('commentsandwhitespace'),
  244. (r'(?:\([^()]+\))?[ ]*[~-]{1,2}>|'
  245. r'(?:\(?[^()\n]+\)?)?[ ]*<[~-]{1,2}', Name.Function),
  246. (r'\+\+|&&|(?<![.$])\b(?:and|x?or|is|isnt|not)\b|\?|:|=|'
  247. r'\|\||\\(?=\n)|(<<|>>>?|==?|!=?|'
  248. r'~(?!\~?>)|-(?!\-?>)|<(?!\[)|(?<!\])>|'
  249. r'[+*`%&|^/])=?',
  250. Operator, 'slashstartsregex'),
  251. (r'[{(\[;,]', Punctuation, 'slashstartsregex'),
  252. (r'[})\].]', Punctuation),
  253. (r'(?<![.$])(for|own|in|of|while|until|loop|break|'
  254. r'return|continue|switch|when|then|if|unless|else|'
  255. r'throw|try|catch|finally|new|delete|typeof|instanceof|super|'
  256. r'extends|this|class|by|const|var|to|til)\b', Keyword,
  257. 'slashstartsregex'),
  258. (r'(?<![.$])(true|false|yes|no|on|off|'
  259. r'null|NaN|Infinity|undefined|void)\b',
  260. Keyword.Constant),
  261. (r'(Array|Boolean|Date|Error|Function|Math|netscape|'
  262. r'Number|Object|Packages|RegExp|String|sun|decodeURI|'
  263. r'decodeURIComponent|encodeURI|encodeURIComponent|'
  264. r'eval|isFinite|isNaN|parseFloat|parseInt|document|window)\b',
  265. Name.Builtin),
  266. (r'[$a-zA-Z_][\w.\-:$]*\s*[:=]\s', Name.Variable,
  267. 'slashstartsregex'),
  268. (r'@[$a-zA-Z_][\w.\-:$]*\s*[:=]\s', Name.Variable.Instance,
  269. 'slashstartsregex'),
  270. (r'@', Name.Other, 'slashstartsregex'),
  271. (r'@?[$a-zA-Z_][\w-]*', Name.Other, 'slashstartsregex'),
  272. (r'[0-9]+\.[0-9]+([eE][0-9]+)?[fd]?(?:[a-zA-Z_]+)?', Number.Float),
  273. (r'[0-9]+(~[0-9a-z]+)?(?:[a-zA-Z_]+)?', Number.Integer),
  274. ('"""', String, 'tdqs'),
  275. ("'''", String, 'tsqs'),
  276. ('"', String, 'dqs'),
  277. ("'", String, 'sqs'),
  278. (r'\\\S+', String),
  279. (r'<\[.*?\]>', String),
  280. ],
  281. 'strings': [
  282. (r'[^#\\\'"]+', String),
  283. # note that all coffee script strings are multi-line.
  284. # hashmarks, quotes and backslashes must be parsed one at a time
  285. ],
  286. 'interpoling_string': [
  287. (r'\}', String.Interpol, "#pop"),
  288. include('root')
  289. ],
  290. 'dqs': [
  291. (r'"', String, '#pop'),
  292. (r'\\.|\'', String), # double-quoted string don't need ' escapes
  293. (r'#\{', String.Interpol, "interpoling_string"),
  294. (r'#', String),
  295. include('strings')
  296. ],
  297. 'sqs': [
  298. (r"'", String, '#pop'),
  299. (r'#|\\.|"', String), # single quoted strings don't need " escapses
  300. include('strings')
  301. ],
  302. 'tdqs': [
  303. (r'"""', String, '#pop'),
  304. (r'\\.|\'|"', String), # no need to escape quotes in triple-string
  305. (r'#\{', String.Interpol, "interpoling_string"),
  306. (r'#', String),
  307. include('strings'),
  308. ],
  309. 'tsqs': [
  310. (r"'''", String, '#pop'),
  311. (r'#|\\.|\'|"', String), # no need to escape quotes in triple-strings
  312. include('strings')
  313. ],
  314. }
  315. class DartLexer(RegexLexer):
  316. """
  317. For `Dart <http://dartlang.org/>`_ source code.
  318. .. versionadded:: 1.5
  319. """
  320. name = 'Dart'
  321. aliases = ['dart']
  322. filenames = ['*.dart']
  323. mimetypes = ['text/x-dart']
  324. flags = re.MULTILINE | re.DOTALL
  325. tokens = {
  326. 'root': [
  327. include('string_literal'),
  328. (r'#!(.*?)$', Comment.Preproc),
  329. (r'\b(import|export)\b', Keyword, 'import_decl'),
  330. (r'\b(library|source|part of|part)\b', Keyword),
  331. (r'[^\S\n]+', Text),
  332. (r'//.*?\n', Comment.Single),
  333. (r'/\*.*?\*/', Comment.Multiline),
  334. (r'\b(class)\b(\s+)',
  335. bygroups(Keyword.Declaration, Text), 'class'),
  336. (r'\b(assert|break|case|catch|continue|default|do|else|finally|for|'
  337. r'if|in|is|new|return|super|switch|this|throw|try|while)\b',
  338. Keyword),
  339. (r'\b(abstract|async|await|const|extends|factory|final|get|'
  340. r'implements|native|operator|set|static|sync|typedef|var|with|'
  341. r'yield)\b', Keyword.Declaration),
  342. (r'\b(bool|double|dynamic|int|num|Object|String|void)\b', Keyword.Type),
  343. (r'\b(false|null|true)\b', Keyword.Constant),
  344. (r'[~!%^&*+=|?:<>/-]|as\b', Operator),
  345. (r'[a-zA-Z_$]\w*:', Name.Label),
  346. (r'[a-zA-Z_$]\w*', Name),
  347. (r'[(){}\[\],.;]', Punctuation),
  348. (r'0[xX][0-9a-fA-F]+', Number.Hex),
  349. # DIGIT+ (‘.’ DIGIT*)? EXPONENT?
  350. (r'\d+(\.\d*)?([eE][+-]?\d+)?', Number),
  351. (r'\.\d+([eE][+-]?\d+)?', Number), # ‘.’ DIGIT+ EXPONENT?
  352. (r'\n', Text)
  353. # pseudo-keyword negate intentionally left out
  354. ],
  355. 'class': [
  356. (r'[a-zA-Z_$]\w*', Name.Class, '#pop')
  357. ],
  358. 'import_decl': [
  359. include('string_literal'),
  360. (r'\s+', Text),
  361. (r'\b(as|show|hide)\b', Keyword),
  362. (r'[a-zA-Z_$]\w*', Name),
  363. (r'\,', Punctuation),
  364. (r'\;', Punctuation, '#pop')
  365. ],
  366. 'string_literal': [
  367. # Raw strings.
  368. (r'r"""([\w\W]*?)"""', String.Double),
  369. (r"r'''([\w\W]*?)'''", String.Single),
  370. (r'r"(.*?)"', String.Double),
  371. (r"r'(.*?)'", String.Single),
  372. # Normal Strings.
  373. (r'"""', String.Double, 'string_double_multiline'),
  374. (r"'''", String.Single, 'string_single_multiline'),
  375. (r'"', String.Double, 'string_double'),
  376. (r"'", String.Single, 'string_single')
  377. ],
  378. 'string_common': [
  379. (r"\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|u\{[0-9A-Fa-f]*\}|[a-z'\"$\\])",
  380. String.Escape),
  381. (r'(\$)([a-zA-Z_]\w*)', bygroups(String.Interpol, Name)),
  382. (r'(\$\{)(.*?)(\})',
  383. bygroups(String.Interpol, using(this), String.Interpol))
  384. ],
  385. 'string_double': [
  386. (r'"', String.Double, '#pop'),
  387. (r'[^"$\\\n]+', String.Double),
  388. include('string_common'),
  389. (r'\$+', String.Double)
  390. ],
  391. 'string_double_multiline': [
  392. (r'"""', String.Double, '#pop'),
  393. (r'[^"$\\]+', String.Double),
  394. include('string_common'),
  395. (r'(\$|\")+', String.Double)
  396. ],
  397. 'string_single': [
  398. (r"'", String.Single, '#pop'),
  399. (r"[^'$\\\n]+", String.Single),
  400. include('string_common'),
  401. (r'\$+', String.Single)
  402. ],
  403. 'string_single_multiline': [
  404. (r"'''", String.Single, '#pop'),
  405. (r'[^\'$\\]+', String.Single),
  406. include('string_common'),
  407. (r'(\$|\')+', String.Single)
  408. ]
  409. }
  410. class TypeScriptLexer(RegexLexer):
  411. """
  412. For `TypeScript <http://typescriptlang.org/>`_ source code.
  413. .. versionadded:: 1.6
  414. """
  415. name = 'TypeScript'
  416. aliases = ['ts', 'typescript']
  417. filenames = ['*.ts', '*.tsx']
  418. mimetypes = ['text/x-typescript']
  419. flags = re.DOTALL | re.MULTILINE
  420. tokens = {
  421. 'commentsandwhitespace': [
  422. (r'\s+', Text),
  423. (r'<!--', Comment),
  424. (r'//.*?\n', Comment.Single),
  425. (r'/\*.*?\*/', Comment.Multiline)
  426. ],
  427. 'slashstartsregex': [
  428. include('commentsandwhitespace'),
  429. (r'/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
  430. r'([gim]+\b|\B)', String.Regex, '#pop'),
  431. (r'(?=/)', Text, ('#pop', 'badregex')),
  432. default('#pop')
  433. ],
  434. 'badregex': [
  435. (r'\n', Text, '#pop')
  436. ],
  437. 'root': [
  438. (r'^(?=\s|/|<!--)', Text, 'slashstartsregex'),
  439. include('commentsandwhitespace'),
  440. (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|'
  441. r'(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'),
  442. (r'[{(\[;,]', Punctuation, 'slashstartsregex'),
  443. (r'[})\].]', Punctuation),
  444. (r'(for|in|while|do|break|return|continue|switch|case|default|if|else|'
  445. r'throw|try|catch|finally|new|delete|typeof|instanceof|void|'
  446. r'this)\b', Keyword, 'slashstartsregex'),
  447. (r'(var|let|with|function)\b', Keyword.Declaration, 'slashstartsregex'),
  448. (r'(abstract|boolean|byte|char|class|const|debugger|double|enum|export|'
  449. r'extends|final|float|goto|implements|import|int|interface|long|native|'
  450. r'package|private|protected|public|short|static|super|synchronized|throws|'
  451. r'transient|volatile)\b', Keyword.Reserved),
  452. (r'(true|false|null|NaN|Infinity|undefined)\b', Keyword.Constant),
  453. (r'(Array|Boolean|Date|Error|Function|Math|netscape|'
  454. r'Number|Object|Packages|RegExp|String|sun|decodeURI|'
  455. r'decodeURIComponent|encodeURI|encodeURIComponent|'
  456. r'Error|eval|isFinite|isNaN|parseFloat|parseInt|document|this|'
  457. r'window)\b', Name.Builtin),
  458. # Match stuff like: module name {...}
  459. (r'\b(module)(\s*)(\s*[\w?.$][\w?.$]*)(\s*)',
  460. bygroups(Keyword.Reserved, Text, Name.Other, Text), 'slashstartsregex'),
  461. # Match variable type keywords
  462. (r'\b(string|bool|number)\b', Keyword.Type),
  463. # Match stuff like: constructor
  464. (r'\b(constructor|declare|interface|as|AS)\b', Keyword.Reserved),
  465. # Match stuff like: super(argument, list)
  466. (r'(super)(\s*)(\([\w,?.$\s]+\s*\))',
  467. bygroups(Keyword.Reserved, Text), 'slashstartsregex'),
  468. # Match stuff like: function() {...}
  469. (r'([a-zA-Z_?.$][\w?.$]*)\(\) \{', Name.Other, 'slashstartsregex'),
  470. # Match stuff like: (function: return type)
  471. (r'([\w?.$][\w?.$]*)(\s*:\s*)([\w?.$][\w?.$]*)',
  472. bygroups(Name.Other, Text, Keyword.Type)),
  473. (r'[$a-zA-Z_]\w*', Name.Other),
  474. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  475. (r'0x[0-9a-fA-F]+', Number.Hex),
  476. (r'[0-9]+', Number.Integer),
  477. (r'"(\\\\|\\"|[^"])*"', String.Double),
  478. (r"'(\\\\|\\'|[^'])*'", String.Single),
  479. (r'`', String.Backtick, 'interp'),
  480. # Match stuff like: Decorators
  481. (r'@\w+', Keyword.Declaration),
  482. ],
  483. # The 'interp*' rules match those in JavascriptLexer. Changes made
  484. # there should be reflected here as well.
  485. 'interp': [
  486. (r'`', String.Backtick, '#pop'),
  487. (r'\\\\', String.Backtick),
  488. (r'\\`', String.Backtick),
  489. (r'\$\{', String.Interpol, 'interp-inside'),
  490. (r'\$', String.Backtick),
  491. (r'[^`\\$]+', String.Backtick),
  492. ],
  493. 'interp-inside': [
  494. # TODO: should this include single-line comments and allow nesting strings?
  495. (r'\}', String.Interpol, '#pop'),
  496. include('root'),
  497. ],
  498. }
  499. def analyse_text(text):
  500. if re.search('^(import.+(from\s+)?["\']|'
  501. '(export\s*)?(interface|class|function)\s+)',
  502. text, re.MULTILINE):
  503. return 1.0
  504. class LassoLexer(RegexLexer):
  505. """
  506. For `Lasso <http://www.lassosoft.com/>`_ source code, covering both Lasso 9
  507. syntax and LassoScript for Lasso 8.6 and earlier. For Lasso embedded in
  508. HTML, use the `LassoHtmlLexer`.
  509. Additional options accepted:
  510. `builtinshighlighting`
  511. If given and ``True``, highlight builtin types, traits, methods, and
  512. members (default: ``True``).
  513. `requiredelimiters`
  514. If given and ``True``, only highlight code between delimiters as Lasso
  515. (default: ``False``).
  516. .. versionadded:: 1.6
  517. """
  518. name = 'Lasso'
  519. aliases = ['lasso', 'lassoscript']
  520. filenames = ['*.lasso', '*.lasso[89]']
  521. alias_filenames = ['*.incl', '*.inc', '*.las']
  522. mimetypes = ['text/x-lasso']
  523. flags = re.IGNORECASE | re.DOTALL | re.MULTILINE
  524. tokens = {
  525. 'root': [
  526. (r'^#![ \S]+lasso9\b', Comment.Preproc, 'lasso'),
  527. (r'(?=\[|<)', Other, 'delimiters'),
  528. (r'\s+', Other),
  529. default(('delimiters', 'lassofile')),
  530. ],
  531. 'delimiters': [
  532. (r'\[no_square_brackets\]', Comment.Preproc, 'nosquarebrackets'),
  533. (r'\[noprocess\]', Comment.Preproc, 'noprocess'),
  534. (r'\[', Comment.Preproc, 'squarebrackets'),
  535. (r'<\?(lasso(script)?|=)', Comment.Preproc, 'anglebrackets'),
  536. (r'<(!--.*?-->)?', Other),
  537. (r'[^[<]+', Other),
  538. ],
  539. 'nosquarebrackets': [
  540. (r'\[noprocess\]', Comment.Preproc, 'noprocess'),
  541. (r'\[', Other),
  542. (r'<\?(lasso(script)?|=)', Comment.Preproc, 'anglebrackets'),
  543. (r'<(!--.*?-->)?', Other),
  544. (r'[^[<]+', Other),
  545. ],
  546. 'noprocess': [
  547. (r'\[/noprocess\]', Comment.Preproc, '#pop'),
  548. (r'\[', Other),
  549. (r'[^[]', Other),
  550. ],
  551. 'squarebrackets': [
  552. (r'\]', Comment.Preproc, '#pop'),
  553. include('lasso'),
  554. ],
  555. 'anglebrackets': [
  556. (r'\?>', Comment.Preproc, '#pop'),
  557. include('lasso'),
  558. ],
  559. 'lassofile': [
  560. (r'\]|\?>', Comment.Preproc, '#pop'),
  561. include('lasso'),
  562. ],
  563. 'whitespacecomments': [
  564. (r'\s+', Text),
  565. (r'//.*?\n', Comment.Single),
  566. (r'/\*\*!.*?\*/', String.Doc),
  567. (r'/\*.*?\*/', Comment.Multiline),
  568. ],
  569. 'lasso': [
  570. # whitespace/comments
  571. include('whitespacecomments'),
  572. # literals
  573. (r'\d*\.\d+(e[+-]?\d+)?', Number.Float),
  574. (r'0x[\da-f]+', Number.Hex),
  575. (r'\d+', Number.Integer),
  576. (r'(infinity|NaN)\b', Number),
  577. (r"'", String.Single, 'singlestring'),
  578. (r'"', String.Double, 'doublestring'),
  579. (r'`[^`]*`', String.Backtick),
  580. # names
  581. (r'\$[a-z_][\w.]*', Name.Variable),
  582. (r'#([a-z_][\w.]*|\d+\b)', Name.Variable.Instance),
  583. (r"(\.\s*)('[a-z_][\w.]*')",
  584. bygroups(Name.Builtin.Pseudo, Name.Variable.Class)),
  585. (r"(self)(\s*->\s*)('[a-z_][\w.]*')",
  586. bygroups(Name.Builtin.Pseudo, Operator, Name.Variable.Class)),
  587. (r'(\.\.?\s*)([a-z_][\w.]*(=(?!=))?)',
  588. bygroups(Name.Builtin.Pseudo, Name.Other.Member)),
  589. (r'(->\\?\s*|&\s*)([a-z_][\w.]*(=(?!=))?)',
  590. bygroups(Operator, Name.Other.Member)),
  591. (r'(?<!->)(self|inherited|currentcapture|givenblock)\b',
  592. Name.Builtin.Pseudo),
  593. (r'-(?!infinity)[a-z_][\w.]*', Name.Attribute),
  594. (r'::\s*[a-z_][\w.]*', Name.Label),
  595. (r'(error_(code|msg)_\w+|Error_AddError|Error_ColumnRestriction|'
  596. r'Error_DatabaseConnectionUnavailable|Error_DatabaseTimeout|'
  597. r'Error_DeleteError|Error_FieldRestriction|Error_FileNotFound|'
  598. r'Error_InvalidDatabase|Error_InvalidPassword|'
  599. r'Error_InvalidUsername|Error_ModuleNotFound|'
  600. r'Error_NoError|Error_NoPermission|Error_OutOfMemory|'
  601. r'Error_ReqColumnMissing|Error_ReqFieldMissing|'
  602. r'Error_RequiredColumnMissing|Error_RequiredFieldMissing|'
  603. r'Error_UpdateError)\b', Name.Exception),
  604. # definitions
  605. (r'(define)(\s+)([a-z_][\w.]*)(\s*=>\s*)(type|trait|thread)\b',
  606. bygroups(Keyword.Declaration, Text, Name.Class, Operator, Keyword)),
  607. (r'(define)(\s+)([a-z_][\w.]*)(\s*->\s*)([a-z_][\w.]*=?|[-+*/%])',
  608. bygroups(Keyword.Declaration, Text, Name.Class, Operator,
  609. Name.Function), 'signature'),
  610. (r'(define)(\s+)([a-z_][\w.]*)',
  611. bygroups(Keyword.Declaration, Text, Name.Function), 'signature'),
  612. (r'(public|protected|private|provide)(\s+)(([a-z_][\w.]*=?|[-+*/%])'
  613. r'(?=\s*\())', bygroups(Keyword, Text, Name.Function),
  614. 'signature'),
  615. (r'(public|protected|private|provide)(\s+)([a-z_][\w.]*)',
  616. bygroups(Keyword, Text, Name.Function)),
  617. # keywords
  618. (r'(true|false|none|minimal|full|all|void)\b', Keyword.Constant),
  619. (r'(local|var|variable|global|data(?=\s))\b', Keyword.Declaration),
  620. (r'(array|date|decimal|duration|integer|map|pair|string|tag|xml|'
  621. r'null|boolean|bytes|keyword|list|locale|queue|set|stack|'
  622. r'staticarray)\b', Keyword.Type),
  623. (r'([a-z_][\w.]*)(\s+)(in)\b', bygroups(Name, Text, Keyword)),
  624. (r'(let|into)(\s+)([a-z_][\w.]*)', bygroups(Keyword, Text, Name)),
  625. (r'require\b', Keyword, 'requiresection'),
  626. (r'(/?)(Namespace_Using)\b', bygroups(Punctuation, Keyword.Namespace)),
  627. (r'(/?)(Cache|Database_Names|Database_SchemaNames|'
  628. r'Database_TableNames|Define_Tag|Define_Type|Email_Batch|'
  629. r'Encode_Set|HTML_Comment|Handle|Handle_Error|Header|If|Inline|'
  630. r'Iterate|LJAX_Target|Link|Link_CurrentAction|Link_CurrentGroup|'
  631. r'Link_CurrentRecord|Link_Detail|Link_FirstGroup|Link_FirstRecord|'
  632. r'Link_LastGroup|Link_LastRecord|Link_NextGroup|Link_NextRecord|'
  633. r'Link_PrevGroup|Link_PrevRecord|Log|Loop|Output_None|Portal|'
  634. r'Private|Protect|Records|Referer|Referrer|Repeating|ResultSet|'
  635. r'Rows|Search_Args|Search_Arguments|Select|Sort_Args|'
  636. r'Sort_Arguments|Thread_Atomic|Value_List|While|Abort|Case|Else|'
  637. r'Fail_If|Fail_IfNot|Fail|If_Empty|If_False|If_Null|If_True|'
  638. r'Loop_Abort|Loop_Continue|Loop_Count|Params|Params_Up|Return|'
  639. r'Return_Value|Run_Children|SOAP_DefineTag|SOAP_LastRequest|'
  640. r'SOAP_LastResponse|Tag_Name|ascending|average|by|define|'
  641. r'descending|do|equals|frozen|group|handle_failure|import|in|into|'
  642. r'join|let|match|max|min|on|order|parent|protected|provide|public|'
  643. r'require|returnhome|skip|split_thread|sum|take|thread|to|trait|'
  644. r'type|where|with|yield|yieldhome)\b',
  645. bygroups(Punctuation, Keyword)),
  646. # other
  647. (r',', Punctuation, 'commamember'),
  648. (r'(and|or|not)\b', Operator.Word),
  649. (r'([a-z_][\w.]*)(\s*::\s*[a-z_][\w.]*)?(\s*=(?!=))',
  650. bygroups(Name, Name.Label, Operator)),
  651. (r'(/?)([\w.]+)', bygroups(Punctuation, Name.Other)),
  652. (r'(=)(n?bw|n?ew|n?cn|lte?|gte?|n?eq|n?rx|ft)\b',
  653. bygroups(Operator, Operator.Word)),
  654. (r':=|[-+*/%=<>&|!?\\]+', Operator),
  655. (r'[{}():;,@^]', Punctuation),
  656. ],
  657. 'singlestring': [
  658. (r"'", String.Single, '#pop'),
  659. (r"[^'\\]+", String.Single),
  660. include('escape'),
  661. (r"\\", String.Single),
  662. ],
  663. 'doublestring': [
  664. (r'"', String.Double, '#pop'),
  665. (r'[^"\\]+', String.Double),
  666. include('escape'),
  667. (r'\\', String.Double),
  668. ],
  669. 'escape': [
  670. (r'\\(U[\da-f]{8}|u[\da-f]{4}|x[\da-f]{1,2}|[0-7]{1,3}|:[^:\n\r]+:|'
  671. r'[abefnrtv?"\'\\]|$)', String.Escape),
  672. ],
  673. 'signature': [
  674. (r'=>', Operator, '#pop'),
  675. (r'\)', Punctuation, '#pop'),
  676. (r'[(,]', Punctuation, 'parameter'),
  677. include('lasso'),
  678. ],
  679. 'parameter': [
  680. (r'\)', Punctuation, '#pop'),
  681. (r'-?[a-z_][\w.]*', Name.Attribute, '#pop'),
  682. (r'\.\.\.', Name.Builtin.Pseudo),
  683. include('lasso'),
  684. ],
  685. 'requiresection': [
  686. (r'(([a-z_][\w.]*=?|[-+*/%])(?=\s*\())', Name, 'requiresignature'),
  687. (r'(([a-z_][\w.]*=?|[-+*/%])(?=(\s*::\s*[\w.]+)?\s*,))', Name),
  688. (r'[a-z_][\w.]*=?|[-+*/%]', Name, '#pop'),
  689. (r'::\s*[a-z_][\w.]*', Name.Label),
  690. (r',', Punctuation),
  691. include('whitespacecomments'),
  692. ],
  693. 'requiresignature': [
  694. (r'(\)(?=(\s*::\s*[\w.]+)?\s*,))', Punctuation, '#pop'),
  695. (r'\)', Punctuation, '#pop:2'),
  696. (r'-?[a-z_][\w.]*', Name.Attribute),
  697. (r'::\s*[a-z_][\w.]*', Name.Label),
  698. (r'\.\.\.', Name.Builtin.Pseudo),
  699. (r'[(,]', Punctuation),
  700. include('whitespacecomments'),
  701. ],
  702. 'commamember': [
  703. (r'(([a-z_][\w.]*=?|[-+*/%])'
  704. r'(?=\s*(\(([^()]*\([^()]*\))*[^)]*\)\s*)?(::[\w.\s]+)?=>))',
  705. Name.Function, 'signature'),
  706. include('whitespacecomments'),
  707. default('#pop'),
  708. ],
  709. }
  710. def __init__(self, **options):
  711. self.builtinshighlighting = get_bool_opt(
  712. options, 'builtinshighlighting', True)
  713. self.requiredelimiters = get_bool_opt(
  714. options, 'requiredelimiters', False)
  715. self._builtins = set()
  716. self._members = set()
  717. if self.builtinshighlighting:
  718. from pygments.lexers._lasso_builtins import BUILTINS, MEMBERS
  719. for key, value in iteritems(BUILTINS):
  720. self._builtins.update(value)
  721. for key, value in iteritems(MEMBERS):
  722. self._members.update(value)
  723. RegexLexer.__init__(self, **options)
  724. def get_tokens_unprocessed(self, text):
  725. stack = ['root']
  726. if self.requiredelimiters:
  727. stack.append('delimiters')
  728. for index, token, value in \
  729. RegexLexer.get_tokens_unprocessed(self, text, stack):
  730. if (token is Name.Other and value.lower() in self._builtins or
  731. token is Name.Other.Member and
  732. value.lower().rstrip('=') in self._members):
  733. yield index, Name.Builtin, value
  734. continue
  735. yield index, token, value
  736. def analyse_text(text):
  737. rv = 0.0
  738. if 'bin/lasso9' in text:
  739. rv += 0.8
  740. if re.search(r'<\?lasso', text, re.I):
  741. rv += 0.4
  742. if re.search(r'local\(', text, re.I):
  743. rv += 0.4
  744. return rv
  745. class ObjectiveJLexer(RegexLexer):
  746. """
  747. For Objective-J source code with preprocessor directives.
  748. .. versionadded:: 1.3
  749. """
  750. name = 'Objective-J'
  751. aliases = ['objective-j', 'objectivej', 'obj-j', 'objj']
  752. filenames = ['*.j']
  753. mimetypes = ['text/x-objective-j']
  754. #: optional Comment or Whitespace
  755. _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)*'
  756. flags = re.DOTALL | re.MULTILINE
  757. tokens = {
  758. 'root': [
  759. include('whitespace'),
  760. # function definition
  761. (r'^(' + _ws + r'[+-]' + _ws + r')([(a-zA-Z_].*?[^(])(' + _ws + r'\{)',
  762. bygroups(using(this), using(this, state='function_signature'),
  763. using(this))),
  764. # class definition
  765. (r'(@interface|@implementation)(\s+)', bygroups(Keyword, Text),
  766. 'classname'),
  767. (r'(@class|@protocol)(\s*)', bygroups(Keyword, Text),
  768. 'forward_classname'),
  769. (r'(\s*)(@end)(\s*)', bygroups(Text, Keyword, Text)),
  770. include('statements'),
  771. ('[{()}]', Punctuation),
  772. (';', Punctuation),
  773. ],
  774. 'whitespace': [
  775. (r'(@import)(\s+)("(?:\\\\|\\"|[^"])*")',
  776. bygroups(Comment.Preproc, Text, String.Double)),
  777. (r'(@import)(\s+)(<(?:\\\\|\\>|[^>])*>)',
  778. bygroups(Comment.Preproc, Text, String.Double)),
  779. (r'(#(?:include|import))(\s+)("(?:\\\\|\\"|[^"])*")',
  780. bygroups(Comment.Preproc, Text, String.Double)),
  781. (r'(#(?:include|import))(\s+)(<(?:\\\\|\\>|[^>])*>)',
  782. bygroups(Comment.Preproc, Text, String.Double)),
  783. (r'#if\s+0', Comment.Preproc, 'if0'),
  784. (r'#', Comment.Preproc, 'macro'),
  785. (r'\n', Text),
  786. (r'\s+', Text),
  787. (r'\\\n', Text), # line continuation
  788. (r'//(\n|(.|\n)*?[^\\]\n)', Comment.Single),
  789. (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
  790. (r'<!--', Comment),
  791. ],
  792. 'slashstartsregex': [
  793. include('whitespace'),
  794. (r'/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
  795. r'([gim]+\b|\B)', String.Regex, '#pop'),
  796. (r'(?=/)', Text, ('#pop', 'badregex')),
  797. default('#pop'),
  798. ],
  799. 'badregex': [
  800. (r'\n', Text, '#pop'),
  801. ],
  802. 'statements': [
  803. (r'(L|@)?"', String, 'string'),
  804. (r"(L|@)?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'",
  805. String.Char),
  806. (r'"(\\\\|\\"|[^"])*"', String.Double),
  807. (r"'(\\\\|\\'|[^'])*'", String.Single),
  808. (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float),
  809. (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
  810. (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex),
  811. (r'0[0-7]+[Ll]?', Number.Oct),
  812. (r'\d+[Ll]?', Number.Integer),
  813. (r'^(?=\s|/|<!--)', Text, 'slashstartsregex'),
  814. (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|'
  815. r'(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?',
  816. Operator, 'slashstartsregex'),
  817. (r'[{(\[;,]', Punctuation, 'slashstartsregex'),
  818. (r'[})\].]', Punctuation),
  819. (r'(for|in|while|do|break|return|continue|switch|case|default|if|'
  820. r'else|throw|try|catch|finally|new|delete|typeof|instanceof|void|'
  821. r'prototype|__proto__)\b', Keyword, 'slashstartsregex'),
  822. (r'(var|with|function)\b', Keyword.Declaration, 'slashstartsregex'),
  823. (r'(@selector|@private|@protected|@public|@encode|'
  824. r'@synchronized|@try|@throw|@catch|@finally|@end|@property|'
  825. r'@synthesize|@dynamic|@for|@accessors|new)\b', Keyword),
  826. (r'(int|long|float|short|double|char|unsigned|signed|void|'
  827. r'id|BOOL|bool|boolean|IBOutlet|IBAction|SEL|@outlet|@action)\b',
  828. Keyword.Type),
  829. (r'(self|super)\b', Name.Builtin),
  830. (r'(TRUE|YES|FALSE|NO|Nil|nil|NULL)\b', Keyword.Constant),
  831. (r'(true|false|null|NaN|Infinity|undefined)\b', Keyword.Constant),
  832. (r'(ABS|ASIN|ACOS|ATAN|ATAN2|SIN|COS|TAN|EXP|POW|CEIL|FLOOR|ROUND|'
  833. r'MIN|MAX|RAND|SQRT|E|LN2|LN10|LOG2E|LOG10E|PI|PI2|PI_2|SQRT1_2|'
  834. r'SQRT2)\b', Keyword.Constant),
  835. (r'(Array|Boolean|Date|Error|Function|Math|netscape|'
  836. r'Number|Object|Packages|RegExp|String|sun|decodeURI|'
  837. r'decodeURIComponent|encodeURI|encodeURIComponent|'
  838. r'Error|eval|isFinite|isNaN|parseFloat|parseInt|document|this|'
  839. r'window)\b', Name.Builtin),
  840. (r'([$a-zA-Z_]\w*)(' + _ws + r')(?=\()',
  841. bygroups(Name.Function, using(this))),
  842. (r'[$a-zA-Z_]\w*', Name),
  843. ],
  844. 'classname': [
  845. # interface definition that inherits
  846. (r'([a-zA-Z_]\w*)(' + _ws + r':' + _ws +
  847. r')([a-zA-Z_]\w*)?',
  848. bygroups(Name.Class, using(this), Name.Class), '#pop'),
  849. # interface definition for a category
  850. (r'([a-zA-Z_]\w*)(' + _ws + r'\()([a-zA-Z_]\w*)(\))',
  851. bygroups(Name.Class, using(this), Name.Label, Text), '#pop'),
  852. # simple interface / implementation
  853. (r'([a-zA-Z_]\w*)', Name.Class, '#pop'),
  854. ],
  855. 'forward_classname': [
  856. (r'([a-zA-Z_]\w*)(\s*,\s*)',
  857. bygroups(Name.Class, Text), '#push'),
  858. (r'([a-zA-Z_]\w*)(\s*;?)',
  859. bygroups(Name.Class, Text), '#pop'),
  860. ],
  861. 'function_signature': [
  862. include('whitespace'),
  863. # start of a selector w/ parameters
  864. (r'(\(' + _ws + r')' # open paren
  865. r'([a-zA-Z_]\w+)' # return type
  866. r'(' + _ws + r'\)' + _ws + r')' # close paren
  867. r'([$a-zA-Z_]\w+' + _ws + r':)', # function name
  868. bygroups(using(this), Keyword.Type, using(this),
  869. Name.Function), 'function_parameters'),
  870. # no-param function
  871. (r'(\(' + _ws + r')' # open paren
  872. r'([a-zA-Z_]\w+)' # return type
  873. r'(' + _ws + r'\)' + _ws + r')' # close paren
  874. r'([$a-zA-Z_]\w+)', # function name
  875. bygroups(using(this), Keyword.Type, using(this),
  876. Name.Function), "#pop"),
  877. # no return type given, start of a selector w/ parameters
  878. (r'([$a-zA-Z_]\w+' + _ws + r':)', # function name
  879. bygroups(Name.Function), 'function_parameters'),
  880. # no return type given, no-param function
  881. (r'([$a-zA-Z_]\w+)', # function name
  882. bygroups(Name.Function), "#pop"),
  883. default('#pop'),
  884. ],
  885. 'function_parameters': [
  886. include('whitespace'),
  887. # parameters
  888. (r'(\(' + _ws + ')' # open paren
  889. r'([^)]+)' # type
  890. r'(' + _ws + r'\)' + _ws + r')' # close paren
  891. r'([$a-zA-Z_]\w+)', # param name
  892. bygroups(using(this), Keyword.Type, using(this), Text)),
  893. # one piece of a selector name
  894. (r'([$a-zA-Z_]\w+' + _ws + r':)', # function name
  895. Name.Function),
  896. # smallest possible selector piece
  897. (r'(:)', Name.Function),
  898. # var args
  899. (r'(,' + _ws + r'\.\.\.)', using(this)),
  900. # param name
  901. (r'([$a-zA-Z_]\w+)', Text),
  902. ],
  903. 'expression': [
  904. (r'([$a-zA-Z_]\w*)(\()', bygroups(Name.Function,
  905. Punctuation)),
  906. (r'(\))', Punctuation, "#pop"),
  907. ],
  908. 'string': [
  909. (r'"', String, '#pop'),
  910. (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
  911. (r'[^\\"\n]+', String), # all other characters
  912. (r'\\\n', String), # line continuation
  913. (r'\\', String), # stray backslash
  914. ],
  915. 'macro': [
  916. (r'[^/\n]+', Comment.Preproc),
  917. (r'/[*](.|\n)*?[*]/', Comment.Multiline),
  918. (r'//.*?\n', Comment.Single, '#pop'),
  919. (r'/', Comment.Preproc),
  920. (r'(?<=\\)\n', Comment.Preproc),
  921. (r'\n', Comment.Preproc, '#pop'),
  922. ],
  923. 'if0': [
  924. (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'),
  925. (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'),
  926. (r'.*?\n', Comment),
  927. ]
  928. }
  929. def analyse_text(text):
  930. if re.search('^\s*@import\s+[<"]', text, re.MULTILINE):
  931. # special directive found in most Objective-J files
  932. return True
  933. return False
  934. class CoffeeScriptLexer(RegexLexer):
  935. """
  936. For `CoffeeScript`_ source code.
  937. .. _CoffeeScript: http://coffeescript.org
  938. .. versionadded:: 1.3
  939. """
  940. name = 'CoffeeScript'
  941. aliases = ['coffee-script', 'coffeescript', 'coffee']
  942. filenames = ['*.coffee']
  943. mimetypes = ['text/coffeescript']
  944. _operator_re = (
  945. r'\+\+|~|&&|\band\b|\bor\b|\bis\b|\bisnt\b|\bnot\b|\?|:|'
  946. r'\|\||\\(?=\n)|'
  947. r'(<<|>>>?|==?(?!>)|!=?|=(?!>)|-(?!>)|[<>+*`%&\|\^/])=?')
  948. flags = re.DOTALL
  949. tokens = {
  950. 'commentsandwhitespace': [
  951. (r'\s+', Text),
  952. (r'###[^#].*?###', Comment.Multiline),
  953. (r'#(?!##[^#]).*?\n', Comment.Single),
  954. ],
  955. 'multilineregex': [
  956. (r'[^/#]+', String.Regex),
  957. (r'///([gim]+\b|\B)', String.Regex, '#pop'),
  958. (r'#\{', String.Interpol, 'interpoling_string'),
  959. (r'[/#]', String.Regex),
  960. ],
  961. 'slashstartsregex': [
  962. include('commentsandwhitespace'),
  963. (r'///', String.Regex, ('#pop', 'multilineregex')),
  964. (r'/(?! )(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
  965. r'([gim]+\b|\B)', String.Regex, '#pop'),
  966. # This isn't really guarding against mishighlighting well-formed
  967. # code, just the ability to infinite-loop between root and
  968. # slashstartsregex.
  969. (r'/', Operator),
  970. default('#pop'),
  971. ],
  972. 'root': [
  973. include('commentsandwhitespace'),
  974. (r'^(?=\s|/)', Text, 'slashstartsregex'),
  975. (_operator_re, Operator, 'slashstartsregex'),
  976. (r'(?:\([^()]*\))?\s*[=-]>', Name.Function, 'slashstartsregex'),
  977. (r'[{(\[;,]', Punctuation, 'slashstartsregex'),
  978. (r'[})\].]', Punctuation),
  979. (r'(?<![.$])(for|own|in|of|while|until|'
  980. r'loop|break|return|continue|'
  981. r'switch|when|then|if|unless|else|'
  982. r'throw|try|catch|finally|new|delete|typeof|instanceof|super|'
  983. r'extends|this|class|by)\b', Keyword, 'slashstartsregex'),
  984. (r'(?<![.$])(true|false|yes|no|on|off|null|'
  985. r'NaN|Infinity|undefined)\b',
  986. Keyword.Constant),
  987. (r'(Array|Boolean|Date|Error|Function|Math|netscape|'
  988. r'Number|Object|Packages|RegExp|String|sun|decodeURI|'
  989. r'decodeURIComponent|encodeURI|encodeURIComponent|'
  990. r'eval|isFinite|isNaN|parseFloat|parseInt|document|window)\b',
  991. Name.Builtin),
  992. (r'[$a-zA-Z_][\w.:$]*\s*[:=]\s', Name.Variable,
  993. 'slashstartsregex'),
  994. (r'@[$a-zA-Z_][\w.:$]*\s*[:=]\s', Name.Variable.Instance,
  995. 'slashstartsregex'),
  996. (r'@', Name.Other, 'slashstartsregex'),
  997. (r'@?[$a-zA-Z_][\w$]*', Name.Other),
  998. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  999. (r'0x[0-9a-fA-F]+', Number.Hex),
  1000. (r'[0-9]+', Number.Integer),
  1001. ('"""', String, 'tdqs'),
  1002. ("'''", String, 'tsqs'),
  1003. ('"', String, 'dqs'),
  1004. ("'", String, 'sqs'),
  1005. ],
  1006. 'strings': [
  1007. (r'[^#\\\'"]+', String),
  1008. # note that all coffee script strings are multi-line.
  1009. # hashmarks, quotes and backslashes must be parsed one at a time
  1010. ],
  1011. 'interpoling_string': [
  1012. (r'\}', String.Interpol, "#pop"),
  1013. include('root')
  1014. ],
  1015. 'dqs': [
  1016. (r'"', String, '#pop'),
  1017. (r'\\.|\'', String), # double-quoted string don't need ' escapes
  1018. (r'#\{', String.Interpol, "interpoling_string"),
  1019. (r'#', String),
  1020. include('strings')
  1021. ],
  1022. 'sqs': [
  1023. (r"'", String, '#pop'),
  1024. (r'#|\\.|"', String), # single quoted strings don't need " escapses
  1025. include('strings')
  1026. ],
  1027. 'tdqs': [
  1028. (r'"""', String, '#pop'),
  1029. (r'\\.|\'|"', String), # no need to escape quotes in triple-string
  1030. (r'#\{', String.Interpol, "interpoling_string"),
  1031. (r'#', String),
  1032. include('strings'),
  1033. ],
  1034. 'tsqs': [
  1035. (r"'''", String, '#pop'),
  1036. (r'#|\\.|\'|"', String), # no need to escape quotes in triple-strings
  1037. include('strings')
  1038. ],
  1039. }
  1040. class MaskLexer(RegexLexer):
  1041. """
  1042. For `Mask <http://github.com/atmajs/MaskJS>`__ markup.
  1043. .. versionadded:: 2.0
  1044. """
  1045. name = 'Mask'
  1046. aliases = ['mask']
  1047. filenames = ['*.mask']
  1048. mimetypes = ['text/x-mask']
  1049. flags = re.MULTILINE | re.IGNORECASE | re.DOTALL
  1050. tokens = {
  1051. 'root': [
  1052. (r'\s+', Text),
  1053. (r'//.*?\n', Comment.Single),
  1054. (r'/\*.*?\*/', Comment.Multiline),
  1055. (r'[{};>]', Punctuation),
  1056. (r"'''", String, 'string-trpl-single'),
  1057. (r'"""', String, 'string-trpl-double'),
  1058. (r"'", String, 'string-single'),
  1059. (r'"', String, 'string-double'),
  1060. (r'([\w-]+)', Name.Tag, 'node'),
  1061. (r'([^.#;{>\s]+)', Name.Class, 'node'),
  1062. (r'(#[\w-]+)', Name.Function, 'node'),
  1063. (r'(\.[\w-]+)', Name.Variable.Class, 'node')
  1064. ],
  1065. 'string-base': [
  1066. (r'\\.', String.Escape),
  1067. (r'~\[', String.Interpol, 'interpolation'),
  1068. (r'.', String.Single),
  1069. ],
  1070. 'string-single': [
  1071. (r"'", String.Single, '#pop'),
  1072. include('string-base')
  1073. ],
  1074. 'string-double': [
  1075. (r'"', String.Single, '#pop'),
  1076. include('string-base')
  1077. ],
  1078. 'string-trpl-single': [
  1079. (r"'''", String.Single, '#pop'),
  1080. include('string-base')
  1081. ],
  1082. 'string-trpl-double': [
  1083. (r'"""', String.Single, '#pop'),
  1084. include('string-base')
  1085. ],
  1086. 'interpolation': [
  1087. (r'\]', String.Interpol, '#pop'),
  1088. (r'\s*:', String.Interpol, 'expression'),
  1089. (r'\s*\w+:', Name.Other),
  1090. (r'[^\]]+', String.Interpol)
  1091. ],
  1092. 'expression': [
  1093. (r'[^\]]+', using(JavascriptLexer), '#pop')
  1094. ],
  1095. 'node': [
  1096. (r'\s+', Text),
  1097. (r'\.', Name.Variable.Class, 'node-class'),
  1098. (r'\#', Name.Function, 'node-id'),
  1099. (r'style[ \t]*=', Name.Attribute, 'node-attr-style-value'),
  1100. (r'[\w:-]+[ \t]*=', Name.Attribute, 'node-attr-value'),
  1101. (r'[\w:-]+', Name.Attribute),
  1102. (r'[>{;]', Punctuation, '#pop')
  1103. ],
  1104. 'node-class': [
  1105. (r'[\w-]+', Name.Variable.Class),
  1106. (r'~\[', String.Interpol, 'interpolation'),
  1107. default('#pop')
  1108. ],
  1109. 'node-id': [
  1110. (r'[\w-]+', Name.Function),
  1111. (r'~\[', String.Interpol, 'interpolation'),
  1112. default('#pop')
  1113. ],
  1114. 'node-attr-value': [
  1115. (r'\s+', Text),
  1116. (r'\w+', Name.Variable, '#pop'),
  1117. (r"'", String, 'string-single-pop2'),
  1118. (r'"', String, 'string-double-pop2'),
  1119. default('#pop')
  1120. ],
  1121. 'node-attr-style-value': [
  1122. (r'\s+', Text),
  1123. (r"'", String.Single, 'css-single-end'),
  1124. (r'"', String.Single, 'css-double-end'),
  1125. include('node-attr-value')
  1126. ],
  1127. 'css-base': [
  1128. (r'\s+', Text),
  1129. (r";", Punctuation),
  1130. (r"[\w\-]+\s*:", Name.Builtin)
  1131. ],
  1132. 'css-single-end': [
  1133. include('css-base'),
  1134. (r"'", String.Single, '#pop:2'),
  1135. (r"[^;']+", Name.Entity)
  1136. ],
  1137. 'css-double-end': [
  1138. include('css-base'),
  1139. (r'"', String.Single, '#pop:2'),
  1140. (r'[^;"]+', Name.Entity)
  1141. ],
  1142. 'string-single-pop2': [
  1143. (r"'", String.Single, '#pop:2'),
  1144. include('string-base')
  1145. ],
  1146. 'string-double-pop2': [
  1147. (r'"', String.Single, '#pop:2'),
  1148. include('string-base')
  1149. ],
  1150. }
  1151. class EarlGreyLexer(RegexLexer):
  1152. """
  1153. For `Earl-Grey`_ source code.
  1154. .. _Earl-Grey: https://breuleux.github.io/earl-grey/
  1155. .. versionadded: 2.1
  1156. """
  1157. name = 'Earl Grey'
  1158. aliases = ['earl-grey', 'earlgrey', 'eg']
  1159. filenames = ['*.eg']
  1160. mimetypes = ['text/x-earl-grey']
  1161. tokens = {
  1162. 'root': [
  1163. (r'\n', Text),
  1164. include('control'),
  1165. (r'[^\S\n]+', Text),
  1166. (r';;.*\n', Comment),
  1167. (r'[\[\]{}:(),;]', Punctuation),
  1168. (r'\\\n', Text),
  1169. (r'\\', Text),
  1170. include('errors'),
  1171. (words((
  1172. 'with', 'where', 'when', 'and', 'not', 'or', 'in',
  1173. 'as', 'of', 'is'),
  1174. prefix=r'(?<=\s|\[)', suffix=r'(?![\w$\-])'),
  1175. Operator.Word),
  1176. (r'[*@]?->', Name.Function),
  1177. (r'[+\-*/~^<>%&|?!@#.]*=', Operator.Word),
  1178. (r'\.{2,3}', Operator.Word), # Range Operator
  1179. (r'([+*/~^<>&|?!]+)|([#\-](?=\s))|@@+(?=\s)|=+', Operator),
  1180. (r'(?<![\w$\-])(var|let)(?:[^\w$])', Keyword.Declaration),
  1181. include('keywords'),
  1182. include('builtins'),
  1183. include('assignment'),
  1184. (r'''(?x)
  1185. (?:()([a-zA-Z$_](?:[\w$\-]*[\w$])?)|
  1186. (?<=[\s{\[(])(\.)([a-zA-Z$_](?:[\w$\-]*[\w$])?))
  1187. (?=.*%)''',
  1188. bygroups(Punctuation, Name.Tag, Punctuation, Name.Class.Start), 'dbs'),
  1189. (r'[rR]?`', String.Backtick, 'bt'),
  1190. (r'[rR]?```', String.Backtick, 'tbt'),
  1191. (r'(?<=[\s\[{(,;])\.([a-zA-Z$_](?:[\w$\-]*[\w$])?)'
  1192. r'(?=[\s\]}),;])', String.Symbol),
  1193. include('nested'),
  1194. (r'(?:[rR]|[rR]\.[gmi]{1,3})?"', String, combined('stringescape', 'dqs')),
  1195. (r'(?:[rR]|[rR]\.[gmi]{1,3})?\'', String, combined('stringescape', 'sqs')),
  1196. (r'"""', String, combined('stringescape', 'tdqs')),
  1197. include('tuple'),
  1198. include('import_paths'),
  1199. include('name'),
  1200. include('numbers'),
  1201. ],
  1202. 'dbs': [
  1203. (r'(\.)([a-zA-Z$_](?:[\w$\-]*[\w$])?)(?=[.\[\s])',
  1204. bygroups(Punctuation, Name.Class.DBS)),
  1205. (r'(\[)([\^#][a-zA-Z$_](?:[\w$\-]*[\w$])?)(\])',
  1206. bygroups(Punctuation, Name.Entity.DBS, Punctuation)),
  1207. (r'\s+', Text),
  1208. (r'%', Operator.DBS, '#pop'),
  1209. ],
  1210. 'import_paths': [
  1211. (r'(?<=[\s:;,])(\.{1,3}(?:[\w\-]*/)*)(\w(?:[\w\-]*\w)*)(?=[\s;,])',
  1212. bygroups(Text.Whitespace, Text)),
  1213. ],
  1214. 'assignment': [
  1215. (r'(\.)?([a-zA-Z$_](?:[\w$\-]*[\w$])?)'
  1216. r'(?=\s+[+\-*/~^<>%&|?!@#.]*\=\s)',
  1217. bygroups(Punctuation, Name.Variable))
  1218. ],
  1219. 'errors': [
  1220. (words(('Error', 'TypeError', 'ReferenceError'),
  1221. prefix=r'(?<![\w\-$.])', suffix=r'(?![\w\-$.])'),
  1222. Name.Exception),
  1223. (r'''(?x)
  1224. (?<![\w$])
  1225. E\.[\w$](?:[\w$\-]*[\w$])?
  1226. (?:\.[\w$](?:[\w$\-]*[\w$])?)*
  1227. (?=[({\[?!\s])''',
  1228. Name.Exception),
  1229. ],
  1230. 'control': [
  1231. (r'''(?x)
  1232. ([a-zA-Z$_](?:[\w$-]*[\w$])?)
  1233. (?!\n)\s+
  1234. (?!and|as|each\*|each|in|is|mod|of|or|when|where|with)
  1235. (?=(?:[+\-*/~^<>%&|?!@#.])?[a-zA-Z$_](?:[\w$-]*[\w$])?)''',
  1236. Keyword.Control),
  1237. (r'([a-zA-Z$_](?:[\w$-]*[\w$])?)(?!\n)\s+(?=[\'"\d{\[(])',
  1238. Keyword.Control),
  1239. (r'''(?x)
  1240. (?:
  1241. (?<=[%=])|
  1242. (?<=[=\-]>)|
  1243. (?<=with|each|with)|
  1244. (?<=each\*|where)
  1245. )(\s+)
  1246. ([a-zA-Z$_](?:[\w$-]*[\w$])?)(:)''',
  1247. bygroups(Text, Keyword.Control, Punctuation)),
  1248. (r'''(?x)
  1249. (?<![+\-*/~^<>%&|?!@#.])(\s+)
  1250. ([a-zA-Z$_](?:[\w$-]*[\w$])?)(:)''',
  1251. bygroups(Text, Keyword.Control, Punctuation)),
  1252. ],
  1253. 'nested': [
  1254. (r'''(?x)
  1255. (?<=[\w$\]})])(\.)
  1256. ([a-zA-Z$_](?:[\w$-]*[\w$])?)
  1257. (?=\s+with(?:\s|\n))''',
  1258. bygroups(Punctuation, Name.Function)),
  1259. (r'''(?x)
  1260. (?<!\s)(\.)
  1261. ([a-zA-Z$_](?:[\w$-]*[\w$])?)
  1262. (?=[}\]).,;:\s])''',
  1263. bygroups(Punctuation, Name.Field)),
  1264. (r'''(?x)
  1265. (?<=[\w$\]})])(\.)
  1266. ([a-zA-Z$_](?:[\w$-]*[\w$])?)
  1267. (?=[\[{(:])''',
  1268. bygroups(Punctuation, Name.Function)),
  1269. ],
  1270. 'keywords': [
  1271. (words((
  1272. 'each', 'each*', 'mod', 'await', 'break', 'chain',
  1273. 'continue', 'elif', 'expr-value', 'if', 'match',
  1274. 'return', 'yield', 'pass', 'else', 'require', 'var',
  1275. 'let', 'async', 'method', 'gen'),
  1276. prefix=r'(?<![\w\-$.])', suffix=r'(?![\w\-$.])'),
  1277. Keyword.Pseudo),
  1278. (words(('this', 'self', '@'),
  1279. prefix=r'(?<![\w\-$.])', suffix=r'(?![\w\-$])'),
  1280. Keyword.Constant),
  1281. (words((
  1282. 'Function', 'Object', 'Array', 'String', 'Number',
  1283. 'Boolean', 'ErrorFactory', 'ENode', 'Promise'),
  1284. prefix=r'(?<![\w\-$.])', suffix=r'(?![\w\-$])'),
  1285. Keyword.Type),
  1286. ],
  1287. 'builtins': [
  1288. (words((
  1289. 'send', 'object', 'keys', 'items', 'enumerate', 'zip',
  1290. 'product', 'neighbours', 'predicate', 'equal',
  1291. 'nequal', 'contains', 'repr', 'clone', 'range',
  1292. 'getChecker', 'get-checker', 'getProperty', 'get-property',
  1293. 'getProjector', 'get-projector', 'consume', 'take',
  1294. 'promisify', 'spawn', 'constructor'),
  1295. prefix=r'(?<![\w\-#.])', suffix=r'(?![\w\-.])'),
  1296. Name.Builtin),
  1297. (words((
  1298. 'true', 'false', 'null', 'undefined'),
  1299. prefix=r'(?<![\w\-$.])', suffix=r'(?![\w\-$.])'),
  1300. Name.Constant),
  1301. ],
  1302. 'name': [
  1303. (r'@([a-zA-Z$_](?:[\w$-]*[\w$])?)', Name.Variable.Instance),
  1304. (r'([a-zA-Z$_](?:[\w$-]*[\w$])?)(\+\+|\-\-)?',
  1305. bygroups(Name.Symbol, Operator.Word))
  1306. ],
  1307. 'tuple': [
  1308. (r'#[a-zA-Z_][\w\-]*(?=[\s{(,;])', Name.Namespace)
  1309. ],
  1310. 'interpoling_string': [
  1311. (r'\}', String.Interpol, '#pop'),
  1312. include('root')
  1313. ],
  1314. 'stringescape': [
  1315. (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  1316. r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
  1317. ],
  1318. 'strings': [
  1319. (r'[^\\\'"]', String),
  1320. (r'[\'"\\]', String),
  1321. (r'\n', String) # All strings are multiline in EG
  1322. ],
  1323. 'dqs': [
  1324. (r'"', String, '#pop'),
  1325. (r'\\\\|\\"|\\\n', String.Escape),
  1326. include('strings')
  1327. ],
  1328. 'sqs': [
  1329. (r"'", String, '#pop'),
  1330. (r"\\\\|\\'|\\\n", String.Escape),
  1331. (r'\{', String.Interpol, 'interpoling_string'),
  1332. include('strings')
  1333. ],
  1334. 'tdqs': [
  1335. (r'"""', String, '#pop'),
  1336. include('strings'),
  1337. ],
  1338. 'bt': [
  1339. (r'`', String.Backtick, '#pop'),
  1340. (r'(?<!`)\n', String.Backtick),
  1341. (r'\^=?', String.Escape),
  1342. (r'.+', String.Backtick),
  1343. ],
  1344. 'tbt': [
  1345. (r'```', String.Backtick, '#pop'),
  1346. (r'\n', String.Backtick),
  1347. (r'\^=?', String.Escape),
  1348. (r'[^`]+', String.Backtick),
  1349. ],
  1350. 'numbers': [
  1351. (r'\d+\.(?!\.)\d*([eE][+-]?[0-9]+)?', Number.Float),
  1352. (r'\d+[eE][+-]?[0-9]+', Number.Float),
  1353. (r'8r[0-7]+', Number.Oct),
  1354. (r'2r[01]+', Number.Bin),
  1355. (r'16r[a-fA-F0-9]+', Number.Hex),
  1356. (r'([3-79]|[12][0-9]|3[0-6])r[a-zA-Z\d]+(\.[a-zA-Z\d]+)?', Number.Radix),
  1357. (r'\d+', Number.Integer)
  1358. ],
  1359. }
  1360. class JuttleLexer(RegexLexer):
  1361. """
  1362. For `Juttle`_ source code.
  1363. .. _Juttle: https://github.com/juttle/juttle
  1364. """
  1365. name = 'Juttle'
  1366. aliases = ['juttle', 'juttle']
  1367. filenames = ['*.juttle']
  1368. mimetypes = ['application/juttle', 'application/x-juttle',
  1369. 'text/x-juttle', 'text/juttle']
  1370. flags = re.DOTALL | re.UNICODE | re.MULTILINE
  1371. tokens = {
  1372. 'commentsandwhitespace': [
  1373. (r'\s+', Text),
  1374. (r'//.*?\n', Comment.Single),
  1375. (r'/\*.*?\*/', Comment.Multiline)
  1376. ],
  1377. 'slashstartsregex': [
  1378. include('commentsandwhitespace'),
  1379. (r'/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
  1380. r'([gim]+\b|\B)', String.Regex, '#pop'),
  1381. (r'(?=/)', Text, ('#pop', 'badregex')),
  1382. default('#pop')
  1383. ],
  1384. 'badregex': [
  1385. (r'\n', Text, '#pop')
  1386. ],
  1387. 'root': [
  1388. (r'^(?=\s|/)', Text, 'slashstartsregex'),
  1389. include('commentsandwhitespace'),
  1390. (r':\d{2}:\d{2}:\d{2}(\.\d*)?:', String.Moment),
  1391. (r':(now|beginning|end|forever|yesterday|today|tomorrow|(\d+(\.\d*)?|\.\d+)(ms|[smhdwMy])?):', String.Moment),
  1392. (r':\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d*)?)?(Z|[+-]\d{2}:\d{2}|[+-]\d{4})?:', String.Moment),
  1393. (r':((\d+(\.\d*)?|\.\d+)[ ]+)?(millisecond|second|minute|hour|day|week|month|year)[s]?'
  1394. r'(([ ]+and[ ]+(\d+[ ]+)?(millisecond|second|minute|hour|day|week|month|year)[s]?)'
  1395. r'|[ ]+(ago|from[ ]+now))*:', String.Moment),
  1396. (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|'
  1397. r'(==?|!=?|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'),
  1398. (r'[{(\[;,]', Punctuation, 'slashstartsregex'),
  1399. (r'[})\].]', Punctuation),
  1400. (r'(import|return|continue|if|else)\b', Keyword, 'slashstartsregex'),
  1401. (r'(var|const|function|reducer|sub|input)\b', Keyword.Declaration, 'slashstartsregex'),
  1402. (r'(batch|emit|filter|head|join|keep|pace|pass|put|read|reduce|remove|'
  1403. r'sequence|skip|sort|split|tail|unbatch|uniq|view|write)\b', Keyword.Reserved),
  1404. (r'(true|false|null|Infinity)\b', Keyword.Constant),
  1405. (r'(Array|Date|Juttle|Math|Number|Object|RegExp|String)\b', Name.Builtin),
  1406. (JS_IDENT, Name.Other),
  1407. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  1408. (r'[0-9]+', Number.Integer),
  1409. (r'"(\\\\|\\"|[^"])*"', String.Double),
  1410. (r"'(\\\\|\\'|[^'])*'", String.Single)
  1411. ]
  1412. }