How to use the nearley.Grammar.fromCompiled function in nearley

To help you get started, we’ve selected a few nearley examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vpdb / vpx-js / test / script.helper.ts View on Github external
export function vbsToAst(vbs: string): Program {
	const parser = new Parser(Grammar.fromCompiled(vbsGrammar));
	parser.feed(vbs.trim() + '\n');
	/* istanbul ignore if */
	if (parser.results.length === 0) {
		throw new Error('Parser returned no results.');
	}
	return parser.results[0] as Program;
}
github vpdb / vpx-js / lib / scripting / transpiler.ts View on Github external
private parse(vbs: string): Program {

		const parser = new Parser(Grammar.fromCompiled(vbsGrammar));
		parser.feed(vbs);
		/* istanbul ignore if */
		if (parser.results.length === 0) {
			throw new Error('Parser returned no results.');
		}
		return parser.results[0];
	}
github cruise-automation / webviz / packages / webviz-core / src / components / MessageHistory / parseRosPath.js View on Github external
// @flow
//
//  Copyright (c) 2018-present, GM Cruise LLC
//
//  This source code is licensed under the Apache License, Version 2.0,
//  found in the LICENSE file in the root directory of this source tree.
//  You may not use this file except in compliance with the License.

import { memoize } from "lodash";
import { Parser, Grammar } from "nearley";

import type { RosPath } from "./internalCommon";
import rosPathGrammar from "./rosPathGrammar.ne";

const grammarObj = Grammar.fromCompiled(rosPathGrammar);

const parseRosPath: (path: string) => ?RosPath = memoize(
  (path: string): ?RosPath => {
    // Need to create a new Parser object for every new string to parse (should be cheap).
    const parser = new Parser(grammarObj);
    try {
      return parser.feed(path).results[0];
    } catch (_) {
      return undefined;
    }
  }
);

export default parseRosPath;
github ballercat / walt / packages / walt-compiler / src / parser / index.js View on Github external
nodes,
    helpers,
    Syntax,
  };

  // All Grammar plugins are factories resulting in an object which must contain
  // a "ParserRules" array which will be added to the base grammar.
  const grammar = grammarList.slice(1).reduce((acc: any, value: Function) => {
    const extra = value.call(context);
    return {
      ...acc,
      ParserRules: acc.ParserRules.concat(extra.ParserRules),
    };
  }, grammarList[0].call(context));

  const parser = new Parser(Grammar.fromCompiled(grammar));

  parser.feed(source);

  // This is a safeguard against ambiguous syntax that may be generated by blending
  // multiple different grammars together. If there is more than one was to parse
  // something then we did something wrong and we hard exit the compiler pipeline.
  invariant(
    parser.results.length === 1,
    `PANIC - Ambiguous Syntax! Number of productions (${parser.results.length})`
  );

  return parser.results[0];
});
github duartealexf / sql-ddl-to-json-schema / lib / parser.js View on Github external
constructor(dialect = 'mysql') {
    if (!dialect || dialect === 'mysql' || dialect === 'mariadb') {
      this.compiledGrammar = NearleyGrammar.fromCompiled(MySQLGrammarRules);
      this.compactFormatter = MySQLCompactFormatter;
      this.jsonSchemaFormatter = MySQLJSONSchemaFormatter;
    }
    else {
      throw new TypeError(`Unsupported SQL dialect given to parser: '${dialect}. ` +
        `Please provide 'mysql', 'mariadb' or none to use default.`);
    }

    this.resetParser();

    /**
     * Parsed statements.
     * @type {string[]}
     */
    this.statements = [];