Skip to content

How it Works

Matteo Basso edited this page Dec 26, 2016 · 8 revisions

natural-regex is a parser based on jison that consists in only one class with 4 properties that wraps the generated parser from language.jison and language.jisonlex file. Basically, when we run the following

npm run build

a new file named language.js is created using jison command line, in src folder. This is our parser. In index.js we can import this file and get the parser attribute. In this way we can expose the 4 static properties:

import { parser } from './language'; // language.js, get parser attribute

NaturalRegex.parser // represents the original parser with no modifications
NaturalRegex.parse(code: string) // use NaturalRegex.parser to parse the given string
NaturalRegex.from(code: string, flags: string or object) // returns a new RegExp using NaturalRegex.parse
NaturalRegex.replace({
  string: string to replace,
  match: natural regex string,
  replace: string that replaces matches,
  flags: string or object,
}) // returns a new string with replaced values

At this point, to modify the language you have to modify only the lexer language.jisonlex and the parser language.jison, so, if you want to contribute, you can add lex and grammar in these file.