quoted-string.ss
#lang scheme/base
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; NET.plt
;;
;; abstraction of common network behaviors and services
;;
;; Bonzai Lab, LLC.  All rights reserved.
;;
;; Licensed under LGPL.
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; quoted-string.ss - parser for the quoted-string as defined in rfc2822
;; yc 2/13/2010 - first version
(require "depend.ss"
         "encoded-word.ss"
         )

;; QUOTED-STRING

;; although the spec said that quoted-string cannot contain encoded-word, the truth
;; is that this is *often* done.
(define p:quoted-pair (seq #\\ c <- (char-not-in '(#\return #\newline))
                           (return c))) 

;; the original design of qcontent is that it can contain all ascii characters
;; besides #\" #\\ #\return #\newline

(define p:qcontent (choice p:quoted-pair 
                           (char-not-in '(#\return #\newline #\" #\\))))


(define p:qstring (seq #\"
                       str <- (choice p:encoded-word 
                                      (zero-many p:qcontent))
                       #\"
                       (return (if (string? str)
                                   str
                                   (list->string str)))))


(define (encode-qstring str)
  (format "~s" str)) 

(provide/contract (p:qstring Parser/c)
                  (encode-qstring (-> string? string?))
                  )