Advertisement
Guest User

Parser

a guest
Feb 2nd, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. Node ParseText(string text)
  2.         {
  3.             if (!text.StartsWith("{")) return null;
  4.             Node node = new Node();
  5.             string buf = "";
  6.             int brackets = 0;
  7.             bool quotes = false;
  8.             string unbracket = text.Substring(1, text.Length - 2);
  9.             foreach (char c in unbracket)
  10.             {
  11.                 if (c == '{')
  12.                     brackets++;
  13.                 if (c == '"')
  14.                     quotes = !quotes;
  15.                 if (c == ',' && brackets == 0 && !quotes)
  16.                 {
  17.                     Node parsed = ParseText(buf) ?? new Node() { Value = buf };
  18.                     node.Nodes.Add(parsed);
  19.                     buf = "";
  20.                 }
  21.                 else
  22.                 {
  23.                     buf += c;
  24.                 }
  25.                 if (c == '}')
  26.                     brackets--;
  27.             }
  28.             if (buf != "")
  29.             {
  30.                 Node parsed = ParseText(buf) ?? new Node() { Value = buf };
  31.                 node.Nodes.Add(parsed);
  32.             }
  33.             return node;
  34.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement