Changed license to GPL; some minor documentation changes.
This commit is contained in:
		
							parent
							
								
									f3abd6276f
								
							
						
					
					
						commit
						c8ae1b3dc4
					
				
							
								
								
									
										10
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								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 | ||||
|  | @ -3029,7 +3029,35 @@ net.brehaut.ClojureTools = (function (SH) { | |||
|   }; | ||||
| })(SyntaxHighlighter); | ||||
| </script><title>mw-parser -- Marginalia</title></head><body><table><tr><td class="docs"><div class="header"><h1 class="project-name">mw-parser</h1><h2 class="project-version">0.1.0-SNAPSHOT</h2><br /><p>Parser for production rules for MicroWorld engine</p> | ||||
| </div><div class="dependencies"><h3>dependencies</h3><table><tr><td class="dep-name">org.clojure/clojure</td><td class="dotted"><hr /></td><td class="dep-version">1.5.1</td></tr><tr><td class="dep-name">mw-engine</td><td class="dotted"><hr /></td><td class="dep-version">0.1.0-SNAPSHOT</td></tr></table></div></td><td class="codes" style="text-align: center; vertical-align: middle;color: #666;padding-right:20px"><br /><br /><br />(this space intentionally left almost blank)</td></tr><tr><td class="docs"><div class="toc"><a name="toc"><h3>namespaces</h3></a><ul><li><a href="#mw-parser.core">mw-parser.core</a></li></ul></div></td><td class="codes"> </td></tr><tr><td class="docs"><div class="docs-header"><a class="anchor" href="#mw-parser.core" name="mw-parser.core"><h1 class="project-name">mw-parser.core</h1><a class="toc-link" href="#toc">toc</a></a></div></td><td class="codes" /></tr><tr><td class="docs"><p>A very simple parser which parses production rules of the following forms:</p> | ||||
| </div><div class="dependencies"><h3>dependencies</h3><table><tr><td class="dep-name">org.clojure/clojure</td><td class="dotted"><hr /></td><td class="dep-version">1.5.1</td></tr><tr><td class="dep-name">mw-engine</td><td class="dotted"><hr /></td><td class="dep-version">0.1.0-SNAPSHOT</td></tr></table></div></td><td class="codes" style="text-align: center; vertical-align: middle;color: #666;padding-right:20px"><br /><br /><br />(this space intentionally left almost blank)</td></tr><tr><td class="docs"><div class="toc"><a name="toc"><h3>namespaces</h3></a><ul><li><a href="#mw-parser.bulk">mw-parser.bulk</a></li><li><a href="#mw-parser.core">mw-parser.core</a></li></ul></div></td><td class="codes"> </td></tr><tr><td class="docs"><div class="docs-header"><a class="anchor" href="#mw-parser.bulk" name="mw-parser.bulk"><h1 class="project-name">mw-parser.bulk</h1><a class="toc-link" href="#toc">toc</a></a></div></td><td class="codes" /></tr><tr><td class="docs"><p>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</p> | ||||
| </td><td class="codes"></td></tr><tr><td class="docs"> | ||||
| </td><td class="codes"><pre class="brush: clojure">(ns mw-parser.bulk | ||||
|   (:use mw-parser.core | ||||
|         mw-engine.utils | ||||
|         clojure.java.io) | ||||
|   (:import (java.io BufferedReader StringReader)))</pre></td></tr><tr><td class="docs"> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-line [line] | ||||
|   (let [initial (first line)] | ||||
|     (cond | ||||
|       (member? initial '(nil \# \;)) nil | ||||
|       true (parse-rule line))))</pre></td></tr><tr><td class="docs"><p>Parse rules from lines returned by this <code>reader</code>. Ignore | ||||
|    lines starting with <code>;;</code>, but otherwise throw an exception if any | ||||
|    line cannot be parsed.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-from-reader  | ||||
|   [reader] | ||||
|   (remove nil? | ||||
|           (map parse-line | ||||
|                (line-seq reader))))</pre></td></tr><tr><td class="docs"><p>Parse rules from successive lines in the file loaded from this <code>filename</code></p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-file  | ||||
|   [filename] | ||||
|   (with-open [rdr (reader filename)] | ||||
|     (remove nil? | ||||
|           (map parse-line  | ||||
|                (line-seq rdr)))))</pre></td></tr><tr><td class="docs"><p>Parse rules from successive lines in this <code>string</code></p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-string | ||||
|    [string] | ||||
|    (parse-from-reader (BufferedReader. (StringReader. string))))</pre></td></tr><tr><td class="spacer docs"> </td><td class="codes" /></tr><tr><td class="docs"><div class="docs-header"><a class="anchor" href="#mw-parser.core" name="mw-parser.core"><h1 class="project-name">mw-parser.core</h1><a class="toc-link" href="#toc">toc</a></a></div></td><td class="codes" /></tr><tr><td class="docs"><p>A very simple parser which parses production rules of the following forms:</p> | ||||
| 
 | ||||
| <ul> | ||||
| <li>"if altitude is less than 100 and state is forest then state should be climax and deer should be 3"</li> | ||||
|  | @ -3058,13 +3086,13 @@ more complex issue which I don't yet know how to address.</p> | |||
| </td><td class="codes"></td></tr><tr><td class="docs"> | ||||
| </td><td class="codes"><pre class="brush: clojure">(ns mw-parser.core | ||||
|   (:use mw-engine.utils | ||||
|         [clojure.string :only [split triml]]))</pre></td></tr><tr><td class="docs"> | ||||
|         [clojure.string :only [split trim triml]]))</pre></td></tr><tr><td class="docs"> | ||||
| </td><td class="codes"><pre class="brush: clojure">(declare parse-conditions) | ||||
| (declare parse-not-condition) | ||||
| (declare parse-simple-condition)</pre></td></tr><tr><td class="docs"><p>a regular expression which matches string representation of numbers</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(def re-number #"^[0-9.]*$")</pre></td></tr><tr><td class="docs"><p>If this token appears to represent an explicit number, return that number; | ||||
|    otherwise, make a keyword of it and return that.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn keyword-or-numeric | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- keyword-or-numeric | ||||
|   [token] | ||||
|   (cond  | ||||
|     (re-matches re-number token) (read-string token) | ||||
|  | @ -3080,18 +3108,18 @@ vector comprising</p> | |||
| <p>In every case if the function cannot parse the desired construct from the | ||||
| front of the sequence of tokens it returns nil.</p> | ||||
| </td><td class="codes"></td></tr><tr><td class="docs"><p>Parse a number.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-numeric-value | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-numeric-value | ||||
|   [[value & remainder]] | ||||
|   (if (re-matches re-number value) [(read-string value) remainder]))</pre></td></tr><tr><td class="docs"><p>Parse a token assumed to be the name of a property of the current cell,  | ||||
|   whose value is assumed to be an integer.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-property-int | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-property-int | ||||
|   [[value & remainder]] | ||||
|   (if value [(list 'get-int 'cell (keyword value)) remainder]))</pre></td></tr><tr><td class="docs"><p>Parse a token assumed to be the name of a property of the current cell.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-property-value | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-property-value | ||||
|   [[value & remainder]] | ||||
|   (if value [(list (keyword value) 'cell) remainder]))</pre></td></tr><tr><td class="docs"><p>Parse a value from the first of these <code>tokens</code>. If <code>expect-int</code> is true, return | ||||
|    an integer or something which will evaluate to an integer.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-simple-value | ||||
| </td><td class="codes"><pre class="brush: clojure">(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.</p> | |||
|   ([tokens] | ||||
|     (parse-simple-value tokens false)))</pre></td></tr><tr><td class="docs"><p>Parse a list of values from among these <code>tokens</code>. If <code>expect-int</code> is true, return | ||||
|    an integer or something which will evaluate to an integer.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-disjunct-value | ||||
| </td><td class="codes"><pre class="brush: clojure">(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.</p> | |||
|        remainder]) | ||||
|     true [nil (cons OR (cons token tokens))]))</pre></td></tr><tr><td class="docs"><p>Parse a value from among these <code>tokens</code>. If <code>expect-int</code> is true, return | ||||
|    an integer or something which will evaluate to an integer.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-value  | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-value  | ||||
|   ([tokens expect-int] | ||||
|     (or  | ||||
|       (parse-disjunct-value tokens expect-int) | ||||
|       (parse-simple-value tokens))) | ||||
|   ([tokens] | ||||
|     (parse-value tokens false)))</pre></td></tr><tr><td class="docs"> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-member-condition | ||||
|     (parse-value tokens false)))</pre></td></tr><tr><td class="docs"><p>Parses a condition of the form '[property] in [value] or [value]...'</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(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])))</pre></td></tr><tr><td class="docs"><p>Parse '[property] less than [value]'.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-less-condition | ||||
| </td><td class="codes"><pre class="brush: clojure">(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]))</pre></td></tr><tr><td class="docs"><p>Parse '[property] more than [value]'.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-more-condition | ||||
| </td><td class="codes"><pre class="brush: clojure">(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]))</pre></td></tr><tr><td class="docs"> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-between-condition | ||||
| </td><td class="codes"><pre class="brush: clojure">(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.</p> | |||
|             (list '> value1 property value2)) rest])))</pre></td></tr><tr><td class="docs"><p>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.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-is-condition | ||||
| </td><td class="codes"><pre class="brush: clojure">(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.</p> | |||
|         (= 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]))))</pre></td></tr><tr><td class="docs"><p>Parse the negation of a simple condition.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-not-condition  | ||||
| </td><td class="codes"><pre class="brush: clojure">(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.</p> | |||
|                      (keyword property) (keyword-or-numeric value))) | ||||
|          quantity) | ||||
|            remainder])</pre></td></tr><tr><td class="docs"><p>Parse conditions of the form '...more than 6 neighbours are [condition]'</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-comparator-neighbours-condition | ||||
| </td><td class="codes"><pre class="brush: clojure">(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.</p> | |||
| ;;            (gen-neighbours-condition '> quantity property value remainder) | ||||
| ;;            (and (= comp1 "less") (= comp2 "than")) | ||||
| ;;            (gen-neighbours-condition '< quantity property value remainder)))))))</pre></td></tr><tr><td class="docs"> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-some-neighbours-condition | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-some-neighbours-condition | ||||
|   [[SOME NEIGHBOURS & rest]] | ||||
|   (cond | ||||
|     (and (= SOME "some") (= NEIGHBOURS "neighbours")) | ||||
|     (parse-comparator-neighbours-condition (concat '("more" "than" "0" "neighbours") rest))))</pre></td></tr><tr><td class="docs"><p>Parse conditions of the form '...6 neighbours are condition'</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-simple-neighbours-condition | ||||
| </td><td class="codes"><pre class="brush: clojure">(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.</p> | |||
| ;;            (gen-neighbours-condition '> quantity property value remainder) | ||||
| ;;            (and (= comp1 "less") (= comp2 "than")) | ||||
| ;;            (gen-neighbours-condition '< quantity property value remainder)))))))</pre></td></tr><tr><td class="docs"><p>Parse conditions referring to neighbours</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-neighbours-condition | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-neighbours-condition | ||||
|   [tokens] | ||||
|   (or | ||||
|     (parse-simple-neighbours-condition tokens) | ||||
|     (parse-comparator-neighbours-condition tokens) | ||||
|     (parse-some-neighbours-condition tokens)))</pre></td></tr><tr><td class="docs"><p>Parse conditions of the form '[property] [comparison] [value]'.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-simple-condition | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-simple-condition | ||||
|   [tokens] | ||||
|   (or | ||||
|     (parse-neighbours-condition tokens) | ||||
|  | @ -3230,19 +3258,19 @@ front of the sequence of tokens it returns nil.</p> | |||
|     (parse-is-condition tokens) | ||||
|     (parse-less-condition tokens) | ||||
|     (parse-more-condition tokens)))</pre></td></tr><tr><td class="docs"><p>Parse '... or [condition]' from <code>tokens</code>, where <code>left</code> is the already parsed first disjunct.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-disjunction-condition | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-disjunction-condition | ||||
|   [left tokens] | ||||
|   (let [partial (parse-conditions tokens)] | ||||
|     (if partial | ||||
|       (let [[right remainder] partial] | ||||
|         [(list 'or left right) remainder]))))</pre></td></tr><tr><td class="docs"><p>Parse '... and [condition]' from <code>tokens</code>, where <code>left</code> is the already parsed first conjunct.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-conjunction-condition | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-conjunction-condition | ||||
|   [left tokens] | ||||
|   (let [partial (parse-conditions tokens)] | ||||
|     (if partial | ||||
|       (let [[right remainder] partial] | ||||
|         [(list 'and left right) remainder]))))</pre></td></tr><tr><td class="docs"><p>Parse conditions from <code>tokens</code>, where conditions may be linked by either 'and' or 'or'.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-conditions | ||||
| </td><td class="codes"><pre class="brush: clojure">(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.</p> | |||
|           (= next "and") (parse-conjunction-condition left remainder) | ||||
|           (= next "or") (parse-disjunction-condition left remainder) | ||||
|           true partial)))))</pre></td></tr><tr><td class="docs"><p>Parse the left hand side ('if...') of a production rule.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-left-hand-side | ||||
|  [tokens] | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-left-hand-side | ||||
|  [[IF & tokens]] | ||||
|  (if | ||||
|    (= (first tokens) "if") | ||||
|    (parse-conditions (rest tokens))))</pre></td></tr><tr><td class="docs"><p>Parse actions of the form '[property] should be [property] [arithmetic-operator] [value]', | ||||
|    (= IF "if") | ||||
|    (parse-conditions tokens)))</pre></td></tr><tr><td class="docs"><p>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'.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-arithmetic-action  | ||||
| </td><td class="codes"><pre class="brush: clojure">(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.</p> | |||
|                                   (cond | ||||
|                                     (re-matches re-number value) (read-string value) | ||||
|                                     true (list 'get-int 'cell (keyword value))))}) rest]))</pre></td></tr><tr><td class="docs"><p>Parse actions of the form '[property] should be [value].'</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-set-action  | ||||
| </td><td class="codes"><pre class="brush: clojure">(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]))</pre></td></tr><tr><td class="docs"> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-simple-action [previous tokens] | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-simple-action [previous tokens] | ||||
|   (or (parse-arithmetic-action previous tokens) | ||||
|       (parse-set-action previous tokens)))</pre></td></tr><tr><td class="docs"><p>Parse actions from tokens.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-actions | ||||
| </td><td class="codes"><pre class="brush: clojure">(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)))))</pre></td></tr><tr><td class="docs"><p>Parse a probability of an action from this collection of tokens</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-probability  | ||||
| </td><td class="codes"><pre class="brush: clojure">(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.</p> | |||
|                           (first (parse-simple-value (list m) true))) | ||||
|                     (first (parse-simple-value (list n) true)))  | ||||
|               action) remainder])))) </pre></td></tr><tr><td class="docs"><p>Parse the right hand side ('then...') of a production rule.</p> | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn parse-right-hand-side | ||||
| </td><td class="codes"><pre class="brush: clojure">(defn- parse-right-hand-side | ||||
|   [[THEN & tokens]] | ||||
|   (if (= THEN "then") | ||||
|     (or | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue