Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'table' property to Readtable entries #2

Open
gabejohnson opened this issue Feb 10, 2017 · 1 comment
Open

Add 'table' property to Readtable entries #2

gabejohnson opened this issue Feb 10, 2017 · 1 comment

Comments

@gabejohnson
Copy link
Owner

gabejohnson commented Feb 10, 2017

It would be useful to generalize dispatch macros and not have to hard-code a single character as the dispatch character.

For instance:

const entry = {
  key: '#',
  table: Readtable.from([...]),
  action(...) { ... }
}

The semantics would be defined in the reader but I'm thinking:

  1. If the entry has a table, look at the next char and see if the table has an entry for it and recurse.
  2. If the entry has a table and an action, do 1 but only after running the action (for side-effects/setup).
  3. If the entry has no table, just execute the action.

Maybe 2 is unnecessary or maybe the action just before the final table found should execute.

Motivation:

The current table-driven reader in sweet.js uses some simple tries for dealing w/ punctuators and keywords. This function could be built into the readtable implementation.

const tEntry = {
  key: 't',
  table: Readtable.from([{
    key: 'h',
    table: Readtable.from([{
      key: 'i',
      table: Readtable.from([{
        key: 's',
        action(stream) {
          stream.readString();
          return KeywordToken({ value: 'this' });
        }
      }])
    }, {
      key: 'r',
      table: Readtable.from([{
        key: 'o',
        table: Readtable.from([{
          key: 'w',
          action(stream) {
            stream.readString();
            return KeywordToken({ value: 'throw'});
          }
        }])
      }])
    }])
  }])
};

Now this would obviously be procedurally generated, but this illustrates the concept.

Note: Another way to achieve similar functionality would be to allow multi-char keys.

@gabejohnson
Copy link
Owner Author

Another possible use could be to have the reader set table as the current readtable. This could simplify certain reader implementations a great deal.

const stringTableEntries = [{
  action: function(stream) {
    return stream.readString() + this.read(stream);
  }
}, {
  key: "'",
  action: function() { return ''; }
}, {
  key: '\\',
  action: readStringEscape
}];

function readStringLiteral(stream) {
  return StringToken({ str: this.read(stream) });
}

const stringEntries = [{
  key: "'",
  table: stringTable,
  action: readStringLiteral
}, {
  key: '"',
  table: stringTable,
  action: readStringLiteral
}];

/cc @disnet

@gabejohnson gabejohnson changed the title Add table property to Readtable entries Add 'table' property to Readtable entries Feb 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant