Inheritance: AstNode
Exemplo n.º 1
0
        public void Visit(FileTable table)
        {
            var descriptor = new TableDescriptor(typeof(FileTable<>));
            var fileTable = new CodeTypeDeclaration(table.Variable) { TypeAttributes = TypeAttributes.NestedPrivate };
            fileTable.BaseTypes.Add(new CodeTypeReference("IRowWriter"));
            fileTable.BaseTypes.Add(new CodeTypeReference("IRowReader"));

            //field
            var fileTableCodeDomType = new CodeTypeReference("FileTable", new CodeTypeReference(table.Variable));
            Scope.Current.Type.Type.Members.Add(
                new CodeMemberField(fileTableCodeDomType, table.Variable) { Attributes = MemberAttributes.Public | MemberAttributes.Final });

            var locationArg = VisitChild(table.Location);

            //constructor
            Scope.Current.Type.Constructor.Statements.Add(new CodeAssignStatement(
                new CodeSnippetExpression(table.Variable),
                new CodeObjectCreateExpression(
                    new CodeTypeReference("FileTable", new CodeTypeReference(table.Variable)))));

            BuildIRowWriterImplementation(fileTable, table.Args);
            BuildIRowReaderImplementation(fileTable, table.Args);

            foreach (var arg in table.Args)
            {
                var domArg = VisitChild(arg);
                fileTable.Members.AddRange(domArg.ParentMemberDefinitions);
                descriptor.Variables.Add(new VariableTypePair { Variable = arg.Variable, Primitive= TablePrimitive.FromString(arg.Type)});
            }

            _mainType.Type.Members.Add(fileTable);

            if (Scope.Current.IsCurrentScopeRegistered(table.Variable))
                Errors.Add(new VariableAlreadyExists(new Semantic.LineInfo(table.Line.Line, table.Line.CharacterPosition), table.Variable));

            Scope.Current.RegisterTable(table.Variable, descriptor, fileTableCodeDomType);

            //Init Code
            CodeMemberMethod method = new CodeMemberMethod();
            method.Name = "Init_" + fileTable.Name;
            method.Attributes = MemberAttributes.Private;

            _mainType.Type.Members.Add(method);
            var assignment = new CodeAssignStatement(new CodeVariableReferenceExpression("_" + Scope.Current.ScopeIdentifier + "." + table.Variable + ".Location"), locationArg.CodeExpression);
            method.Statements.Add(assignment);

            assignment = new CodeAssignStatement(new CodeVariableReferenceExpression("_" + Scope.Current.ScopeIdentifier + "." + table.Variable + ".FieldTerminator"), new CodePrimitiveExpression(table.FieldTerminator));
            method.Statements.Add(assignment);

            assignment = new CodeAssignStatement(new CodeVariableReferenceExpression("_" + Scope.Current.ScopeIdentifier + "." + table.Variable + ".RowTerminator"), new CodePrimitiveExpression(table.RowTerminator));
            method.Statements.Add(assignment);

            method.Statements.Add(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("_" + Scope.Current.ScopeIdentifier + "." + table.Variable), "Load"));

            var methodcall = new CodeMethodInvokeExpression(
               new CodeMethodReferenceExpression(null, method.Name));

            _codeStack.Peek().ParentStatements.Add(methodcall);
            _codeStack.Peek().CodeExpression = methodcall;
        }
Exemplo n.º 2
0
        public void Visit(FileTable table, CommonTree tree)
        {
            Parent(tree).Children.Add(table);
            SetLine(table, tree);

            Debug.Assert(tree.Children[0].Type == AntlrParser.ID);
            Debug.Assert(tree.Children[1].Type == AntlrParser.TABLE_COLUMN_ARGS);
            Debug.Assert(tree.Children[2].Type == AntlrParser.WITH);
            Debug.Assert(tree.Children[3].Type == AntlrParser.LOCATION);

            table.Variable = tree.Children[0].Text;
            var args = tree.Children[1] as CommonTree;
            foreach (var arg in args.Children)
                table.Children.Add(new TableColumnArg() { Variable = arg.GetChild(0).Text, Type = arg.GetChild(1).Text });

            Debug.Assert(tree.Children[2].GetChild(0).Type == AntlrParser.FIELD_TERMINATOR);
            Debug.Assert(tree.Children[2].GetChild(1).Type == AntlrParser.ROW_TERMINATOR);
            table.FieldTerminator = ParseLiteral(tree.Children[2].GetChild(0).GetChild(0).Text);
            table.RowTerminator = ParseLiteral(tree.Children[2].GetChild(1).GetChild(0).Text);

            Visit(tree.Children[3].GetChild(0));
        }