Skip to content

Instantly share code, notes, and snippets.

@bluegraybox
Created July 15, 2011 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluegraybox/1083966 to your computer and use it in GitHub Desktop.
Save bluegraybox/1083966 to your computer and use it in GitHub Desktop.
(defun parse-text-line (line)
;; converts " text" to (:content "text" :indent 4)
(let ((content (string-left-trim '(#\Space #\Tab) line)))
(list :content content :indent (- (length line) (length content)))))
(defun split-list (criterion data-list)
;; break a list into two lists; the second begins with the first element that matches the criterion.
(if data-list
(if (funcall criterion (first data-list))
(list () data-list)
(let ((me (pop data-list)))
(let ((chunks (split-list criterion data-list)))
(push me (first chunks))
chunks)))
(list () ())))
(defun build-nodes (lines)
(if lines
(let* ((me (pop lines))
;; split off our children from the rest of the lines.
;; the first line with an indent <= ours is a sibling, not a child
(chunks (split-list (lambda (x) (<= (getf x :indent) (getf me :indent))) lines)))
(let ((children (build-nodes (pop chunks)))
(siblings (build-nodes (pop chunks))))
(let ((node (list :content (getf me :content))))
(if children (setf (getf node :children) children))
;; (format t "~a~%" (getf node :content))
(push node siblings))))
nil))
@bluegraybox
Copy link
Author

Here's the full code for the Lisp version of the indent parser. There's also a sequential Erlang version and a concurrent Erlang version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment