]> gitweb.factorcode.org Git - factor.git/commitdiff
strings.parser: support octal escapes of 1, 2, or 3 octal digits
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 26 Jul 2022 19:58:31 +0000 (12:58 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 26 Jul 2022 19:58:31 +0000 (12:58 -0700)
core/strings/parser/parser-tests.factor
core/strings/parser/parser.factor
core/syntax/syntax-docs.factor

index eae2eb0bc3682383ec856eba90c88cb9f726a691..441177eea44d92ee4c0062bbe1f780ff33db66e2 100644 (file)
@@ -27,3 +27,10 @@ USING: accessors eval kernel lexer strings.parser tools.test ;
 { "foo" } [ "\"\\\nfoo\"" eval( -- obj ) ] unit-test
 { "foo" } [ "\"foo\\\n\"" eval( -- obj ) ] unit-test
 { "foo bar" } [ "\"foo \\\nbar\"" eval( -- obj ) ] unit-test
+
+{ "a" } [ "\"\\141\"" eval( -- obj ) ] unit-test
+{ "\0" } [ "\"\\0\"" eval( -- obj ) ] unit-test
+{ "\x01" } [ "\"\\01\"" eval( -- obj ) ] unit-test
+{ "\n" } [ "\"\\012\"" eval( -- obj ) ] unit-test
+{ "\x01d2" } [ "\"\\01d2\"" eval( -- obj ) ] unit-test
+{ "\x018" } [ "\"\\018\"" eval( -- obj ) ] unit-test
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
 
index 31aa1ddb7ae02377bd90a2fcea0d1e23e99d0b1b..24f72dc51df7753862ee09db4f680a20f9f861a4 100644 (file)
@@ -192,6 +192,7 @@ ARTICLE: "escape" "Character escape codes"
     { { $snippet "\\u" { $emphasis "xxxxxx" } } { "The Unicode code point with hexadecimal number " { $snippet { $emphasis "xxxxxx" } } } }
     { { $snippet "\\u{" { $emphasis "xx" } "}" } { "The Unicode code point with hexadecimal number " { $snippet { $emphasis "xx" } } } }
     { { $snippet "\\u{" { $emphasis "name" } "}" } { "The Unicode code point named " { $snippet { $emphasis "name" } } } }
+    { { $snippet "\\xxx" } "an octal escape specified by one, two, or three octal digits" }
 } ;
 
 ARTICLE: "syntax-strings" "Character and string syntax"