Skip to content

Add 'table' property to Readtable entries #2

Description

@gabejohnson

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions