001  (ns ^{:doc "parse multiple rules from a stream, possibly a file."
002        :author "Simon Brooke"}
003    mw-parser.bulk
004    (:use mw-parser.core
005          mw-engine.utils
006          clojure.java.io
007          [clojure.string :only [split trim]])
008    (:import (java.io BufferedReader StringReader)))
009  
010  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
011  ;;;;
012  ;;;; mw-parser: a rule parser for MicroWorld.
013  ;;;;
014  ;;;; This program is free software; you can redistribute it and/or
015  ;;;; modify it under the terms of the GNU General Public License
016  ;;;; as published by the Free Software Foundation; either version 2
017  ;;;; of the License, or (at your option) any later version.
018  ;;;;
019  ;;;; This program is distributed in the hope that it will be useful,
020  ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
021  ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
022  ;;;; GNU General Public License for more details.
023  ;;;;
024  ;;;; You should have received a copy of the GNU General Public License
025  ;;;; along with this program; if not, write to the Free Software
026  ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
027  ;;;; USA.
028  ;;;;
029  ;;;; Copyright (C) 2014 Simon Brooke
030  ;;;;
031  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
032  
033  
034  (defn comment?
035    "Is this `line` a comment?"
036    [line]
037    (or (empty? (trim line)) (member? (first line) '(nil \# \;))))
038  
039  (defn parse-string
040    "Parse rules from successive lines in this `string`, assumed to have multiple
041     lines delimited by the new-line character. Return a list of S-expressions."
042    [string]
043          ;; TODO: tried to do this using with-open, but couldn't make it work.
044    (map #(parse-rule (trim %)) (remove comment? (split string #"\n"))))
045  
046  (defn parse-file
047    "Parse rules from successive lines in the file loaded from this `filename`.
048     Return a list of S-expressions."
049    [filename]
050    (parse-string (slurp filename)))
051  
052  (defn compile-string
053    "Compile each non-comment line of this `string` into an executable anonymous
054     function, and return the sequence of such functions."
055    [string]
056    (map #(compile-rule % true) (remove comment? (split string #"\n"))))
057  
058  (defn compile-file
059    "Compile each non-comment line of the file indicated by this `filename` into
060     an executable anonymous function, and return the sequence of such functions."
061    [filename]
062    (compile-string (slurp filename)))