-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.js
159 lines (130 loc) · 5.35 KB
/
grammar.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
module.exports = grammar({
name: 'http',
extras: $ => [],
rules: {
source_file: $ => optional(
choice(
seq(
$._start_line,
repeat($.header),
alias($._form_content, $.header),
repeat($.header),
optional(seq($._newline, $.form_body)),
),
seq(
$._start_line,
repeat($.header),
alias($._multipart_content, $.header),
repeat($.header),
optional(seq($._newline, $.multipart_body)),
),
seq(
$._start_line,
repeat($.header),
alias($._html_content, $.header),
repeat($.header),
optional(seq($._newline, $.html_body)),
),
seq(
$._start_line,
repeat($.header),
alias($._css_content, $.header),
repeat($.header),
optional(seq($._newline, $.css_body)),
),
seq(
$._start_line,
repeat($.header),
alias($._javascript_content, $.header),
repeat($.header),
optional(seq($._newline, $.javascript_body)),
),
seq(
$._start_line,
repeat($.header),
alias($._json_content, $.header),
repeat($.header),
optional(seq($._newline, $.json_body)),
),
seq(
$._start_line,
repeat($.header),
alias($._xml_content, $.header),
repeat($.header),
optional(seq($._newline, $.xml_body)),
),
seq(
$._start_line,
repeat($.header),
alias($._other_content, $.header),
repeat($.header),
optional(seq($._newline, $.body)),
),
seq(
$._start_line,
repeat1($.header),
optional(seq($._newline, $.body)),
),
)
),
_start_line: $ => choice($.request_line, $.status_line),
request_line: $ =>
seq($.method, $._space, $._target, $._space, $.version, $._newline),
method: _ => choice('GET','PUT','ACL','HEAD','POST','COPY','LOCK','MOVE','BIND','LINK','PATCH','TRACE','MKCOL','MERGE','PURGE','NOTIFY','SEARCH','UNLOCK','REBIND','UNBIND','REPORT','DELETE','UNLINK','CONNECT','MSEARCH','OPTIONS','PROPFIND','CHECKOUT','PROPPATCH','SUBSCRIBE','MKCALENDAR','MKACTIVITY','UNSUBSCRIBE','SOURCE'),
_target: $ =>
seq($._path, optional(seq('?', $.query_item, repeat(seq('&', $.query_item)))), optional($.hash)),
_path: $ =>
repeat1(seq('/', $.path_component)),
path_component: _ => /[^ /?#]*/,
query_item: $ =>
seq($.query_name, optional(seq('=', $.query_value))),
query_name: _ => /[^ =&#]+/,
query_value: _ => /[^ &#]*/,
hash: _ => /#\w*/,
version: _ => /HTTP\/\d\.\d/,
status_line: $ =>
seq($.version, $._space, $.status, $._space, $.reason, $._newline),
status: _ => /\d+/,
reason: _ => /.+/,
_form_content: $ =>
seq(alias($._content_type_name, $.header_name), ':', $._whitespace, alias($._form_header_value, $.header_value), $._newline),
_multipart_content: $ =>
seq(alias($._content_type_name, $.header_name), ':', $._whitespace, alias($._multipart_header_value, $.header_value), $._newline),
_html_content: $ =>
seq(alias($._content_type_name, $.header_name), ':', $._whitespace, alias($._html_header_value, $.header_value), $._newline),
_css_content: $ =>
seq(alias($._content_type_name, $.header_name), ':', $._whitespace, alias($._css_header_value, $.header_value), $._newline),
_javascript_content: $ =>
seq(alias($._content_type_name, $.header_name), ':', $._whitespace, alias($._javascript_header_value, $.header_value), $._newline),
_json_content: $ =>
seq(alias($._content_type_name, $.header_name), ':', $._whitespace, alias($._json_header_value, $.header_value), $._newline),
_xml_content: $ =>
seq(alias($._content_type_name, $.header_name), ':', $._whitespace, alias($._xml_header_value, $.header_value), $._newline),
_other_content: $ =>
seq(alias($._content_type_name, $.header_name), ':', $._whitespace, $.header_value, $._newline),
header: $ =>
seq($.header_name, ':', $._whitespace, $.header_value, $._newline),
_content_type_name: _ => /[Cc]ontent-[Tt]ype/,
_form_header_value: _ => /application\/x-www-form-urlencoded.*/,
_multipart_header_value: _ => /multipart\/.*/,
_html_header_value: _ => /text\/html.*/,
_css_header_value: _ => /text\/css.*/,
_javascript_header_value: _ => choice(/application\/javascript.*/, /application\/x-javascript.*/, /text\/javascript.*/),
_json_header_value: _ => /application\/json.*/,
_xml_header_value: _ => choice(/text\/xml.*/, /application\/xml.*/, /application\/xhtml\+xml.*/, /image\/svg\+xml.*/),
header_name: _ => /[A-Za-z0-9-_]+/,
header_value: _ => /.*/,
body: $ => repeat1(choice($._data, $._newline)),
form_body: $ => repeat1(choice($._data, $._newline)),
multipart_body: $ => repeat1(choice($._data, $._newline)),
html_body: $ => repeat1(choice($._data, $._newline)),
css_body: $ => repeat1(choice($._data, $._newline)),
javascript_body: $ => repeat1(choice($._data, $._newline)),
json_body: $ => repeat1(choice($._data, $._newline)),
xml_body: $ => repeat1(choice($._data, $._newline)),
_whitespace: _ => /\s+/,
_space: _ => ' ',
_newline: _ => /\r?\n/,
_data: _ => /.+/,
}
});