Code Monkey home page Code Monkey logo

zigparsec's Introduction

Zig Parsec

During my exploration of functionnal programming, I encountered the notion of Parser Combinator when working on a small compiler for LLVM's Kaleidoscope example. Later on I saw a few articles and repository for Parser Combinator in Zig (mecha, hexop's blog)

This is my approach to implement the concept following closely what can be found in the Parsec library for the Haskell language.

Quick run down

All parsers are function, with the same base signature:

fn parser(Stream, std.mem.Allocator, *BaseState) anyerror!Result(T);

fn letterA(stream: Stream, _: std.mem.Allocator, _: *BaseState) anyerror!Result(u8)
{
    if (stream.isEOF()) return Result(u8).unexpected(stream);

    const c = stream.peek();
    if (c == 'A') {
        return Result(u8).success('A', stream.eat(1));
    }
    return Result(u8).unexpected(stream);
}

Stream is a simple object that represent a string with some information on the position of the cursor.

BaseState is a object used to define user state variable and state extension (for specialized parser).

zigparsec's People

Contributors

quentintessier 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.