@mojir/lits 2.0.13 → 2.0.14

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.
@@ -1,3 +1,7 @@
1
+ import type { DefNode } from '../builtin/specialExpressions/def';
2
+ import type { DefnNode } from '../builtin/specialExpressions/functions';
3
+ import type { Arity, FunctionArguments } from '../builtin/utils';
4
+ import type { A_SymbolToken } from '../tokenizer/algebraic/algebraicTokens';
1
5
  import type { TokenStream } from '../tokenizer/interface';
2
6
  import type { AstNode, ParseState } from './interface';
3
7
  export declare class AlgebraicParser {
@@ -5,7 +9,7 @@ export declare class AlgebraicParser {
5
9
  private parseState;
6
10
  constructor(tokenStream: TokenStream, parseState: ParseState);
7
11
  private advance;
8
- parse(): AstNode;
12
+ parse(): AstNode[];
9
13
  private parseExpression;
10
14
  private parseOperand;
11
15
  private parseOperandPart;
@@ -13,11 +17,17 @@ export declare class AlgebraicParser {
13
17
  private parseArray;
14
18
  private parseFunctionCall;
15
19
  parseLambdaFunction(): AstNode | null;
20
+ parseFunctionArguments(): {
21
+ functionArguments: FunctionArguments;
22
+ arity: Arity;
23
+ };
16
24
  private parseShorthandLamdaFunction;
17
25
  private parseLet;
18
26
  private parseFor;
19
27
  private parseForLoopBinding;
20
28
  private parseBinding;
29
+ parseDef(token: A_SymbolToken): DefNode;
30
+ parseDefn(token: A_SymbolToken): DefnNode;
21
31
  private isAtEnd;
22
32
  private peek;
23
33
  private peekAhead;
@@ -7,7 +7,7 @@ export declare const algebraicValueTokenTypes: readonly ["String", "A_Whitespace
7
7
  export declare const algebraicTokenTypes: readonly ["LBrace", "LBracket", "LParen", "RBrace", "RBracket", "RParen", "AlgNotation", "PolNotation", "EndNotation", "String", "A_Whitespace", "A_Operator", "A_Symbol", "A_ReservedSymbol", "A_SingleLineComment", "A_MultiLineComment", "A_Number", "A_BasePrefixedNumber"];
8
8
  declare const symbolicUnaryOperators: readonly ["!", "~", "+", "-"];
9
9
  declare const symbolicBinaryOperators: readonly ["**", "*", "/", "%", "+", "-", "<<", ">>", ">>>", "++", "<", "<=", ">", ">=", "==", "!=", "&", "^", "|", "&&", "||", "??"];
10
- declare const symbolicOperators: readonly ["!", "~", "+", "-", "**", "*", "/", "%", "+", "-", "<<", ">>", ">>>", "++", "<", "<=", ">", ">=", "==", "!=", "&", "^", "|", "&&", "||", "??", "=>", "...", ".", ",", "="];
10
+ declare const symbolicOperators: readonly ["!", "~", "+", "-", "**", "*", "/", "%", "+", "-", "<<", ">>", ">>>", "++", "<", "<=", ">", ">=", "==", "!=", "&", "^", "|", "&&", "||", "??", "=>", "...", ".", ",", "=", ";"];
11
11
  export declare function isFunctionOperator(operator: string): boolean;
12
12
  export type SymbolicUnaryOperator = typeof symbolicUnaryOperators[number];
13
13
  export type SymbolicBinaryOperator = typeof symbolicBinaryOperators[number];
@@ -408,6 +408,7 @@ var otherSymbolicOperators = [
408
408
  '.', // property accessor
409
409
  ',', // item separator
410
410
  '=', // property assignment
411
+ ';', // statement terminator
411
412
  ];
412
413
  var symbolicOperators = __spreadArray(__spreadArray(__spreadArray([], __read(symbolicUnaryOperators), false), __read(symbolicBinaryOperators), false), __read(otherSymbolicOperators), false);
413
414
  var nonFunctionOperators = [
@@ -3080,7 +3081,7 @@ var mathNormalExpression = {
3080
3081
  },
3081
3082
  };
3082
3083
 
3083
- var version = "2.0.13";
3084
+ var version = "2.0.14";
3084
3085
 
3085
3086
  var uuidTemplate = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
3086
3087
  var xyRegexp = /[xy]/g;
@@ -7110,6 +7111,7 @@ function fromBinaryOperatorToAstNode(operator, left, right, token) {
7110
7111
  token: getTokenDebugData(token) && token,
7111
7112
  };
7112
7113
  /* v8 ignore next 8 */
7114
+ case ';':
7113
7115
  case '!':
7114
7116
  case '~':
7115
7117
  case '=':
@@ -7130,14 +7132,33 @@ var AlgebraicParser = /** @class */ (function () {
7130
7132
  this.parseState.position += 1;
7131
7133
  };
7132
7134
  AlgebraicParser.prototype.parse = function () {
7133
- return this.parseExpression();
7135
+ var nodes = [];
7136
+ while (!this.isAtEnd()) {
7137
+ nodes.push(this.parseExpression());
7138
+ if (!isA_OperatorToken(this.peek(), ';')) {
7139
+ break;
7140
+ }
7141
+ this.advance();
7142
+ }
7143
+ return nodes;
7134
7144
  };
7135
7145
  AlgebraicParser.prototype.parseExpression = function (precedence) {
7136
7146
  var _a;
7137
7147
  if (precedence === void 0) { precedence = 0; }
7148
+ var firstToken = this.peek();
7149
+ if (isA_SymbolToken(firstToken) && firstToken[1] === 'def') {
7150
+ return this.parseDef(firstToken);
7151
+ }
7152
+ if (isA_SymbolToken(firstToken) && firstToken[1] === 'defn') {
7153
+ return this.parseDefn(firstToken);
7154
+ }
7138
7155
  var left = this.parseOperand();
7139
- while (!this.isAtEnd() && !isA_OperatorToken(this.peek(), ',') && !isRBracketToken(this.peek()) && !isRParenToken(this.peek())) {
7140
- var operator = this.peek();
7156
+ var operator = this.peek();
7157
+ while (!this.isAtEnd()
7158
+ && !isA_OperatorToken(operator, ',')
7159
+ && !isA_OperatorToken(operator, ';')
7160
+ && !isRBracketToken(operator)
7161
+ && !isRParenToken(operator)) {
7141
7162
  if (isA_BinaryOperatorToken(operator)) {
7142
7163
  var name_1 = operator[1];
7143
7164
  var newPrecedece = getPrecedence(name_1);
@@ -7167,6 +7188,7 @@ var AlgebraicParser = /** @class */ (function () {
7167
7188
  else {
7168
7189
  break;
7169
7190
  }
7191
+ operator = this.peek();
7170
7192
  }
7171
7193
  if (!left) {
7172
7194
  throw new LitsError('Expected operand', (_a = getTokenDebugData(this.peek())) === null || _a === void 0 ? void 0 : _a.sourceCodeInfo);
@@ -7413,13 +7435,11 @@ var AlgebraicParser = /** @class */ (function () {
7413
7435
  }
7414
7436
  case 'let':
7415
7437
  return this.parseLet(symbol, params);
7416
- case 'def':
7417
7438
  case 'defs':
7418
7439
  case 'if-let':
7419
7440
  case 'when-let':
7420
7441
  case 'when-first':
7421
7442
  case 'fn':
7422
- case 'defn':
7423
7443
  case 'defns':
7424
7444
  case 'try':
7425
7445
  case 'recur':
@@ -7442,8 +7462,32 @@ var AlgebraicParser = /** @class */ (function () {
7442
7462
  }
7443
7463
  };
7444
7464
  AlgebraicParser.prototype.parseLambdaFunction = function () {
7445
- var _a, _b;
7446
7465
  var firstToken = this.peek();
7466
+ try {
7467
+ var _a = this.parseFunctionArguments(), functionArguments = _a.functionArguments, arity = _a.arity;
7468
+ if (!isA_OperatorToken(this.peek(), '=>')) {
7469
+ return null;
7470
+ }
7471
+ this.advance();
7472
+ var body = this.parseExpression();
7473
+ return {
7474
+ t: AstNodeType.SpecialExpression,
7475
+ n: 'fn',
7476
+ p: [],
7477
+ o: [{
7478
+ as: functionArguments,
7479
+ b: [body],
7480
+ a: arity,
7481
+ }],
7482
+ token: getTokenDebugData(firstToken) && firstToken,
7483
+ };
7484
+ }
7485
+ catch (_b) {
7486
+ return null;
7487
+ }
7488
+ };
7489
+ AlgebraicParser.prototype.parseFunctionArguments = function () {
7490
+ var _a, _b, _c, _d, _e;
7447
7491
  this.advance();
7448
7492
  var rest = false;
7449
7493
  var letBindingObject;
@@ -7466,7 +7510,7 @@ var AlgebraicParser = /** @class */ (function () {
7466
7510
  }
7467
7511
  var symbolToken = this.peek();
7468
7512
  if (!isA_SymbolToken(symbolToken)) {
7469
- return null;
7513
+ throw new LitsError('Expected symbol', (_c = getTokenDebugData(this.peek())) === null || _c === void 0 ? void 0 : _c.sourceCodeInfo);
7470
7514
  }
7471
7515
  if (rest) {
7472
7516
  restArg = symbolToken[1];
@@ -7477,47 +7521,36 @@ var AlgebraicParser = /** @class */ (function () {
7477
7521
  this.advance();
7478
7522
  }
7479
7523
  if (!isA_OperatorToken(this.peek(), ',') && !isRParenToken(this.peek())) {
7480
- return null;
7524
+ throw new LitsError('Expected comma or closing parenthesis', (_d = getTokenDebugData(this.peek())) === null || _d === void 0 ? void 0 : _d.sourceCodeInfo);
7481
7525
  }
7482
7526
  if (isA_OperatorToken(this.peek(), ',')) {
7483
7527
  this.advance();
7484
7528
  }
7485
7529
  }
7530
+ var arity = restArg !== undefined ? { min: args.length } : args.length;
7486
7531
  if (!isRParenToken(this.peek())) {
7487
- return null;
7488
- }
7489
- this.advance();
7490
- if (!isA_OperatorToken(this.peek(), '=>')) {
7491
- return null;
7532
+ throw new LitsError('Expected closing parenthesis', (_e = getTokenDebugData(this.peek())) === null || _e === void 0 ? void 0 : _e.sourceCodeInfo);
7492
7533
  }
7493
- this.advance();
7494
7534
  var letBindings = letBindingObject ? arrayToPairs(letBindingObject.p) : [];
7495
- var body = this.parseExpression();
7496
- var arity = restArg !== undefined ? { min: args.length } : args.length;
7535
+ var functionArguments = {
7536
+ m: args,
7537
+ r: restArg,
7538
+ b: letBindings.map(function (pair) {
7539
+ var key = pair[0];
7540
+ var value = pair[1];
7541
+ return {
7542
+ t: AstNodeType.Binding,
7543
+ n: key.v,
7544
+ v: value,
7545
+ p: [],
7546
+ token: getTokenDebugData(key.token) && key.token,
7547
+ };
7548
+ }),
7549
+ };
7550
+ this.advance();
7497
7551
  return {
7498
- t: AstNodeType.SpecialExpression,
7499
- n: 'fn',
7500
- p: [],
7501
- o: [{
7502
- as: {
7503
- m: args,
7504
- r: restArg,
7505
- b: letBindings.map(function (pair) {
7506
- var key = pair[0];
7507
- var value = pair[1];
7508
- return {
7509
- t: AstNodeType.Binding,
7510
- n: key.v,
7511
- v: value,
7512
- p: [],
7513
- token: getTokenDebugData(key.token) && key.token,
7514
- };
7515
- }),
7516
- },
7517
- b: [body],
7518
- a: arity,
7519
- }],
7520
- token: getTokenDebugData(firstToken) && firstToken,
7552
+ functionArguments: functionArguments,
7553
+ arity: arity,
7521
7554
  };
7522
7555
  };
7523
7556
  AlgebraicParser.prototype.parseShorthandLamdaFunction = function () {
@@ -7709,6 +7742,50 @@ var AlgebraicParser = /** @class */ (function () {
7709
7742
  };
7710
7743
  return node;
7711
7744
  };
7745
+ AlgebraicParser.prototype.parseDef = function (token) {
7746
+ this.advance();
7747
+ var symbol = parseSymbol(this.tokenStream, this.parseState);
7748
+ assertA_OperatorToken(this.peek(), '=');
7749
+ this.advance();
7750
+ var value = this.parseExpression();
7751
+ return {
7752
+ t: AstNodeType.SpecialExpression,
7753
+ n: 'def',
7754
+ p: [symbol, value],
7755
+ token: getTokenDebugData(token) && token,
7756
+ };
7757
+ };
7758
+ AlgebraicParser.prototype.parseDefn = function (token) {
7759
+ var _a;
7760
+ this.advance();
7761
+ var symbol = parseSymbol(this.tokenStream, this.parseState);
7762
+ var _b = this.parseFunctionArguments(), functionArguments = _b.functionArguments, arity = _b.arity;
7763
+ assertLBraceToken(this.peek());
7764
+ this.advance();
7765
+ var body = [];
7766
+ while (!this.isAtEnd() && !isRBraceToken(this.peek())) {
7767
+ body.push(this.parseExpression());
7768
+ if (isA_OperatorToken(this.peek(), ';')) {
7769
+ this.advance();
7770
+ }
7771
+ }
7772
+ if (!isRBraceToken(this.peek())) {
7773
+ throw new LitsError('Expected closing brace', (_a = getTokenDebugData(this.peek())) === null || _a === void 0 ? void 0 : _a.sourceCodeInfo);
7774
+ }
7775
+ this.advance();
7776
+ return {
7777
+ t: AstNodeType.SpecialExpression,
7778
+ n: 'defn',
7779
+ f: symbol,
7780
+ p: [],
7781
+ o: [{
7782
+ as: functionArguments,
7783
+ b: body,
7784
+ a: arity,
7785
+ }],
7786
+ token: getTokenDebugData(token) && token,
7787
+ };
7788
+ };
7712
7789
  AlgebraicParser.prototype.isAtEnd = function () {
7713
7790
  return this.parseState.position >= this.tokenStream.tokens.length;
7714
7791
  };
@@ -7998,10 +8075,18 @@ function parsePolishToken(tokenStream, parseState) {
7998
8075
  parseState.position += 1;
7999
8076
  parseState.algebraic = true;
8000
8077
  var algebraicParser = new AlgebraicParser(tokenStream, parseState);
8001
- var node = algebraicParser.parse();
8078
+ var nodes = algebraicParser.parse();
8002
8079
  assertEndNotationToken(tokenStream.tokens[parseState.position++]);
8003
8080
  parseState.algebraic = false;
8004
- return node;
8081
+ if (nodes.length === 1) {
8082
+ return nodes[0];
8083
+ }
8084
+ return {
8085
+ t: AstNodeType.SpecialExpression,
8086
+ n: 'do',
8087
+ p: nodes,
8088
+ token: nodes[0].token,
8089
+ };
8005
8090
  }
8006
8091
  case 'PolNotation': {
8007
8092
  var astNodes = [];
@@ -8068,7 +8153,16 @@ function removeUnnecessaryTokens(tokenStream) {
8068
8153
  function parseToken(tokenStream, parseState) {
8069
8154
  if (parseState.algebraic) {
8070
8155
  var algebraicParser = new AlgebraicParser(tokenStream, parseState);
8071
- return algebraicParser.parse();
8156
+ var nodes = algebraicParser.parse();
8157
+ if (nodes.length === 1) {
8158
+ return nodes[0];
8159
+ }
8160
+ return {
8161
+ t: AstNodeType.SpecialExpression,
8162
+ n: 'do',
8163
+ p: nodes,
8164
+ token: nodes[0].token,
8165
+ };
8072
8166
  }
8073
8167
  return parsePolishToken(tokenStream, parseState);
8074
8168
  }
@@ -8158,13 +8252,10 @@ var algebraicReservedNamesRecord = {
8158
8252
  'false': { value: false },
8159
8253
  'nil': { value: null },
8160
8254
  'null': { value: null },
8161
- 'def': { value: null, forbidden: true },
8162
- 'defs': { value: null, forbidden: true },
8163
8255
  'if-let': { value: null, forbidden: true },
8164
8256
  'when-let': { value: null, forbidden: true },
8165
8257
  'when-first': { value: null, forbidden: true },
8166
8258
  'fn': { value: null, forbidden: true },
8167
- 'defn': { value: null, forbidden: true },
8168
8259
  'defns': { value: null, forbidden: true },
8169
8260
  'try': { value: null, forbidden: true },
8170
8261
  'recur': { value: null, forbidden: true },