javascript.py 61 KB

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