How to use the nearley.Parser 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 tjvr / tosh2 / editor / index.js View on Github external
compile() {
    const source = this.cm.getValue()
    // TODO cache compiled-ness
    const model = this._model
    const parser = new nearley.Parser(grammar)
    try {
      parser.feed(source)
    } catch (e) {
      console.error(e)
      model.scripts = { error: e }
      return
    }
    const results = parser.results
    if (results.length > 1) throw new Error("Ambiguous!")
    const scripts = []
    const x = 10
    var y = 10
    for (let script of results[0]) {
      scripts.push([x, y, script])
      y += measure(script) + 10
    }
github accordproject / cicero / packages / cicero-core / src / parsermanager.js View on Github external
static compileGrammar(sourceCode) {

        try {
            // Parse the grammar source into an AST
            const grammarParser = new nearley.Parser(nearleyGrammar);
            grammarParser.feed(sourceCode);
            const grammarAst = grammarParser.results[0]; // TODO check for errors

            // Compile the AST into a set of rules
            const grammarInfoObject = compile(grammarAst, {});
            // Generate JavaScript code from the rules
            const grammarJs = generate(grammarInfoObject, 'grammar');

            // Pretend this is a CommonJS environment to catch exports from the grammar.
            const module = {
                exports: {}
            };
            eval(grammarJs);
            return module.exports;
        } catch (err) {
            Logger.error(err);
github alyssarosenzweig / uPresent / browser / IDE / browser.publish.js View on Github external
function publish(input, shouldMinify, useFS, filePrefix) {
    var parser = new nearley.Parser(grammar.ParserRules, grammar.ParserStart);
    
    if(!filePrefix) filePrefix = "";

    var cssFile = filePrefix + "common.css";
    var themeFile = filePrefix + "themes/modern.dark.css";
    var jsFile = filePrefix + "scripts.js";

    if(useFS) {
        fs = require("fs"); // intentional lack of var keyword
    }

    parser.current = 0;
    parser.feed(input);

    var res = parser.results[0];
    var code = "";
github tategakibunko / TypeNovel / dist / typenovel-parser.js View on Github external
NearlyParser.prototype.astListFromString = function (source) {
        var parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar_1.default));
        var results = parser.feed(source).results;
        if (results.length === 0) {
            console.error('Parsing failed!');
            return [];
        }
        if (results.length > 1) {
            console.error('Ambigous grammar!');
        }
        var astList = results[0].map(function (ret) { return ret[0]; });
        return astList;
    };
    NearlyParser.prototype.astFromFile = function (path, opt) {
github mahirshah / css-property-parser / src / formatters / grammarFormatters / JsonGrammarFormatter2.js View on Github external
static format(formalSyntax) {
    const parser = new nearley.Parser(grammar.ParserRules, grammar.ParserStart).feed(formalSyntax);
    const [rootNode] = parser.results;
    return JsonGrammarFormatter2._evaluateNode(rootNode);
  }
github accordproject / cicero / packages / cicero-core / src / parsermanager.js View on Github external
getParser() {
        if (!this.grammarAst) {
            throw new Error('Must call setGrammar or buildGrammar before calling getParser');
        }

        return new nearley.Parser(nearley.Grammar.fromCompiled(this.grammarAst));
    }