javascript.py 62 KB

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