Git Product home page Git Product logo

czpeg's Introduction

Compile-Time Parsing Expression Grammars for Zig

cZPeg generates recursive descent parsers at compile-time in Zig using a functional API inspired by LPeg to declare and compose patterns. There is also an re module for generating parsers from more compact PEG-style pattern strings. The compiled parser can then be used to match strings at runtime or comptime!

Install

Make sure you have a recent Zig, we're currently testing with 0.8.0-dev drops from early 2021.

Until Zig's package distribution infrastructure has evolved, it's probably easiest to simply clone the contents of the czpeg/src/ tree into your project somewhere, then from your build.zig, add:

// build.zig

// if this is your LibExeObjStep
const exe = b.addExecutable(...);

// add new package like this
exe.addPackagePath("czpeg", "lib/czpeg.zig");

Now you can import and use the package in your project:

const std = @import("std");
const re = @import("czpeg").re;
test "greeting" {
    const pat = re.compile("'Hello' %s+ {%w+}'!'", .{});
    std.testing.expectEqualStrings("world", p.matchLean("Hello world!").?);
}

Test

From the cZPeg root, run all available tests using the usual chant:

$ zig build test

It's a good idea to run this regularly as Zig matures to make sure everything stays in sync.

API

A brief interface comparison for reference. p and q are other patterns, n is a non-negative integer:

cZPeg cZPeg.re LPeg LPeg.re PCRE
pat("s")
str("s")
's'
"s"
P(s)
S(s)
's'
"s"
s
pat(1)
any(1)
. P(1) . .
pat(n)
any(n)
.^n P(n) .^n .{n}
set("aeiou") [aeiou] S('aeiou') [aeiou] [aeiou]
span('0', '9') [0-9]
%d
R('09') [0-9]
%d
[0-9]
\d
pat(-1) !. P(-1) !. $
rep(0, 1, p)
p.rep(0, 1)
p? patt^-1 p? p?
rep(0, -1, p)
p.rep(0, -1)
p* patt^0 p* p*
rep(1, -1, p)
p.rep(1, -1)
p+ patt^1 p+ p+
rep(n, -1, p)
p.rep(n, -1)
p^+n p^n p^+n p{n,}
rep(0, n, p)
p.rep(0, n)
p^-n p^-n p^-n p{,n}
rep(n, n, p)
p.rep(n, n)
p^n p^n p{n}
pat(.{p, q})
seq(.{p, q})
p q p * q p q pq
alt(.{p, q}) p / q p + q p / q p|q
if_(p) &p #p &p (?=p)
not(p) !p -p !p (?!p)
(p) (p) (p) (p) (:?p)
pos() {} Cp() {}
cap(p)
p.cap()
{p} C(p) {p} (p)
grok(type, f, p)
p.grok(type, f)
p => f Cmt(p, f) p => f
foldRep() Cf() ~>
ref(&g, "name", type) name V('name') name
name = p name <- p { name = p } name <- p
unsupported / cap B Carg
Cb Cc Cg Cs
{:p:} {~p~} {|p|}
=name ->
\n

czpeg's People

Contributors

spadix0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

rdirtbike

czpeg's Issues

About zbar project

Hello @spadix0 ,

I am a user of the zbar project, but the project has not been maintained for many years,
I searched the original author on the Internet,
if you are the original author of zbar, can you review the code of the zbar project again and fix it Which bugs, provide some patches?

thanks.

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.