How to use the nearley.Grammar 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 tridactyl / tridactyl / src / background / nearley_utils.ts View on Github external
constructor(grammar) {
        this.parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar))
        this.initial_state = this.parser.save()
        /* this.results = this.parser.results */
    }
github bijection / nearley-playground / src / client / compile.js View on Github external
}
            if (opts.alreadycompiled.indexOf(path) === -1) {
                opts.alreadycompiled.push(path)
                if (path === 'postprocessors.ne') {
                    var f = require('nearley/builtin/postprocessors.ne')
                } else if (path === 'whitespace.ne') {
                    var f = require('nearley/builtin/whitespace.ne')
                } else if (path === 'string.ne') {
                    var f = require('nearley/builtin/string.ne')
                } else if (path === 'number.ne') {
                    var f = require('nearley/builtin/number.ne')
                } else if (path === 'cow.ne') {
                    var f = require('nearley/builtin/cow.ne')
                }

                var parserGrammar = nearley.Grammar.fromCompiled(bootstraped)
                var parser = new nearley.Parser(parserGrammar)
                parser.feed(f)
                var c = Compile(parser.results[0], {
                    file: path,
                    __proto__: opts
                })

                result.rules = result.rules.concat(c.rules)
                result.body = result.body.concat(c.body)
                // result.customTokens = result.customTokens.concat(c.customTokens);
                Object.keys(c.config).forEach(function(k) {
                    result.config[k] = c.config[k]
                })
                Object.keys(c.macros).forEach(function(k) {
                    result.macros[k] = c.macros[k]
                })
github mahirshah / css-property-parser / src / expandShorthandProperty.js View on Github external
throw new UnsupportedPropertyError(propertyName);
  } else if (CSS_CONSTANTS.globalValues.includes(propertyValue)) {
    return getShorthandComputedProperties(propertyName).reduce((propertyMap, computedPropertyName) => (
      Object.assign({ [computedPropertyName]: propertyValue }, propertyMap)
    ), {});
  }

  // get the compiled grammar file for this property
  const grammar = grammars[propertyName];
  // remove any block style comments and extra whitespace
  const formattedPropertyValue = propertyValue.replace(R_BLOCK_COMMENT, ' ').replace(/\s+/g, ' ').trim();
  let parser;

  // attempt to parse the css value, using the property specific parser
  try {
    parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar)).feed(formattedPropertyValue);
  } catch (parseError) {
    throw new ParseError(`Error parsing shorthand property ${propertyName}: ${propertyValue}. ${parseError.message}`);
  }

  // get the first parsing and use the formatter for the specific shorthand type for this property
  const { shorthandType } = shorthandProperties[propertyName];
  const [rootNode] = parser.results;
  LocationIndexTracker.reset();
  let propertyExpansion = shorthandPropertyTypeToActionDictionaryFactoryMap[shorthandType]
    .format(propertyName, rootNode, formattedPropertyValue);

  if (recursivelyResolve) {
    Object.keys(propertyExpansion).forEach((prop) => {
      if (shorthandProperties[prop]) {
        Object.assign(
          propertyExpansion,
github mimic-sussex / sema / client / compiler / compiler-low-level.js View on Github external
}
			if (opts.alreadycompiled.indexOf(path) === -1) {
				opts.alreadycompiled.push(path);
				if (path === "postprocessors.ne") {
					var f = require("nearley/builtin/postprocessors.ne");
				} else if (path === "whitespace.ne") {
					var f = require("nearley/builtin/whitespace.ne");
				} else if (path === "string.ne") {
					var f = require("nearley/builtin/string.ne");
				} else if (path === "number.ne") {
					var f = require("nearley/builtin/number.ne");
				} else if (path === "cow.ne") {
					var f = require("nearley/builtin/cow.ne");
				}

				var parserGrammar = nearley.Grammar.fromCompiled(bootstraped);
				var parser = new nearley.Parser(parserGrammar);
				parser.feed(f);
				var c = Compile(parser.results[0], {
					file: path,
					__proto__: opts
				});

				result.rules = result.rules.concat(c.rules);
				result.body = result.body.concat(c.body);
				// result.customTokens = result.customTokens.concat(c.customTokens);
				Object.keys(c.config).forEach(function(k) {
					result.config[k] = c.config[k];
				});
				Object.keys(c.macros).forEach(function(k) {
					result.macros[k] = c.macros[k];
				});
github mahirshah / css-property-parser / src / isValidDeclaration.js View on Github external
module.exports = function isValidDeclaration(property, value) {
  if (!properties[property]) {
    return false;
  } else if (CSS.globalValues.includes(value) || CSS.variableRegex.test(value)) {
    return true;
  }

  const propertyGrammar = grammars[property];

  try {
    const parser = new nearley.Parser(nearley.Grammar.fromCompiled(propertyGrammar)).feed(value);
    return !!parser.results.length;
  } catch (parseError) {
    return false;
  }
};
github tategakibunko / TypeNovel / src / typenovel-parser.ts View on Github external
public astListFromString(source: string): Ast[] {
    const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
    const results = parser.feed(source).results as Ast[][][];
    if (results.length === 0) {
      console.error('Parsing failed!');
      return [];
    }
    if (results.length > 1) {
      console.error('Ambigous grammar!');
    }
    const astList: Ast[] = results[0].map(ret => ret[0]);
    return astList;
  }
github magma / magma / nms / app / fbcnms-packages / fbcnms-alarms / components / prometheus / PromQLParser.js View on Github external
export function Parser() {
  return new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
}
github lifeomic / alpha / src / adapters / helpers / parseLambdaUrl.js View on Github external
module.exports = (url) => {
  const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
  try {
    parser.feed(url);
    const parts = parser.results;
    return parts[0];
  } catch (error) {
    return null;
  }
};
github Cryptonomic / ConseilJS / src / chain / tezos / TezosContractIntrospector.ts View on Github external
export function generateEntryPointsFromParams(params: string): EntryPoint[] {
        const parser: nearley.Parser = new nearley.Parser(nearley.Grammar.fromCompiled(EntryPointTemplate));
        parser.feed(params);
        return parser.results[0];
    }
github iondv / framework / core / impl / QueryParser / index.js View on Github external
function parseGrammar(query) {
    const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
    let 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];
  }