PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/parser/marshall.inc

http://drawshield.googlecode.com/
PHP | 281 lines | 228 code | 33 blank | 20 comment | 66 complexity | b21d0fd6aea34d03daa1f0ff61e407de MD5 | raw file
  1. <?php /* Copyright 2010 Karl R. Wilcox
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. function plain() {
  12. global $dom, $p_globals;
  13. search_match( array ( array( null, 'on a field ?of' )) );
  14. $node = null;
  15. if ( ($field = tincture() ) != null ) {
  16. $plain = $dom->createElement('plain');
  17. comma();
  18. $p_globals['offield'] = deep_copy($field->firstChild);
  19. $plain->appendChild ( $field );
  20. // Check for a second division
  21. if ( ($div2 = division(2)) != null ) {
  22. $tinc2 = $dom->createElement('tincture');
  23. $tinc2->appendChild($div2);
  24. $plain->appendChild($tinc2);
  25. }
  26. if ( ($ord_chgs = ord_chgs()) != null ) {
  27. $plain->appendChild($ord_chgs);
  28. }
  29. $node = $dom->createElement('shield');
  30. $node->setAttribute('index','0');
  31. $node->appendChild($plain);
  32. /* if ( !semicolon() and ($overall = overall()) != null )
  33. $node->appendChild($overall); */
  34. }
  35. return $node;
  36. }
  37. function overall() {
  38. global $dom;
  39. $overall = array (
  40. array ( true, 'overall' ),
  41. array ( true, 'over all' ),
  42. array ( true, 'over ?all ?the ?# (quarters|quarterings|halves|parts|sections)' ),
  43. array ( true, 'all with(in)?' ),
  44. );
  45. $node = null;
  46. if ( search_match($overall) != null ) {
  47. $node = $dom->createElement('overall');
  48. if ( ($over = ord_chgs()) == null ) {
  49. parser_message('blazon','Nothing to go "overall"');
  50. return null;
  51. }
  52. $node->appendChild($over);
  53. }
  54. return $node;
  55. }
  56. function quartered() {
  57. global $dom;
  58. $quartered = array (
  59. array ( 'ed', 'quartered' ), // Always a marshalling
  60. array ( 'ly', 'quarterly' ), // Could be a marshalling or a division
  61. );
  62. $quarters = array (
  63. array ( 1, '(first|1st|i|1|one)' ),
  64. array ( 2, '(second|2nd|ii|2|two)' ),
  65. array ( 3, '(third|3rd|iii|3|three)' ),
  66. array ( 4, '(fourth|4th|iv|iiii|4|four)' ),
  67. array ( 5, '(fifth|5th|v|5|five)' ),
  68. array ( 6, '(sixth|6th|vi|6|six)' ),
  69. array ( 7, '(seventh|7th|vii|7|seven)' ),
  70. array ( 8, '(eigth|8th|viii|8|eight)' ),
  71. );
  72. $subshields = array(0,null,null,null,null,null,null,null,null);
  73. $state = save();
  74. $node = null;
  75. if ( ($marshall = search_match($quartered)) != null ) {
  76. $quartered = $dom->createElement('quartered');
  77. $foundQuarter = false;
  78. do { // Look for quarters
  79. $found = false;
  80. $indices = array();
  81. while ( ($match = search_match($quarters)) != null ) {
  82. $indices[] = $match[0]; // make a list of quarter numbers
  83. comma();
  84. andd();
  85. $found = $foundQuarter = true;
  86. }
  87. if ( $found ) {
  88. $quarter = shield(); // Look for the description of this quarter
  89. if ( $quarter != null and count($indices) > 1 ) {
  90. $quarter->setAttribute( 'ID',($ID=unique('shield')));
  91. $quarter->setIdAttribute('ID',true);
  92. }
  93. if ( $subshields[$indices[0]] != null )
  94. parser_message('error','Duplicate quarter' );
  95. else
  96. $subshields[$indices[0]] = $quarter; // Set the first quarter to this description
  97. for ( $i = 1; $i < count($indices); $i++ ) {
  98. $reference = $dom->createElement('shield');
  99. $reference->setAttribute('IDREF',$ID); // Set other quarters to point to this one
  100. if ( $subshields[$indices[$i]] != null )
  101. parser_message('error','Duplicate quarter' );
  102. else
  103. $subshields[$indices[$i]] = $reference;
  104. }
  105. } // Keep going as long as we find quarters
  106. } while ( $found );
  107. if ( $marshall[0] == 'ly' and !$foundQuarter ) {
  108. restore($state); // backtrack, assume this is quarterly division
  109. return null;
  110. }
  111. $highest = false;
  112. for ( $i = 8; $i > 0; $i-- ) {
  113. if ( $subshields[$i] == null ) {
  114. if ( !$highest )
  115. continue;
  116. else { // highest already been found
  117. $subshields[$i] = $dom->createElement('missing');
  118. parser_message('blazon','Missing quarter (' . $i . ')');
  119. }
  120. } else { // NOT null
  121. if ( !$highest )
  122. $highest = $i;
  123. $subshields[$i]->setAttribute('index',"$i");
  124. }
  125. }
  126. $quartered->setAttribute('order',$highest);
  127. for ( $i = 1; $i <= $highest; $i++ )
  128. $quartered->appendChild($subshields[$i]);
  129. $node = $dom->createElement('shield');
  130. $node->setAttribute('index','0');
  131. $node->appendChild($quartered);
  132. }
  133. return $node;
  134. }
  135. function halved() {
  136. global $dom;
  137. $splits = array (
  138. array ( 'impaled', 'impaled !with' ),
  139. array ( 'dimidiated', 'dimidiated !with' ),
  140. );
  141. $with = array (
  142. array ( true, '(with|upon)' ),
  143. );
  144. $node = null;
  145. if ( ($match = search_match($splits)) != null ) {
  146. $half = $dom->createElement($match[0]);
  147. if ( ($shield1 = shield()) == null ) {
  148. parser_error('warning', 'Expected 1st shield description');
  149. $shield1 = $dom->createElement('missing');
  150. }
  151. $shield1->setAttribute('index','1');
  152. if ( ! search_match($with) ) {
  153. // uncomment to require "impaled A with B"
  154. // parser_error('warning','expected "with"');
  155. // return $shield1;
  156. }
  157. if ( ($shield2 = shield()) == null ) {
  158. parser_error('warning', 'Expected 2nd shield description');
  159. $shield2 = $dom->createElement('missing');
  160. }
  161. $shield2->setAttribute('index','2');
  162. $half = $dom->createElement($match[0]);
  163. $half->appendChild($shield1);
  164. $half->appendChild($shield2);
  165. $node = $dom->createElement('shield');
  166. $node->setAttribute('index','0');
  167. $node->appendChild($half);
  168. }
  169. return $node;
  170. }
  171. function parse() {
  172. global $pending_items;
  173. $node = shield();
  174. semicolon();
  175. if ( ($overall = overall()) != null )
  176. $node->appendChild($overall);
  177. foreach ( $pending_items as $item ) {
  178. $item->setAttribute('origin','missing');
  179. parser_message('blazon', 'Tincture missing');
  180. }
  181. return $node;
  182. }
  183. function builtin() {
  184. global $cur_word;
  185. $theshieldof = array (
  186. array ( true, '?the (shield|arms) of' ),
  187. );
  188. $state = save();
  189. $retval = null;
  190. search_match($theshieldof);
  191. if ( ($named_blazon = checkDB()) != null ) {
  192. context('push');
  193. fill_words($named_blazon);
  194. $retval = shield();
  195. context('pop');
  196. $cur_word += 1;
  197. } else
  198. restore($state);
  199. return $retval;
  200. }
  201. function shield() {
  202. global $dom;
  203. $splits = array (
  204. array ( 'impaled', 'impaled with' ),
  205. array ( 'dimidiated', 'dimidiated with' ),
  206. );
  207. $node1 = builtin();
  208. if ( $node1 == null ) $node1 = quartered();
  209. if ( $node1 == null ) $node1 = halved();
  210. if ( $node1 == null ) $node1 = plain();
  211. if ( $node1 == null ) {
  212. $node1 = $dom->createElement('shield');
  213. $node1->appendChild($dom->createElement('missing'));
  214. parser_message('blazon', 'Expected a shield description');
  215. }
  216. if ( !semicolon() and ($overall = overall()) != null )
  217. $node1->appendChild($overall);
  218. $retval = $node1;
  219. // Now do it all again, in case of "A impaled with B"
  220. if ( ($match = search_match($splits)) != null ) {
  221. $node2 = builtin();
  222. if ( $node2 == null ) $node2 = quartered();
  223. if ( $node2 == null ) $node2 = halved();
  224. if ( $node2 == null ) $node2 = plain();
  225. if ( $node2 == null ) {
  226. $node2 = $dom->createElement('shield');
  227. $node2->appendChild($dom->createElement('missing'));
  228. parser_message('blazon', 'Expected a shield description');
  229. }
  230. if ( !semicolon() and ($overall = overall()) != null )
  231. $node2->appendChild($overall);
  232. // Got two nodes, now create a mother node
  233. $motherNode = $dom->createElement('shield');
  234. $motherNode->setAttribute('index','0');
  235. $daughterNode = $dom->createElement($match[0]);
  236. $node1->setAttribute('index','1');
  237. $node2->setAttribute('index','2');
  238. $daughterNode->appendChild($node1);
  239. $daughterNode->appendChild($node2);
  240. $motherNode->appendChild($daughterNode);
  241. $retval = $motherNode;
  242. }
  243. return $retval;
  244. }
  245. ?>