Giter Site home page Giter Site logo

tarser's Introduction

Tarser

Tarser is the successor to my "parser-combinator" library.

Installation

npm install https://github.com/Palladium02/tarser.git

Documentation

char(c: string)

many(p: Parser)

many takes a parser which is then applied to the input. The parser will eat from the input until it can't. It may not eat anything.

many1(p: Parser)

Similar to many but many1 will eat at least once. If that is not possible this and all following parser will fail.

sequence(...ps: Parser[])

sequence takes a set of parsers which as the name implies we be working on the input one after another. As soon as one parser fails the whole sequence will fail and yield an error result.

word(w: string)

word converts a string into a parser that expects that string.

import {word} from 'tarser';

const Hello = word('Hello');

Hello.run('Hello World!');
//    ^? { type: 'ok', index: 6, input: 'Hello World!', value: ['H', 'e', 'l, 'l', 'o'] }

Here one might already see that word is just the combination of char and sequence as we receive ['H', 'e', 'l, 'l', 'o'] as parsed value. How we can change the parsed will be explained in the Converter section.

optional(p: Parser)

optional takes a parser that might but must not be satisfied by the input. Based on whether the parser was satisfied we advance the index.

choice(...ps: Parser[])

choice takes a set of parsers of which one must be satisfied. It will always take the first succeeding parser.

float

A parser that expects the string representation a floating point number.

digit

A parser that expects a singular digit.

whitespace

A parser that expects a whitespace character.

lazy

lazy takes a function that returns a parsers and only call the given function if needed allowing you to build recursive parsers. E.g. examples/array.ts

Converter

Parser return ParserStates containing a value field if the parser succeeded. These values might not have the shape you want. Parsers have a map method that allows you to convert the value field to a reasonable value. Tarser ships with a selection of converters for all built-in terminal parsers.

forDigit

import * as Tarser from 'tarser';

const digit = Tarser.digit.map(Converter.forDigit);

digit.run('1');
//    ^? { type: 'ok', index: 1, input: '1', value: 1 }

forFloat

import * as Tarser from 'tarser';

const float = Tarser.float.map(Converter.forFloat);

float.run('-1.2');
//    ^? { type: 'ok', index: 1, input: '1', value: -1.2 }

Examples

Examples can be found in the examples directory of this repo.

tarser'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.