Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • S sweet-core
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 62
    • Issues 62
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 4
    • Merge requests 4
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • sweet-js
  • sweet-core
  • Merge requests
  • !565

Add the ability to create a reset point on a macro context

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Gabe Johnson requested to merge github/fork/gabejohnson/mark into master Aug 05, 2016
  • Overview 26
  • Commits 7
  • Pipelines 0
  • Changes 4

The ability to move back one term can be implemented as a utility function. Unfortunately the time complexity is O(n) in the number of syntax items.

To my mind a big reason to have this ability is to expand a term based on the type of syntax read:

import { prev } from './utils' for syntax;

syntax m = ctx => {
  ctx.next() // eatThis
  let stx = ctx.next().value;
  let ret;
  if(stx.isIdentifier()) {
    ret = #`${stx}`;
  } else if(stx.isDelimiter() && stx.isBracket()) {
    prev(ctx); // <- call to prev
    ret = #`${ctx.expand('ArrayExpression')}`;
  }
  return ret;
}

m eatThis foo; // foo
m eatThis [1,2,3] // [1,2,3]

But the same thing can be accomplished with lookahead:

import { lookahead } from './utils' for syntax;

syntax m = ctx => {
  ctx.next(); // eatThis
  let stx = lookahead(ctx); // <- call to lookahead
  let ret;
  if(stx.isIdentifier()) {
    ret = #`${stx}`;
  } else if(stx.isDelimiter() && stx.isBracket()) {
    ret = #`${ctx.expand('ArrayExpression')}`;
  }
  return ret;
}

m eatThis foo; // foo
m eatThis [1,2,3] // [1,2,3]

With this PR lookahead(1) can be implemented in constant time and lookahead(m) is O(m).

The feature works as follows:

syntax m = ctx => {
  ctx.next(); // eatThis
  ctx.mark(); // <- marks a reset point
  let stx = ctx.next().value
  let ret;
  if(stx.isIdentifier()) {
    ret = #`${stx}`;
  } else if(stx.isDelimiter() && stx.isBracket()) {
    ctx.reset(); // <- back to reset point
    ret = #`${ctx.expand('ArrayExpression')}`;
  }
  return ret;
}

m foo; // foo
m [1,2,3] // [1,2,3]
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/gabejohnson/mark