javascript.py 62 KB

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