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