aboutsummaryrefslogtreecommitdiff
path: root/module/c/unlex.scm
diff options
context:
space:
mode:
Diffstat (limited to 'module/c/unlex.scm')
-rw-r--r--module/c/unlex.scm31
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))