Exemple #1
0
        internal protected override SmarterStringBuilder Build(SmarterStringBuilder b, bool closeScope)
        {
            if (_fDef != null)
            {
                if (b.Builder != null)
                {
                    _fDef.Write(b.Builder);
                }
                else
                {
                    b.Append(_fDef.ToString());
                }
                b.HasNewLine = false;
            }
            bool lambda = CodePart.StartsWith("=>") == true;

            if (!lambda)
            {
                b.AppendLine().Append("{").AppendLine();
            }
            CodePart.Build(b);
            _funcs.Build(b);
            if (closeScope && !lambda)
            {
                b.AppendLine().Append("}");
            }
            return(b);
        }
Exemple #2
0
 private protected NamedScopeImpl(CodeWorkspaceImpl workspace, INamedScope?parent)
 {
     _workspace = workspace;
     CodePart   = new CodePart(this);
     Parent     = parent;
     if (parent == null)
     {
         _name     = String.Empty;
         _fullName = String.Empty;
     }
 }
 internal void MergeWith(TypeScopeImpl other)
 {
     Debug.Assert(other != null);
     if (TypeKey != other.TypeKey)
     {
         throw new InvalidOperationException($"Unable to merge type '{_typeDef}' with '{other._typeDef}'.");
     }
     _typeDef.MergeWith(other._typeDef);
     CodePart.MergeWith(other.CodePart);
     MergeTypes(other);
     _funcs.MergeWith(Workspace, this, other._funcs);
 }
Exemple #4
0
        internal void Initialize()
        {
            var b = new SmarterStringBuilder(new StringBuilder());
            // We store the declaration and clears the code buffer.
            var declaration = CodePart.Build(b).ToString();

            CodePart.Parts.Clear();
            _fDef = FunctionDefinition.Parse(declaration, out string?bodyStart);
            if (bodyStart != null)
            {
                CodePart.Parts.Add(bodyStart);
            }
            SetName(_fDef.Key);
        }
Exemple #5
0
 internal void MergeWith(NamespaceScopeImpl other)
 {
     Debug.Assert(other != null);
     _beforeNamespace.MergeWith(other._beforeNamespace);
     foreach (var u in other._usings)
     {
         DoEnsureUsing(u.Key, u.Value.Key, u.Value.Value);
     }
     CodePart.MergeWith(other.CodePart);
     MergeTypes(other);
     foreach (var oNS in other._subNamespaces)
     {
         var my = _subNamespaces.FirstOrDefault(x => x.Name == oNS.Name);
         if (my == null)
         {
             my = new NamespaceScopeImpl(Workspace, this, oNS.Name);
             _subNamespaces.Add(my);
         }
         my.MergeWith(oNS);
     }
 }
        /// <summary>
        /// Extracts the name.
        /// The declaration itself is updated as one string and the scope opener is injected if needed.
        /// </summary>
        internal void Initialize()
        {
            var b = new SmarterStringBuilder(new StringBuilder());
            // We store the declaration and clears the code buffer.
            var declaration = CodePart.Build(b).ToString();

            CodePart.Parts.Clear();
            var m = declaration.AsSpan();

            m.SkipWhiteSpacesAndJSComments();
            if (!m.MatchTypeDefinition(out var typeDef, IsNestedType, out bool hasCodeOpener))
            {
                Throw.InvalidOperationException($"Error: Unable to parse type declaration '{declaration}'.");
            }
            _typeDef = typeDef;
            if (hasCodeOpener)
            {
                CodePart.Parts.Add(declaration.Substring(declaration.Length - m.Length));
            }
            SetName(_typeDef.Name.ToString());
        }
Exemple #7
0
 internal protected override SmarterStringBuilder Build(SmarterStringBuilder b, bool closeScope)
 {
     _beforeNamespace.Build(b);
     // A global using prevent any attribute definition (and [assembly::...] attributes cannot be defined
     // in a namespace.
     // We write the global usings in the TOP namespaces: they are duplicated among the top-level namespaces
     // but this enables to define attributes at the top of one file.
     if (Workspace.Global != this)
     {
         b.Append("namespace ")
         .Append(Name)
         .AppendLine()
         .Append("{")
         .AppendLine();
         if (Parent == Workspace.Global)
         {
             Parent.WriteThisUsings(b);
         }
         WriteThisUsings(b);
     }
     else if (_subNamespaces.Count == 0)
     {
         // However, if there is no namespace defined, the global
         // usings must be written. We have no choice.
         WriteThisUsings(b);
     }
     CodePart.Build(b);
     foreach (var ns in _subNamespaces)
     {
         ns.Build(b, true);
     }
     BuildTypes(b);
     if (Workspace.Global != this && closeScope)
     {
         b.AppendLine().Append("}");
     }
     return(b);
 }
 internal protected override SmarterStringBuilder Build(SmarterStringBuilder b, bool closeScope)
 {
     if (_typeDef != null)
     {
         if (b.Builder != null)
         {
             _typeDef.Write(b.Builder);
         }
         else
         {
             b.Append(_typeDef.ToString());
         }
         b.HasNewLine = false;
     }
     b.AppendLine().Append("{").AppendLine();
     CodePart.Build(b);
     _funcs.Build(b);
     BuildTypes(b);
     if (closeScope)
     {
         b.AppendLine().Append("}").AppendLine();
     }
     return(b);
 }
Exemple #9
0
 internal void MergeWith(FunctionScopeImpl other)
 {
     Debug.Assert(other != null);
     CodePart.MergeWith(other.CodePart);
     _funcs.MergeWith(Workspace, this, other._funcs);
 }