As an example, this...
- a
- b c
- d e f
- g h
would get transformed into something like this...
'(a (b c) (d e f (g h)))
You can implement this in 2 stages. Stage 1 is line based parsing, turn
the input text into this list:
'((0 a)
(2 b c)
(2 d e f)
(4 g h)))
the number is the indent level. This part is quite easy, you could do
this with regex.
Stage 2 is a bit more difficult, the algorithm to do this is based on
recursive stream processing.
You are reading or 'parsing' a tree off of a flat string of indent
levels.
When processing a head at indent level N you read off all the subtrees
at indent level >N you can see,
then return two values: the tree and the remainder of the stream.
(define (collect inputs)
(let-values (((h t) (collect^ inputs)))
(unless (null? t)
(error 'collect "failed to collect everything" t))
h))
(define (collect^ inputs)
(let ((indent (caar inputs))
(head (cdar inputs))
(inputs (cdr inputs)))
(n-collect indent head inputs)))
(define (n-collect n head stream)
;; n-collect will collect up all
;; subtrees off the stream whose
;; level is > N
(let loop ((subtrees '())
(stream stream))
(if (or (null? stream)
(<= (caar stream) n))
(values (append head (reverse subtrees)) stream)
(let-values (((subtree stream) (collect^ stream)))
(loop (cons subtree subtrees)
stream)))))
--
You received this message because you are subscribed to the Google Groups "Racket
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.