context: // Generated by TinyPG v1.3 available at www.codeproject.com
  
  using System;
  using System.Collections.Generic;
  
  namespace kOS.Safe.Compilation.KS
  {
      #region Parser
  
      public partial class Parser 
      {
          private Scanner scanner;
          private ParseTree tree;
          
          public Parser(Scanner scanner)
          {
              this.scanner = scanner;
          }
  
           public ParseTree Parse(string input)
          {
              return Parse(input, "", new ParseTree());
          }
  
          public ParseTree Parse(string input, string fileName)
          {
              return Parse(input, fileName, new ParseTree());
          }
  
          public ParseTree Parse(string input, string fileName, ParseTree tree)
          {
              scanner.Init(input, fileName);
  
              this.tree = tree;
              ParseStart(tree);
              tree.Skipped = scanner.Skipped;
  
              return tree;
          }
  
          private void ParseStart(ParseNode parent)
          {
              Token tok;
              ParseNode n;
              ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.Start), "Start");
              parent.Nodes.Add(node);
  
  
              
              tok = scanner.LookAhead(TokenType.SET, TokenType.IF, TokenType.UNTIL, TokenType.FROM, TokenType.UNLOCK, TokenType.PRINT, TokenType.ON, TokenType.TOGGLE, TokenType.WAIT, TokenType.WHEN, TokenType.STAGE, TokenType.CLEARSCREEN, TokenType.ADD, TokenType.REMOVE, TokenType.LOG, TokenType.BREAK, TokenType.PRESERVE, TokenType.PARAMETER, TokenType.FUNCTION, TokenType.LOCK, TokenType.DECLARE, TokenType.LOCAL, TokenType.GLOBAL, TokenType.RETURN, TokenType.SWITCH, TokenType.COPY, TokenType.RENAME, TokenType.DELETE, TokenType.EDIT, TokenType.RUN, TokenType.COMPILE, TokenType.LIST, TokenType.REBOOT, TokenType.SHUTDOWN, TokenType.FOR, TokenType.UNSET, TokenType.CURLYOPEN, TokenType.INTEGER, TokenType.DOUBLE, TokenType.TRUEFALSE, TokenType.IDENTIFIER, TokenType.FILEIDENT, TokenType.BRACKETOPEN, TokenType.STRING, TokenType.ATSIGN);
              while (tok.Type == TokenType.SET
                  || tok.Type == TokenType.IF
                  || tok.Type == TokenType.UNTIL
                  || tok.Type == TokenType.FROM
                  || tok.Type == TokenType.UNLOCK
                  || tok.Type == TokenType.PRINT
                  || tok.Type == TokenType.ON
                  || tok.Type == TokenType.TOGGLE
                  || tok.Type == TokenType.WAIT
                  || tok.Type == TokenType.WHEN
                  || tok.Type == TokenType.STAGE
                  || tok.Type == TokenType.CLEARSCREEN
                  || tok.Type == TokenType.ADD
                  || tok.Type == TokenType.REMOVE
                  || tok.Type == TokenType.LOG
                  || tok.Type == TokenType.BREAK
                  || tok.Type == TokenType.PRESERVE
                  || tok.Type == TokenType.PARAMETER
                  || tok.Type == TokenType.FUNCTION
                  || tok.Type == TokenType.LOCK
                  || tok.Type == TokenType.DECLARE
                  || tok.Type == TokenType.LOCAL
                  || tok.Type == TokenType.GLOBAL
                  || tok.Type == TokenType.RETURN
                  || tok.Type == TokenType.SWITCH
                  || tok.Type == TokenType.COPY
                  || tok.Type == TokenType.RENAME
                  || tok.Type == TokenType.DELETE
                  || tok.Type == TokenType.EDIT
                  || tok.Type == TokenType.RUN
                  || tok.Type == TokenType.COMPILE
                  || tok.Type == TokenType.LIST
                  || tok.Type == TokenType.REBOOT
                  || tok.Type == TokenType.SHUTDOWN
                  || tok.Type == TokenType.FOR
                  || tok.Type == TokenType.UNSET
                  || tok.Type == TokenType.CURLYOPEN
                  || tok.Type == TokenType.INTEGER
                  || tok.Type == TokenType.DOUBLE
                  || tok.Type == TokenType.TRUEFALSE
                  || tok.Type == TokenType.IDENTIFIER
                  || tok.Type == TokenType.FILEIDENT
                  || tok.Type == TokenType.BRACKETOPEN
                  || tok.Type == TokenType.STRING
                  || tok.Type == TokenType.ATSIGN)
              {
                  Parseinstruction(node);
              tok = scanner.LookAhead(TokenType.SET, TokenType.IF, TokenType.UNTIL, TokenType.FROM, TokenType.UNLOCK, TokenType.PRINT, TokenType.ON, TokenType.TOGGLE, TokenType.WAIT, TokenType.WHEN, TokenType.STAGE, TokenType.CLEARSCREEN, TokenType.ADD, TokenType.REMOVE, TokenType.LOG, TokenType.BREAK, TokenType.PRESERVE, TokenType.PARAMETER, TokenType.FUNCTION, TokenType.LOCK, TokenType.DECLARE, TokenType.LOCAL, TokenType.GLOBAL, TokenType.RETURN, TokenType.SWITCH, TokenType.COPY, TokenType.RENAME, TokenType.DELETE, TokenType.EDIT, TokenType.RUN, TokenType.COMPILE, TokenType.LIST, TokenType.REBOOT, TokenType.SHUTDOWN, TokenType.FOR, TokenType.UNSET, TokenType.CURLYOPEN, TokenType.INTEGER, TokenType.DOUBLE, TokenType.TRUEFALSE, TokenType.IDENTIFIER, TokenType.FILEIDENT, TokenType.BRACKETOPEN, TokenType.STRING, TokenType.ATSIGN);
              }
  
              
              tok = scanner.Scan(TokenType.EOF);
              n = node.CreateNode(tok, tok.ToString() );
              node.Token.UpdateRange(tok);
              node.Nodes.Add(n);
              if (tok.Type != TokenType.EOF) {
                  tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.EOF.ToString(), 0x1001, tok));
                  return;
              }
  
              parent.Token.UpdateRange(node.Token);
          }
  
          private void Parseinstruction_block(ParseNode parent)
          {
              Token tok;
              ParseNode n;
              ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.instruction_block), "instruction_block");
              parent.Nodes.Add(node);
  
  
              
              tok = scanner.Scan(TokenType.CURLYOPEN);
              n = node.CreateNode(tok, tok.ToString() );
              node.Token.UpdateRange(tok);
              node.Nodes.Add(n);
              if (tok.Type != TokenType.CURLYOPEN) {
                  tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.CURLYOPEN.ToString(), 0x1001, tok));
                  return;
              }
  
              
              tok = scanner.LookAhead(TokenType.SET, TokenType.IF, TokenType.UNTIL, TokenType.FROM, TokenType.UNLOCK, TokenType.PRINT, TokenType.ON, TokenType.TOGGLE, TokenType.WAIT, TokenType.WHEN, TokenType.STAGE, TokenType.CLEARSCREEN, TokenType.ADD, TokenType.REMOVE, TokenType.LOG, TokenType.BREAK, TokenType.PRESERVE, TokenType.PARAMETER, TokenType.FUNCTION, TokenType.LOCK, TokenType.DECLARE, TokenType.LOCAL, TokenType.GLOBAL, TokenType.RETURN, TokenType.SWITCH, TokenType.COPY, TokenType.RENAME, TokenType.DELETE, TokenType.EDIT, TokenType.RUN, TokenType.COMPILE, TokenType.LIST, TokenType.REBOOT, TokenType.SHUTDOWN, TokenType.FOR, TokenType.UNSET, TokenType.CURLYOPEN, TokenType.INTEGER, TokenType.DOUBLE, TokenType.TRUEFALSE, TokenType.IDENTIFIER, TokenType.FILEIDENT, TokenType.BRACKETOPEN, TokenType.STRING, TokenType.ATSIGN);
              while (tok.Type == TokenType.SET
                  || tok.Type == TokenType.IF
                  || tok.Type == TokenType.UNTIL
                  || tok.Type == TokenType.FROM
                  || tok.Type == TokenType.UNLOCK
                  || tok.Type == TokenType.PRINT
                  || tok.Type == TokenType.ON
                  || tok.Type == TokenType.TOGGLE
                  || tok.Type == TokenType.WAIT
                  || tok.Type == TokenType.WHEN
                  || tok.Type == TokenType.STAGE
                  || tok.Type == TokenType.CLEARSCREEN
                  || tok.Type == TokenType.ADD
                  || tok.Type == TokenType.REMOVE
                  || tok.Type == TokenType.LOG
                  || tok.Type == TokenType.BREAK
                  || tok.Type == TokenType.PRESERVE
                  || tok.Type == TokenType.PARAMETER
                  || tok.Type == TokenType.FUNCTION
                  || tok.Type == TokenType.LOCK
                  || tok.Type == TokenType.DECLARE
                  || tok.Type == TokenType.LOCAL
                  || tok.Type == TokenType.GLOBAL
                  || tok.Type == TokenType.RETURN
                  || tok.Type == TokenType.SWITCH
                  || tok.Type == TokenType.COPY
                  || tok.Type == TokenType.RENAME
                  || tok.Type == TokenType.DELETE
                  || tok.Type == TokenType.EDIT
                  || tok.Type == TokenType.RUN
                  || tok.Type == TokenType.COMPILE
                  || tok.Type == TokenType.LIST
                  || tok.Type == TokenType.REBOOT
                  || tok.Type == TokenType.SHUTDOWN
                  || tok.Type == TokenType.FOR
                  || tok.Type == TokenType.UNSET
                  || tok.Type == TokenType.CURLYOPEN
                  || tok.Type == TokenType.INTEGER
                  || tok.Type == TokenType.DOUBLE
                  || tok.Type == TokenType.TRUEFALSE
                  || tok.Type == TokenType.IDENTIFIER
                  || tok.Type == TokenType.FILEIDENT
                  || tok.Type == TokenType.BRACKETOPEN
                  || tok.Type == TokenType.STRING
                  || tok.Type == TokenType.ATSIGN)
              {
                  Parseinstruction(node);
              tok = scanner.LookAhead(TokenType.SET, TokenType.IF, TokenType.UNTIL, TokenType.FROM, TokenType.UNLOCK, TokenType.PRINT, TokenType.ON, TokenType.TOGGLE, TokenType.WAIT, TokenType.WHEN, TokenType.STAGE, TokenType.CLEARSCREEN, TokenType.ADD, TokenType.REMOVE, TokenType.LOG, TokenType.BREAK, TokenType.PRESERVE, TokenType.PARAMETER, TokenType.FUNCTION, TokenType.LOCK, TokenType.DECLARE, TokenType.LOCAL, TokenType.GLOBAL, TokenType.RETURN, TokenType.SWITCH, TokenType.COPY, TokenType.RENAME, TokenType.DELETE, TokenType.EDIT, TokenType.RUN, TokenType.COMPILE, TokenType.LIST, TokenType.REBOOT, TokenType.SHUTDOWN, TokenType.FOR, TokenType.UNSET, TokenType.CURLYOPEN, TokenType.INTEGER, TokenType.DOUBLE, TokenType.TRUEFALSE, TokenType.IDENTIFIER, TokenType.FILEIDENT, TokenType.BRACKETOPEN, TokenType.STRING, TokenType.ATSIGN);
              }
  
              
              tok = scanner.Scan(TokenType.CURLYCLOSE);
              n = node.CreateNode(tok, tok.ToString() );
              node.Token.UpdateRange(tok);
              node.Nodes.Add(n);
              if (tok.Type != TokenType.CURLYCLOSE) {
                  tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.CURLYCLOSE.ToString(), 0x1001, tok));
                  return;
              }
  
              
              tok = scanner.LookAhead(TokenType.EOI);
              if (tok.Type == TokenType.EOI)
              {
                  tok = scanner.Scan(TokenType.EOI);
                  n = node.CreateNode(tok, tok.ToString() );
                  node.Token.UpdateRange(tok);
                  node.Nodes.Add(n);
                  if (tok.Type != TokenType.EOI) {
                      tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.EOI.ToString(), 0x1001, tok));
                      return;
                  }
              }
  
              parent.Token.UpdateRange(node.Token);
          }
  
          private void Parseinstruction(ParseNode parent)
          {
              Token tok;
              ParseNode n;
              ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.instruction), "instruction");
              parent.Nodes.Add(node);
  
              tok = scanner.LookAhead(TokenType.SET, TokenType.IF, TokenType.UNTIL, TokenType.FROM, TokenType.UNLOCK, TokenType.PRINT, TokenType.ON, TokenType.TOGGLE, TokenType.WAIT, TokenType.WHEN, TokenType.STAGE, TokenType.CLEARSCREEN, TokenType.ADD, TokenType.REMOVE, TokenType.LOG, TokenType.BREAK, TokenType.PRESERVE, TokenType.PARAMETER, TokenType.FUNCTION, TokenType.LOCK, TokenType.DECLARE, TokenType.LOCAL, TokenType.GLOBAL, TokenType.RETURN, TokenType.SWITCH, TokenType.COPY, TokenType.RENAME, TokenType.DELETE, TokenType.EDIT, TokenType.RUN, TokenType.COMPILE, TokenType.LIST, TokenType.REBOOT, TokenType.SHUTDOWN, TokenType.FOR, TokenType.UNSET, TokenType.CURLYOPEN, TokenType.INTEGER, TokenType.DOUBLE, TokenType.TRUEFALSE, TokenType.IDENTIFIER, TokenType.FILEIDENT, TokenType.BRACKETOPEN, TokenType.STRING, TokenType.ATSIGN);
              switch (tok.Type)
              {
                  case TokenType.SET:
                      Parseset_stmt(node);
                      break;
                  case TokenType.IF:
                      Parseif_stmt(node);
                      break;
                  case TokenType.UNTIL:
                      Parseuntil_stmt(node);
                      break;
                  case TokenType.FROM:
                      Parsefromloop_stmt(node);
                      break;
                  case TokenType.UNLOCK:
                      Parseunlock_stmt(node);
                      break;
                  case TokenType.PRINT:
                      Parseprint_stmt(node);
                      break;
                  case TokenType.ON:
                      Parseon_stmt(node);
                      break;
                  case TokenType.TOGGLE:
                      Parsetoggle_stmt(node);
                      break;
                  case TokenType.WAIT:
                      Parsewait_stmt(node);
                      break;
                  case TokenType.WHEN:
                      Parsewhen_stmt(node);
                      break;
                  case TokenType.STAGE:
                      Parsestage_stmt(node);
                      break;
                  case TokenType.CLEARSCREEN:
                      Parseclear_stmt(node);
                      break;
                  case TokenType.ADD:
                      Parseadd_stmt(node);
                      break;
                  case TokenType.REMOVE:
                      Parseremove_stmt(node);
                      break;
                  case TokenType.LOG:
                      Parselog_stmt(node);
                      break;
                  case TokenType.BREAK:
                      Parsebreak_stmt(node);
                      break;
                  case TokenType.PRESERVE:
                      Parsepreserve_stmt(node);
                      break;
                  case TokenType.PARAMETER:
                  case TokenType.FUNCTION:
                  case TokenType.LOCK:
                  case TokenType.DECLARE:
                  case TokenType.LOCAL:
                  case TokenType.GLOBAL:
                      Parsedeclare_stmt(node);
                      break;
                  case TokenType.RETURN:
                      Parsereturn_stmt(node);
                      break;
                  case TokenType.SWITCH:
                      Parseswitch_stmt(node);
                      break;
                  case TokenType.COPY:
                      Parsecopy_stmt(node);
                      break;
                  case TokenType.RENAME:
                      Parserename_stmt(node);
                      break;
                  case TokenType.DELETE:
                      Parsedelete_stmt(node);
                      break;
                  case TokenType.EDIT:
                      Parseedit_stmt(node);
                      break;
                  case TokenType.RUN:
                      Parserun_stmt(node);
                      break;
                  case TokenType.COMPILE:
                      Parsecompile_stmt(node);
                      break;
                  case TokenType.LIST:
                      Parselist_stmt(node);
                      break;
                  case TokenType.REBOOT:
                      Parsereboot_stmt(node);
                      break;
                  case TokenType.SHUTDOWN:
                      Parseshutdown_stmt(node);
                      break;
                  case TokenType.FOR:
                      Parsefor_stmt(node);
                      break;
                  case TokenType.UNSET:
                      Parseunset_stmt(node);
                      break;
                  case TokenType.CURLYOPEN:
                      Parseinstruction_block(node);
                      break;
                  case TokenType.INTEGER:
                  case TokenType.DOUBLE:
                  case TokenType.TRUEFALSE:
                  case TokenType.IDENTIFIER:
                  case TokenType.FILEIDENT:
                  case TokenType.BRACKETOPEN:
                  case TokenType.STRING:
                      Parseidentifier_led_stmt(node);
                      break;
                  case TokenType.ATSIGN:
                      Parsedirective(node);
                      break;
                  default:
                      tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found.", 0x0002, tok));
                      break;
              }
  
              parent.Token.UpdateRange(node.Token);
          }
  
          private void Parselazyglobal_directive(ParseNode parent)
          {
              Token tok;
              ParseNode n;
              ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.lazyglobal_directive), "lazyglobal_directive");
              parent.Nodes.Add(node);
  
  
              
              tok = scanner.Scan(TokenType.ATSIGN);
              n = node.CreateNode(tok, tok.ToString() );
              node.Token.UpdateRange(tok);
              node.Nodes.Add(n);
              if (tok.Type != TokenType.ATSIGN) {
                  tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.ATSIGN.ToString(), 0x1001, tok));
                  return;
              }
  
              
  
gt:             tok = scanner.Scan(TokenType.LAZYGLOBAL);