javascript.py 59 KB

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