-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-desc.rkt
58 lines (48 loc) · 1.97 KB
/
parse-desc.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
;; Copyright 2014, 2015 Hamish Ivey-Law
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
#lang racket
(provide read-function-descriptions)
(require "utils.rkt")
(define descpath "/home/hlaw/src/pari/src/desc/pari.desc")
(define (line->pair line)
(let* ([idx (string-index-of line #\:)]
[title (substring line 0 idx)]
[content (string-trim (substring line (add1 idx)))])
(cons title content)))
(define (fndoc-acc x acc)
(if (string-empty? x)
;; Only add a new list if the current head is not empty
(if (empty? (car acc)) acc (cons '() acc))
(cons (cons x (car acc)) (cdr acc))))
(define (group-at-blank-lines lines)
(let ([res (foldl fndoc-acc '(()) lines)])
;; Remove empty list at front if there was one. There will only
;; ever be one, and then only if there were blank lines at the end
;; of the file.
(if (empty? (car res)) (cdr res) res)))
(define (field-acc x acc)
(if (string-starts-with? x #\space)
(cons (string-append (car acc)
(string #\newline)
(substring x 1))
(cdr acc))
(cons x acc)))
(define (join-at-field-continuation lines)
(foldl field-acc '() lines))
(define (read-function-descriptions)
(map (λ (x) (make-immutable-hash (map line->pair x)))
(group-at-blank-lines
(join-at-field-continuation
(file->lines descpath)))))