Tiny/Grammar.pm

filter: Statementstmt 100% 77 / 77 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 81 / 81 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Tiny.pm ↑ Summary
1 package TOML::Tiny::Grammar;
2 # ABSTRACT: exports regex definitions used to parse TOML source
3 287 $TOML::Tiny::Grammar::VERSION = '0.21';
4 287 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 287 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 287 ▶use v5.18;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 287 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 287 our @EXPORT = qw(
11 $WS
12 $CRLF
13 $EOL
14 $Comment
15 $NonASCII
16  
17 $BareKey
18 $QuotedKey
19 $SimpleKey
20 $DottedKey
21 $Key
22  
23 $Boolean
24  
25 $Escape
26 $StringLiteral
27 $MultiLineStringLiteral
28 $BasicString
29 $MultiLineString
30 $String
31  
32 $Date
33 $Time
34 $DateTime
35 $TimeOffset
36  
37 $Hex
38 $Oct
39 $Bin
40 $Dec
41 $Integer
42  
43 $Float
44 $SpecialFloat
45 );
46  
47 #-------------------------------------------------------------------------------
48 # Primitives
49 #-------------------------------------------------------------------------------
50 287 our $WS = qr/[\x20\x09]/; # space, tab
51 287 our $CRLF = qr/\x0D?\x0A/; # cr? lf
52 287 our $CommentChar = qr/(?>[^[:cntrl:]]|\t)/; # non-control chars other than tab
53 287 our $Comment = qr/\x23$CommentChar*/; # #comment
54 287 our $EOL = qr/$Comment?$CRLF/; # crlf or comment + crlf
55 287 our $Boolean = qr/\b(?:true)|(?:false)\b/;
56 287 our $NonASCII = qr/[\x80-\x{D7FF}\x{E000}-\x{10FFFF}]/;
57  
58 #-------------------------------------------------------------------------------
59 # Strings
60 #-------------------------------------------------------------------------------
61 287 our $Escape = qr{
62 \x5C # leading \
63 (?>
64 [\x5C"btfnr] # escapes: \\ \" \b \t \n \f \r
65 | (?> u [_0-9a-fA-F]{4}) # unicode (4 bytes)
66 | (?> U [_0-9a-fA-F]{8}) # unicode (8 bytes)
67 )
68 }x;
69  
70 287 our $LiteralChar = qr{ [\x09\x20-\x26\x28-\x7E] | $NonASCII }x;
71 287 our $StringLiteral = qr{ ' (?: $LiteralChar )* ' }x;
72  
73 287 our $MLLChar = qr{ [\x09\x20-\x26\x28-\x7E] | $NonASCII }x;
74 287 our $MLLContent = qr{ $MLLChar | $CRLF }x;
75 287 our $MLLQuotes = qr{ '{1,2} }x;
76 287 our $MLLBody = qr{ $MLLContent* (?: $MLLQuotes | $MLLContent{0,1} )*? $MLLQuotes? }x;
77 287 our $MultiLineStringLiteral = qr{ ''' (?: $CRLF? $MLLBody ) ''' }x;
78  
79 287 our $BasicChar = qr{ $WS | [\x21\x23-\x5B\x5D-\x7E] | $NonASCII | $Escape }x;
80 287 our $BasicString = qr{ " (?: $BasicChar )* " }x;
81  
82 287 our $MLBEscapedNL = qr{ \x5c $WS* $CRLF (?: $WS | $CRLF)* }x;
83 287 our $MLBUnescaped = qr{ $WS | [\x21\x23-\x5B\x5D-\x7E] | $NonASCII }x;
84 287 our $MLBQuotes = qr{ "{1,2} }x;
85 287 our $MLBChar = qr{ $MLBUnescaped | $Escape }x;
86 287 our $MLBContent = qr{ $MLBChar | $CRLF | $MLBEscapedNL }x;
87 287 our $MLBasicBody = qr{ $MLBContent* (?: $MLBQuotes | $MLBContent{0,1} )*? $MLBQuotes? }x;
88 287 our $MultiLineString = qr{ """ $CRLF? $MLBasicBody """ }x;
89  
90 287 our $String = qr/$MultiLineString | $BasicString | $MultiLineStringLiteral | $StringLiteral/x;
91  
92 #-------------------------------------------------------------------------------
93 # Keys
94 #-------------------------------------------------------------------------------
95 287 our $BareKey = qr/[-_\p{PosixAlnum}]+/;
96 287 our $QuotedKey = qr/$BasicString|$StringLiteral/;
97 287 our $SimpleKey = qr/$QuotedKey|$BareKey/;
98 287 our $DottedKey = qr/$SimpleKey (?: $WS* \. $WS* $SimpleKey)+/x;
99 287 our $Key = qr{ (?: $DottedKey | $SimpleKey ) }x;
100  
101  
102 #-----------------------------------------------------------------------------
103 # Dates (RFC 3339)
104 # 1985-04-12T23:20:50.52Z
105 #-----------------------------------------------------------------------------
106 287 our $DateFullYear = qr{ \d{4} }x;
107 287 our $DateMonth = qr{ (?: 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 ) }x;
108 287 our $DateDay = qr{ (?: 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 ) }x;
109 287 our $TimeDelim = qr{ (?: [tT] | \x20 ) }x;
110 287 our $TimeHour = qr{ (?: 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 ) }x;
111 287 our $TimeMinute = qr{ (?: 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 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 ) }x;
112 287 our $TimeSecond = qr{ (?: 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 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 ) }x; # may be 60 during leap second
113 287 our $TimeSecFrac = qr{ \. \d+ }x;
114 287 our $TimeNumOffset = qr{ (?: [-+] $TimeHour : $TimeMinute ) }x;
115 287 our $TimeOffset = qr{ (?: [zZ] | $TimeNumOffset ) }x;
116  
117 287 our $PartialTime = qr{ (?: $TimeHour : $TimeMinute : $TimeSecond $TimeSecFrac? ) }x;
118 287 our $FullTime = qr{ (?: $PartialTime $TimeOffset ) }x;
119 287 our $FullDate = qr{ (?: $DateFullYear - $DateMonth - $DateDay ) }x;
120  
121 287 our $OffsetDateTime = qr{ (?: $FullDate $TimeDelim $FullTime ) }x;
122 287 our $LocalDateTime = qr{ (?: $FullDate $TimeDelim $PartialTime ) }x;
123 287 our $LocalDate = qr{ (?: $FullDate ) }x;
124 287 our $LocalTime = qr{ (?: $PartialTime ) }x;
125 287 our $DateTime = qr{ (?: $OffsetDateTime | $LocalDateTime | $LocalDate | $LocalTime ) }x;
126  
127 #-----------------------------------------------------------------------------
128 # Integer
129 #-----------------------------------------------------------------------------
130 287 our $DecFirstChar = qr/[1-9]/;
131 287 our $DecChar = qr/[0-9]/;
132 287 our $HexChar = qr/[0-9a-fA-F]/;
133 287 our $OctChar = qr/[0-7]/;
134 287 our $BinChar = qr/[01]/;
135  
136 287 our $Zero = qr/[-+]? 0/x;
137 287 our $Hex = qr/0x $HexChar (?> _? $HexChar )*/x;
138 287 our $Oct = qr/0o $OctChar (?> _? $OctChar )*/x;
139 287 our $Bin = qr/0b $BinChar (?> _? $BinChar )*/x;
140 287 our $Dec = qr/$Zero | (?> [-+]? $DecFirstChar (?> _? $DecChar )* )/x;
141 287 our $Integer = qr/$Hex | $Oct | $Bin | $Dec/x;
142  
143 #-----------------------------------------------------------------------------
144 # Float
145 #-----------------------------------------------------------------------------
146 287 our $SpecialFloat = qr/[-+]? (?: (?:inf) | (?:nan) | (?:NaN) )/x;
147 287 our $Fraction = qr/\. $DecChar (?> _? $DecChar)*/x;
148  
149 287 our $Exponent = qr{
150 [eE]
151 (?>
152 $Zero+ # dec matches only one zero, but toml exponents apparently accept e00
153 | $Dec
154 )
155 }x;
156  
157 287 our $Float = qr{
158 (?> $Dec (?> (?> $Fraction $Exponent?) | $Exponent ) )
159 | $SpecialFloat
160 }x;
161  
162  
163 287 1;
164  
165 __END__
166  
167 =pod
168  
169 =encoding UTF-8
170  
171 =head1 NAME
172  
173 TOML::Tiny::Grammar - exports regex definitions used to parse TOML source
174  
175 =head1 VERSION
176  
177 version 0.21
178  
179 =head1 SYNOPSIS
180  
181 use TOML::Tiny::Grammar;
182  
183 if ($src =~ /$MultiLineString/) {
184 ...
185 }
186  
187 =head1 DESCRIPTION
188  
189 Exports various regexex for parsing TOML source.
190  
191 =head1 PATTERNS
192  
193 =head2 White space and ignorables
194  
195 =head3 $WS
196  
197 =head3 $CRLF
198  
199 =head3 $EOL
200  
201 =head3 $Comment
202  
203 =head2 Keys
204  
205 =head3 $BareKey
206  
207 =head3 $QuotedKey
208  
209 =head3 $SimpleKey
210  
211 =head3 $DottedKey
212  
213 =head3 $Key
214  
215 =head2 Values
216  
217 =head3 $Boolean
218  
219 =head3 $Escape
220  
221 =head3 $StringLiteral
222  
223 =head3 $MultiLineStringLiteral
224  
225 =head3 $BasicString
226  
227 =head3 $MultiLineString
228  
229 =head3 $String
230  
231 =head3 $Date
232  
233 =head3 $Time
234  
235 =head3 $DateTime
236  
237 =head3 $Hex
238  
239 =head3 $Oct
240  
241 =head3 $Bin
242  
243 =head3 $Dec
244  
245 =head3 $Integer
246  
247 =head3 $Float
248  
249 =head2 $SpecialFloat
250  
251 =head1 AUTHOR
252  
253 Jeff Ober <sysread@fastmail.fm>
254  
255 =head1 COPYRIGHT AND LICENSE
256  
257 This software is copyright (c) 2026 by Jeff Ober.
258  
259 This is free software; you can redistribute it and/or modify it under
260 the same terms as the Perl 5 programming language system itself.
261  
262 =cut
« Tiny.pm ↑ Summary