Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 122 additions & 8 deletions src/RCParsing/ParserBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using RCParsing.Building;
using RCParsing.Building.ParserRules;
using RCParsing.Building.TokenPatterns;
using RCParsing.ParserRules;
using RCParsing.TokenPatterns;
using RCParsing.Utils;

Expand Down Expand Up @@ -69,7 +65,6 @@ public TokenBuilder CreateToken(string name)
{
if (_tokenPatterns.ContainsKey(name))
throw new ArgumentException($"Token with name '{name}' already exists.");

var token = new TokenBuilder();
_tokenPatterns[name] = token;
return token;
Expand Down Expand Up @@ -114,6 +109,33 @@ public TokenBuilder GetOrCreateToken(string name)
return token;
}

/// <summary>
/// Renames a token pattern. If the new name already exists, it will not be renamed and <see langword="false"/> will be returned.
/// </summary>
/// <param name="oldName">The current name of the token pattern.</param>
/// <param name="newName">The new name for the token pattern. Must be unique among all token patterns.</param>
/// <returns><see langword="true"/> if the token pattern was successfully renamed; otherwise, <see langword="false"/>.</returns>
public bool RenameToken(string oldName, string newName)
{
if (!_tokenPatterns.TryGetValue(oldName, out var token))
return false;
if (_tokenPatterns.ContainsKey(newName))
return false;
_tokenPatterns.Remove(oldName);
_tokenPatterns[newName] = token;
return true;
}

/// <summary>
/// Removes a token pattern by its name. If the token pattern does not exist, it will not be removed and <see langword="false"/> will be returned.
/// </summary>
/// <param name="name">The name of the token pattern to remove.</param>
/// <returns><see langword="true"/> if the token pattern was successfully removed; otherwise, <see langword="false"/>.</returns>
public bool RemoveToken(string name)
{
return _tokenPatterns.Remove(name);
}

/// <summary>
/// Creates a rule builder and registers it under the given name.
/// </summary>
Expand All @@ -124,7 +146,6 @@ public RuleBuilder CreateRule(string name)
{
if (_rules.ContainsKey(name))
throw new ArgumentException($"Rule with name '{name}' already exists.");

var rule = new RuleBuilder();
_rules[name] = rule;
return rule;
Expand Down Expand Up @@ -169,6 +190,33 @@ public RuleBuilder GetOrCreateRule(string name)
return rule;
}

/// <summary>
/// Renames a rule. If the new name already exists, it will not be renamed and <see langword="false"/> will be returned.
/// </summary>
/// <param name="oldName">The current name of the rule.</param>
/// <param name="newName">The new name for the rule. Must be unique among all rules.</param>
/// <returns><see langword="true"/> if the rule was successfully renamed; otherwise, <see langword="false"/>.</returns>
public bool RenameRule(string oldName, string newName)
{
if (!_rules.TryGetValue(oldName, out var rule))
return false;
if (_rules.ContainsKey(newName))
return false;
_rules.Remove(oldName);
_rules[newName] = rule;
return true;
}

/// <summary>
/// Removes a rule by its name. If the rule does not exist, it will not be removed and <see langword="false"/> will be returned.
/// </summary>
/// <param name="name">The name of the rule to remove.</param>
/// <returns><see langword="true"/> if the rule was successfully removed; otherwise, <see langword="false"/>.</returns>
public bool RemoveRule(string name)
{
return _rules.Remove(name);
}

/// <summary>
/// Creates a rule builder and registers it as the main rule.
/// </summary>
Expand All @@ -181,7 +229,6 @@ public RuleBuilder CreateMainRule()
{
if (_mainRuleBuilder != null)
throw new ArgumentException("Main rule has already been set.");

_mainRuleBuilder = new RuleBuilder();
return _mainRuleBuilder;
}
Expand All @@ -201,7 +248,6 @@ public RuleBuilder CreateMainRule(string name)
throw new ArgumentException("Main rule has already been set.");
if (_rules.ContainsKey(name))
throw new ArgumentException($"Rule with name '{name}' already exists.");

_mainRuleBuilder = new RuleBuilder();
_rules[name] = _mainRuleBuilder;
return _mainRuleBuilder;
Expand Down Expand Up @@ -230,6 +276,74 @@ public RuleBuilder GetMainRule()
return null;
}

/// <summary>
/// Gets or creates the main rule builder. If no main rule exists, it will be created and returned.
/// </summary>
/// <returns>A <see cref="RuleBuilder"/> instance for building the main rule.</returns>
public RuleBuilder? GetOrCreateMainRule()
{
if (_mainRuleBuilder != null)
return _mainRuleBuilder;
_mainRuleBuilder = new RuleBuilder();
return _mainRuleBuilder;
}

/// <summary>
/// Gets the main rule name. If no main rule exists or the main rule has no name, it will return <see langword="null"/>.
/// </summary>
/// <returns>The name of the main rule or <see langword="null"/> if no main rule exists.</returns>
public string? GetMainRuleName()
{
if (_mainRuleBuilder == null)
return null;
return _rules.FirstOrDefault(r => r.Value == _mainRuleBuilder).Key;
}

/// <summary>
/// Renames the main rule. If the new name already exists, it will not be renamed and <see langword="false"/> will be returned.
/// </summary>
/// <param name="name">The new name for the main rule. Must be unique among all rules.</param>
/// <returns><see langword="true"/> if the main rule was successfully renamed; otherwise, <see langword="false"/>.</returns>
public bool NameMainRule(string name)
{
if (_mainRuleBuilder == null)
return false;
if (_rules.ContainsKey(name))
return false;
var oldName = _rules.FirstOrDefault(r => r.Value == _mainRuleBuilder).Key;
if (oldName != null)
_rules.Remove(oldName);
_rules[name] = _mainRuleBuilder;
return true;
}

/// <summary>
/// Removes the name from main rule. If main rule not exists or the main rule has no name, name will not be removed and <see langword="false"/> will be returned.
/// </summary>
/// <returns><see langword="true"/> if the name was successfully removed; otherwise, <see langword="false"/>.</returns>
public bool RemoveMainRuleName()
{
if (_mainRuleBuilder == null)
return false;
var oldName = _rules.FirstOrDefault(r => r.Value == _mainRuleBuilder).Key;
return oldName != null && _rules.Remove(oldName);
}

/// <summary>
/// Removes the main rule completely. If no main rule exists, it will not be removed and <see langword="false"/> will be returned.
/// </summary>
/// <returns><see langword="true"/> if the main rule was successfully removed; otherwise, <see langword="false"/>.</returns>
public bool RemoveMainRule()
{
if (_mainRuleBuilder == null)
return false;
var oldName = _rules.FirstOrDefault(r => r.Value == _mainRuleBuilder).Key;
if (oldName != null)
_rules.Remove(oldName);
_mainRuleBuilder = null;
return true;
}



/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/RCParsing/RCParsing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<PackageId>RCParsing</PackageId>
<Version>5.2.0</Version>
<Version>5.2.1</Version>
<Authors>Roman K.</Authors>
<Company>RomeCore</Company>
<Product>RCParsing</Product>
Expand Down
Loading