public static CompileResult Compile(string typeSql, string typeSqlFileName) { //create the antlr-based lexer and parser var lexer = new TypeSqlLexer(new ANTLRStringStream(typeSql)); var rewriteTokenStream = new TokenRewriteStream(lexer); var parser = new TypeSqlParser(rewriteTokenStream); //parse the typeSql, producing the AST var ast = (CommonTree) parser.typesql().Tree; var nodeStream = new CommonTreeNodeStream(ast); //transform the AST into raw-sql var rawSqlOutput = new RawSqlTransform(nodeStream); nodeStream.TokenStream = rewriteTokenStream; rawSqlOutput.typeSql(); string rawSql = rewriteTokenStream.ToString(); //reset lexer.Reset(); rewriteTokenStream.Reset(); nodeStream.Reset(); //and transform the AST into DAO source code var daoTransform = new DaoTransform(nodeStream){TemplateGroup = new StringTemplateGroup( new StreamReader(typeof(TypeSqlCompiler).Assembly.GetManifestResourceStream("TypeSql.Parsing.DapperDao.stg")), typeof (TemplateLexer))}; var template = daoTransform.typeSql(typeSqlFileName, rawSql).Template; string daoSourceCode = template.ToString(); return new CompileResult(daoSourceCode, rawSql); }
public void Template() { //arrange const string sql = @"SELECT UserId:int FROM Users"; //act var input = new ANTLRStringStream(sql); var lexer = new TypeSqlLexer(input); var tokenStream = new CommonTokenStream(lexer); var parser = new TypeSqlParser(tokenStream); var parseResult = parser.typesql(); var templateGroup = new StringTemplateGroup( new StreamReader(new FileStream(@"..\..\..\TypeSql\Parsing\DapperDao.stg", FileMode.Open)), typeof (TemplateLexer)); var dapperDaoTransform = new DaoTransform(new CommonTreeNodeStream(parseResult.Tree)) { TemplateGroup = templateGroup }; //var result = dapperDaoTransform.sql("UserIds").Template.ToString(); }