File: demo_metagrammar.pl

package info (click to toggle)
libregexp-grammars-perl 1.058-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,328 kB
  • sloc: perl: 53,328; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 2,326 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use v5.10;
use warnings;

use Regexp::Grammars;
use re 'eval';

my $metagrammar = q{
    <RegexGrammar>

    <token: RegexGrammar>
        \A
        <ActivePattern= RuleBody>
        <[Definitions=  Ruledef]>*
        \z

    <token: Ruledef>
        \<  <Obj=(obj|)> <Type=(rule|token)>  \s*+ : \s*+  <Rulename=IDENT>  \>
            <Body=RuleBody>

    <token: RuleBody>
        <[Std=StdRegex]>+ % <[NonStd=Translatable]>

    <token: StdRegex>
        <MATCH= ([^<]*+ .*?)>

    <token: Translatable>
        <Subrule= SeparatedList>
      | <Subrule= SubruleCall>
      | <Directive>

    <token: Directive>
        \<  debug \s*+ : \s*+ 
            <Debug=(?: off | run | jump | step | continue | match | try )>
        \s*+  \>
      |  
        \<  logfile \s*+ : \s*+ <Log=(\S+)> \s*+ \>
        
    <token: SeparatedList>
              <Subrule= SubruleCall>
                  <ws1= (\s*+)>
                        \*\*
                  <ws2= (\s*+)>
        (?: <Separator= SubruleCall>
          | <Separator= PARENS> 
        )

    <token: SubruleCall>
            <WS=(\s++)>
        |
            \<
            (?:     <Noncapturing=(\.?)>         <Subrule=IDENT> 
                |   \[                      \s*+ <Subrule=IDENT> \s*+  \]
                |      <Alias=IDENT> \s*+ = \s*+ <Subrule=IDENT> \s*+
                |   \[ <Alias=IDENT> \s*+ = \s*+ <Subrule=IDENT> \s*+ \]
                |
                    <Noncapturing= (\.?)>
                           <Alias= IDENT>
                                   \s*+ = \s*+
                      ( <Action= PARENBLOCK> | <Subpattern= PARENS> )
                                   \s*+
            )
            \>

    <token: IDENT>
        [^\W\d]\w*+

    <token: CHARSET>
        (?> \[  \^?+  \]?+  [^]]*+  \] )

    <token: PARENBLOCK>
        \(\?\{ (?: \\\\. | <.BRACES> | <.PARENS> | <.CHARSET> | [^][()\\\\]++ )* \}\)

    <token: PARENS>
        \( (?: \\\\. | <.PARENS> | <.CHARSET> | [^][()\\\\]++ )* \)

    <token: BRACES>
        \{  (?: \\\\. | <.BRACES> | [^{}\\\\]++ )*  \}
};                          

my $parser = qr{
    $metagrammar
}xms;

#use Data::Dumper 'Dumper'; warn Dumper [ $parser ];

if ($metagrammar =~ $parser) {
    my $structure = $/{RegexGrammar};
    use Data::Dumper 'Dumper';
    say Dumper $structure;
}