/
parser.pegjs
124 lines (111 loc) · 2.58 KB
/
parser.pegjs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// User-Agent = product *( RWS ( product / comment ) )
// https://tools.ietf.org/html/rfc7231#section-5.5.3
userAgent
= firstProduct : product
restWithWhitespace : ( RWS ( product / comment ) ) *
{
const rest = restWithWhitespace
.map(entry => {
const result = entry[1]
if (typeof result === 'string') {
return {
type: 'comment',
text: result.substring(1, result.length - 1)
}
} else {
return result
}
})
return [firstProduct].concat(rest)
}
// product = token ["/" product-version]
// product-version = token
// https://tools.ietf.org/html/rfc7231#section-5.5.3
product
= product : token
versionWithSlash : ( '/' token ) ?
{
const version = versionWithSlash ? versionWithSlash[1] : null
return {
type: 'product',
product,
version
}
}
// token = 1*tchar
// https://tools.ietf.org/html/rfc7230#section-3.2.6
token
= token : ( tchar + )
{ return token.join('') }
// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
// / DIGIT / ALPHA
// ; any VCHAR, except delimiters
// https://tools.ietf.org/html/rfc7230#section-3.2.6
tchar
= '!'
/ '#'
/ '$'
/ '%'
/ '&'
/ "'"
/ '*'
/ '+'
/ '-'
/ '.'
/ '^'
/ '_'
/ '`'
/ '|'
/ '~'
/ DIGIT
/ ALPHA
// comment = "(" *( ctext / quoted-pair / comment ) ")"
// https://tools.ietf.org/html/rfc7230#section-3.2.6
comment
= '(' text : ( ctext / quotedPair / comment ) * ')'
{ return '(' + text.join('') + ')' }
// ctext = HTAB / SP / %x21-27 / %x2A-5B / %x5D-7E / obs-text
// https://tools.ietf.org/html/rfc7230#section-3.2.6
ctext
= HTAB
/ SP
/ [\x21-\x27]
/ [\x2A-\x5B]
/ [\x5D-\x7E]
/ obsText
// obs-text = %x80-FF
// https://tools.ietf.org/html/rfc7230#section-3.2.6
obsText
= [\x80-\xFF]
// quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
// https://tools.ietf.org/html/rfc7230#section-3.2.6
quotedPair
= '\\' char : ( HTAB / SP / VCHAR / obsText )
{ return '\\' + char }
// HTAB = %x09
// ; horizontal tab
// https://tools.ietf.org/html/rfc5234#appendix-B.1
HTAB
= '\t'
// SP = %x20
// https://tools.ietf.org/html/rfc5234#appendix-B.1
SP
= ' '
// DIGIT = %x30-39
// ; 0-9
// https://tools.ietf.org/html/rfc5234#appendix-B.1
DIGIT
= [0-9]
// ALPHA = %x41-5A / %x61-7A
// ; A-Z / a-z
ALPHA
= [a-zA-Z]
// VCHAR = %x21-7E
// ; visible (printing) characters
VCHAR
= [\x21-\x7E]
// RWS = 1*( SP / HTAB )
// https://tools.ietf.org/html/rfc7230#appendix-B
RWS
= ( HTAB / SP ) +