diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-07-10 23:36:56 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-07-10 23:36:56 +0200 |
commit | f7b18cc72dd5b2ca90b6670dbe81c3ef3204d6d9 (patch) | |
tree | dcc40399f08285a9a308079098e735fb5bf192bd /module/c/cpp-util.scm | |
parent | Add of-type? to (hnh util type). (diff) | |
download | calp-f7b18cc72dd5b2ca90b6670dbe81c3ef3204d6d9.tar.gz calp-f7b18cc72dd5b2ca90b6670dbe81c3ef3204d6d9.tar.xz |
Resolve recursive macros.
Diffstat (limited to 'module/c/cpp-util.scm')
-rw-r--r-- | module/c/cpp-util.scm | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/module/c/cpp-util.scm b/module/c/cpp-util.scm new file mode 100644 index 00000000..420c8739 --- /dev/null +++ b/module/c/cpp-util.scm @@ -0,0 +1,62 @@ +(define-module (c cpp-util) + :use-module ((srfi srfi-1) :select (drop-while break)) + :use-module ((hnh util) :select (->)) + :use-module (hnh util type) + :use-module ((c lex2) :select (lex lexeme?)) + :use-module (c cpp-types) + :export (tokens-until-eol + squeeze-whitespace + drop-whitespace + drop-whitespace-right + drop-whitespace-both + cleanup-whitespace)) + +;; Returns two values: +;; - tokens until a newline token is met +;; - (potentially the newline token) and the remaining tokens +(define (tokens-until-eol tokens) + (typecheck tokens (list-of lexeme?)) + (break newline-token? tokens)) + + +;; Replace all whitespace with single spaces. +(define (squeeze-whitespace tokens) + (cond ((null? tokens) '()) + ((null? (cdr tokens)) + (list + (if (whitespace-token? (car tokens)) + (car (lex " ")) + (car tokens)))) + ((and (whitespace-token? (car tokens)) + (whitespace-token? (cadr tokens))) + (squeeze-whitespace (cons (car (lex " ")) + (cddr tokens)))) + (else (cons (car tokens) + (squeeze-whitespace (cdr tokens)))))) + +;; Drop leading whitespace tokens +(define (drop-whitespace tokens) + (typecheck tokens (list-of lexeme?)) + (drop-while whitespace-token? tokens)) + +(define (drop-whitespace-right tokens) + (typecheck tokens (list-of lexeme?)) + (-> tokens reverse drop-whitespace reverse)) + +(define (drop-whitespace-both tokens) + (typecheck tokens (list-of lexeme?)) + (-> tokens + drop-whitespace + drop-whitespace-right)) + +;; helper procedure to parse-parameter-list. +;; If a parameter is complex then whitespace is kept, but squeezed to single spaces. Surounding whitespace is removed. +;; Example: +;; #define str(x, y) #y +;; str(x, ( 2, 4 ) ) +;; expands to: +;; "( 2, 4 )" +;; 6.10.3.2 p 2 +(define (cleanup-whitespace tokens) + (typecheck tokens (list-of lexeme?)) + (-> tokens drop-whitespace-both squeeze-whitespace)) |