1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators.short-circuit math math.order math.parser
4 kernel sequences sequences.deep peg peg.parsers assocs arrays
5 hashtables strings unicode.case namespaces make ascii ;
8 : except ( quot -- parser )
9 [ not ] compose satisfy ; inline
11 : except-these ( quots -- parser )
12 [ 1|| ] curry except ; inline
15 { [ 0 31 between? ] [ 127 = ] } 1|| ;
17 : tspecial? ( ch -- ? )
18 "()<>@,;:\\\"/[]?={} \t" member? ;
20 : 'token' ( -- parser )
21 { [ ctl? ] [ tspecial? ] } except-these repeat1 ;
23 : case-insensitive ( parser -- parser' )
24 [ flatten >string >lower ] action ;
26 : case-sensitive ( parser -- parser' )
27 [ flatten >string ] action ;
29 : 'space' ( -- parser )
30 [ " \t" member? ] satisfy repeat0 hide ;
32 : one-of ( strings -- parser )
33 [ token ] map choice ;
35 : 'http-method' ( -- parser )
36 { "OPTIONS" "GET" "HEAD" "POST" "PUT" "DELETE" "TRACE" "CONNECT" } one-of ;
39 [ " \t\r\n" member? ] except repeat1 case-sensitive ;
41 : 'http-version' ( -- parser )
50 ] seq* [ concat >string ] action ;
52 PEG: parse-request-line ( string -- triple )
53 #! Triple is { method url version }
64 : 'text' ( -- parser )
67 : 'response-code' ( -- parser )
68 [ digit? ] satisfy 3 exactly-n [ string>number ] action ;
70 : 'response-message' ( -- parser )
71 'text' repeat0 case-sensitive ;
73 PEG: parse-response-line ( string -- triple )
74 #! Triple is { version code message }
84 : 'crlf' ( -- parser )
88 [ " \t" member? ] satisfy repeat1 ;
90 : 'qdtext' ( -- parser )
91 { [ CHAR: " = ] [ ctl? ] } except-these ;
93 : 'quoted-char' ( -- parser )
94 "\\" token hide any-char 2seq ;
96 : 'quoted-string' ( -- parser )
97 'quoted-char' 'qdtext' 2choice repeat0 "\"" "\"" surrounded-by ;
99 : 'ctext' ( -- parser )
100 { [ ctl? ] [ "()" member? ] } except-these ;
102 : 'comment' ( -- parser )
103 'ctext' 'comment' 2choice repeat0 "(" ")" surrounded-by ;
105 : 'field-name' ( -- parser )
106 'token' case-insensitive ;
108 : 'field-content' ( -- parser )
109 'quoted-string' case-sensitive
110 'text' repeat0 case-sensitive
113 PEG: parse-header-line ( string -- pair )
114 #! Pair is either { name value } or { f value }. If f, its a
115 #! continuation of the previous header line.
124 'lws' [ drop f ] action ,
129 : 'word' ( -- parser )
130 'token' 'quoted-string' 2choice ;
132 : 'value' ( -- parser )
134 [ ";" member? ] except repeat0
135 2choice case-sensitive ;
137 : 'attr' ( -- parser )
138 'token' case-insensitive ;
140 : 'av-pair' ( -- parser )
145 [ "=" token , 'space' , 'value' , ] seq* [ peek ] action optional ,
149 : 'av-pairs' ( -- parser )
150 'av-pair' ";" token list-of optional ;
152 PEG: (parse-set-cookie) ( string -- alist )
153 'av-pairs' just [ sift ] action ;
155 : 'cookie-value' ( -- parser )
165 [ ";,=" member? not ] satisfy repeat1 [ drop f ] action
168 PEG: (parse-cookie) ( string -- alist )
169 'cookie-value' [ ";," member? ] satisfy list-of
170 optional just [ sift ] action ;