@ryusei/code
Version:
<div align="center"> <a href="https://code.ryuseijs.com"> <img alt="RyuseiCode" src="https://code.ryuseijs.com/images/svg/logo.svg" width="70"> </a>
14 lines (13 loc) • 5.73 kB
JavaScript
!function(t,e){"function"==typeof define&&define.amd?define([],e):"undefined"!=typeof exports?e():(e(),t.unknown={})}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,function(){"use strict";function r(t,e){return(r=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}
/*!
* RyuseiCode.js
* Version : 0.1.17
* License : MIT
* Copyright: 2021 Naotoshi Fujita
*/
/*!
* RyuseiLight.js
* Version : 1.2.0
* License : MIT
* Copyright: 2020 Naotoshi Fujita
*/var e="keyword",i="comment",n="identifier",o="string",s="number",a="boolean",c="function",u="class",f="decorator",h="regexp",d="operator",l="bracket",v="delimiter",b="space",m="text";function p(t,e){if(t)for(var i=Object.keys(t),n=0;n<i.length;n++)e(t[i[n]],i[n])}function g(t,e){if(void 0===e&&(e=""),!t)throw new Error(e)}function k(t,e){return t.charAt(0)===e}var w=!(void 0===/x/.sticky),y=/[+-]?(\d+\.?\d*|\d*\.?\d+)([eE][+-]?\d+)?/,$=/\b(?:true|false)\b/,x=/[[\]{}()]/,F=/[ \t]+/,j=/'(?:\\'|.)*?'/,_=/"(?:\\"|.)*?"/,A=/\/\*[\s\S]*?(\*\/|$)/,E=/\/\/.*/,O=/\b(?:break|catch|class|continue|do|else|extends|finally|for|function|if|implements|in|instanceof|interface|new|null|return|throw|try|while)\b/;var q=function(t){var e,i;function n(){return t.apply(this,arguments)||this}return i=t,(e=n).prototype=Object.create(i.prototype),r(e.prototype.constructor=e,i),n.prototype.run=function(t,e){for(var i=this.tokenize(t,e),n=0;n<i.length;n++)for(var r=i[n],o=0,s=0;s<r.length;s++){var a=r[s],c=a[1].length,u=a[2],f="rc__token rc__"+a[0].split(".")[0],h=a[1].replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");a[2]={category:a[0],code:a[1],html:'<code class="'+f+'">'+h+"</code>",from:o,to:o+c,index:s,state:u.state,depth:u.depth,head:u.head,tail:u.tail,distance:u.distance,language:u.language,split:u.split},o+=c}return i},n}(function(){function t(t){this.language=t,this.init(t)}var e=t.prototype;return e.init=function(i){var n=this;p(i.grammar,function(t,e){i.grammar[e]=n.merge(i,t)}),p(i.use,this.init.bind(this))},e.merge=function(e,t){for(var n=[],i=0;i<t.length;i++){var r=t[i],o=t[i],s=o[0],a=o[1];k(s,"#")&&!a?n.push.apply(n,this.merge(e,e.grammar[s.slice(1)])):function(){var t=a.toString().match(/[gimsy]*$/)[0].replace(/[gy]/g,""),i=a.source+(w?"":"|()");p(e.source,function(t,e){i=i.replace(new RegExp("%"+e,"g"),t.source)}),r[1]=new RegExp(i,(w?"y":"g")+t),n.push(r)}()}return n},e.parse=function(t,e,i,n){var r=0,o=0;this.depth++;t:for(;r<t.length&&!this.aborted;){for(var s=0;s<i.length;s++){var a=i[s],c=a[1],u=a[2];c.lastIndex=r;c=c.exec(t);if(c&&c[0]){if(o<r&&this.push([m,t.slice(o,r)],e,n),"@back"===u){o=r;break t}o=r+=this.handle(c,e,a,n)||1;if("@break"===u)break t;continue t}}r++}return o<r&&this.push([m,t.slice(o)],e,n),this.depth--,r},e.push=function(t,e,i){for(var n=this.depth,r=t[0],o=t[1],s=this.index,a=0,c=0;-1<a&&!this.aborted;){var a=o.indexOf("\n",c),u=this.lines[this.index],f=c===a&&!u.length,h=f?"\n":o.slice(c,a<0?void 0:a),d={depth:n,language:e.id,state:i};h&&(r!==m&&(d.head=-1<a&&!c,d.tail=a<0&&!!c,d.split=-1<a||!!c,d.distance=this.index-s),u.push([r===m&&f?"lb":r,h,d])),-1<a&&(this.index++,this.aborted=this.limit&&this.index>=this.limit,this.aborted||(c=a+1,this.lines[this.index]=[]))}},e.handle=function(t,e,i,n){var r=i[0];if(!r)return 0;var o=t[0];if("@debug"===i[3]&&console.log(o,i),k(r,"@")){g(e.use);var s=e.use[r.slice(1)];return g(s),this.parse(o,s,s.grammar.main,r)}if(k(r,"#")){s=e.grammar[r.slice(1)];return g(s),"@rest"===i[2]&&(o=t.input.slice(t.index)),this.parse(o,e,s,r)}return this.push([r,o],e,n),o.length},e.tokenize=function(t,e){return this.lines=[[]],this.index=0,this.depth=-1,this.limit=e||0,this.aborted=!1,this.parse(t,this.language,this.language.grammar.main,"#main"),this.lines},t}());function z(n){for(var t=arguments.length,e=new Array(1<t?t-1:0),i=1;i<t;i++)e[i-1]=arguments[i];return e.forEach(function(i){var t;null!==(t=i)&&"object"==typeof t&&function(t,e){if(t)for(var i=Object.keys(t),n=0;n<i.length&&!1!==e(t[i[n]],i[n]);n++);}(i,function(t,e){void 0!==i[e]&&(n[e]=i[e])})}),n}function t(){var t={id:"javascript",name:"JavaScript",alias:["js"],source:{func:/[_$a-z\xA0-\uFFFF][_$a-z0-9\xA0-\uFFFF]*/},grammar:{main:[[o,j],[o,_],["#backtick",/`/,"@rest"],[i,A],[i,E],[h,/\/(\[.*?]|\\\/|.)+?\/[gimsuy]*/],[e,O],[e,/\b(?:as|async|await|case|catch|const|debugger|default|delete|enum|export|from|import|let|package|private|protected|public|super|switch|static|this|typeof|undefined|var|void|with|yield)\b/],[e,/\b((get|set)(?=\s+%func))/i],[u,/\b[A-Z][\w$]*\b/],[c,/%func(?=\s*\()/i],[a,$],[f,/@[^\s(@]+/],[n,/\b[a-z_$][\w$]*\b/],[s,y],[d,/=>/],[d,/\+[+=]?|-[-=]?|\*\*?=?|[/%^]=?|&&?=?|\|\|?=?|\?\??=?|<<?=?|>>>=?|>>?=?|[!=]=?=?|[~:^]/],[l,x],[v,/[;.,]+/],[b,F]],backtick:[[o,/^`/],[o,/(\$[^{]|\\[$`]|[^`$])+/],["#expression",/\${/,"@rest"],[o,/`/,"@break"]],expression:[[v,/^\${/],[v,/}/,"@break"],["#main"]]}};return z({id:t.id,language:t,lexer:new q(t)},{lineComment:["//",""],blockComment:["/*","*/"],multiline:[["`","`",o,"#backtick"],["/*","*/",i]],indent:[[/\($/,/^\)/,t=["!comment","!string"]],[/\[$/,/^]/,t],[/{$/,/^}/,t]],autoClose:[["(",")",t={close:t,skip:t,remove:t}],["[","]",t],["{","}",t],["'","'",t={close:"@quotes",skip:"@quotes",remove:"@quotes"}],['"','"',t],["`","`",{close:C,skip:R,remove:R}]]})}function C(t){var e=t.Components,i=e.Input,t=i.info;if(t&&t.category===o)return!1;i=i.after;return!e.Scope.isIn("comment")&&(!i||/^\s/.test(i))}function R(t){var e=t.Components,t=e.Selection.get().start,e=e.Code.lines.getInfoAt(t);return!(!e||e.category!==o)&&0===(e=[(t=t)[0],e.to-1],t[0]-e[0]||t[1]-e[1])}"undefined"!=typeof window&&window.RyuseiCode&&window.RyuseiCode.register(t())});