title: What About Abstract Syntax Tree Recursivity In Javascript?
body: <p>I am trying to build a compiler in javascript and until now I've managed to build a lexer that creates tokens based on input: </p>
  
  <p>= Test Input (with optional semicolon):</p>
  
  <pre><code>data myVariable = 4 
  data myVariable2 = "myName";
  task Eat {
      receives (whatToEat : String, howMuchTime : Float)
      print whatToEat
      returns (Nothing : Void)
  }
  </code></pre>
  
  <p>= Actual Lexer Result (From Console - The Array Of Tokens):</p>
  
  <pre><code>[0: {content: "data", denominal: "keyword"}
   1: {content: "myVariable", denominal: "identifier"}
   2: {content: "=", denominal: "operator"}
   3: {content: "4", denominal: "number"}
  
   4: {content: "data", denominal: "keyword"}
   5: {content: "myVariable2", denominal: "identifier"}
   6: {content: "=", denominal: "operator"}
   7: {content: ""myName"", denominal: "string"}
   8: {content: ";", denominal: "punctuator"}
  
   9: {content: "task", denominal: "keyword"}
   10: {content: "Eat", denominal: "identifier"}
   11: {content: "{", denominal: "punctuator"}
   12: {content: "receives", denominal: "identifier"}
   13: {content: "(", denominal: "punctuator"}
   14: {content: "whatToEat", denominal: "identifier"}
   15: {content: ":", denominal: "punctuator"}
   16: {content: "String", denominal: "identifier"}
   17: {content: ",", denominal: "punctuator"}
   18: {content: "howMuchTime", denominal: "identifier"}
   19: {content: ":", denominal: "punctuator"}
   20: {content: "Float", denominal: "identifier"}
   21: {content: ")", denominal: "punctuator"}
   22: {content: "print", denominal: "keyword"}
   23: {content: "whatToEat", denominal: "identifier"}
   24: {content: "returns", denominal: "keyword"}
   25: {content: "(", denominal: "punctuator"}
   26: {content: "Nothing", denominal: "identifier"}
   27: {content: ":", denominal: "punctuator"}
   28: {content: "Void", denominal: "identifier"}
   29: {content: ")", denominal: "punctuator"}
   30: {content: "}", denominal: "punctuator"}]
  </code></pre>
  
  <p>the lexer is just doing fine (data and task being <strong><em>keywords</em></strong> for <em>variable</em> and <em>function</em>) <strong>BUT</strong> i would want to create something regex-like that captures me a function declaration, a variable declaration etc. <strong><em>USING ONLY</em></strong> this current token object as input
  If it would have been text, I would have captured the function declaration with following regex:</p>
  
  <pre><code>task\s+[a-zA-Z][a-zA-Z0-9]*\s*\{\s*(1)*\s*\}
  </code></pre>
  
  <p>(1) being regex code for instruction block, including keyword functions as receives etc.</p>
  
  <p>Is there a way to match variable / function declarations, in this case starting from an index that changes during a for loop?</p>
  
  <p><strong><em>for example:</em></strong></p>
  
  <p>= I've passed through my token list using a <strong><em>for loop</em></strong> and at index 9 I've found <strong>this</strong> object for the <strong>first</strong> time:</p>
  
  <pre><code>9: {content: "task", denominal: "keyword"}
  </code></pre>
  
  <p>= Now, i want to start searching for a function declaration on the object. This implies:</p>
  
  <p>1) - if the function <strong><em>is correct</em></strong> as <em>declaration</em>, <em>parantheses</em> etc. etc.</p>
  
  <p>2) - <strong><em>how many objects</em></strong> does this function imply - like <strong><em>from index 9 to index 30</em></strong>, all these objects <strong>form a function called</strong> 'Eat', <strong>which has</strong> 3 instruction blocks:</p>
  
  <ul>
  <li><p>1 special <strong><em>receives</em></strong> instruction block, put <strong><em>mandatory</em></strong> at start of the function (even empty), containing arguments as correct format <strong>[variableName : variableType]</strong> </p></li>
  <li><p>1 special <strong><em>print</em></strong> instruction block with its parameters given correctly</p></li>
  <li>1 special <strong><em>returns</em></strong> instruction block, put <strong><em>mandatory</em></strong> at start of the function (if empty, returning Nothing : Void), containing arguments as correct format <strong>[variableName : variableType]</strong></li>
  </ul>
  
  <p>3) - where to <strong><em>stop</em></strong>, so now I know the function definition is <strong><em>over</em></strong> and I can start searching from the <strong><em>final index + 1 = 31</em></strong> in this case, for other things (ex. variable declarations, EOF etc.)</p>
  
  <p>If you are kind to tell me the method(s) so I can establish the existence of a specific instruction block, creating the upper function description example, it would be awesome!</p>
  
  <p>The ideal result (for this problem) would be an array like this:</p>
  
  <pre><code>Instruction Object:
  
  [0: {
       "instruction": "variable_declaration",
       "variable_name": "myVariable",
       "variable_value": "4",
       "variable_type": "Integer"
      }
   1: {
       "instruction": "variable_declaration",
       "variable_name": "myVariable2",
       "variable_value": ""myName"",
       "variable_type": "String"
      }
   2: {
       "instruction": "function_declaration",
       "function_name": "Eat",
       "body_instructions": [0: {
                                 "instruction": "receives_instruction", 
                                 "arguments": [0: {
                                                   "argument_name": "whatToEat",
                                                   "argument_type": "String"
                                                  }
                                               1: {
                                                   "argument_name": "howMuchTime",
                                                   "argument_type": "Float"
                                                  }]
                             1: {
                                 "instruction": "print_instruction",
                                 "arguments": [0: {
                                                   "argument_name": "myVariable",
                                                   "argument_value": "4",
                                                   "argument_type": "Integer"
                                                  }]
                             2: {
                                 "instruction": "returns_instruction", 
                                 "arguments": [0: {
                                                   "argument_name": "Nothing",
                                                   "argument_value": "",
                                                   "argument_type": "Void"
                                                  }]
                                }]
      }] // EOF object optional
  
  </code></pre>
  
  <p><strong>I appreciate all your help!</strong></p>
  
  <p><strong>Thanks a lot in advance!</strong></p>