Git Product home page Git Product logo

parser-combinator's Introduction

parser-combinator

parser-combinator is a parser library that delivers small parsers that can be combined into bigger parsers.

Usage

char(x: string)

Generates a parser that parses the given input for the given character. Equivalent to any literal in a regular expression.

const parser = char('a'); // <=> Regex(/a/)

parser.parse('a'); // accepts
parser.parse('b'); // rejects

sequence(ps: ...Parser[])

Takes an array of parsers. The generated parses parses a string and checks if the parsers are satisfied in the given order.

const parser = sequence(char('a'), char('b')); // <=> Regex(/ab/)

parser.parse('ab'); // accepts
parser.parse('ba'); // rejects

anyOf(ps: ...Parser[])

Takes an array of parsers. The input must only satisfy only one of the given parsers.

const parser = anyOf(char('a'), char('b')); // <=> Regex(/(a|b)/)

parser.parse('a'); // accepts
parser.parse('b'); // accepts
parser.parse('c'); // rejects

nOf(n: number, p: Parser)

Takes a number and a parser. The given parser must be satisfied n consecutive times.

const parser = nOf(2, char('a')); // <=> Regex(/a{2}/)

parser.parse('aa'); // accepts
parser.parse('ab'); // rejects

zeroOrMore(p: Parser)

Takes a parser. That parser can but must not be satisfied. If it is satisfied the parser greedily eats from the input until it cannot be satisfied anymore.

const parser = zeroOrMore(char('a')); // <=> Regex(/a*/)

parser.parse(''); // accepts
parser.parse('a'); // accepts
parser.parse('aaa'); // also accepts

oneOrMore(p: Parser)

Takes a parser. That parser but must be satisfied at least once. If it is satisfied the parser greedily eats from the input until it cannot be satisfied anymore.

const parser = zeroOrMore(char('a')); // <=> Regex(/a+/)

parser.parse('a'); // accepts
parser.parse('aaa'); // accepts
parser.parse(''); // rejects

minOf(n: number, p: Parser)

Takes a number n and a parser. The input must satisfy the parser at least n times.

const parser = minOf(2, char('a')); // <=> Regex(/a{2,}/)

parser.parse('aa'); // accepts
parser.parse('aaaa'); // accepts
parser.parse('a'); // rejects

maxOf(n: number, p: Parser)

const parser = maxOf(2, char('a')); // <=> Regex(/a{,2}/)

parser.parse('aa'); // accepts
parser.parse('a'); // accepts
parser.parse('aaaa'); // rejects

rangeOf(r: [number, number], p: Parser)

const parser = rangeOf([2,3], char('a')); // <=> Regex(/a{2,}/)

parser.parse('aa'); // accepts
parser.parse('aaa'); // accepts
parser.parse('a'); // rejects

optional(p: Parser)

Takes a parser. That parser can be satisfied but mustn't. It will only be satisfied once.

const parser = optional(char('a')); // <=> Regex(/a?/)

parser.parse(''); // accepts
parser.parse('a'); // accepts

any()

This parser generator represents accepts any character as input.

any().parse('?'); // accepts
any().parse('m'); // also fine

strict(p: Parser)

Generates a parser that will only accept when there is no rest after parsing.

const parser = strict(nOf(3, char('a'))); // <=> Regex(/^a{3}$/)

parser.parse('aaa'); // accepts
parser.parse('aaaa'); // rejects

word(w: string)

This parser generator takes a string and transforms it into a sequence of char() parsers.

lowercase

This is a parser that accpets any lowercase latin character.

uppercase

This is a parser that accepts any uppercase latin character.

Comping parsers

In this section I am going to show you how to compose parser to get a more complex parser. We will build a parser that can parser numbers that are written in scientific notation (e.g. 12e-1 <=> 1.2).

const uint = anyOf(
  char('0'),
  char('1'),
  char('2'),
  char('3'),
  char('4'),
  char('5'),
  char('6'),
  char('7'),
  char('8'),
  char('9')
);

const int = sequence(
  optional(char('-')),
  uint
);

const float = sequence(int, char('.'), uint);

const scientific = sequence(
  float,
  char('e'),
  int
);

parser-combinator's People

Contributors

palladium02 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.