Exemple #1
0
        public void Enter_ClassDefinition(Class_definitionContext context)
        {
            var id = context.identifier();

            _currentClassName = _tokenStream.GetText(id.Start, id.Stop);

            var classRuleAttr = ValidationRuleProvider <ClassNameValidationAttribute>(nameof(Enter_ClassDefinition)) as ClassNameValidationAttribute;

            if (classRuleAttr != null)
            {
                var nameRuleError = classRuleAttr.Validate(_currentNamespace, _currentClassName, string.Empty, string.Empty, string.Empty);
                if (nameRuleError != null)
                {
                    _nameRuleErrorsList.Add(nameRuleError);
                }
            }

            //var match = Regex.Match(_currentClassName, RegexConstants.PascalCaseClassMethodRegex);
            //if (match.Length < _currentClassName.Length)
            //{
            //    _nameRuleErrorsList.Add(new NameRuleError(NameRuleViolations.ClassNameRuleViolation, _currentNamespace, _currentClassName, string.Empty, string.Empty));
            //}

            var tpcc = context.type_parameter_constraints_clauses();
            var tpl  = context.type_parameter_list();

            if (tpl != null)
            {
                var gen = _tokenStream.GetText(tpl.Start, tpl.Stop);
            }

            if (context.children.Count >= 4)
            {
                //Class has Implemented Interfaces
                Class_baseContext interfaceImpl = (Class_baseContext)context.children.FirstOrDefault((x) => x.GetType() == typeof(Class_baseContext));
                if (interfaceImpl?.Start?.Text == ":")
                {
                    //This implements one or multiple interfaces
                    _classAlreadyImplementsInterface = true;
                }
            }

            if (_classAlreadyImplementsInterface)
            {
                return;
            }
            //var parent = context.Parent as Common_member_declarationContext;
            //var accessMod = ts.GetText(parent.Start, parent.Start);
            var interfaceName         = string.Format("{0}{1}", "I", _currentClassName);
            var compiledInterfaceName = string.Format("public interface {0}", interfaceName);

            _interfaceDefinitionBuilder.AppendLine();
            _interfaceDefinitionBuilder.Append(compiledInterfaceName);
            _interfaceDefinitionBuilder.AppendLine();
            _interfaceDefinitionBuilder.Append("{");
        }
        private void ParseTypeDeclaration(Type_declarationContext node)
        {
            Class_definitionContext     classContext     = node.class_definition();
            Interface_definitionContext interfaceContext = node.interface_definition();
            Delegate_definitionContext  delegateContext  = node.delegate_definition();
            Enum_definitionContext      enumContext      = node.enum_definition();

            bool isClass     = classContext != null;
            bool isInterface = interfaceContext != null;
            bool isDelegate  = delegateContext != null;
            bool isEnum      = enumContext != null;

            string type = "";
            string name = "";

            if (isClass)
            {
                type = "Class";
                name = this.GetNodeName(classContext.identifier());
            }
            else if (isInterface)
            {
                type = "Interface";
                name = this.GetNodeName(interfaceContext.identifier());
            }
            else if (isDelegate)
            {
                type = "Delegate";
                name = this.GetNodeName(delegateContext.identifier());
            }
            else if (isEnum)
            {
                type = "Enum";
                name = this.GetNodeName(enumContext.identifier());
            }

            if (!string.IsNullOrEmpty(name))
            {
                this.WriteLine();
                this.WriteKeyValue(type, name);
            }

            if (isClass)
            {
                this.WriteBeginBrace();
                this.ParseClassDeclaration(classContext);
                this.WriteEndBrace();
            }
            else if (isInterface)
            {
                this.WriteBeginBrace();
                this.ParseInterfaceDeclaration(interfaceContext);
                this.WriteEndBrace();
            }
        }
        /// <summary>
        /// Define class scope and symbol
        /// </summary>
        /// <param name="context"></param>
        public override void EnterClass_definition([NotNull] Class_definitionContext context)
        {
            var         classIdentityContext = context.identifier();
            ClassSymbol classSymbol          = new ClassSymbol(classIdentityContext.GetText());

            classSymbol.SetEnclosingScope(currentScope);
            classSymbol.DefNode = context;

            classIdentityContext.Symbol = classSymbol;
            classIdentityContext.Scope  = classSymbol;

            Define(classSymbol);
            linker[currentFileAnalysis].Add(classSymbol);
            currentScope = classSymbol;
        }