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

Added docs for save()/restore() #404

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

diegoperini
Copy link

I hope my additions reflect the suggested use cases for save and restore operations.

@diegoperini
Copy link
Author

npm install && npm run make failed on my computer, thus Travis is angry with me I guess.

@abitrolly
Copy link

@diegoperini did you run them from the docs/? What error have you got?

@abitrolly
Copy link

This docker command shall not fail, because it doesn't not depend on the state of local packages.

docker run -v "$(pwd)":"/app" -w "/app/docs" -it --rm node bash -c "npm install && npm run make"

@gajus
Copy link

gajus commented Nov 8, 2021

Can this also be used to speed up parser initialization?, e.g.

import nearley from 'nearley';
import grammar from './grammar';
import type {
  Ast,
} from './types';

const rules = nearley.Grammar.fromCompiled(grammar);
const parser = new nearley.Parser(rules);
const state = parser.save();

export const parse = (query: string): Ast => {
  parser.restore(state);

  const results = parser.feed(query).results;

  if (results.length === 0) {
    throw new Error('Found no parsings.');
  }

  if (results.length > 1) {
    throw new Error('Ambiguous results.');
  }

  return results[0];
};

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

Successfully merging this pull request may close these issues.

None yet

3 participants