Skip to content
/ plib Public

collection of multiple modules, which includes a parser generator and parsers generated by it.

Notifications You must be signed in to change notification settings

Luna1996/plib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PLib

plib (short for parser library) is a collection of multiple modules, which includes a parser generator and parsers generated by it.

plib takes a pre-generated ABNF structure as input to produce a parser.
parser will take raw text as input to generate the AST of it.

Note

Contribution is much welcomed, but beware that currently this is just a personal project for fun.

Warning

Lack of comments and documents. You need to dig into the bare code to use and knew the logic behind the library.

Modules

  • plib
    core module, parser generator
  • abnf
    abnf file parser/serializer, ABNF structure generator, generate itself
  • toml
    toml file parser/serializer, follow toml v1.0.0, passed all toml-test tests

How To

Generate ABNF

Use following command to generate the ABNF structure that defines ABNF syntax.

zig build -Dstep=gen_abnf -Dname=file_name

This command runs gen_abnf.zig, takes src/raw/<file_name>.abnf and outputs src/gen/<file_name>.zig.

Test Module

Use following command to test <mod_name> module.

zig build -Dstep=test -Dname=mod_name

toml-test

To test the toml module against toml-test, first make sure toml-test is on your PATH, then

zig build -Dstep=toml_test

Currently all tests is passed.

toml-test v2024-05-31 [C:\repo\repo.zig\plib\zig-out\bin\toml_encoder.exe]: using embedded tests
encoder tests: 182 passed,  0 failed
toml-test v2024-05-31 [C:\repo\repo.zig\plib\zig-out\bin\toml_decoder.exe]: using embedded tests
  valid tests: 182 passed,  0 failed
invalid tests: 371 passed,  0 failed

Add New Parser

Suppose you want to add a json parser (maybe not since it's included in std lib). You should:

  1. Add src/raw/json.abnf, which contains json's abnf definition.
  2. Run zig build -Dstep=gen_abnf -Dname=json to generate src/gen/json.zig.
  3. Modify zig.build (and zig.build.zong if so desired) to build a json module.
  4. Add src/mod/json/root.zig, which is the root source file of json module. See src/mod/toml/root.zig for example.
  5. If you are using vscode, you might also want to modify .vscode/tasks.json.

Use As Dependency

In your build.zig.zon add:

  .dependencies = .{
    .plib = .{
      .url = "https://github.com/Luna1996/abnf/archive/main.tar.gz",
    },
  },

In your build.zig add:

const plib_dep = b.dependency("plib", .{.toml = true});
const plib_mod = plib_dep.module("plib");
const toml_mod = plib_dep.module("toml");
main_mod.addImport("plib", plib);
main_mod.addImport("plib", toml);

In your project add:

const plib = @import("plib");
const toml = @import("toml");