1. Ivan Kurnosov
  2. PonyFormatter

Commits

Ivan Kurnosov  committed 95ad1f8

sketch for formatter and its tests

  • Participants
  • Parent commits 69a40fa
  • Branches default

Comments (0)

Files changed (4)

File PonyFormatter/Formatter.cs

View file
 using System.Linq;
 using System.Text;
 
+using Antlr.Runtime;
+using Antlr.Runtime.Tree;
+using CT = Antlr.Runtime.Tree.CommonTree;
+using MysqlParser;
+
 namespace PonyFormatter
 {
     public class Formatter
     {
         private string Query;
+        private string Result;
 
         public Formatter(string query)
         {
 
         public string Format()
         {
-            return "";
+            var charstream = new ANTLRStringStream(Query);
+            var lexer = new PonySQLLexer(charstream);
+
+            var tokenstream = new CommonTokenStream(lexer);
+            var parser = new PonySQLParser(tokenstream);
+
+            var ast = (CommonTree)(parser.query().Tree);
+
+            Build(ast);
+
+            return Result;
+        }
+
+        private void Build(CT node)
+        {
+            Result += node.Text + " ";
+
+            if (node.Children == null)
+                return;
+
+            foreach (CT child in node.Children)
+            {
+                Build(child);
+            }
         }
     }
 }

File PonyFormatter/PonyFormatter.csproj

View file
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Antlr3.Runtime">
+      <HintPath>..\bin\antlr\Antlr3.Runtime.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Xml.Linq" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\MysqlParser\MysqlParser.csproj">
+      <Project>{488FCDA4-830C-4495-A547-7FEFB71D2A5E}</Project>
+      <Name>MysqlParser</Name>
+    </ProjectReference>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

File PonyFormatter/Program.cs

View file
 using System.Linq;
 using System.Text;
 
+using Antlr.Runtime;
+using Antlr.Runtime.Tree;
+using CT = Antlr.Runtime.Tree.CommonTree;
+using MysqlParser;
+
 namespace PonyFormatter
 {
     class Program
     {
+        static public string body;
+        static private int depth = 0;
+
         static void Main(string[] args)
         {
+            var charstream = new ANTLRStringStream(@"select a,b,c");
+            var lexer = new PonySQLLexer(charstream);
+
+            var tokenstream = new CommonTokenStream(lexer);
+            var parser = new PonySQLParser(tokenstream);
+
+            var ast = (CommonTree)(parser.query().Tree);
+            Print(ast);
+            Console.WriteLine(body);
+        }
+
+        public static void PrintChildren(CT ast)
+        {
+            PrintChildren(ast, " ", true);
+        }
+
+        public static void PrintChildren(CT ast, string delim, bool final)
+        {
+            if (ast.Children == null)
+            {
+                depth--;
+                return;
+            }
+
+            int num = ast.Children.Count;
+
+            for (int i = 0; i < num; ++i)
+            {
+                CT d = (CT)(ast.Children[i]);
+                Print(d);
+                if (final || i < num - 1)
+                {
+                    body += delim;
+                }
+            }
+
+            depth--;
+        }
+
+        public static void Print(CommonTree ast)
+        {
+            var shift = new String('-', depth * 3);
+            depth++;
+            body += Environment.NewLine + shift + ast.Text + "(" + ast.Type + ")";
+            PrintChildren(ast);
         }
     }
 }

File PonyFormatterTests/SimpleQueriesTest.cs

View file
         public void OnlySelect()
         {
             var formatter = new Formatter("SELECT field");
-            formatter.Format();
+            Assert.That("SELECT field ", Is.EqualTo(formatter.Format()));
         }
     }
 }