#programming-language #grammar #prism #recursion #left #built #adaptation

prism-parser

A PEG parsing library built for the Prism programming language

2 unstable releases

0.2.0 Aug 18, 2024
0.1.0 Jun 14, 2024

#252 in Programming languages

Download history 137/week @ 2024-08-17 8/week @ 2024-08-24 3/week @ 2024-08-31 19/week @ 2024-09-14 13/week @ 2024-09-21 7/week @ 2024-09-28

105 downloads per month
Used in prism-compiler

MPL-2.0 license

105KB
3K SLoC

Prism Parser

A PEG parsing library built for the Prism programming language

About

This crate provides a PEG parser with the following features:

  • Support for left recursion
  • Adaptation: The grammar can be changed dynamically during parsing

This crate is under heavy development and not yet ready to be used in production code.

Example

This defines the grammar of a simple arithmetic language, and support to adapt the language:

rule start = block;
rule block {
    b <- "grammar" "{" g:grammar(prule_action) "}" ";" b:#adapt(g, block);
    s :: b <- s:stmt ";" b:block;
    [] <- "";
}

rule stmt {
    Let(e) <- "let" e:expr;
    Do() <- "do";
}

rule expr {
    group additive {
        Add(x, y) <- x:#next "+" y:#this;
    }
    group multiplicative {
        Mul(x, y) <- x:#next "*" y:#this;
    }
    group base {
        Block(b) <- "(" b:block ")";
        UnaryMinus(v) <- "-" v:#this;
        Num(n) <- n:#str([0-9]*);
    }
}

rule layout = [' ' | '\n'];

Example programs in this language:

1 * 2 + -3
grammar {
    rule expr {
        group additive {
            1 + (-2) <- x:#next "-" y:#this;
        }
    }
};
1 - 2

Dependencies

~2.1–10MB
~98K SLoC