// L-System Rule Parser
function RuleParser(str) {
  this.str = str;
  this.rules = {};

  this.parse = function() {
    var c, d;
    var env = 0;
    var rule_str;
    var variable;

    for (var i = 0; i < this.str.length; i++) {
      c = this.str[i];
      if (i > 0) {
	d = this.str[i-1];
      } else {
	d = null;
      }

      if (c == '(') {
	env = 1;
	rule_str = "";
      } else if ((c.search(/[A-Z]/) == 0 || c.search(/[\+\-\[\]]/) == 0)
                 && env == 1) {
	variable = c;
      } else if (c == '>' && d == '-') {
	env = 2;
      } else if ((c.search(/[A-Z]/) == 0 || c.search(/[\+-\[\]]/) == 0)
                 && env == 2) {
	rule_str += c;
      } else if (c == ')') {
	this.rules[variable] = rule_str;
	env = 0;
      }
    }

    return this.rules;
  }
}

module.exports = RuleParser;

@@ -178,16 +178,88 @@
 iable;%0A%0A
+    var variable_regex = /%5BA-Z%5D/;%0A    var control_regex = /%5B%5C+-%5C%5B%5C%5D%5D/;%0A%0A
     for 
@@ -460,33 +460,91 @@
 lse if (
-(c.search(/%5BA-Z%5D/
+c == '%3E' && d == '-') %7B%0A%09env = 3;%0A      %7D else if ((c.search(variable_regex
 ) == 0 %7C
@@ -558,20 +558,21 @@
 rch(
-/%5B%5C+%5C-%5C%5B%5C%5D%5D/
+control_regex
 ) ==
@@ -626,49 +626,8 @@
  c;%0A
-      %7D else if (c == '%3E' && d == '-') %7B%0A
 %09env
@@ -659,23 +659,30 @@
 .search(
-/%5BA-Z%5D/
+variable_regex
 ) == 0 %7C
@@ -692,27 +692,29 @@
 .search(
-/%5B%5C+-%5C%5B%5C%5D%5D/
+control_regex
 ) == 0)%0A
@@ -740,17 +740,17 @@
  env == 
-2
+3
 ) %7B%0A%09rul

Fix rule parser to allow certain rules