internal void Clear() { SourceSegment seg = _lastSegment; SourceSegment prev = null; while (seg != null) { prev = seg.Previous; Pooling.SourceSegments.Put(seg); seg = prev; } _firstSegment = null; _lastSegment = null; _curSegment = null; foreach (ScopeInfo si in _scopes) { Pooling.Scopes.Put(si); } Pooling.Scopes.Put(_currentScope); _rootScope = null; _currentScope = null; _language = null; }
internal void Initialize(ShaderTranslationContext sc, TranslationFlags flags) { _flags = flags; _language = sc.Language; _currentScope = Pooling.Scopes.Get(); _currentScope.Type = ScopeType.Class; _currentScope.TypeInfo = new ShaderType(_language, sc.ShaderType.Name, sc.ShaderType); _currentScope.Namespace = $"{sc.ShaderType.Namespace}.{sc.ShaderType.Name}"; _rootScope = _currentScope; _firstSegment = Pooling.SourceSegments.Get(); _firstSegment.Value = ""; _curSegment = _firstSegment; _lastSegment = _curSegment; }
internal void CloseScope() { if (_scopes.Count == 0) { throw new ScopeException("Cannot close block. No blocks left to close."); } SourceSegment closingSegment = null; if ((_currentScope.Settings.ClosingSyntax.NewLine & NewLineFlags.Before) == NewLineFlags.Before) { AppendLineBreak(); } // We only take the actual closing syntax as a segment, we don't care about the line breaks for closing, when formatting. if (!string.IsNullOrEmpty(_currentScope.Settings.ClosingSyntax.Value)) { closingSegment = Append(_currentScope.Settings.ClosingSyntax.Value); } if ((_currentScope.Settings.ClosingSyntax.NewLine & NewLineFlags.After) == NewLineFlags.After) { AppendLineBreak(); } closingSegment = closingSegment ?? Append(""); if (_currentScope.Settings.Indent) { closingSegment.Indentation = IndentMode.Decrement; } // Tie opening and closing segments together closingSegment.ScopePartner = _currentScope.OpeningSegment; closingSegment.Tracker = _currentScope.OpeningSegment.Tracker; closingSegment.Scope = ScopeMode.Closing; _currentScope.OpeningSegment.ScopePartner = closingSegment; Pooling.Scopes.Put(_currentScope); _currentScope = _scopes.Pop(); }
internal SourceSegment Append(string src) { SourceSegment seg = Pooling.SourceSegments.Get(); seg.Previous = _curSegment; seg.Next = _curSegment.Next; seg.Value = src; if (_curSegment.Next != null) { _curSegment.Next.Previous = seg; } _curSegment.Next = seg; if (_curSegment == _lastSegment) { _lastSegment = seg; } _curSegment = seg; return(seg); }
internal void GoToSegment(SourceSegment seg) { _curSegment = seg; }
public override string ToString() { SourceSegment seg = _firstSegment; int indent = 0; bool lineEmpty = true; bool whitespaceAllowed = (_flags & TranslationFlags.NoWhitespace) != TranslationFlags.NoWhitespace; while (seg != null) { if (seg.Indentation == IndentMode.Decrement) { indent--; } if (whitespaceAllowed && lineEmpty && indent > 0 && seg.Value.Length > 0) { _sb.Append(new string('\t', indent)); } if (seg.Tracker != null && seg.Scope == ScopeMode.Opening) { seg.Tracker.StartIndex = _sb.Length; } if (seg.Value == Environment.NewLine) { if (whitespaceAllowed) { _sb.Append(seg.Value); } lineEmpty = true; } else { _sb.Append(seg.Value); if (seg.Value.Length > 0) { lineEmpty = false; } } string test = _sb.Length.ToString(); if (seg.Tracker != null && seg.Scope == ScopeMode.Closing) { seg.Tracker.EndIndex = _sb.Length; } if (seg.Indentation == IndentMode.Increment) { indent++; } seg = seg.Next; } string result = _sb.ToString(); _sb.Clear(); return(result); }
internal void GoToEnd() { _curSegment = _lastSegment; }