javascript.py 62 KB

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