Pegex::Compiler(3pm)
π Pegex::Compiler π
okay bestie, you found the manual page for Pegex::Compiler, which is the Pegex Compiler (yes, that's literally what the NAME says, we're keeping it π ). It ships as part of the User Contributed Perl Documentation and it is lowkey iconic. Let's get into it.
β¨ NAME β¨
Pegex::Compiler β the Pegex Compiler. That's it, that's the name, periodt. π
πΈ SYNOPSIS πΈ
Here's the vibe, copy it exactly bestie:
use Pegex::Compiler;
my $grammar_text = '... grammar text ...';
my $pegex_compiler = Pegex::Compiler->new();
my $grammar_tree = $pegex_compiler->compile($grammar_text)->tree;
or, the other way:
perl -Ilib -MYourGrammarModule=compile
π¦ DESCRIPTION π¦
So what does this thing do? Pegex::Compiler takes a Pegex grammar string (or a file, she's flexible π ) and transforms it into a compiled form. That compiled form is called a grammar tree, and it's literally just a nested data structure. Simple. Elegant. Slay.
And the way that grammar tree can be serialized to YAML, JSON, Perl, or honestly any other programming language? Extremely portable, no cap. Pegex::Grammar has methods for serializing to all of those forms, so you're covered. β¨
NOTE: unless you're out here developing Pegex based modules, you can safely ignore this module. And even if you are developing them, you probably won't use it directly anyway. Go look at [In Place Compilation] below, that's the one you actually want. π
π METHODS π
The following public methods are available, and they're all kind of a big deal:
$compiler = Pegex::Compiler->new();β returns you a brand new Pegex::Compiler object. Fresh out the box. π$grammar_tree = $compiler->compile($grammar_input);β compiles a grammar text into a grammar tree that a Pegex::Parser can use. This method calls theparseandcombinatemethods and returns the resulting tree. π«
The input can be a string, a string ref, a file path, a file handle, or a Pegex::Input object β so many options, she contains multitudes. It returns $self so you can chain it to other methods. (Chain chaining, we love to see it. π₯)
$compiler->parse($grammar_text)β the first step of acompileisparse. This applies the Pegex language grammar to your grammar text and produces an unoptimized tree. π±
This method returns $self so you can chain it to other methods.
$compiler->combinate()β before a Pegex grammar tree can be used to parse things, it needs to be combinated. This process turns the regex tokens into real regexes. It also combines some rules together and eliminates rules that are not needed or have been combinated. The result is a Pegex grammar tree that a Pegex::Parser can use. π§
NOTE: while the parse phase of a compile is always the same across various programming languages, the combinate phase takes into consideration any special needs of the target language. Pegex::Compiler only combinates for Perl, although this is often sufficient in similar languages like Ruby or Python (PCRE based regexes). Languages like Java probably need to use their own combinators. π
$compiler->tree()β returns the current state of the grammar tree (as a hash ref). π$compiler->to_yaml()β serializes the current grammar tree to YAML. β$compiler->to_json()β serializes the current grammar tree to JSON. β$compiler->to_perl()β serializes the current grammar tree to Perl. β
π IN PLACE COMPILATION π
Okay so this is the part you actually care about. When you write a Pegex based module, you're gonna want to precompile your grammar into Perl so it has no load penalty. Main character energy: zero lag. Pegex::Grammar provides a special mechanism for exactly this. Say you have a class like this:
package MyThing::Grammar;
use Pegex::Base;
extends 'Pegex::Grammar';
use constant file => '../mything-grammar-repo/mything.pgx';
sub make_tree {
}
Simply use this command:
perl -Ilib -MMyThing::Grammar=compile
and Pegex::Grammar will call Pegex::Compile to put your compiled grammar inside your make_tree subroutine. It will actually write the text into your module. The way that makes it trivial to update your grammar module after making changes to the grammar file?? Obsessed. ππ
See Pegex::JSON for an example.
π 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, you ate that π«Ά this one was generated from perl v5.36.0, dated 2022-11-19, and it lives in the Pegex::Compiler(3pm) manual. go forth and compile, ily π β¨
π¦ 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