]> gitweb.factorcode.org Git - factor.git/blobdiff - core/strings/parser/parser.factor
strings.parser: support octal escapes of 1, 2, or 3 octal digits
[factor.git] / core / strings / parser / parser.factor
index 98f43be80d8ea35593b08bce7a8471a999959694..9656a1308c3cf63099331eb0afaa722dcdc88b9e 100644 (file)
@@ -1,7 +1,8 @@
 ! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors assocs combinators kernel kernel.private lexer
-math math.parser namespaces sbufs sequences splitting strings ;
+math math.order math.parser namespaces sbufs sequences splitting
+strings ;
 IN: strings.parser
 
 ERROR: bad-escape char ;
@@ -32,6 +33,12 @@ name>char-hook [
 : hex-escape ( str -- ch str' )
     2 cut-slice [ hex> ] dip ;
 
+: oct-escape ( str -- ch/f str' )
+    dup [
+        3 short head-slice [ CHAR: 0 CHAR: 7 between? not ] find drop
+    ] keep '[ _ length ] unless* [ f ] when-zero
+    [ cut-slice [ oct> ] dip ] [ f swap ] if* ;
+
 : unicode-escape ( str -- ch str' )
     "{" ?head-slice [
         CHAR: } over index cut-slice [
@@ -46,12 +53,14 @@ name>char-hook [
     ] if ;
 
 : next-escape ( str -- ch str' )
-    unclip-slice {
-        { CHAR: u [ unicode-escape ] }
-        { CHAR: x [ hex-escape ] }
-        { CHAR: \n [ f swap ] }
-        [ escape swap ]
-    } case ;
+    oct-escape over [
+        nip unclip-slice {
+            { CHAR: u [ unicode-escape ] }
+            { CHAR: x [ hex-escape ] }
+            { CHAR: \n [ f swap ] }
+            [ escape swap ]
+        } case
+    ] unless ;
 
 <PRIVATE