Line data Source code
1 : // Copyright 2026 The Authors. See the AUTHORS file for details.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // https://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : /// ABNF core rules as defined in RFC 5234, Appendix B.1.
16 : library;
17 :
18 : import 'grammar.dart';
19 :
20 12 : final alphaLower = rule('ALPHA-LOWER', valueRange(0x61, 0x7A));
21 :
22 8 : final alphaUpper = rule('ALPHA-UPPER', valueRange(0x41, 0x5A));
23 :
24 14 : final alpha = rule('ALPHA', alternatives([alphaLower, alphaUpper]));
25 :
26 5 : final bit = rule('BIT', alternativeLiterals(['0', '1']));
27 :
28 4 : final char = rule('CHAR', valueRange(0x01, 0xFF));
29 :
30 4 : final cr = rule('CR', character(0x0D));
31 :
32 7 : final crlf = rule('CRLF', Concatenation([cr, lf]));
33 :
34 : /// Controls characters (range %x00-1F / %x7F).
35 3 : final ctl = rule(
36 : 'CTL',
37 4 : alternatives([valueRange(0x00, 0x1F), character(0x7f)]),
38 : );
39 :
40 8 : final digit = rule('DIGIT', valueRange(0x30, 0x39));
41 :
42 7 : final alphanum = rule('ALPHANUM', alternatives([alpha, digit]));
43 :
44 4 : final dquote = rule('DQUOTE', character(0x22));
45 :
46 6 : final hexletters = rule(
47 : 'HEX-LETTERS',
48 : // alternativeLiterals is case-insensitive by default (RFC 5234 §2.3),
49 : // so uppercase A–F are matched automatically.
50 4 : alternativeLiterals(['a', 'b', 'c', 'd', 'e', 'f']),
51 : );
52 :
53 14 : final hexdig = rule('HEXDIG', alternatives([hexletters, digit]));
54 :
55 4 : final htab = rule('HTAB', character(0x09));
56 :
57 4 : final lf = rule('LF', character(0x0A));
58 :
59 4 : final sp = rule('SP', character(0x20));
60 :
61 : /// Visible (printing) characters (range %x21-7E).
62 4 : final vchar = rule('VCHAR', valueRange(0x21, 0x7E));
63 :
64 7 : final wsp = rule('WSP', alternatives([sp, htab]));
65 :
66 5 : final optionalWsp = rule('Optional-WSP', variableRepetition(wsp));
|