Pegex::Grammar(3pm)
π Pegex::Grammar β the base class era π
okay so bestie, welcome to Pegex::Grammar(3pm) β this is the manual page for the Pegex Grammar Base Class, and yes it's giving "let me hold your hand while you build an entire grammar" π
β¨ NAME β¨
Pegex::Grammar β it's the Pegex Grammar Base Class, aka the mother ship you inherit from when you wanna define your own grammar. π
πΈ SYNOPSIS πΈ
so first, you define a Pegex grammar (for the Foo syntax, obvi):
package Pegex::Foo::Grammar;
use base 'Pegex::Base';
extends 'Pegex::Grammar';
has text => q{
foo: bar baz
... rest of Foo grammar ...
};
and then you use it to parse some Foo:
use Pegex::Parser;
my $parse_tree = Pegex::Parser->new(
grammar => 'Pegex::Foo::Grammar',
receiver => 'Pegex::Tree',
)->parse('my/file.foo');
like. that's it. that's the whole vibe. π¦
π«Ά DESCRIPTION π«Ά
Pegex::Grammar is a base class for defining your own Pegex grammar classes. all you gotta do is provide the grammar with the text attribute or the file attribute β pick your fighter, bestie, not both (we'll get to that π).
and then β the iconic part β when Pegex::Parser uses your grammar, it wants it in the tree (compiled) form, so Pegex::Grammar comes through with automatic compilation support. no cap. it just handles it for you. π
π PROPERTIES AND METHODS π
π tree
This is the data structure containing the compiled grammar for your syntax. It is usually produced by Pegex::Compiler. You can inline it in the tree method, or else the make_tree method will be called to produce it.
The make_tree method will call on Pegex::Compiler to compile the text property by default. You can define your own make_tree method to do override this behavior.
Often times you will want to generate your own Pegex::Grammar subclasses in an automated fashion. The Pegex and TestML modules do this to be performant. This also allows you to keep your grammar text in a separate file, and often in a separate repository, so it can be shared by multiple programming language's module implementations.
See <https://github.com/ingydotnet/pegex-pgx> and <https://github.com/ingydotnet/pegex-pm/blob/master/lib/Pegex/Pegex/Grammar.pm>.
the "generate your own subclasses automatically" thing is main character energy honestly β keep the grammar text in its own file, its own repo, share it across every language implementation. iconic. π₯
π text
This is simply the text of your grammar. if you define this, you should (probably) not define the tree property. This grammar text will be automatically compiled when the tree is required.
ok so it's basically:textortree, not both. choose wisely bestie. π
π§ file
This is the file where your Pegex grammar lives. It is usually used when you are making a Pegex module. The path is relative to your top level module directory.
β make_tree
This method is called when the grammar needs the compiled version.
π AUTHOR π
Ingy dΓΆt Net <ingy@cpan.org> π
π COPYRIGHT AND LICENSE π
Copyright 2010-2020. Ingy dΓΆt Net.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <http://www.perl.com/perl/misc/Artistic.html>
and that's the whole page, bestie!! π«Ά this was perl v5.36.0, dated 2022-11-19, and it's the Pegex::Grammar(3pm) manpage β go forth and compile your grammars, you absolute legend. slay. πβ¨
π¦ where this page comes from
- debian bookworm: libpegex-perl 0.75-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libpegex-perl 0.75-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libpegex-perl 0.75-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libpegex-perl 0.75-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libpegex-perl 0.75-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms