private void MatchCurlyBraces(MemoryStream<JavaToken> ts)
 {
     while (!ts.TryMatch(t => t.Type == JavaTokenType.Operator && t.Value == "}"))
         if (ts.TryMatch(t => t.Type == JavaTokenType.Operator && t.Value == "{"))
             MatchCurlyBraces(ts);
         else
             ts.Read();
 }
 private void ParseClassBody(MemoryStream<JavaToken> ts)
 {
     while (!ts.TryMatch(t => t.Type == JavaTokenType.Operator && t.Value == "}"))
     {
         int pos = ts.Position;
         if (ts.TryMatch(t => t.Type == JavaTokenType.Operator && t.Value == "{"))
             MatchCurlyBraces(ts);
         else if (ts.TryMatch(t => t.Type == JavaTokenType.Keyword && (t.Value == "public" || t.Value == "protected" || t.Value == "private")))
             ParsePossibleMethod(ts, pos);
         else
             ts.Read();
     }
 }
 private void ParsePossibleMethod(MemoryStream<JavaToken> ts, int pos)
 {
     while (!ts.TryMatch(t => t.Type == JavaTokenType.Operator && t.Value == ";"))
     {
         if (ts.TryMatch(t => t.Type == JavaTokenType.Operator && t.Value == "{"))
         {
             MatchCurlyBraces(ts);
             m_MethodBlocks.Add(new JavaMethodBlock(new SimpleTextSpan(m_Tokens[pos].Location.Start, m_Tokens[ts.Position - 1].Location.End)));
             return;
         }
         else
             ts.Read();
     }
 }
 protected JavaScriptBlock TryParseLVar(MemoryStream<JavaScriptToken> ts)
 {
     int before = ts.Position;
     if (ts.TryMatch((token) => token.Type == JavaScriptTokenType.Identifier))
     {
         JavaScriptBlock block = new JavaScriptBlock(before, JavaScriptBlockType.LVar, this, m_TopLevel, m_Tokens);
         while (true)
         {
             while (block.TryParseBrackets(ts, "[", "]") != null)
                 ;
             if (!ts.TryMatch((token) => token.Type == JavaScriptTokenType.Operator && token.Value == "."))
                 break;
             if (!ts.TryMatch((token) => token.Type == JavaScriptTokenType.Identifier))
                 break;
         }
         block.m_SubBlocks.Clear(); // Don't want sub blocks in LVar blocks.
         AddBlock(block, ts);
         return block;
     }
     return null;
 }
 protected JavaScriptBlock TryParseFunction(MemoryStream<JavaScriptToken> ts)
 {
     int before = ts.Position;
     if (ts.TryMatch((token) => token.Type == JavaScriptTokenType.Keyword && token.Value == "function"))
     {
         JavaScriptBlock block = new JavaScriptBlock(before, JavaScriptBlockType.Function, this, m_TopLevel, m_Tokens);
         block.TryMatchComments(ts);
         block.TryParseLVar(ts); // Too generic for optional function name, but never mind
         block.TryMatchComments(ts);
         block.TryParseBrackets(ts, "(", ")");
         block.TryMatchComments(ts);
         block.m_FunctionBody = block.TryParseBrackets(ts, "{", "}");
         AddBlock(block, ts);
         return block;
     }
     return null;
 }
 protected JavaScriptBlock TryParseBrackets(MemoryStream<JavaScriptToken> ts, string ch, string endCh)
 {
     int before = ts.Position;
     if (ts.TryMatch((token) => token.Type == JavaScriptTokenType.Operator && token.Value == ch))
     {
         JavaScriptBlock block = new JavaScriptBlock(before, JavaScriptBlockType.Brackets, this, m_TopLevel, m_Tokens);
         block.TryParseAny(ts, (token) => token.Type == JavaScriptTokenType.Operator && token.Value == endCh);
         ts.Read();
         AddBlock(block, ts);
         return block;
     }
     return null;
 }
 protected void TryMatchComments(MemoryStream<JavaScriptToken> ts)
 {
     while (ts.TryMatch(token => token.Type == JavaScriptTokenType.Comment || token.Type == JavaScriptTokenType.MultiLineComment))
         ;
 }
 private void ParseStream(MemoryStream<JavaToken> ts)
 {
     while (true)
     {
         if (ts.TryMatch(t => t.Type == JavaTokenType.Keyword && t.Value == "class"))
             ParseClass(ts);
         else
             ts.Read();
     }
 }
 private void ParseClass(MemoryStream<JavaToken> ts)
 {
     while (!ts.TryMatch(t => t.Type == JavaTokenType.Operator && t.Value == "{"))
         ts.Read();
     ParseClassBody(ts);
 }