@intend-it/parser 1.2.1 → 1.3.0

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.
package/README.md CHANGED
@@ -105,6 +105,7 @@ interface IntentDeclaration {
105
105
  |---------|--------|---------|
106
106
  | **Intents** | `intent Name(params) -> Type { ... }` | `intent Add(a: number, b: number) -> number` |
107
107
  | **Steps** | `step "instruction"` | `step "Validate the input"` |
108
+ | **Direct Calls** | `Intent(args)` | `Log(msg) => const res` |
108
109
  | **Result Variables** | `=> const x` or `=> let x` | `step "Calculate" => const result` |
109
110
  | **Invariants** | `invariant "rule"` | `invariant "Input must be positive"` |
110
111
  | **Ensures** | `ensure expression` | `ensure result is defined` |
package/dist/index.js CHANGED
@@ -9185,7 +9185,12 @@ var Equals = createToken({ name: "Equals", pattern: /===/ });
9185
9185
  var GreaterThan = createToken({ name: "GreaterThan", pattern: />/ });
9186
9186
  var LessThan = createToken({ name: "LessThan", pattern: /</ });
9187
9187
  var Dot = createToken({ name: "Dot", pattern: /\./ });
9188
- var StringLiteral = createToken({ name: "StringLiteral", pattern: /"(:?[^\\"\n\r]+|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/ });
9188
+ var StringLiteral = createToken({
9189
+ name: "StringLiteral",
9190
+ pattern: /"(?:[^"\\]|\\.)*"/,
9191
+ line_breaks: true
9192
+ });
9193
+ var NumberLiteral = createToken({ name: "NumberLiteral", pattern: /-?\d+(\.\d+)?/ });
9189
9194
  var Identifier = createToken({ name: "Identifier", pattern: /[a-zA-Z_]\w*/ });
9190
9195
  var WhiteSpace = createToken({
9191
9196
  name: "WhiteSpace",
@@ -9227,6 +9232,7 @@ var allTokens = [
9227
9232
  Colon,
9228
9233
  Dot,
9229
9234
  StringLiteral,
9235
+ NumberLiteral,
9230
9236
  Identifier
9231
9237
  ];
9232
9238
 
@@ -9318,7 +9324,8 @@ class IntendParser extends CstParser {
9318
9324
  { ALT: () => this.SUBRULE(this.invariantStatement) },
9319
9325
  { ALT: () => this.SUBRULE(this.stepStatement) },
9320
9326
  { ALT: () => this.SUBRULE(this.ensureStatement) },
9321
- { ALT: () => this.SUBRULE(this.returnStatement) }
9327
+ { ALT: () => this.SUBRULE(this.returnStatement) },
9328
+ { ALT: () => this.SUBRULE(this.callStatement) }
9322
9329
  ]);
9323
9330
  });
9324
9331
  invariantStatement = this.RULE("invariantStatement", () => {
@@ -9369,6 +9376,39 @@ class IntendParser extends CstParser {
9369
9376
  this.CONSUME(Semicolon);
9370
9377
  });
9371
9378
  });
9379
+ callStatement = this.RULE("callStatement", () => {
9380
+ this.CONSUME(Identifier, { LABEL: "IntentName" });
9381
+ this.CONSUME(LParen);
9382
+ this.OPTION(() => {
9383
+ this.SUBRULE(this.callArguments);
9384
+ });
9385
+ this.CONSUME(RParen);
9386
+ this.OPTION2(() => {
9387
+ this.CONSUME(FatArrow);
9388
+ this.OR([
9389
+ { ALT: () => this.CONSUME(Const) },
9390
+ { ALT: () => this.CONSUME(Let) }
9391
+ ]);
9392
+ this.CONSUME2(Identifier, { LABEL: "ResultVar" });
9393
+ });
9394
+ this.OPTION3(() => {
9395
+ this.CONSUME(Semicolon);
9396
+ });
9397
+ });
9398
+ callArguments = this.RULE("callArguments", () => {
9399
+ this.SUBRULE(this.value);
9400
+ this.MANY(() => {
9401
+ this.CONSUME(Comma);
9402
+ this.SUBRULE2(this.value);
9403
+ });
9404
+ });
9405
+ value = this.RULE("value", () => {
9406
+ this.OR([
9407
+ { ALT: () => this.CONSUME(StringLiteral) },
9408
+ { ALT: () => this.CONSUME(NumberLiteral) },
9409
+ { ALT: () => this.CONSUME(Identifier) }
9410
+ ]);
9411
+ });
9372
9412
  }
9373
9413
 
9374
9414
  // src/ast/visitor.ts
@@ -9438,7 +9478,7 @@ class IntendASTVisitor extends BaseIntendVisitor {
9438
9478
  const result = this.visit(stmt);
9439
9479
  if (result.type === "Invariant") {
9440
9480
  invariants.push(result);
9441
- } else if (result.type === "Step") {
9481
+ } else if (result.type === "Step" || result.type === "Call") {
9442
9482
  steps.push(result);
9443
9483
  } else if (result.type === "Ensure") {
9444
9484
  ensures.push(result);
@@ -9458,6 +9498,8 @@ class IntendASTVisitor extends BaseIntendVisitor {
9458
9498
  return this.visit(ctx.ensureStatement);
9459
9499
  } else if (ctx.returnStatement) {
9460
9500
  return this.visit(ctx.returnStatement);
9501
+ } else if (ctx.callStatement) {
9502
+ return this.visit(ctx.callStatement);
9461
9503
  }
9462
9504
  throw new Error("Unknown statement type");
9463
9505
  }
@@ -9494,6 +9536,47 @@ class IntendASTVisitor extends BaseIntendVisitor {
9494
9536
  identifier: ctx.Identifier[0].image
9495
9537
  };
9496
9538
  }
9539
+ callStatement(ctx) {
9540
+ const intent = ctx.IntentName[0].image;
9541
+ const args = ctx.callArguments ? this.visit(ctx.callArguments) : [];
9542
+ let resultVariable;
9543
+ let variableKind;
9544
+ if (ctx.ResultVar) {
9545
+ resultVariable = ctx.ResultVar[0].image;
9546
+ variableKind = ctx.Let && ctx.Let.length > 0 ? "let" : "const";
9547
+ }
9548
+ return {
9549
+ type: "Call",
9550
+ intent,
9551
+ args,
9552
+ resultVariable,
9553
+ variableKind
9554
+ };
9555
+ }
9556
+ callArguments(ctx) {
9557
+ if (!ctx.value)
9558
+ return [];
9559
+ return ctx.value.map((v) => this.visit(v));
9560
+ }
9561
+ value(ctx) {
9562
+ if (ctx.StringLiteral) {
9563
+ return {
9564
+ type: "String",
9565
+ value: this.unquoteString(ctx.StringLiteral[0].image)
9566
+ };
9567
+ } else if (ctx.NumberLiteral) {
9568
+ return {
9569
+ type: "Number",
9570
+ value: ctx.NumberLiteral[0].image
9571
+ };
9572
+ } else if (ctx.Identifier) {
9573
+ return {
9574
+ type: "Identifier",
9575
+ value: ctx.Identifier[0].image
9576
+ };
9577
+ }
9578
+ throw new Error("Unknown value type");
9579
+ }
9497
9580
  ensureStatement(ctx) {
9498
9581
  const tokens = [
9499
9582
  ...ctx.Identifier || [],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intend-it/parser",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "Parser for the Intend programming language",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",