diff --git a/README.md b/README.md index 329710d..a11cb06 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,6 @@ FIXME You will need [Leiningen][1] 2.0 or above installed. -[1]: https://github.com/technomancy/leiningen - ## Running To start a web server for the application, run: @@ -16,4 +14,10 @@ To start a web server for the application, run: ## License -Copyright © 2014 FIXME +Copyright © 2014 Simon Brooke + +Distributed under the terms of the [GNU General Public License v2][2] + + +[1]: https://github.com/technomancy/leiningen +[2]: http://www.gnu.org/licenses/gpl-2.0.html \ No newline at end of file diff --git a/resources/public/docs/mw-parser/uberdoc.html b/resources/public/docs/mw-parser/uberdoc.html index 5e6043a..199cb45 100644 --- a/resources/public/docs/mw-parser/uberdoc.html +++ b/resources/public/docs/mw-parser/uberdoc.html @@ -3029,7 +3029,35 @@ net.brehaut.ClojureTools = (function (SH) { }; })(SyntaxHighlighter);
mw-parser0.1.0-SNAPSHOTParser for production rules for MicroWorld engine -dependencies
| (this space intentionally left almost blank) | ||||||
A very simple parser which parses production rules of the following forms: +dependencies
| (this space intentionally left almost blank) | ||||||
parse multiple rules from a stream, possibly a file - although the real +objective is to parse rules out of a block of text from a textarea + | |||||||
+ | (ns mw-parser.bulk + (:use mw-parser.core + mw-engine.utils + clojure.java.io) + (:import (java.io BufferedReader StringReader))) | ||||||
+ | (defn parse-line [line] + (let [initial (first line)] + (cond + (member? initial '(nil \# \;)) nil + true (parse-rule line)))) | ||||||
Parse rules from lines returned by this | (defn- parse-from-reader + [reader] + (remove nil? + (map parse-line + (line-seq reader)))) | ||||||
Parse rules from successive lines in the file loaded from this | (defn parse-file + [filename] + (with-open [rdr (reader filename)] + (remove nil? + (map parse-line + (line-seq rdr))))) | ||||||
Parse rules from successive lines in this | (defn parse-string + [string] + (parse-from-reader (BufferedReader. (StringReader. string)))) | ||||||
A very simple parser which parses production rules of the following forms:
| |||||||
(ns mw-parser.core (:use mw-engine.utils - [clojure.string :only [split triml]])) | |||||||
+ [clojure.string :only [split trim triml]])) | |||||||
(declare parse-conditions) (declare parse-not-condition) (declare parse-simple-condition) | |||||||
a regular expression which matches string representation of numbers | (def re-number #"^[0-9.]*$") | ||||||
If this token appears to represent an explicit number, return that number; otherwise, make a keyword of it and return that. - | (defn keyword-or-numeric + | (defn- keyword-or-numeric [token] (cond (re-matches re-number token) (read-string token) @@ -3080,18 +3108,18 @@ vector comprising | |||||
Parse a number. - | (defn parse-numeric-value + | (defn- parse-numeric-value [[value & remainder]] (if (re-matches re-number value) [(read-string value) remainder])) | |||||
Parse a token assumed to be the name of a property of the current cell, whose value is assumed to be an integer. - | (defn parse-property-int + | (defn- parse-property-int [[value & remainder]] (if value [(list 'get-int 'cell (keyword value)) remainder])) | |||||
Parse a token assumed to be the name of a property of the current cell. - | (defn parse-property-value + | (defn- parse-property-value [[value & remainder]] (if value [(list (keyword value) 'cell) remainder])) | |||||
Parse a value from the first of these | (defn parse-simple-value + | (defn- parse-simple-value ([tokens expect-int] (or (parse-numeric-value tokens) @@ -3101,7 +3129,7 @@ front of the sequence of tokens it returns nil. ([tokens] (parse-simple-value tokens false))) | |||||
Parse a list of values from among these | (defn parse-disjunct-value + | (defn- parse-disjunct-value [[OR token & tokens] expect-int] (cond (member? OR '("or" "in")) (let [[others remainder] (parse-disjunct-value tokens expect-int)] @@ -3113,27 +3141,27 @@ front of the sequence of tokens it returns nil. remainder]) true [nil (cons OR (cons token tokens))])) | |||||
Parse a value from among these | (defn parse-value + | (defn- parse-value ([tokens expect-int] (or (parse-disjunct-value tokens expect-int) (parse-simple-value tokens))) ([tokens] - (parse-value tokens false))) | |||||
- | (defn parse-member-condition + (parse-value tokens false))) | ||||||
Parses a condition of the form '[property] in [value] or [value]...' + | (defn- parse-member-condition [[property IN & rest]] (if (= IN "in") (let [[l remainder] (parse-disjunct-value (cons "in" rest) false)] [(list 'member? (keyword property) l) remainder]))) | ||||||
Parse '[property] less than [value]'. - | (defn parse-less-condition + | (defn- parse-less-condition [[property LESS THAN value & rest]] (cond (and (= LESS "less") (= THAN "than")) [(list '< (list 'get-int 'cell (keyword property)) (read-string value)) rest])) | |||||
Parse '[property] more than [value]'. - | (defn parse-more-condition + | (defn- parse-more-condition [[property MORE THAN value & rest]] (cond (and (= MORE "more") (= THAN "than")) [(list '> (list 'get-int 'cell (keyword property)) (read-string value)) rest])) | |||||
- | (defn parse-between-condition + | (defn- parse-between-condition [[p BETWEEN v1 AND v2 & rest]] (cond (and (= BETWEEN "between") (= AND "and") (not (nil? v2))) (let [property (first (parse-simple-value (list p) true)) @@ -3144,7 +3172,7 @@ front of the sequence of tokens it returns nil. (list '> value1 property value2)) rest]))) | |||||
Parse clauses of the form 'x is y', 'x is in y or z...', 'x is between y and z', 'x is more than y' or 'x is less than y'. It is necessary to disambiguate whether value is a numeric or keyword. - | (defn parse-is-condition + | (defn- parse-is-condition [[property IS value & rest]] (cond (member? IS '("is" "are")) @@ -3156,7 +3184,7 @@ front of the sequence of tokens it returns nil. (= value "less") (parse-less-condition tokens) (re-matches re-number value) [(list '= (list 'get-int 'cell (keyword property)) (read-string value)) rest] value [(list '= (list (keyword property) 'cell) (keyword value)) rest])))) | |||||
Parse the negation of a simple condition. - | (defn parse-not-condition + | (defn- parse-not-condition [[property IS NOT & rest]] (cond (and (member? IS '("is" "are")) (= NOT "not")) (let [partial (parse-simple-condition (cons property (cons "is" rest)))] @@ -3171,7 +3199,7 @@ front of the sequence of tokens it returns nil. (keyword property) (keyword-or-numeric value))) quantity) remainder]) | |||||
Parse conditions of the form '...more than 6 neighbours are [condition]' - | (defn parse-comparator-neighbours-condition + | (defn- parse-comparator-neighbours-condition [[MORE THAN n NEIGHBOURS have-or-are & rest]] (let [quantity (first (parse-numeric-value (list n))) comparator (cond (= MORE "more") '> @@ -3193,12 +3221,12 @@ front of the sequence of tokens it returns nil. ;; (gen-neighbours-condition '> quantity property value remainder) ;; (and (= comp1 "less") (= comp2 "than")) ;; (gen-neighbours-condition '< quantity property value remainder))))))) | |||||
- | (defn parse-some-neighbours-condition + | (defn- parse-some-neighbours-condition [[SOME NEIGHBOURS & rest]] (cond (and (= SOME "some") (= NEIGHBOURS "neighbours")) (parse-comparator-neighbours-condition (concat '("more" "than" "0" "neighbours") rest)))) | |||||
Parse conditions of the form '...6 neighbours are condition' - | (defn parse-simple-neighbours-condition + | (defn- parse-simple-neighbours-condition [[n NEIGHBOURS have-or-are & rest]] (let [quantity (first (parse-numeric-value (list n)))] (cond @@ -3215,13 +3243,13 @@ front of the sequence of tokens it returns nil. ;; (gen-neighbours-condition '> quantity property value remainder) ;; (and (= comp1 "less") (= comp2 "than")) ;; (gen-neighbours-condition '< quantity property value remainder))))))) | |||||
Parse conditions referring to neighbours - | (defn parse-neighbours-condition + | (defn- parse-neighbours-condition [tokens] (or (parse-simple-neighbours-condition tokens) (parse-comparator-neighbours-condition tokens) (parse-some-neighbours-condition tokens))) | |||||
Parse conditions of the form '[property] [comparison] [value]'. - | (defn parse-simple-condition + | (defn- parse-simple-condition [tokens] (or (parse-neighbours-condition tokens) @@ -3230,19 +3258,19 @@ front of the sequence of tokens it returns nil. (parse-is-condition tokens) (parse-less-condition tokens) (parse-more-condition tokens))) | |||||
Parse '... or [condition]' from | (defn parse-disjunction-condition + | (defn- parse-disjunction-condition [left tokens] (let [partial (parse-conditions tokens)] (if partial (let [[right remainder] partial] [(list 'or left right) remainder])))) | |||||
Parse '... and [condition]' from | (defn parse-conjunction-condition + | (defn- parse-conjunction-condition [left tokens] (let [partial (parse-conditions tokens)] (if partial (let [[right remainder] partial] [(list 'and left right) remainder])))) | |||||
Parse conditions from | (defn parse-conditions + | (defn- parse-conditions [tokens] (let [partial (parse-simple-condition tokens)] (if partial @@ -3251,13 +3279,13 @@ front of the sequence of tokens it returns nil. (= next "and") (parse-conjunction-condition left remainder) (= next "or") (parse-disjunction-condition left remainder) true partial))))) | |||||
Parse the left hand side ('if...') of a production rule. - | (defn parse-left-hand-side - [tokens] + | (defn- parse-left-hand-side + [[IF & tokens]] (if - (= (first tokens) "if") - (parse-conditions (rest tokens)))) | |||||
Parse actions of the form '[property] should be [property] [arithmetic-operator] [value]', + (= IF "if") + (parse-conditions tokens))) | |||||||
Parse actions of the form '[property] should be [property] [arithmetic-operator] [value]', e.g. 'fertility should be fertility + 1', or 'deer should be deer - wolves'. - | (defn parse-arithmetic-action + | (defn- parse-arithmetic-action [previous [prop1 should be prop2 operator value & rest]] (if (and (= should "should") (= be "be") @@ -3267,22 +3295,22 @@ front of the sequence of tokens it returns nil. (cond (re-matches re-number value) (read-string value) true (list 'get-int 'cell (keyword value))))}) rest])) | |||||
Parse actions of the form '[property] should be [value].' - | (defn parse-set-action + | (defn- parse-set-action [previous [property should be value & rest]] (if (and (= should "should") (= be "be")) [(list 'merge (or previous 'cell) {(keyword property) (cond (re-matches re-number value) (read-string value) true (keyword value))}) rest])) | |||||
- | (defn parse-simple-action [previous tokens] + | (defn- parse-simple-action [previous tokens] (or (parse-arithmetic-action previous tokens) (parse-set-action previous tokens))) | |||||
Parse actions from tokens. - | (defn parse-actions + | (defn- parse-actions [previous tokens] (let [[left remainder] (parse-simple-action previous tokens)] (cond left (cond (= (first remainder) "and") (parse-actions left (rest remainder)) true (list left))))) | |||||
Parse a probability of an action from this collection of tokens - | (defn parse-probability + | (defn- parse-probability [previous [n CHANCE IN m & tokens]] (cond (and (= CHANCE "chance")(= IN "in")) @@ -3294,7 +3322,7 @@ front of the sequence of tokens it returns nil. (first (parse-simple-value (list m) true))) (first (parse-simple-value (list n) true))) action) remainder])))) | |||||
Parse the right hand side ('then...') of a production rule. - | (defn parse-right-hand-side + | (defn- parse-right-hand-side [[THEN & tokens]] (if (= THEN "then") (or |