Advertisement
sashomaga

Parse string

Feb 1st, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. class Program
  8. {
  9.     static void Main()
  10.     {
  11.         List<string> rows = new List<string>();
  12.         while (true)
  13.         {
  14.             string row = Console.ReadLine();
  15.             rows.Add(row);
  16.             if (row.StartsWith("?>"))
  17.             {
  18.                 break;
  19.             }
  20.         }
  21.         bool inNormalBrackets2 = false;
  22.         bool inNormalBrackets = false;
  23.         bool inComment = false; /* global */
  24.         bool inDoubleQuotes = false; //
  25.         bool inSingleQuotes = false;
  26.         bool inBrackets = false;
  27.         bool isVariable = false; //
  28.        
  29.         StringBuilder sb = new StringBuilder();
  30.         List<string> variables = new List<string>();
  31.         for (int i = 0; i < rows.Count; i++)
  32.         {
  33.             List<string> buffer = new List<string>();
  34.             string nextRow;
  35.             nextRow = rows[i];
  36.             char nextChar;
  37.             for (int pos = 0; pos < nextRow.Length; pos++)
  38.             {
  39.                 nextChar = nextRow[pos];
  40.                 string test = nextChar.ToString();
  41.                 if (nextChar == ';')
  42.                 {
  43.                     foreach (var item in buffer)
  44.                     {
  45.                         if (!variables.Contains(item))
  46.                         {
  47.                             variables.Add(item);
  48.                         }
  49.                     }
  50.                 }
  51.                 if (isVariable == true)
  52.                 {
  53.                     if (sb.Length == 0)
  54.                     {
  55.                        if (Regex.Matches(test,"[a-zA-Z_]+").Count > 0)
  56.                        {
  57.                          
  58.                        }
  59.                        else
  60.                        {
  61.                            isVariable = false;
  62.                            continue;
  63.                        }
  64.                     }
  65.                    
  66.                     if (Regex.Matches(test,"\\w+").Count == 0)
  67.                     {
  68.                         if (sb.Length > 0)
  69.                         {
  70.                             if (!buffer.Contains(sb.ToString()))
  71.                             {
  72.                                 buffer.Add(sb.ToString());
  73.                             }                            
  74.                             sb.Clear();
  75.                         }
  76.                         isVariable = false;
  77.                     }                
  78.                     else
  79.                     {
  80.                         sb.Append(nextChar);
  81.                     }
  82.                 }
  83.                 if (nextChar == '#' && inSingleQuotes == false && inDoubleQuotes == false)
  84.                 {
  85.                     break;
  86.                 }
  87.                 if (nextChar == '\\') //in test
  88.                 {
  89.                     pos++;
  90.                     continue;
  91.                 }
  92.                 if (nextChar == '/' && inSingleQuotes == false && inDoubleQuotes == false )
  93.                 {
  94.                     if (pos < nextRow.Length - 1)
  95.                     {
  96.                         if (nextRow[pos + 1] == '/') // comment to the end
  97.                         {
  98.                             break;
  99.                         }
  100.                         else if (nextRow[pos + 1] == '*') /* */
  101.                         {
  102.                             inComment = true;
  103.                             pos++;
  104.                         }
  105.                     }
  106.                 }
  107.                 if (nextChar == '\'') // inSingleQuote
  108.                 {
  109.                     if (inSingleQuotes == true)
  110.                     {
  111.                         inSingleQuotes = false;
  112.                     }
  113.                     else
  114.                     {
  115.                         inSingleQuotes = true;
  116.                     }
  117.                 }
  118.                 if (nextChar == '{')
  119.                 {
  120.                     inBrackets = true;
  121.                 }
  122.                 if (nextChar == '}')
  123.                 {
  124.                     inBrackets = false;
  125.                 }
  126.                 if (nextChar == '[')
  127.                 {
  128.                     inNormalBrackets = true;
  129.                 }
  130.                 if (nextChar == ']')
  131.                 {
  132.                     inNormalBrackets = true;
  133.                 }
  134.                 if (nextChar == '(')
  135.                 {
  136.                     inNormalBrackets2 = true;
  137.                 }
  138.                 if (nextChar == ')')
  139.                 {
  140.                     inNormalBrackets2 = false;
  141.                 }
  142.                 if (nextChar == '"') //inDoubleQuote
  143.                 {
  144.                     if (inDoubleQuotes == true)
  145.                     {
  146.                         inDoubleQuotes = false;
  147.                     }
  148.                     else
  149.                     {
  150.                         inDoubleQuotes = true;
  151.                     }
  152.                 }
  153.                 if (nextChar == '*' && inSingleQuotes == false && inDoubleQuotes == false)
  154.                 {
  155.                     if (pos < nextRow.Length - 1)
  156.                     {
  157.                         if (nextRow[pos + 1] == '/') // end of comment
  158.                         {
  159.                             inComment = false;
  160.                             pos++;
  161.                         }
  162.                     }
  163.                 }
  164.                 if (nextChar == '$' && inComment == false)
  165.                 {
  166.                     if (inSingleQuotes == false)
  167.                     {
  168.                         isVariable = true;
  169.                     }
  170.                     else if (inSingleQuotes == true && inBrackets == true)
  171.                     {
  172.                         isVariable = true;
  173.                     }
  174.                     else if (inSingleQuotes == true && inNormalBrackets == true)
  175.                     {
  176.                         isVariable = true;
  177.                     }
  178.                     else if (inSingleQuotes == true && inNormalBrackets2 == true)
  179.                     {
  180.                         isVariable = true;
  181.                     }
  182.                 }
  183.             }
  184.  
  185.         }
  186.         Console.WriteLine(variables.Count);
  187.         variables.Sort();
  188.         foreach (var item in variables)
  189.         {
  190.             Console.WriteLine(item);
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement