1 ! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators continuations kernel
4 kernel.private lexer math math.parser namespaces sbufs sequences
8 ERROR: bad-escape char ;
10 : escape ( escape -- ch )
25 } ?at [ bad-escape ] unless ;
27 SYMBOL: name>char-hook
30 [ "Unicode support not available" throw ]
33 : hex-escape ( str -- ch str' )
34 2 cut-slice [ hex> ] dip ;
36 : unicode-escape ( str -- ch str' )
38 CHAR: } over index cut-slice [
42 >string name>char-hook get call( name -- char )
46 6 cut-slice [ hex> ] dip
49 : next-escape ( str -- ch str' )
51 { CHAR: u [ unicode-escape ] }
52 { CHAR: x [ hex-escape ] }
53 { CHAR: \n [ f swap ] }
59 : (unescape-string) ( accum str i/f -- accum )
60 { sbuf object object } declare
62 cut-slice [ append! ] dip
63 rest-slice next-escape [ [ suffix! ] when* ] dip
64 CHAR: \\ over index (unescape-string)
71 : unescape-string ( str -- str' )
73 [ [ length <sbuf> ] keep ] dip (unescape-string)
78 : lexer-subseq ( i lexer -- before )
79 { fixnum lexer } declare
80 [ [ column>> ] [ line-text>> ] bi swapd subseq ]
83 : rest-of-line ( lexer -- seq )
85 [ line-text>> ] [ column>> ] bi tail-slice ;
87 : current-char ( lexer -- ch/f )
89 [ column>> ] [ line-text>> ] bi ?nth ;
91 : advance-char ( lexer -- )
93 [ 1 + ] change-column drop ;
95 : next-char ( lexer -- ch/f )
97 dup still-parsing-line? [
98 [ current-char ] [ advance-char ] bi
103 : next-line% ( accum lexer -- )
104 { sbuf lexer } declare
105 [ rest-of-line swap push-all ] [ next-line ] bi ;
107 : find-next-token ( lexer -- i elt )
109 [ column>> ] [ line-text>> ] bi
110 [ "\"\\" member-eq? ] find-from ;
112 DEFER: (parse-string)
114 : parse-found-token ( accum lexer i elt -- )
115 { sbuf lexer fixnum fixnum } declare
116 [ over lexer-subseq pick push-all ] dip
118 dup dup [ next-char ] bi@
119 [ [ pick push ] bi@ ]
120 [ drop 2dup next-line% ] if*
124 dup current-char forbid-tab {
125 { CHAR: \s [ advance-char ] }
127 [ "[space]" swap 1string "'" dup surround unexpected ]
131 : (parse-string) ( accum lexer -- )
132 { sbuf lexer } declare
134 dup find-next-token [
142 "'\"'" "[eof]" unexpected
147 : parse-string ( -- str )
149 lexer get (parse-string)
150 ] keep unescape-string ;