From c1cf0693982d9c1f1b871966752140fee5d76d19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Wed, 13 Jul 2022 04:39:14 +0200 Subject: work --- module/c/unlex.scm | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'module/c/unlex.scm') diff --git a/module/c/unlex.scm b/module/c/unlex.scm index 18e800d9..e3d36f86 100644 --- a/module/c/unlex.scm +++ b/module/c/unlex.scm @@ -5,6 +5,7 @@ :use-module (c cpp-types) :use-module (c cpp-util) :export (unlex + unlex-aggressive stringify-token stringify-tokens)) @@ -24,11 +25,37 @@ ((whitespace-token? x) " "))) (squeeze-whitespace tokens)))) +(define (stringify-escape-sequence sub-token) + (match sub-token + (`(simple-escape-sequence ,x) + (format #f "\\~a" x)) + (`(octal-escape-sequence ,x) + (format #f "\\~a" x)) + (`(hexadecimal-escape-sequence ,x) + (format #f "\\x~a" x)) + (`(universal-character-name ,x) + (case (string-length x) + ((4) (format #f "\\u~a" x)) + ((8) (format #f "\\U~a" x)))))) + +(define (stringify-string-tokens fragments) + (with-output-to-string + (lambda () + (display #\") + (for-each (match-lambda + (`(escape-sequence ,x) + (display (stringify-escape-sequence x))) + (s (display s))) + fragments) + (display #\")))) + ;; Returns the "source" of the token, as a preprocessing string literal token (define (stringify-token preprocessing-token) (match (lexeme-body preprocessing-token) - (`(string-literal ,s) - (format #f "~s" s)) + (('string-literal `(encoding-prefix ,prefix) parts ...) + (stringify-string-tokens parts)) + (('string-literal parts ...) + (stringify-string-tokens parts)) (`(header-name (q-string ,s)) (format #f "~s" s)) (`(header-name (h-string ,s)) -- cgit v1.2.3