/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of xAPI4Unity. */ #if UNITY_EDITOR using System.Collections.Generic; namespace xAPI4Unity.Editor.Parser.Code.SyntaxTree { /// /// Represents an abstract syntax node in the syntax tree. /// /// The derived type of the syntax node, enabling fluent API chaining. internal abstract class SyntaxNode where T : SyntaxNode { // Holds the list of modifiers associated with this syntax node. private readonly List _modifiers = new List(); /// /// Name of the syntax node /// public string Name { get; private set; } /// /// Access modifiers of the syntax node (e.g., public, private, static) /// public IReadOnlyList Modifiers => _modifiers.AsReadOnly(); /// /// Comment/Description of the syntax node /// public string Comment { get; private set; } /// /// Initializes an empty syntax node with no name or comment. /// public SyntaxNode() { } /// /// Initializes a syntax node with a specified name and optional comment. /// /// The name of the node. /// The comment/description of the node. Optional. public SyntaxNode(string name, string comment = default) { Name = name; Comment = comment; } /// /// Sets the name of the syntax node. /// /// The name to assign. /// The same syntax node instance for fluent chaining. public T WithName(string name) { Name = name; return (T)this; } /// /// Adds a single modifier to the syntax node. /// /// The modifier to add. /// The same syntax node instance for fluent chaining. public T AddModifier(Modifier mod) { _modifiers.Add(mod); return (T)this; } /// /// Adds multiple modifiers to the syntax node. /// /// The modifiers to add. /// The same syntax node instance for fluent chaining. public T AddModifiers(params Modifier[] mods) { _modifiers.AddRange(mods); return (T)this; } /// /// Adds a collection of modifiers to the syntax node. /// /// The collection of modifiers to add. /// The same syntax node instance for fluent chaining. public T AddModifiers(IEnumerable mods) { _modifiers.AddRange(mods); return (T)this; } /// /// Sets the comment/description for the syntax node. /// /// The description or XML comment to assign. Replaces < and > for safety. /// The same syntax node instance for fluent chaining. public T WithComment(string comment) { Comment = comment.Replace(">", ">").Replace("<", "<"); return (T)this; } } } #endif