]> gitweb.factorcode.org Git - factor.git/blob - basis/xml/tokenize/tokenize.factor
factor: trim using lists
[factor.git] / basis / xml / tokenize / tokenize.factor
1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors ascii assocs combinators
4 combinators.short-circuit hints io kernel math math.parser
5 namespaces sbufs sequences splitting strings xml.char-classes
6 xml.entities xml.errors xml.state ;
7 IN: xml.tokenize
8
9 ! * Basic utility words
10
11 : assure-good-char ( spot ch -- )
12     [
13         over {
14             [ version-1.0?>> over text? not ]
15             [ check>> ]
16         } 1&&
17         [
18             [ [ 1 + ] change-column drop ] dip
19             disallowed-char
20         ] [ 2drop ] if
21     ] [ drop ] if* ;
22
23 HINTS: assure-good-char { spot fixnum } ;
24
25 : record ( spot char -- spot )
26     over char>> [
27         CHAR: \n eq?
28         [ [ 1 + ] change-line -1 ] [ dup column>> 1 + ] if
29         >>column
30     ] [ drop ] if ;
31
32 HINTS: record { spot fixnum } ;
33
34 :: (next) ( spot -- spot char )
35     spot next>> :> old-next
36     spot stream>> stream-read1 :> new-next
37     old-next CHAR: \r eq? [
38         spot CHAR: \n >>char
39         new-next CHAR: \n eq?
40         [ spot stream>> stream-read1 >>next ]
41         [ new-next >>next ] if
42     ] [ spot old-next >>char new-next >>next ] if
43     spot next>> ; inline
44
45 : next* ( spot -- )
46     dup char>> [ unexpected-end ] unless
47     (next) [ record ] keep assure-good-char ;
48
49 HINTS: next* { spot } ;
50
51 : next ( -- )
52     spot get next* ;
53
54 : init-parser ( -- )
55     0 1 0 0 f t f <spot>
56         input-stream get >>stream
57         read1 >>next
58     spot set next ;
59
60 : with-state ( stream quot -- )
61     ! with-input-stream implicitly creates a new scope which we use
62     swap [ init-parser call ] with-input-stream ; inline
63
64 :: (skip-until) ( ... quot: ( ... char -- ... ? ) spot -- ... )
65     spot char>> [
66         quot call [
67             spot next* quot spot (skip-until)
68         ] unless
69     ] when* ; inline recursive
70
71 : skip-until ( ... quot: ( ... char -- ... ? ) -- ... )
72     spot get (skip-until) ; inline
73
74 : take-until ( ... quot: ( ... char -- ... ? ) -- ... string )
75     ! Take the substring of a string starting at spot
76     ! from code until the quotation given is true and
77     ! advance spot to after the substring.
78    10 <sbuf> [
79        '[ _ keep over [ drop ] [ _ push ] if ] skip-until
80    ] keep "" like ; inline
81
82 : take-to ( seq -- string )
83     '[ _ member? ] take-until ; inline
84
85 : pass-blank ( -- )
86     ! Advance code past any whitespace, including newlines
87     [ blank? not ] skip-until ;
88
89 : next-matching ( pos ch str -- pos' )
90     overd nth eq? [ 1 + ] [ drop 0 ] if ; inline
91
92 : string-matcher ( str -- quot: ( pos char -- pos ? ) )
93     dup length 1 - '[ _ next-matching dup _ > ] ; inline
94
95 :: (take-string) ( match spot -- sbuf matched? )
96     10 <sbuf> f [
97         spot char>> [
98             nip over push
99             spot next*
100             dup match tail? dup not
101         ] [ f ] if*
102     ] loop ; inline
103
104 : take-string ( match -- string )
105     [ spot get (take-string) [ missing-close ] unless ]
106     [ dupd [ length ] bi@ - over shorten "" like ] bi ;
107
108 : expect ( string -- )
109     dup length spot get '[ _ [ char>> ] keep next* ] "" replicate-as
110     2dup = [ 2drop ] [ expected ] if ;
111
112 ! Suddenly XML-specific
113
114 : parse-named-entity ( accum string -- )
115     dup entities at [ swap push ] [
116         dup extra-entities get at
117         [ swap push-all ] [ no-entity ] ?if
118     ] ?if ;
119
120 : take-; ( -- string )
121     next ";" take-to next ;
122
123 : parse-entity ( accum -- )
124     take-; "#" ?head [
125         "x" ?head 16 10 ? base> swap push
126     ] [ parse-named-entity ] if ;
127
128 : parse-pe ( accum -- )
129     take-; dup pe-table get at
130     [ swap push-all ] [ no-entity ] ?if ;
131
132 :: (parse-char) ( quot: ( ch -- ? ) accum spot -- )
133     spot char>> :> char
134     {
135         { [ char not ] [ ] }
136         { [ char quot call ] [ spot next* ] }
137         { [ char CHAR: & eq? ] [
138             accum parse-entity
139             quot accum spot (parse-char)
140         ] }
141         { [ char CHAR: % eq? [ in-dtd? get ] [ f ] if ] [
142             accum parse-pe
143             quot accum spot (parse-char)
144         ] }
145         [
146             char accum push
147             spot next*
148             quot accum spot (parse-char)
149         ]
150     } cond ; inline recursive
151
152 : parse-char ( quot: ( ch -- ? ) -- seq )
153     512 <sbuf> [ spot get (parse-char) ] keep "" like ; inline
154
155 : assure-no-]]> ( pos char -- pos' )
156     "]]>" next-matching dup 2 > [ text-w/]]> ] when ; inline
157
158 :: parse-text ( -- string )
159     depth get zero? :> no-text
160     0 :> pos!
161     [| char |
162         pos char assure-no-]]> pos!
163         no-text [
164             char blank? char CHAR: < eq? or [
165                 char 1string t pre/post-content
166             ] unless
167         ] when
168         char CHAR: < eq?
169     ] parse-char ;
170
171 : close ( -- )
172     pass-blank ">" expect ;
173
174 : normalize-quote ( str -- str )
175     [ dup "\t\r\n" member? [ drop CHAR: \s ] when ] map! ;
176
177 : (parse-quote) ( <-disallowed? ch -- string )
178     swap '[
179         dup _ eq? [ drop t ]
180         [ CHAR: < eq? _ and [ attr-w/< ] [ f ] if ] if
181     ] parse-char normalize-quote get-char
182     [ unclosed-quote ] unless ; inline
183
184 : parse-quote* ( <-disallowed? -- seq )
185     pass-blank get-char dup "'\"" member?
186     [ next (parse-quote) ] [ quoteless-attr ] if ; inline
187
188 : parse-quote ( -- seq )
189    f parse-quote* ;