1
|
|
|
<?php /** @noinspection PhpUndefinedFieldInspection */
|
2
|
|
|
|
3
|
|
|
/**
|
4
|
|
|
* RFC 2234 - Augmented BNF for Syntax Specifications: ABNF
|
5
|
|
|
*
|
6
|
|
|
* Obsoleted by RFC 4234
|
7
|
|
|
*
|
8
|
|
|
* @see https://tools.ietf.org/html/rfc2234
|
9
|
|
|
* @package Vanderlee\Comprehend\Library
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
namespace Vanderlee\Comprehend\Library;
|
13
|
|
|
|
14
|
|
|
use Vanderlee\Comprehend\builder\AbstractRuleset;
|
15
|
|
|
use Vanderlee\Comprehend\Parser\Parser;
|
16
|
|
|
|
17
|
|
|
require_once 'functions.php';
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* @property-read Parser ALPHA Alphabetic characters (upper- and lowercase)
|
21
|
|
|
* @property-read Parser DIGIT Decimal character
|
22
|
|
|
* @property-read Parser HEXDIG Hexadecimal character
|
23
|
|
|
* @property-read Parser BIT Binary digit
|
24
|
|
|
* @property-read Parser SB Whitespace
|
25
|
|
|
*
|
26
|
|
|
* @package Vanderlee\Comprehend\Library
|
27
|
|
|
*/
|
28
|
|
|
class Rfc2234 extends AbstractRuleset
|
29
|
|
|
{
|
30
|
|
|
protected static $name = 'Rfc2234';
|
31
|
|
|
|
32
|
|
|
public function __construct($overwrites = [])
|
33
|
|
|
{
|
34
|
|
|
/*
|
35
|
|
|
* Support rules.
|
36
|
|
|
* These are not part of the published specification, but help make the published rules more manageable without
|
37
|
|
|
* altering meaning or syntax. They exist outside the named scope.
|
38
|
|
|
*/
|
39
|
|
|
$hexdigs = plus($this->HEXDIG);
|
40
|
|
|
$digits = plus($this->DIGIT);
|
41
|
|
|
$bits = plus($this->BIT);
|
42
|
|
|
$c_wsps = star($this->c_wsp);
|
|
|
|
|
43
|
|
|
|
44
|
|
|
/*
|
45
|
|
|
* Normal rules
|
46
|
|
|
*/
|
47
|
|
|
$rules = [
|
48
|
|
|
/*
|
49
|
|
|
* Core rules
|
50
|
|
|
*/
|
51
|
|
|
'ALPHA' => set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'),
|
52
|
|
|
'BIT' => set('01'),
|
53
|
|
|
'CHAR' => range(0x01, 0x7F),
|
54
|
|
|
'CR' => char("\r"),
|
55
|
|
|
'CRLF' => text("\r\n"),
|
56
|
|
|
'DIGIT' => set('0123456789'),
|
57
|
|
|
'DQUOTE' => char('"'),
|
58
|
|
|
'HEXDIG' => set('0123456789ABCDEF'),
|
59
|
|
|
'HTAB' => char("\t"),
|
60
|
|
|
'LF' => char("\n"),
|
61
|
|
|
'LWSP' => regex("/(?:[ \t]|(?:\r\n[ \t]))*/"),
|
62
|
|
|
'OCTET' => range(0x00, 0xFF),
|
63
|
|
|
'SP' => char(' '),
|
64
|
|
|
'VCHAR' => range(0x21, 0x7E), // ['!', '~']
|
65
|
|
|
'WSP' => set(" \t"),
|
66
|
|
|
|
67
|
|
|
/*
|
68
|
|
|
* Definition of ABNF syntax (in reverse order for performance)
|
69
|
|
|
*/
|
70
|
|
|
'prose_val' => s('<', star(c(range(0x20, 0x3D), range(0x3F, 0x7E))), '>'),
|
71
|
|
|
'hex_val' => s('x', $hexdigs, opt(c(plus(['.', $hexdigs]), ['-', $hexdigs]))),
|
72
|
|
|
'dec_val' => s('d', $digits, opt(c(plus(['.', $digits]), ['-', $digits]))),
|
73
|
|
|
'bin_val' => s('b', $bits, opt(c(plus(['.', $bits]), ['-', $bits]))),
|
74
|
|
|
'num_val' => s('%', [$this->bin_val, $this->dec_val, $this->hex_val]),
|
|
|
|
|
75
|
|
|
'char_val' => s($this->DQUOTE, star(c(range(0x20, 0x21), range(0x23, 0x7E))), $this->DQUOTE),
|
|
|
|
|
76
|
|
|
'option' => s('[', $c_wsps, $this->alternation, $c_wsps, ']'),
|
|
|
|
|
77
|
|
|
'group' => s('(', $c_wsps, $this->alternation, $c_wsps, ')'),
|
78
|
|
|
'element' => c($this->rulename, $this->group, $this->option, $this->char_val, $this->num_val,
|
|
|
|
|
79
|
|
|
$this->prose_val),
|
|
|
|
|
80
|
|
|
'repeat' => c($digits, [star($this->digit), '*', star($this->digit)]),
|
|
|
|
|
81
|
|
|
'repetition' => s(opt($this->repeat), $this->element),
|
|
|
|
|
82
|
|
|
'concatenation' => s($this->repetition, star([plus($this->c_wsp), $this->repetition])),
|
|
|
|
|
83
|
|
|
'alternation' => s($this->concatenation, star([$c_wsps, '/', $c_wsps, $this->concatenation])),
|
|
|
|
|
84
|
|
|
'comment' => s(';', star(c($this->WSP, $this->VCHAR)), $this->CRLF),
|
|
|
|
|
85
|
|
|
'c_nl' => c($this->comment, $this->CRLF),
|
|
|
|
|
86
|
|
|
'c_wsp' => c($this->WSP, [$this->c_nl, $this->WSP]),
|
|
|
|
|
87
|
|
|
'elements' => [$this->alternation, $c_wsps],
|
88
|
|
|
'defined_as' => [$c_wsps, ['=', '=/'], $c_wsps],
|
89
|
|
|
'rulename' => s($this->ALPHA, star(c($this->ALPHA, $this->DIGIT, '-'))),
|
90
|
|
|
'rule' => s($this->rulename, $this->defined_as, $this->elements, $this->c_nl),
|
|
|
|
|
91
|
|
|
'rulelist' => plus(c($this->rule, [$c_wsps, $this->c_nl])),
|
|
|
|
|
92
|
|
|
|
93
|
|
|
self::DEFAULT => $this->rulelist,
|
|
|
|
|
94
|
|
|
];
|
95
|
|
|
|
96
|
|
|
parent::__construct(array_merge($rules, $overwrites));
|
97
|
|
|
}
|
98
|
|
|
} |