tex2typst 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -150,6 +150,9 @@ export abstract class TypstNode {
150
150
  // Serialize a tree of TypstNode into a list of TypstToken
151
151
  abstract serialize(env: TypstWriterEnvironment, options: TypstWriterOptions): TypstToken[];
152
152
 
153
+ abstract bottomTopTraversalTransform(transform: (node: TypstNode) => TypstNode): TypstNode;
154
+
155
+
153
156
  public setOptions(options: TypstNamedParams) {
154
157
  this.options = options;
155
158
  }
@@ -246,6 +249,10 @@ export class TypstTerminal extends TypstNode {
246
249
  }
247
250
  return [this.head];
248
251
  }
252
+
253
+ public bottomTopTraversalTransform(transform: (node: TypstNode) => TypstNode): TypstNode {
254
+ return transform(this);
255
+ }
249
256
  }
250
257
 
251
258
  class TypstTokenQueue {
@@ -340,6 +347,11 @@ export class TypstGroup extends TypstNode {
340
347
  }
341
348
  return queue;
342
349
  }
350
+
351
+ public bottomTopTraversalTransform(transform: (node: TypstNode) => TypstNode): TypstNode {
352
+ const g = new TypstGroup(this.items.map((n) => n.bottomTopTraversalTransform(transform)));
353
+ return transform(g);
354
+ }
343
355
  }
344
356
 
345
357
 
@@ -392,6 +404,15 @@ export class TypstSupsub extends TypstNode {
392
404
  }
393
405
  return queue;
394
406
  }
407
+
408
+ public bottomTopTraversalTransform(transform: (node: TypstNode) => TypstNode): TypstNode {
409
+ const s = new TypstSupsub({
410
+ base: this.base.bottomTopTraversalTransform(transform),
411
+ sup: this.sup?.bottomTopTraversalTransform(transform) || null,
412
+ sub: this.sub?.bottomTopTraversalTransform(transform) || null
413
+ });
414
+ return transform(s);
415
+ }
395
416
  }
396
417
 
397
418
  export class TypstFuncCall extends TypstNode {
@@ -439,6 +460,12 @@ export class TypstFuncCall extends TypstNode {
439
460
  env.insideFunctionDepth--;
440
461
  return queue;
441
462
  }
463
+
464
+ public bottomTopTraversalTransform(transform: (node: TypstNode) => TypstNode): TypstNode {
465
+ const f = new TypstFuncCall(this.head, this.args.map((n) => n.bottomTopTraversalTransform(transform)));
466
+ f.options = this.options;
467
+ return transform(f);
468
+ }
442
469
  }
443
470
 
444
471
  export class TypstFraction extends TypstNode {
@@ -470,6 +497,11 @@ export class TypstFraction extends TypstNode {
470
497
  queue.push(...denominator.serialize(env, options));
471
498
  return queue;
472
499
  }
500
+
501
+ public bottomTopTraversalTransform(transform: (node: TypstNode) => TypstNode): TypstNode {
502
+ const f = new TypstFraction(this.args.map((n) => n.bottomTopTraversalTransform(transform)));
503
+ return transform(f);
504
+ }
473
505
  }
474
506
 
475
507
  const TYPST_LEFT_PARENTHESIS: TypstToken = new TypstToken(TypstTokenType.ELEMENT, '(');
@@ -529,6 +561,16 @@ export class TypstLeftright extends TypstNode {
529
561
  }
530
562
  return queue;
531
563
  }
564
+
565
+
566
+ public bottomTopTraversalTransform(transform: (node: TypstNode) => TypstNode): TypstNode {
567
+ const l = new TypstLeftright(this.head, {
568
+ body: this.body.bottomTopTraversalTransform(transform),
569
+ left: this.left,
570
+ right: this.right,
571
+ });
572
+ return transform(l);
573
+ }
532
574
  }
533
575
 
534
576
 
@@ -609,6 +651,13 @@ export class TypstMatrixLike extends TypstNode {
609
651
  return queue;
610
652
  }
611
653
 
654
+ public bottomTopTraversalTransform(transform: (node: TypstNode) => TypstNode): TypstNode {
655
+ const m = this.matrix.map((row) => row.map((cell) => cell.bottomTopTraversalTransform(transform)));
656
+ const ml = new TypstMatrixLike(this.head, m);
657
+ ml.options = this.options;
658
+ return transform(ml);
659
+ }
660
+
612
661
  static readonly MAT = new TypstToken(TypstTokenType.SYMBOL, 'mat');
613
662
  static readonly CASES = new TypstToken(TypstTokenType.SYMBOL, 'cases');
614
663
  }
@@ -667,4 +716,10 @@ export class TypstMarkupFunc extends TypstNode {
667
716
  queue.push(new TypstToken(TypstTokenType.LITERAL, ']'));
668
717
  return queue;
669
718
  }
719
+
720
+ public bottomTopTraversalTransform(transform: (node: TypstNode) => TypstNode): TypstNode {
721
+ const mf = new TypstMarkupFunc(this.head, this.fragments.map((n) => n.bottomTopTraversalTransform(transform)));
722
+ mf.options = this.options;
723
+ return transform(mf);
724
+ }
670
725
  }
package/dist/parser.js DELETED
@@ -1,23 +0,0 @@
1
- "use strict";class M{constructor(o,n,t=[]){this.type=o,this.value=n,this.args=t}}function U(l,o){Object.defineProperty(this,"name",{enumerable:!1,writable:!1,value:"JisonParserError"}),l==null&&(l="???"),Object.defineProperty(this,"message",{enumerable:!1,writable:!0,value:l}),this.hash=o;var n;if(o&&o.exception instanceof Error){var t=o.exception;this.message=t.message||l,n=t.stack}n||(Error.hasOwnProperty("captureStackTrace")?Error.captureStackTrace(this,this.constructor):n=new Error(l).stack),n&&Object.defineProperty(this,"stack",{enumerable:!1,writable:!1,value:n})}typeof Object.setPrototypeOf=="function"?Object.setPrototypeOf(U.prototype,Error.prototype):U.prototype=Object.create(Error.prototype),U.prototype.constructor=U,U.prototype.name="JisonParserError";function V(l){for(var o=[],n=l.pop,t=l.rule,i=0,r=n.length;i<r;i++)o.push([n[i],t[i]]);return o}function z(l){for(var o=[],n=l.len,t=l.symbol,i=l.type,r=l.state,s=l.mode,e=l.goto,h=0,_=n.length;h<_;h++){for(var v=n[h],u={},d=0;d<v;d++){var a=t.shift();switch(i.shift()){case 2:u[a]=[s.shift(),e.shift()];break;case 0:u[a]=r.shift();break;default:u[a]=[3]}}o.push(u)}return o}function g(l,o,n){n=n||0;for(var t=0;t<o;t++)this.push(l),l+=n}function T(l,o){for(l=this.length-l,o+=l;l<o;l++)this.push(this[l])}function N(l){for(var o=[],n=0,t=l.length;n<t;n++){var i=l[n];typeof i=="function"?(n++,i.apply(o,l[n])):o.push(i)}return o}var I={trace:function(){},JisonParserError:U,yy:{},options:{type:"lalr",hasPartialLrUpgradeOnConflict:!0,errorRecoveryTokenDiscardCount:3},symbols_:{$accept:0,$end:1,"*":5,"+":3,"-":4,"/":6,EOF:1,IDENTIFIER:7,NUMBER:8,error:2,expression:10,typinput:9},terminals_:{1:"EOF",2:"error",3:"+",4:"-",5:"*",6:"/",7:"IDENTIFIER",8:"NUMBER"},TERROR:2,EOF:1,originalQuoteName:null,originalParseError:null,cleanupAfterParse:null,constructParseErrorInfo:null,yyMergeLocationInfo:null,__reentrant_call_depth:0,__error_infos:[],__error_recovery_infos:[],quoteName:function(o){return'"'+o+'"'},getSymbolName:function(o){if(this.terminals_[o])return this.terminals_[o];var n=this.symbols_;for(var t in n)if(n[t]===o)return t;return null},describeSymbol:function(o){if(o!==this.EOF&&this.terminal_descriptions_&&this.terminal_descriptions_[o])return this.terminal_descriptions_[o];if(o===this.EOF)return"end of input";var n=this.getSymbolName(o);return n?this.quoteName(n):null},collect_expected_token_set:function(o,n){var t=this.TERROR,i=[],r={};if(!n&&this.state_descriptions_&&this.state_descriptions_[o])return[this.state_descriptions_[o]];for(var s in this.table[o])if(s=+s,s!==t){var e=n?s:this.describeSymbol(s);e&&!r[e]&&(i.push(e),r[e]=!0)}return i},productions_:V({pop:N([9,g,[10,6]]),rule:N([2,1,1,g,[3,4]])}),performAction:function(o,n,t){var i=this.yy,r=i.parser,s=i.lexer;switch(o){case 0:this.$=t[n-1];break;case 1:return this.$=t[n-1],t[n-1];break;case 2:this.$=new M("IDENTIFIER",t[n]);break;case 3:this.$=new M("NUMBER",t[n]);break;case 4:this.$=new M("ADD",t[n-1],[t[n-2],t[n]]);break;case 5:this.$=new M("SUB",t[n-1],[t[n-2],t[n]]);break;case 6:this.$=new M("MUL",t[n-1],[t[n-2],t[n]]);break;case 7:this.$=new M("DIV",t[n-1],[t[n-2],t[n]]);break}},table:z({len:N([4,1,5,g,[0,3],g,[3,4],5,T,[9,3]]),symbol:N([g,[7,4,1],1,1,g,[3,6,1],10,T,[3,9],T,[17,5],T,[5,5]]),type:N([2,2,0,0,1,g,[2,7],0,T,[3,11],g,[2,8]]),state:N([1,2,g,[10,4,1]]),mode:N([g,[1,15],g,[2,3],T,[5,7]]),goto:N([g,[3,7,1],3,4,3,4,T,[4,4],g,[4,3],8,9,g,[5,3],8,9])}),defaultActions:{3:2,4:3,5:1,12:6,13:7},parseError:function(o,n,t){if(n.recoverable)typeof this.trace=="function"&&this.trace(o),n.destroy();else throw typeof this.trace=="function"&&this.trace(o),t||(t=this.JisonParserError),new t(o,n)},parse:function(o){var n=this,t=new Array(128),i=new Array(128),r=new Array(128),s=this.table,e=0,h=0,_=this.TERROR,v=this.EOF,u=this.options.errorRecoveryTokenDiscardCount|0||3,d=[0,14],a;this.__lexer__?a=this.__lexer__:a=this.__lexer__=Object.create(this.lexer);var f={parseError:void 0,quoteName:void 0,lexer:void 0,parser:void 0,pre_parse:void 0,post_parse:void 0,pre_lex:void 0,post_lex:void 0},x;typeof assert!="function"?x=function(y,R){if(!y)throw new Error("assertion failed: "+(R||"***"))}:x=assert,this.yyGetSharedState=function(){return f};function j(c,y){for(var R in y)typeof c[R]>"u"&&Object.prototype.hasOwnProperty.call(y,R)&&(c[R]=y[R])}j(f,this.yy),f.lexer=a,f.parser=this,typeof f.parseError=="function"?this.parseError=function(y,R,w){return w||(w=this.JisonParserError),f.parseError.call(this,y,R,w)}:this.parseError=this.originalParseError,typeof f.quoteName=="function"?this.quoteName=function(y){return f.quoteName.call(this,y)}:this.quoteName=this.originalQuoteName,this.cleanupAfterParse=function(y,R,w){var A;if(R){var S;(f.post_parse||this.post_parse)&&(S=this.constructParseErrorInfo(null,null,null,!1)),f.post_parse&&(A=f.post_parse.call(this,f,y,S),typeof A<"u"&&(y=A)),this.post_parse&&(A=this.post_parse.call(this,f,y,S),typeof A<"u"&&(y=A)),S&&S.destroy&&S.destroy()}if(this.__reentrant_call_depth>1)return y;if(a.cleanupAfterLex&&a.cleanupAfterLex(w),f&&(f.lexer=void 0,f.parser=void 0,a.yy===f&&(a.yy=void 0)),f=void 0,this.parseError=this.originalParseError,this.quoteName=this.originalQuoteName,t.length=0,i.length=0,r.length=0,e=0,!w){for(var Y=this.__error_infos.length-1;Y>=0;Y--){var C=this.__error_infos[Y];C&&typeof C.destroy=="function"&&C.destroy()}this.__error_infos.length=0}return y},this.constructParseErrorInfo=function(y,R,w,A){var S={errStr:y,exception:R,text:a.match,value:a.yytext,token:this.describeSymbol(h)||h,token_id:h,line:a.yylineno,expected:w,recoverable:A,state:E,action:P,new_state:k,symbol_stack:t,state_stack:i,value_stack:r,stack_pointer:e,yy:f,lexer:a,parser:this,destroy:function(){var C=!!this.recoverable;for(var X in this)this.hasOwnProperty(X)&&typeof X=="object"&&(this[X]=void 0);this.recoverable=C}};return this.__error_infos.push(S),S};function q(c){var y=n.getSymbolName(c);return y||(y=c),y}function B(){var c=a.lex();return typeof c!="number"&&(c=n.symbols_[c]||c),c||v}function $(){var c=a.fastLex();return typeof c!="number"&&(c=n.symbols_[c]||c),c||v}var Q=B,E,P,p,F,L={$:!0,_$:void 0,yy:f},m,D,O,k,b=!1;try{if(this.__reentrant_call_depth++,a.setInput(o,f),typeof a.canIUse=="function"){var Z=a.canIUse();Z.fastLex&&typeof $=="function"&&(Q=$)}for(r[e]=null,i[e]=0,t[e]=0,++e,this.pre_parse&&this.pre_parse.call(this,f),f.pre_parse&&f.pre_parse.call(this,f),k=i[e-1];;){if(E=k,this.defaultActions[E])P=2,k=this.defaultActions[E];else if(h||(h=Q()),F=s[E]&&s[E][h]||d,k=F[1],P=F[0],!P){var J,W=this.describeSymbol(h)||h,G=this.collect_expected_token_set(E);typeof a.yylineno=="number"?J="Parse error on line "+(a.yylineno+1)+": ":J="Parse error: ",typeof a.showPosition=="function"&&(J+=`
2
- `+a.showPosition(69,10)+`
3
- `),G.length?J+="Expecting "+G.join(", ")+", got unexpected "+W:J+="Unexpected "+W,m=this.constructParseErrorInfo(J,null,G,!1),p=this.parseError(m.errStr,m,this.JisonParserError),typeof p<"u"&&(b=p);break}switch(P){default:if(P instanceof Array){m=this.constructParseErrorInfo("Parse Error: multiple actions possible at state: "+E+", token: "+h,null,null,!1),p=this.parseError(m.errStr,m,this.JisonParserError),typeof p<"u"&&(b=p);break}m=this.constructParseErrorInfo("Parsing halted. No viable error recovery approach available due to internal system failure.",null,null,!1),p=this.parseError(m.errStr,m,this.JisonParserError),typeof p<"u"&&(b=p);break;case 1:t[e]=h,r[e]=a.yytext,i[e]=k,++e,h=0;continue;case 2:if(O=this.productions_[k-1],D=O[1],p=this.performAction.call(L,k,e-1,r),typeof p<"u"){b=p;break}e-=D;var H=O[0];t[e]=H,r[e]=L.$,k=s[i[e-1]][H],i[e]=k,++e;continue;case 3:e!==-2&&(b=!0,e--,typeof r[e]<"u"&&(b=r[e]));break}break}}catch(c){if(c instanceof this.JisonParserError)throw c;if(a&&typeof a.JisonLexerError=="function"&&c instanceof a.JisonLexerError)throw c;m=this.constructParseErrorInfo("Parsing aborted due to exception.",c,null,!1),b=!1,p=this.parseError(m.errStr,m,this.JisonParserError),typeof p<"u"&&(b=p)}finally{b=this.cleanupAfterParse(b,!0,!0),this.__reentrant_call_depth--}return b}};I.originalParseError=I.parseError,I.originalQuoteName=I.quoteName;var tt=function(){function l(n,t){Object.defineProperty(this,"name",{enumerable:!1,writable:!1,value:"JisonLexerError"}),n==null&&(n="???"),Object.defineProperty(this,"message",{enumerable:!1,writable:!0,value:n}),this.hash=t;var i;if(t&&t.exception instanceof Error){var r=t.exception;this.message=r.message||n,i=r.stack}i||(Error.hasOwnProperty("captureStackTrace")?Error.captureStackTrace(this,this.constructor):i=new Error(n).stack),i&&Object.defineProperty(this,"stack",{enumerable:!1,writable:!1,value:i})}typeof Object.setPrototypeOf=="function"?Object.setPrototypeOf(l.prototype,Error.prototype):l.prototype=Object.create(Error.prototype),l.prototype.constructor=l,l.prototype.name="JisonLexerError";var o={EOF:1,ERROR:2,__currentRuleSet__:null,__error_infos:[],__decompressed:!1,done:!1,_backtrack:!1,_input:"",_more:!1,_signaled_error_token:!1,conditionStack:[],match:"",matched:"",matches:!1,yytext:"",offset:0,yyleng:0,yylineno:0,yylloc:null,constructLexErrorInfo:function(t,i,r){if(t=""+t,r==null&&(r=!(t.indexOf(`
4
- `)>0&&t.indexOf("^")>0)),this.yylloc&&r){if(typeof this.prettyPrintRange=="function"){var s=this.prettyPrintRange(this.yylloc);/\n\s*$/.test(t)||(t+=`
5
- `),t+=`
6
- Erroneous area:
7
- `+this.prettyPrintRange(this.yylloc)}else if(typeof this.showPosition=="function"){var e=this.showPosition();e&&(t.length&&t[t.length-1]!==`
8
- `&&e[0]!==`
9
- `?t+=`
10
- `+e:t+=e)}}var h={errStr:t,recoverable:!!i,text:this.match,token:null,line:this.yylineno,loc:this.yylloc,yy:this.yy,lexer:this,destroy:function(){var v=!!this.recoverable;for(var u in this)this.hasOwnProperty(u)&&typeof u=="object"&&(this[u]=void 0);this.recoverable=v}};return this.__error_infos.push(h),h},parseError:function(t,i,r){if(r||(r=this.JisonLexerError),this.yy){if(this.yy.parser&&typeof this.yy.parser.parseError=="function")return this.yy.parser.parseError.call(this,t,i,r)||this.ERROR;if(typeof this.yy.parseError=="function")return this.yy.parseError.call(this,t,i,r)||this.ERROR}throw new r(t,i)},yyerror:function(t){var i="";this.yylloc&&(i=" on line "+(this.yylineno+1));var r=this.constructLexErrorInfo("Lexical error"+i+": "+t,this.options.lexerErrorsAreRecoverable),s=Array.prototype.slice.call(arguments,1);return s.length&&(r.extra_error_attributes=s),this.parseError(r.errStr,r,this.JisonLexerError)||this.ERROR},cleanupAfterLex:function(t){if(this.setInput("",{}),!t){for(var i=this.__error_infos.length-1;i>=0;i--){var r=this.__error_infos[i];r&&typeof r.destroy=="function"&&r.destroy()}this.__error_infos.length=0}return this},clear:function(){this.yytext="",this.yyleng=0,this.match="",this.matches=!1,this._more=!1,this._backtrack=!1;var t=this.yylloc?this.yylloc.last_column:0;this.yylloc={first_line:this.yylineno+1,first_column:t,last_line:this.yylineno+1,last_column:t,range:[this.offset,this.offset]}},setInput:function(t,i){if(this.yy=i||this.yy||{},!this.__decompressed){for(var r=this.rules,s=0,e=r.length;s<e;s++){var h=r[s];typeof h=="number"&&(r[s]=r[h])}var _=this.conditions;for(var v in _){for(var u=_[v],d=u.rules,e=d.length,a=new Array(e+1),f=new Array(e+1),s=0;s<e;s++){var x=d[s],h=r[x];a[s+1]=h,f[s+1]=x}u.rules=f,u.__rule_regexes=a,u.__rule_count=e}this.__decompressed=!0}return this._input=t||"",this.clear(),this._signaled_error_token=!1,this.done=!1,this.yylineno=0,this.matched="",this.conditionStack=["INITIAL"],this.__currentRuleSet__=null,this.yylloc={first_line:1,first_column:0,last_line:1,last_column:0,range:[0,0]},this.offset=0,this},editRemainingInput:function(t,i){var r=t.call(this,this._input,i);return typeof r!="string"?r&&(this._input=""+r):this._input=r,this},input:function(){if(!this._input)return null;var t=this._input[0];this.yytext+=t,this.yyleng++,this.offset++,this.match+=t,this.matched+=t;var i=1,r=!1;if(t===`
11
- `)r=!0;else if(t==="\r"){r=!0;var s=this._input[1];s===`
12
- `&&(i++,t+=s,this.yytext+=s,this.yyleng++,this.offset++,this.match+=s,this.matched+=s,this.yylloc.range[1]++)}return r?(this.yylineno++,this.yylloc.last_line++,this.yylloc.last_column=0):this.yylloc.last_column++,this.yylloc.range[1]++,this._input=this._input.slice(i),t},unput:function(t){var i=t.length,r=t.split(/(?:\r\n?|\n)/g);if(this._input=t+this._input,this.yytext=this.yytext.substr(0,this.yytext.length-i),this.yyleng=this.yytext.length,this.offset-=i,this.match=this.match.substr(0,this.match.length-i),this.matched=this.matched.substr(0,this.matched.length-i),r.length>1){this.yylineno-=r.length-1,this.yylloc.last_line=this.yylineno+1;var s=this.match,e=s.split(/(?:\r\n?|\n)/g);e.length===1&&(s=this.matched,e=s.split(/(?:\r\n?|\n)/g)),this.yylloc.last_column=e[e.length-1].length}else this.yylloc.last_column-=i;return this.yylloc.range[1]=this.yylloc.range[0]+this.yyleng,this.done=!1,this},more:function(){return this._more=!0,this},reject:function(){if(this.options.backtrack_lexer)this._backtrack=!0;else{var t="";this.yylloc&&(t=" on line "+(this.yylineno+1));var i=this.constructLexErrorInfo("Lexical error"+t+": You can only invoke reject() in the lexer when the lexer is of the backtracking persuasion (options.backtrack_lexer = true).",!1);this._signaled_error_token=this.parseError(i.errStr,i,this.JisonLexerError)||this.ERROR}return this},less:function(t){return this.unput(this.match.slice(t))},pastInput:function(t,i){var r=this.matched.substring(0,this.matched.length-this.match.length);t<0?t=r.length:t||(t=20),i<0?i=r.length:i||(i=1),r=r.substr(-t*2-2);var s=r.replace(/\r\n|\r/g,`
13
- `).split(`
14
- `);return s=s.slice(-i),r=s.join(`
15
- `),r.length>t&&(r="..."+r.substr(-t)),r},upcomingInput:function(t,i){var r=this.match;t<0?t=r.length+this._input.length:t||(t=20),i<0?i=t:i||(i=1),r.length<t*2+2&&(r+=this._input.substring(0,t*2+2));var s=r.replace(/\r\n|\r/g,`
16
- `).split(`
17
- `);return s=s.slice(0,i),r=s.join(`
18
- `),r.length>t&&(r=r.substring(0,t)+"..."),r},showPosition:function(t,i){var r=this.pastInput(t).replace(/\s/g," "),s=new Array(r.length+1).join("-");return r+this.upcomingInput(i).replace(/\s/g," ")+`
19
- `+s+"^"},deriveLocationInfo:function(t,i,r,s){var e={first_line:1,first_column:0,last_line:1,last_column:0,range:[0,0]};return t&&(e.first_line=t.first_line|0,e.last_line=t.last_line|0,e.first_column=t.first_column|0,e.last_column=t.last_column|0,t.range&&(e.range[0]=t.range[0]|0,e.range[1]=t.range[1]|0)),(e.first_line<=0||e.last_line<e.first_line)&&(e.first_line<=0&&i&&(e.first_line=i.last_line|0,e.first_column=i.last_column|0,i.range&&(e.range[0]=t.range[1]|0)),(e.last_line<=0||e.last_line<e.first_line)&&r&&(e.last_line=r.first_line|0,e.last_column=r.first_column|0,r.range&&(e.range[1]=t.range[0]|0)),e.first_line<=0&&s&&(e.last_line<=0||s.last_line<=e.last_line)&&(e.first_line=s.first_line|0,e.first_column=s.first_column|0,s.range&&(e.range[0]=s.range[0]|0)),e.last_line<=0&&s&&(e.first_line<=0||s.first_line>=e.first_line)&&(e.last_line=s.last_line|0,e.last_column=s.last_column|0,s.range&&(e.range[1]=s.range[1]|0))),e.last_line<=0&&(e.first_line<=0?(e.first_line=this.yylloc.first_line,e.last_line=this.yylloc.last_line,e.first_column=this.yylloc.first_column,e.last_column=this.yylloc.last_column,e.range[0]=this.yylloc.range[0],e.range[1]=this.yylloc.range[1]):(e.last_line=this.yylloc.last_line,e.last_column=this.yylloc.last_column,e.range[1]=this.yylloc.range[1])),e.first_line<=0&&(e.first_line=e.last_line,e.first_column=0,e.range[1]=e.range[0]),e.first_column<0&&(e.first_column=0),e.last_column<0&&(e.last_column=e.first_column>0?e.first_column:80),e},prettyPrintRange:function(t,i,r){t=this.deriveLocationInfo(t,i,r);const s=3,e=1,h=2;var _=this.matched+this._input,v=_.split(`
20
- `),u=Math.max(1,i?i.first_line:t.first_line-s),d=Math.max(1,r?r.last_line:t.last_line+e),a=1+Math.log10(d|1)|0,f=new Array(a).join(" "),x=[],j=v.slice(u-1,d+1).map(function(E,P){var p=P+u,F=(f+p).substr(-a),L=F+": "+E,m=new Array(a+1).join("^"),D=3,O=0;if(p===t.first_line?(D+=t.first_column,O=Math.max(2,(p===t.last_line?t.last_column:E.length)-t.first_column+1)):p===t.last_line?O=Math.max(2,t.last_column+1):p>t.first_line&&p<t.last_line&&(O=Math.max(2,E.length+1)),O){var k=new Array(D).join("."),b=new Array(O).join("^");L+=`
21
- `+m+k+b,E.trim().length>0&&x.push(P)}return L=L.replace(/\t/g," "),L});if(x.length>2*h){var q=x[h-1]+1,B=x[x.length-h]-1,$=new Array(a+1).join(" ")+" (...continued...)";$+=`
22
- `+new Array(a+1).join("-")+" (---------------)",j.splice(q,B-q+1,$)}return j.join(`
23
- `)},describeYYLLOC:function(t,i){var r=t.first_line,s=t.last_line,e=t.first_column,h=t.last_column,_=s-r,v=h-e,u;if(_===0?(u="line "+r+", ",v<=1?u+="column "+e:u+="columns "+e+" .. "+h):u="lines "+r+"(column "+e+") .. "+s+"(column "+h+")",t.range&&i){var d=t.range[0],a=t.range[1]-1;a<=d?u+=" {String Offset: "+d+"}":u+=" {String Offset range: "+d+" .. "+a+"}"}return u},test_match:function(t,i){var r,s,e,h,_;if(this.options.backtrack_lexer&&(e={yylineno:this.yylineno,yylloc:{first_line:this.yylloc.first_line,last_line:this.yylloc.last_line,first_column:this.yylloc.first_column,last_column:this.yylloc.last_column,range:this.yylloc.range.slice(0)},yytext:this.yytext,match:this.match,matches:this.matches,matched:this.matched,yyleng:this.yyleng,offset:this.offset,_more:this._more,_input:this._input,yy:this.yy,conditionStack:this.conditionStack.slice(0),done:this.done}),h=t[0],_=h.length,s=h.split(/(?:\r\n?|\n)/g),s.length>1?(this.yylineno+=s.length-1,this.yylloc.last_line=this.yylineno+1,this.yylloc.last_column=s[s.length-1].length):this.yylloc.last_column+=_,this.yytext+=h,this.match+=h,this.matched+=h,this.matches=t,this.yyleng=this.yytext.length,this.yylloc.range[1]+=_,this.offset+=_,this._more=!1,this._backtrack=!1,this._input=this._input.slice(_),r=this.performAction.call(this,this.yy,i,this.conditionStack[this.conditionStack.length-1]),this.done&&this._input&&(this.done=!1),r)return r;if(this._backtrack){for(var v in e)this[v]=e[v];return this.__currentRuleSet__=null,!1}else if(this._signaled_error_token)return r=this._signaled_error_token,this._signaled_error_token=!1,r;return!1},next:function(){if(this.done)return this.clear(),this.EOF;this._input||(this.done=!0);var t,i,r,s;this._more||this.clear();var e=this.__currentRuleSet__;if(!e&&(e=this.__currentRuleSet__=this._currentRules(),!e||!e.rules)){var h="";this.options.trackPosition&&(h=" on line "+(this.yylineno+1));var _=this.constructLexErrorInfo("Internal lexer engine error"+h+': The lex grammar programmer pushed a non-existing condition name "'+this.topState()+'"; this is a fatal error and should be reported to the application programmer team!',!1);return this.parseError(_.errStr,_,this.JisonLexerError)||this.ERROR}for(var v=e.rules,u=e.__rule_regexes,d=e.__rule_count,a=1;a<=d;a++)if(r=this._input.match(u[a]),r&&(!i||r[0].length>i[0].length)){if(i=r,s=a,this.options.backtrack_lexer){if(t=this.test_match(r,v[a]),t!==!1)return t;if(this._backtrack){i=void 0;continue}else return!1}else if(!this.options.flex)break}if(i)return t=this.test_match(i,v[s]),t!==!1?t:!1;if(this._input){var h="";this.options.trackPosition&&(h=" on line "+(this.yylineno+1));var _=this.constructLexErrorInfo("Lexical error"+h+": Unrecognized text.",this.options.lexerErrorsAreRecoverable),f=this._input,x=this.topState(),j=this.conditionStack.length;return t=this.parseError(_.errStr,_,this.JisonLexerError)||this.ERROR,t===this.ERROR&&!this.matches&&f===this._input&&x===this.topState()&&j===this.conditionStack.length&&this.input(),t}else return this.done=!0,this.clear(),this.EOF},lex:function(){var t;for(typeof this.pre_lex=="function"&&(t=this.pre_lex.call(this,0)),typeof this.options.pre_lex=="function"&&(t=this.options.pre_lex.call(this,t)||t),this.yy&&typeof this.yy.pre_lex=="function"&&(t=this.yy.pre_lex.call(this,t)||t);!t;)t=this.next();return this.yy&&typeof this.yy.post_lex=="function"&&(t=this.yy.post_lex.call(this,t)||t),typeof this.options.post_lex=="function"&&(t=this.options.post_lex.call(this,t)||t),typeof this.post_lex=="function"&&(t=this.post_lex.call(this,t)||t),t},fastLex:function(){for(var t;!t;)t=this.next();return t},canIUse:function(){var t={fastLex:!(typeof this.pre_lex=="function"||typeof this.options.pre_lex=="function"||this.yy&&typeof this.yy.pre_lex=="function"||this.yy&&typeof this.yy.post_lex=="function"||typeof this.options.post_lex=="function"||typeof this.post_lex=="function")&&typeof this.fastLex=="function"};return t},begin:function(t){return this.pushState(t)},pushState:function(t){return this.conditionStack.push(t),this.__currentRuleSet__=null,this},popState:function(){var t=this.conditionStack.length-1;return t>0?(this.__currentRuleSet__=null,this.conditionStack.pop()):this.conditionStack[0]},topState:function(t){return t=this.conditionStack.length-1-Math.abs(t||0),t>=0?this.conditionStack[t]:"INITIAL"},_currentRules:function(){return this.conditionStack.length&&this.conditionStack[this.conditionStack.length-1]?this.conditions[this.conditionStack[this.conditionStack.length-1]]:this.conditions.INITIAL},stateStackSize:function(){return this.conditionStack.length},options:{trackPosition:!0},JisonLexerError:l,performAction:function(t,i,r){var s=this,e=r;switch(i){case 0:break;default:return this.simpleCaseActionClusters[i]}},simpleCaseActionClusters:{1:8,2:5,3:6,4:3,5:4,6:7,7:1,8:"INVALID"},rules:[/^(?:\s+)/,/^(?:\d+(\.\d+)?\b)/,/^(?:\*)/,/^(?:\/)/,/^(?:\+)/,/^(?:-)/,/^(?:[^\W\d]+)/,/^(?:$)/,/^(?:.)/],conditions:{INITIAL:{rules:[0,1,2,3,4,5,6,7,8],inclusive:!0}}};return o}();I.lexer=tt;function K(){this.yy={}}K.prototype=I,I.Parser=K;function et(){return I.parse.apply(I,arguments)}export default{parser:I,Parser:K,parse:et};
package/src/jslex.ts DELETED
@@ -1,311 +0,0 @@
1
- /**
2
- * Adapted from jslex - A lexer in JavaScript. https://github.com/jimbojw/jslex
3
- * Licensed under MIT license
4
- */
5
-
6
-
7
- interface ILexSpec<T> {
8
- [key: string]: Map<string, (arg0: Scanner<T>) => T | T[]>;
9
- }
10
-
11
- interface IRule<T> {
12
- re: RegExp;
13
- action: (a: Scanner<T>) => T | T[];
14
- }
15
-
16
- interface IMatch<T> {
17
- index: number;
18
- rule: IRule<T>;
19
- reMatchArray: RegExpMatchArray;
20
- }
21
-
22
-
23
- // End of File marker
24
- const EOF = {};
25
-
26
- /**
27
- * Utility function for comparing two matches.
28
- * @param {object} m1 Left-hand side match.
29
- * @param {object} m2 Right-hand side match.
30
- * @return {int} Difference between the matches.
31
- */
32
- function matchcompare<T>(m1: IMatch<T>, m2: IMatch<T>): number {
33
- const m1_len = m1.reMatchArray[0].length;
34
- const m2_len = m2.reMatchArray[0].length;
35
- if(m2_len !== m1_len) {
36
- return m2_len - m1_len;
37
- } else {
38
- return m1.index - m2.index;
39
- }
40
- }
41
-
42
- export class Scanner<T> {
43
- private _input: string;
44
- private _lexer: JSLex<T>;
45
-
46
- // position within input stream
47
- private _pos: number = 0;
48
-
49
- // current line number
50
- private _line: number = 0;
51
-
52
- // current column number
53
- private _col: number = 0;
54
-
55
- private _offset: number = 0;
56
- private _less: number | null = null;
57
- private _go: boolean = false;
58
- private _newstate: string | null = null;
59
- private _state: string;
60
-
61
- private _text: string | null = null;
62
- private _leng: number | null = null;
63
- private _reMatchArray: RegExpMatchArray | null = null;
64
-
65
- constructor(input: string, lexer: JSLex<T>) {
66
- this._input = input;
67
- this._lexer = lexer;
68
- this._state = lexer.states[0];
69
- }
70
-
71
- /**
72
- * Analogous to yytext and yyleng in lex - will be set during scan.
73
- */
74
- public text(): string | null {
75
- return this._text;
76
- }
77
-
78
- public leng(): number | null {
79
- return this._leng;
80
- }
81
-
82
- public reMatchArray(): RegExpMatchArray | null {
83
- return this._reMatchArray;
84
- }
85
-
86
- /**
87
- * Position of in stream, line number and column number of match.
88
- */
89
- public pos(): number {
90
- return this._pos;
91
- }
92
-
93
- public line(): number {
94
- return this._line;
95
- }
96
-
97
- public column(): number {
98
- return this._col;
99
- }
100
-
101
- /**
102
- * Analogous to input() in lex.
103
- * @return {string} The next character in the stream.
104
- */
105
- public input(): string {
106
- return this._input.charAt(this._pos + this._leng! + this._offset++);
107
- }
108
-
109
- /**
110
- * Similar to unput() in lex, but does not allow modifying the stream.
111
- * @return {int} The offset position after the operation.
112
- */
113
- public unput(): number {
114
- return this._offset = this._offset > 0 ? this._offset-- : 0;
115
- }
116
-
117
- /**
118
- * Analogous to yyless(n) in lex - retains the first n characters from this pattern, and returns
119
- * the rest to the input stream, such that they will be used in the next pattern-matching operation.
120
- * @param {int} n Number of characters to retain.
121
- * @return {int} Length of the stream after the operation has completed.
122
- */
123
- public less(n: number): number {
124
- this._less = n;
125
- this._offset = 0;
126
- this._text = this._text!.substring(0, n);
127
- return this._leng = this._text.length;
128
- }
129
-
130
- /**
131
- * Like less(), but instead of retaining the first n characters, it chops off the last n.
132
- * @param {int} n Number of characters to chop.
133
- * @return {int} Length of the stream after the operation has completed.
134
- */
135
- public pushback(n: number): number {
136
- return this.less(this._leng! - n);
137
- }
138
-
139
- /**
140
- * Similar to REJECT in lex, except it doesn't break the current execution context.
141
- * TIP: reject() should be the last instruction in a spec callback.
142
- */
143
- public reject(): void {
144
- this._go = true;
145
- }
146
-
147
- /**
148
- * Analogous to BEGIN in lex - sets the named state (start condition).
149
- * @param {string|int} state Name of state to switch to, or ordinal number (0 is first, etc).
150
- * @return {string} The new state on successful switch, throws exception on failure.
151
- */
152
- public begin(state: string | number): string {
153
- if (this._lexer.specification[state]) {
154
- return this._newstate = state as string;
155
- }
156
- const s = this._lexer.states[parseInt(state as string)];
157
- if (s) {
158
- return this._newstate = s;
159
- }
160
- throw "Unknown state '" + state + "' requested";
161
- }
162
-
163
- /**
164
- * Simple accessor for reading in the current state.
165
- * @return {string} The current state.
166
- */
167
- public state(): string {
168
- return this._state;
169
- }
170
-
171
- /**
172
- * Scan method to be returned to caller - grabs the next token and fires appropriate calback.
173
- * @return {T} The next token extracted from the stream.
174
- */
175
- public scan(): T | T[] {
176
- if(this._pos >= this._input.length) {
177
- return EOF as T;
178
- }
179
-
180
- const str = this._input.substring(this._pos);
181
- const rules = this._lexer.specification[this._state];
182
- const matches: IMatch<T>[] = [];
183
- for (let i = 0; i < rules.length; i++) {
184
- const rule = rules[i];
185
- const mt = str.match(rule.re);
186
- if (mt !== null && mt[0].length > 0) {
187
- matches.push({
188
- index: i,
189
- rule: rule,
190
- reMatchArray: mt,
191
- });
192
- }
193
- }
194
- if (matches.length === 0) {
195
- throw new Error("No match found for input '" + str + "'");
196
- }
197
- matches.sort(matchcompare);
198
- this._go = true;
199
-
200
- let result: T | T[];
201
- let matched_text: string;
202
- for (let j = 0, n = matches.length; j < n && this._go; j++) {
203
- this._offset = 0;
204
- this._less = null;
205
- this._go = false;
206
- this._newstate = null;
207
- const m = matches[j];
208
- matched_text = m.reMatchArray[0];
209
- this._text = matched_text;
210
- this._leng = matched_text.length;
211
- this._reMatchArray = m.reMatchArray;
212
- result = m.rule.action(this);
213
- if (this._newstate && this._newstate != this._state) {
214
- this._state = this._newstate;
215
- break;
216
- }
217
- }
218
- const text = this._less === null ? matched_text! : matched_text!.substring(0, this._less);
219
- const len = text.length;
220
- this._pos += len + this._offset;
221
-
222
- const nlm = text.match(/\n/g);
223
- if (nlm !== null) {
224
- this._line += nlm.length;
225
- this._col = len - text.lastIndexOf("\n") - 1;
226
- } else {
227
- this._col += len;
228
- }
229
- return result!;
230
- }
231
- }
232
-
233
- export class JSLex<T> {
234
- public states: string[];
235
- public specification: Record<string, IRule<T>[]>;
236
-
237
- constructor(spec: ILexSpec<T>) {
238
- this.states = Object.keys(spec);
239
- this.specification = {};
240
-
241
- // build out internal representation of the provided spec
242
- for (const s of this.states) {
243
- // e.g. s = "start"
244
- const rule_map = spec[s] as Map<string, (arg0: Scanner<T>) => T | T[]>;
245
-
246
- if (s in this.specification) {
247
- throw "Duplicate state declaration encountered for state '" + s + "'";
248
- }
249
-
250
- this.specification[s] = [] as IRule<T>[];
251
-
252
- for (const [k,v] of rule_map.entries()) {
253
- let re: RegExp;
254
- try {
255
- re = new RegExp('^' + k);
256
- } catch (err) {
257
- throw "Invalid regexp '" + k + "' in state '" + s + "' (" + (err as Error).message + ")";
258
- }
259
- this.specification[s].push({
260
- re: re,
261
- action: v
262
- });
263
- }
264
- }
265
- }
266
-
267
- /**
268
- * Scanner function - makes a new scanner object which is used to get tokens one at a time.
269
- * @param {string} input Input text to tokenize.
270
- * @return {function} Scanner function.
271
- */
272
- public scanner(input: string): Scanner<T> {
273
- return new Scanner(input, this);
274
- }
275
-
276
- /**
277
- * Similar to lex's yylex() function, consumes all input, calling calback for each token.
278
- * @param {string} input Text to lex.
279
- * @param {function} callback Function to execute for each token.
280
- */
281
- public lex(input: string, callback: (arg0: T | T[]) => void) {
282
- const scanner = this.scanner(input);
283
- while (true) {
284
- const token = scanner.scan();
285
- if (token === EOF) {
286
- return;
287
- }
288
- if (token !== undefined) {
289
- callback(token);
290
- }
291
- }
292
- }
293
-
294
- /**
295
- * Consumes all input, collecting tokens along the way.
296
- * @param {string} input Text to lex.
297
- * @return {array} List of tokens, may contain an Error at the end.
298
- */
299
- public collect(input: string): T[] {
300
- const tokens: T[] = [];
301
- const callback = function(item: T | T[]) {
302
- if (Array.isArray(item)) {
303
- tokens.push(...item);
304
- } else {
305
- tokens.push(item);
306
- }
307
- };
308
- this.lex(input, callback);
309
- return tokens;
310
- }
311
- };