diff --git a/pitfall/pitfall/minimal-pdf-source.rkt b/pitfall/pitfall/minimal-pdf-source.rkt index a727e723..869c7812 100644 --- a/pitfall/pitfall/minimal-pdf-source.rkt +++ b/pitfall/pitfall/minimal-pdf-source.rkt @@ -1,7 +1,6 @@ #lang s-exp pitfall/render -(co-version 1.1) -(co-comment "%¥±ë") +(co-header "%PDF-1.1\n%¥±ë") (co-io 1 0 diff --git a/pitfall/pitfall/parse.rkt b/pitfall/pitfall/parse.rkt index bceaf847..94faa0df 100644 --- a/pitfall/pitfall/parse.rkt +++ b/pitfall/pitfall/parse.rkt @@ -7,7 +7,8 @@ (module+ reader (provide read-syntax)) (define (read-syntax src port) - (define parse-tree (parse (make-tokenizer port src))) + ;; use latin-1 reencoding to make one char = one byte + (define parse-tree (parse (make-tokenizer (open-input-string (bytes->string/latin-1 (port->bytes port))) src))) (strip-bindings #`(module pitfall-parse-mod pitfall/parse #,parse-tree))) @@ -84,6 +85,6 @@ (define-macro (pf-indirect-object-ref (OBJ GEN _)) #'(co-io-ref OBJ GEN)) -(define (pf-version num) (co-version num)) +(define (pf-header num) (co-header num)) (define (pf-comment text) (co-comment text)) diff --git a/pitfall/pitfall/parser.rkt b/pitfall/pitfall/parser.rkt index bed394a3..c9c75187 100644 --- a/pitfall/pitfall/parser.rkt +++ b/pitfall/pitfall/parser.rkt @@ -1,7 +1,7 @@ #lang brag pf-program : pf-object* -@pf-object : pf-null | CHAR | BOOLEAN | INT | REAL | pf-name | pf-string | pf-array | pf-dict | pf-stream | pf-indirect-object | pf-indirect-object-ref | pf-version | pf-comment +@pf-object : pf-null | CHAR | BOOLEAN | INT | REAL | pf-name | pf-string | pf-array | pf-dict | pf-stream | pf-indirect-object | pf-indirect-object-ref | pf-header | pf-comment @pf-null : NULL pf-name : NAME pf-string : STRING-TOK | /"<" HEX-DIGIT-PAIR* /">" @@ -12,5 +12,5 @@ pf-dict : /"<" /"<" (pf-dict-key pf-dict-value)* /">" /">" pf-stream : pf-dict STREAM-DATA pf-indirect-object : INT INT /"obj" pf-object /"endobj" pf-indirect-object-ref : INDIRECT-OBJECT-REF-TOK -pf-version : PDF-VERSION +pf-header : PDF-HEADER pf-comment : COMMENT \ No newline at end of file diff --git a/pitfall/pitfall/render.rkt b/pitfall/pitfall/render.rkt index 68d9c8ba..8857f147 100644 --- a/pitfall/pitfall/render.rkt +++ b/pitfall/pitfall/render.rkt @@ -21,6 +21,7 @@ (cond [(co-version? x) @string-append{%%PDF-@(number->string (co-version-num x))}] + [(co-header? x) (co-header-string x)] [(co-array? x) @string-append{[ @(string-join (map loop (co-array-items x)) " ") ]}] [(co-io? x) diff --git a/pitfall/pitfall/struct.rkt b/pitfall/pitfall/struct.rkt index 8b73a187..93266fe5 100644 --- a/pitfall/pitfall/struct.rkt +++ b/pitfall/pitfall/struct.rkt @@ -5,6 +5,7 @@ (struct co-array (items) #:transparent) (struct co-stream (dict data) #:transparent) (struct co-version (num) #:transparent) +(struct co-header (string) #:transparent) (struct co-io (idx rev thing) #:transparent) (struct co-io-ref (idx rev) #:transparent) (struct co-comment (text) #:transparent) \ No newline at end of file diff --git a/pitfall/pitfall/tokenizer.rkt b/pitfall/pitfall/tokenizer.rkt index 17cf86bf..79100951 100644 --- a/pitfall/pitfall/tokenizer.rkt +++ b/pitfall/pitfall/tokenizer.rkt @@ -12,8 +12,8 @@ (define-lex-abbrev blackspace (:~ pdf-whitespace)) (define-lex-abbrev not-right-paren (:~ ")")) (define-lex-abbrev substring (:seq "(" (:* not-right-paren) ")")) - (define-lex-abbrev nonreg-char (:seq "#" hex-digit hex-digit)) +(define-lex-abbrev pdf-eol (:or #\return #\newline (:: #\return #\newline))) (define (make-tokenizer port [src #f]) (port-count-lines! port) @@ -24,7 +24,8 @@ [(:seq "%%EOF" any-string) eof] [(:seq digits (:+ pdf-whitespace) digits (:+ pdf-whitespace) "R") (token 'INDIRECT-OBJECT-REF-TOK (map string->number (string-split lexeme)))] - [(:seq "%PDF-" digits "." digits) (token 'PDF-VERSION (string->number (trim-ends "%PDF-" lexeme "")))] + [(:seq "%PDF-" digits "." digits + (:? (:: pdf-eol "%" (:>= 4 (:~ (union #\return #\newline)))))) (token 'PDF-HEADER lexeme)] [pdf-whitespace (token 'IGNORE lexeme #:skip? #t)] [(from/stop-before "%" #\newline) (token 'COMMENT lexeme)] [(:or "true" "false") (token 'BOOLEAN (equal? lexeme "true"))]