diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-07-13 04:39:14 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-07-13 04:39:14 +0200 |
commit | c1cf0693982d9c1f1b871966752140fee5d76d19 (patch) | |
tree | 65cf5c501f5b2029eb08bed7fa9844b9530385f3 /module/c/unlex.scm | |
parent | Resolve # ## # (diff) | |
download | calp-c1cf0693982d9c1f1b871966752140fee5d76d19.tar.gz calp-c1cf0693982d9c1f1b871966752140fee5d76d19.tar.xz |
work
Diffstat (limited to '')
-rw-r--r-- | module/c/unlex.scm | 31 |
1 files changed, 29 insertions, 2 deletions
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)) |