diff --git a/GParse/Parsing/IPrattParser.cs b/GParse/Parsing/IPrattParser.cs index b6d0dd2..a04e4ee 100644 --- a/GParse/Parsing/IPrattParser.cs +++ b/GParse/Parsing/IPrattParser.cs @@ -1,6 +1,6 @@ using System; -using System.Diagnostics.CodeAnalysis; using GParse.Lexing; +using Tsu; namespace GParse.Parsing { @@ -31,15 +31,13 @@ public interface IPrattParser /// of possible parselets. /// /// - /// - /// - Boolean TryParseExpression(Int32 minPrecedence, [NotNullWhen(true)] out TExpressionNode expression); + /// The parsed expression if successful. + Option ParseExpression(int minPrecedence); /// /// Attempts to parse an expression. /// - /// The parsed expression if successful. - /// Whether the parsing happened without any fatal errors. - Boolean TryParseExpression([NotNullWhen(true)] out TExpressionNode expression); + /// The parsed expression if successful. + Option ParseExpression(); } } \ No newline at end of file diff --git a/GParse/Parsing/IPrattParserBuilder.cs b/GParse/Parsing/IPrattParserBuilder.cs index 12cc837..753a755 100644 --- a/GParse/Parsing/IPrattParserBuilder.cs +++ b/GParse/Parsing/IPrattParserBuilder.cs @@ -1,5 +1,4 @@ using System; -using GParse; using GParse.Lexing; using GParse.Parsing.Parselets; @@ -26,7 +25,7 @@ public interface IPrattParserBuilder /// /// /// - void Register(TTokenType tokenType, String id, IPrefixParselet prefixModule); + void Register(TTokenType tokenType, string id, IPrefixParselet prefixModule); /// /// Registers an infix expression parser module @@ -41,7 +40,7 @@ public interface IPrattParserBuilder /// /// /// - void Register(TTokenType tokenType, String id, IInfixParselet infixModule); + void Register(TTokenType tokenType, string id, IInfixParselet infixModule); /// /// Initializes a new Pratt Parser diff --git a/GParse/Parsing/Parselets/IInfixParselet.cs b/GParse/Parsing/Parselets/IInfixParselet.cs index e6b0f8b..48cf7cc 100644 --- a/GParse/Parsing/Parselets/IInfixParselet.cs +++ b/GParse/Parsing/Parselets/IInfixParselet.cs @@ -1,7 +1,6 @@ using System; -using System.Diagnostics.CodeAnalysis; -using GParse; using GParse.Lexing; +using Tsu; namespace GParse.Parsing.Parselets { @@ -16,7 +15,7 @@ public interface IInfixParselet /// /// The precedence of the operator this module parses. /// - Int32 Precedence { get; } + int Precedence { get; } /// /// Attempts to parse an infix/postfix expression. State should be restored by the caller on failure. @@ -24,12 +23,10 @@ public interface IInfixParselet /// The parser that called this parselet. /// The expression that was parsed on the left side of the infix. /// The diagnostic list to be used when reporting new diagnostics. - /// The resulting parsed expression. - /// Whether the parsing was succesful. - Boolean TryParse( + /// The resulting parsed expression if parsing was succesful. + Option Parse( IPrattParser parser, TExpressionNode expression, - DiagnosticList diagnostics, - [NotNullWhen(true)] out TExpressionNode parsedExpression); + DiagnosticList diagnostics); } } \ No newline at end of file diff --git a/GParse/Parsing/Parselets/IPrefixParselet.cs b/GParse/Parsing/Parselets/IPrefixParselet.cs index b92f09a..eacd7b7 100644 --- a/GParse/Parsing/Parselets/IPrefixParselet.cs +++ b/GParse/Parsing/Parselets/IPrefixParselet.cs @@ -1,7 +1,5 @@ -using System; -using System.Diagnostics.CodeAnalysis; -using GParse; -using GParse.Lexing; +using GParse.Lexing; +using Tsu; namespace GParse.Parsing.Parselets { @@ -18,11 +16,9 @@ public interface IPrefixParselet /// /// The parser that called this parselet. /// The diagnostic list to use when reporting new diagnostics. - /// The resulting parsed expression. - /// Whether the parsing was successful. - Boolean TryParse( + /// The resulting parsed expression if successful. + Option Parse( IPrattParser parser, - DiagnosticList diagnostics, - [NotNullWhen(true)] out TExpressionNode parsedExpression); + DiagnosticList diagnostics); } } \ No newline at end of file diff --git a/GParse/Parsing/Parselets/LiteralParselet.cs b/GParse/Parsing/Parselets/LiteralParselet.cs index dc0fe12..8c8d46b 100644 --- a/GParse/Parsing/Parselets/LiteralParselet.cs +++ b/GParse/Parsing/Parselets/LiteralParselet.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.CodeAnalysis; using GParse.Lexing; +using Tsu; namespace GParse.Parsing.Parselets { @@ -11,12 +12,10 @@ namespace GParse.Parsing.Parselets /// The base type of expression nodes. /// The obtained token. /// The diagnostic list to use when reporting new diagnostics. - /// The resulting expression node. - /// Whether the expression was able to be parsed. - public delegate Boolean LiteralNodeFactory( + /// The resulting expression node if it was succesful. + public delegate Option LiteralNodeFactory( Token token, - DiagnosticList diagnostics, - [NotNullWhen(true)] out TExpressionNode expression) + DiagnosticList diagnostics) where TTokenType : notnull; /// @@ -27,7 +26,7 @@ public delegate Boolean LiteralNodeFactory( public class LiteralParselet : IPrefixParselet where TTokenType : notnull { - private readonly LiteralNodeFactory factory; + private readonly LiteralNodeFactory _factory; /// /// Initializes a new literal parselet. @@ -35,21 +34,20 @@ public class LiteralParselet : IPrefixParseletThe function that transforms a token into an expression node. public LiteralParselet(LiteralNodeFactory factory) { - this.factory = factory ?? throw new ArgumentNullException(nameof(factory)); + _factory = factory ?? throw new ArgumentNullException(nameof(factory)); } /// - public Boolean TryParse( + public Option Parse( IPrattParser parser, - DiagnosticList diagnostics, - [NotNullWhen(true)] out TExpressionNode parsedExpression) + DiagnosticList diagnostics) { if (parser is null) throw new ArgumentNullException(nameof(parser)); if (diagnostics is null) throw new ArgumentNullException(nameof(diagnostics)); - return this.factory(parser.TokenReader.Consume(), diagnostics, out parsedExpression); + return _factory(parser.TokenReader.Consume(), diagnostics); } } } \ No newline at end of file diff --git a/GParse/Parsing/Parselets/SingleTokenInfixOperatorParselet.cs b/GParse/Parsing/Parselets/SingleTokenInfixOperatorParselet.cs index d8855b0..cbc48b2 100644 --- a/GParse/Parsing/Parselets/SingleTokenInfixOperatorParselet.cs +++ b/GParse/Parsing/Parselets/SingleTokenInfixOperatorParselet.cs @@ -1,6 +1,6 @@ using System; -using System.Diagnostics.CodeAnalysis; using GParse.Lexing; +using Tsu; namespace GParse.Parsing.Parselets { @@ -12,13 +12,11 @@ namespace GParse.Parsing.Parselets /// The expression on the left side of the operator. /// The operator token. /// The expression on the right side of the operator. - /// The resulting parsed expression. - /// Whether it was possible to parse the expression or not. - public delegate Boolean InfixNodeFactory( + /// The resulting parsed expression if it was successful. + public delegate Option InfixNodeFactory( TExpressionNode left, Token op, - TExpressionNode right, - [NotNullWhen(true)] out TExpressionNode expression) + TExpressionNode right) where TTokenType : notnull; /// @@ -29,11 +27,11 @@ public delegate Boolean InfixNodeFactory( public class SingleTokenInfixOperatorParselet : IInfixParselet where TTokenType : notnull { - private readonly Boolean isRightAssociative; - private readonly InfixNodeFactory factory; + private readonly bool _isRightAssociative; + private readonly InfixNodeFactory _factory; /// - public Int32 Precedence { get; } + public int Precedence { get; } /// /// Initializes a new single token infix operator parselet. @@ -42,21 +40,20 @@ public class SingleTokenInfixOperatorParselet : IIn /// Whether the operator is right-associative. /// The method that transforms the infix operation into an expression node. public SingleTokenInfixOperatorParselet( - Int32 precedence, - Boolean isRightAssociative, + int precedence, + bool isRightAssociative, InfixNodeFactory factory) { - this.Precedence = precedence; - this.isRightAssociative = isRightAssociative; - this.factory = factory ?? throw new ArgumentNullException(nameof(factory)); + Precedence = precedence; + _isRightAssociative = isRightAssociative; + _factory = factory ?? throw new ArgumentNullException(nameof(factory)); } /// - public Boolean TryParse( + public Option Parse( IPrattParser parser, TExpressionNode expression, - DiagnosticList diagnostics, - [NotNullWhen(true)] out TExpressionNode parsedExpression) + DiagnosticList diagnostics) { if (parser is null) throw new ArgumentNullException(nameof(parser)); @@ -65,23 +62,20 @@ public Boolean TryParse( if (diagnostics is null) throw new ArgumentNullException(nameof(diagnostics)); - parsedExpression = default!; - Token op = parser.TokenReader.Consume(); + var op = parser.TokenReader.Consume(); // We decrease the precedence by one on right-associative operators because the minimum // precedence passed to TryParseExpression is exclusive (meaning that the precedence of the // infix parselets must be higher than the one we pass it. // TODO: Check if this cannot create bugs with other operators that have the same precedence. - Int32 minPrecedence; - if (this.isRightAssociative) - minPrecedence = this.Precedence - 1; + int minPrecedence; + if (_isRightAssociative) + minPrecedence = Precedence - 1; else - minPrecedence = this.Precedence; + minPrecedence = Precedence; - if (parser.TryParseExpression(minPrecedence, out TExpressionNode nextExpr)) - return this.factory(expression, op, nextExpr, out parsedExpression); - else - return false; + return parser.ParseExpression(minPrecedence) + .AndThen(nextExpr => _factory(expression, op, nextExpr)); } } } \ No newline at end of file diff --git a/GParse/Parsing/Parselets/SingleTokenPostfixOperatorParselet.cs b/GParse/Parsing/Parselets/SingleTokenPostfixOperatorParselet.cs index d52a32f..d69bd86 100644 --- a/GParse/Parsing/Parselets/SingleTokenPostfixOperatorParselet.cs +++ b/GParse/Parsing/Parselets/SingleTokenPostfixOperatorParselet.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.CodeAnalysis; using GParse.Lexing; +using Tsu; namespace GParse.Parsing.Parselets { @@ -12,13 +13,11 @@ namespace GParse.Parsing.Parselets /// The operand expression. /// The operator token. /// The diagnostic list to use when reporting new diagnostics. - /// The parsed expression if successful. - /// Whether the expression was able to be parsed. - public delegate Boolean PostfixNodeFactory( + /// The parsed expression if successful. + public delegate Option PostfixNodeFactory( TExpressionNode operand, Token @operator, - DiagnosticList diagnostics, - [NotNullWhen(true)] out TExpressionNode expression) + DiagnosticList diagnostics) where TTokenType : notnull; /// @@ -30,31 +29,30 @@ public delegate Boolean PostfixNodeFactory( public class SingleTokenPostfixOperatorParselet : IInfixParselet where TTokenType : notnull { - private readonly PostfixNodeFactory factory; + private readonly PostfixNodeFactory _factory; /// - public Int32 Precedence { get; } + public int Precedence { get; } /// /// Initializes a new single token postfix operator parselet. /// /// The precedence of the operator this parselet is parsing. /// The method responsible for creating the postfix expression node. - public SingleTokenPostfixOperatorParselet(Int32 precedence, PostfixNodeFactory factory) + public SingleTokenPostfixOperatorParselet(int precedence, PostfixNodeFactory factory) { if (precedence < 1) throw new ArgumentOutOfRangeException(nameof(precedence), "Precedence of the operator must be greater than 0"); - this.Precedence = precedence; - this.factory = factory ?? throw new ArgumentNullException(nameof(factory)); + Precedence = precedence; + _factory = factory ?? throw new ArgumentNullException(nameof(factory)); } /// - public Boolean TryParse( + public Option Parse( IPrattParser parser, TExpressionNode expression, - DiagnosticList diagnostics, - [NotNullWhen(true)] out TExpressionNode parsedExpression) + DiagnosticList diagnostics) { if (parser is null) throw new ArgumentNullException(nameof(parser)); @@ -63,7 +61,7 @@ public Boolean TryParse( if (diagnostics is null) throw new ArgumentNullException(nameof(diagnostics)); - return this.factory(expression, parser.TokenReader.Consume(), diagnostics, out parsedExpression); + return _factory(expression, parser.TokenReader.Consume(), diagnostics); } } } \ No newline at end of file diff --git a/GParse/Parsing/Parselets/SingleTokenPrefixOperatorParselet.cs b/GParse/Parsing/Parselets/SingleTokenPrefixOperatorParselet.cs index f150647..2e6b567 100644 --- a/GParse/Parsing/Parselets/SingleTokenPrefixOperatorParselet.cs +++ b/GParse/Parsing/Parselets/SingleTokenPrefixOperatorParselet.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.CodeAnalysis; using GParse.Lexing; +using Tsu; namespace GParse.Parsing.Parselets { @@ -12,13 +13,11 @@ namespace GParse.Parsing.Parselets /// The operator token. /// The operand expression. /// The diagnostic list to use when reporting new diagnostics. - /// The resulting parsed expression. - /// - public delegate Boolean PrefixNodeFactory( + /// The resulting parsed expression if successful. + public delegate Option PrefixNodeFactory( Token @operator, TExpressionNode operand, - DiagnosticList diagnostics, - [NotNullWhen(true)] out TExpressionNode expression) + DiagnosticList diagnostics) where TTokenType : notnull; /// @@ -29,40 +28,36 @@ public delegate Boolean PrefixNodeFactory( public class SingleTokenPrefixOperatorParselet : IPrefixParselet where TTokenType : notnull { - private readonly Int32 precedence; - private readonly PrefixNodeFactory factory; + private readonly int _precedence; + private readonly PrefixNodeFactory _factory; /// /// Initializes this class /// /// /// - public SingleTokenPrefixOperatorParselet(Int32 precedence, PrefixNodeFactory factory) + public SingleTokenPrefixOperatorParselet(int precedence, PrefixNodeFactory factory) { if (precedence < 1) throw new ArgumentOutOfRangeException(nameof(precedence), "Precedence must be a value greater than 0."); - this.precedence = precedence; - this.factory = factory ?? throw new ArgumentNullException(nameof(factory)); + _precedence = precedence; + _factory = factory ?? throw new ArgumentNullException(nameof(factory)); } /// - public Boolean TryParse( + public Option Parse( IPrattParser parser, - DiagnosticList diagnostics, - [NotNullWhen(true)] out TExpressionNode parsedExpression) + DiagnosticList diagnostics) { if (parser is null) throw new ArgumentNullException(nameof(parser)); if (diagnostics is null) throw new ArgumentNullException(nameof(diagnostics)); - parsedExpression = default!; - Token prefix = parser.TokenReader.Consume(); - if (parser.TryParseExpression(this.precedence, out TExpressionNode expression)) - return this.factory(prefix, expression, diagnostics, out parsedExpression); - else - return false; + var prefix = parser.TokenReader.Consume(); + return parser.ParseExpression(_precedence) + .AndThen(expression => _factory(prefix, expression, diagnostics)); } } } \ No newline at end of file diff --git a/GParse/Parsing/PrattParser.cs b/GParse/Parsing/PrattParser.cs index 4cc6896..8403df8 100644 --- a/GParse/Parsing/PrattParser.cs +++ b/GParse/Parsing/PrattParser.cs @@ -1,7 +1,7 @@ using System; -using System.Diagnostics.CodeAnalysis; using GParse.Lexing; using GParse.Parsing.Parselets; +using Tsu; namespace GParse.Parsing { @@ -40,8 +40,8 @@ protected internal PrattParser( DiagnosticList diagnostics) : base(diagnostics, tokenReader) { - this.PrefixModuleTree = prefixModuleTree ?? throw new ArgumentNullException(nameof(prefixModuleTree)); - this.InfixModuleTree = infixModuleTree ?? throw new ArgumentNullException(nameof(infixModuleTree)); + PrefixModuleTree = prefixModuleTree ?? throw new ArgumentNullException(nameof(prefixModuleTree)); + InfixModuleTree = infixModuleTree ?? throw new ArgumentNullException(nameof(infixModuleTree)); } #region PrattParser @@ -49,50 +49,51 @@ protected internal PrattParser( #region ParseExpression /// - public virtual Boolean TryParseExpression(Int32 minPrecedence, [NotNullWhen(true)] out TExpressionNode expression) + public virtual Option ParseExpression(int minPrecedence) { - expression = default!; var foundExpression = false; - foreach (IPrefixParselet module in this.PrefixModuleTree.GetSortedCandidates(this.TokenReader)) + var expressionOpt = Option.None(); + foreach (var module in PrefixModuleTree.GetSortedCandidates(TokenReader)) { - var start = this.TokenReader.Position; - if (module.TryParse(this, this.Diagnostics, out expression)) + var start = TokenReader.Position; + expressionOpt = module.Parse(this, Diagnostics); + if (expressionOpt.IsSome) { foundExpression = true; break; } - if (this.TokenReader.Position != start) - this.TokenReader.Restore(start); + TokenReader.Restore(start); } if (!foundExpression) - return false; + return Option.None(); - Boolean couldParse; + bool couldParse; do { couldParse = false; - foreach (IInfixParselet module in this.InfixModuleTree.GetSortedCandidates(this.TokenReader)) + foreach (var module in InfixModuleTree.GetSortedCandidates(TokenReader)) { - var start = this.TokenReader.Position; - if (minPrecedence < module.Precedence - && module.TryParse(this, expression, this.Diagnostics, out TExpressionNode tmpExpr)) + var start = TokenReader.Position; + if (minPrecedence < module.Precedence) { - couldParse = true; - expression = tmpExpr; - break; + var result = module.Parse(this, expressionOpt.Value, Diagnostics); + if (result.IsSome) + { + couldParse = true; + expressionOpt = result; + break; + } } - if (this.TokenReader.Position != start) - this.TokenReader.Restore(start); + TokenReader.Restore(start); } } while (couldParse); - return true; + return expressionOpt; } /// - public virtual Boolean TryParseExpression([NotNullWhen(true)] out TExpressionNode expression) => - this.TryParseExpression(0, out expression); + public virtual Option ParseExpression() => ParseExpression(0); #endregion ParseExpression diff --git a/GParse/Parsing/PrattParserBuilder.cs b/GParse/Parsing/PrattParserBuilder.cs index 94d7e22..0246e87 100644 --- a/GParse/Parsing/PrattParserBuilder.cs +++ b/GParse/Parsing/PrattParserBuilder.cs @@ -33,14 +33,16 @@ public class PrattParserBuilder : IPrattParserBuild /// /// /// - public virtual void Register(TTokenType tokenType, IPrefixParselet parselet) + public virtual void Register( + TTokenType tokenType, + IPrefixParselet parselet) { if (tokenType is null) throw new ArgumentNullException(nameof(tokenType)); if (parselet is null) throw new ArgumentNullException(nameof(parselet)); - this.PrefixModuleTree.AddModule(tokenType, parselet); + PrefixModuleTree.AddModule(tokenType, parselet); } /// @@ -49,7 +51,10 @@ public virtual void Register(TTokenType tokenType, IPrefixParselet /// /// - public virtual void Register(TTokenType tokenType, String id, IPrefixParselet parselet) + public virtual void Register( + TTokenType tokenType, + string id, + IPrefixParselet parselet) { if (tokenType is null) throw new ArgumentNullException(nameof(tokenType)); @@ -58,7 +63,7 @@ public virtual void Register(TTokenType tokenType, String id, IPrefixParselet @@ -66,14 +71,16 @@ public virtual void Register(TTokenType tokenType, String id, IPrefixParselet /// /// - public void Register(TTokenType tokenType, IInfixParselet parselet) + public void Register( + TTokenType tokenType, + IInfixParselet parselet) { if (tokenType is null) throw new ArgumentNullException(nameof(tokenType)); if (parselet is null) throw new ArgumentNullException(nameof(parselet)); - this.InfixModuleTree.AddModule(tokenType, parselet); + InfixModuleTree.AddModule(tokenType, parselet); } /// @@ -82,7 +89,10 @@ public void Register(TTokenType tokenType, IInfixParselet /// /// - public void Register(TTokenType tokenType, String id, IInfixParselet parselet) + public void Register( + TTokenType tokenType, + string id, + IInfixParselet parselet) { if (tokenType is null) throw new ArgumentNullException(nameof(tokenType)); @@ -91,7 +101,7 @@ public void Register(TTokenType tokenType, String id, IInfixParselet /// /// - public virtual void RegisterLiteral(TTokenType tokenType, LiteralNodeFactory factory) => - this.Register(tokenType, new LiteralParselet(factory)); + public virtual void RegisterLiteral( + TTokenType tokenType, + LiteralNodeFactory factory) => + Register(tokenType, new LiteralParselet(factory)); /// /// Registers a literal token @@ -112,8 +124,11 @@ public virtual void RegisterLiteral(TTokenType tokenType, LiteralNodeFactory /// /// - public virtual void RegisterLiteral(TTokenType tokenType, String ID, LiteralNodeFactory factory) => - this.Register(tokenType, ID, new LiteralParselet(factory)); + public virtual void RegisterLiteral( + TTokenType tokenType, + string ID, + LiteralNodeFactory factory) => + Register(tokenType, ID, new LiteralParselet(factory)); #endregion RegisterLiteral @@ -125,8 +140,11 @@ public virtual void RegisterLiteral(TTokenType tokenType, String ID, LiteralNode /// /// /// - public virtual void RegisterSingleTokenPrefixOperator(TTokenType tokenType, Int32 precedence, PrefixNodeFactory factory) => - this.Register(tokenType, new SingleTokenPrefixOperatorParselet(precedence, factory)); + public virtual void RegisterSingleTokenPrefixOperator( + TTokenType tokenType, + int precedence, + PrefixNodeFactory factory) => + Register(tokenType, new SingleTokenPrefixOperatorParselet(precedence, factory)); /// /// Registers a prefix operator composed of a single token @@ -135,8 +153,12 @@ public virtual void RegisterSingleTokenPrefixOperator(TTokenType tokenType, Int3 /// /// /// - public virtual void RegisterSingleTokenPrefixOperator(TTokenType tokenType, String ID, Int32 precedence, PrefixNodeFactory factory) => - this.Register(tokenType, ID, new SingleTokenPrefixOperatorParselet(precedence, factory)); + public virtual void RegisterSingleTokenPrefixOperator( + TTokenType tokenType, + string ID, + int precedence, + PrefixNodeFactory factory) => + Register(tokenType, ID, new SingleTokenPrefixOperatorParselet(precedence, factory)); #endregion RegisterSingleTokenPrefixOperator @@ -149,8 +171,12 @@ public virtual void RegisterSingleTokenPrefixOperator(TTokenType tokenType, Stri /// /// /// - public virtual void RegisterSingleTokenInfixOperator(TTokenType tokenType, Int32 precedence, Boolean isRightAssociative, InfixNodeFactory factory) => - this.Register(tokenType, new SingleTokenInfixOperatorParselet(precedence, isRightAssociative, factory)); + public virtual void RegisterSingleTokenInfixOperator( + TTokenType tokenType, + int precedence, + bool isRightAssociative, + InfixNodeFactory factory) => + Register(tokenType, new SingleTokenInfixOperatorParselet(precedence, isRightAssociative, factory)); /// /// Registers an infix operator composed of a single token @@ -160,8 +186,13 @@ public virtual void RegisterSingleTokenInfixOperator(TTokenType tokenType, Int32 /// /// /// - public virtual void RegisterSingleTokenInfixOperator(TTokenType tokenType, String ID, Int32 precedence, Boolean isRightAssociative, InfixNodeFactory factory) => - this.Register(tokenType, ID, new SingleTokenInfixOperatorParselet(precedence, isRightAssociative, factory)); + public virtual void RegisterSingleTokenInfixOperator( + TTokenType tokenType, + string ID, + int precedence, + bool isRightAssociative, + InfixNodeFactory factory) => + Register(tokenType, ID, new SingleTokenInfixOperatorParselet(precedence, isRightAssociative, factory)); #endregion RegisterSingleTokenInfixOperator @@ -173,8 +204,11 @@ public virtual void RegisterSingleTokenInfixOperator(TTokenType tokenType, Strin /// /// /// - public virtual void RegisterSingleTokenPostfixOperator(TTokenType tokenType, Int32 precedence, PostfixNodeFactory factory) => - this.Register(tokenType, new SingleTokenPostfixOperatorParselet(precedence, factory)); + public virtual void RegisterSingleTokenPostfixOperator( + TTokenType tokenType, + int precedence, + PostfixNodeFactory factory) => + Register(tokenType, new SingleTokenPostfixOperatorParselet(precedence, factory)); /// /// Registers a postfix operator composed of a single token @@ -183,8 +217,12 @@ public virtual void RegisterSingleTokenPostfixOperator(TTokenType tokenType, Int /// /// /// - public void RegisterSingleTokenPostfixOperator(TTokenType tokenType, String ID, Int32 precedence, PostfixNodeFactory factory) => - this.Register(tokenType, ID, new SingleTokenPostfixOperatorParselet(precedence, factory)); + public void RegisterSingleTokenPostfixOperator( + TTokenType tokenType, + string ID, + int precedence, + PostfixNodeFactory factory) => + Register(tokenType, ID, new SingleTokenPostfixOperatorParselet(precedence, factory)); #endregion RegisterSingleTokenPostfixOperator @@ -197,6 +235,6 @@ public void RegisterSingleTokenPostfixOperator(TTokenType tokenType, String ID, public virtual IPrattParser CreateParser( ITokenReader reader, DiagnosticList diagnostics) => - new PrattParser(reader, this.PrefixModuleTree, this.InfixModuleTree, diagnostics); + new PrattParser(reader, PrefixModuleTree, InfixModuleTree, diagnostics); } } \ No newline at end of file diff --git a/GParse/Parsing/PrattParserModuleTree.cs b/GParse/Parsing/PrattParserModuleTree.cs index 16f8e34..42145b1 100644 --- a/GParse/Parsing/PrattParserModuleTree.cs +++ b/GParse/Parsing/PrattParserModuleTree.cs @@ -12,9 +12,9 @@ namespace GParse.Parsing public class PrattParserModuleTree where TTokenType : notnull { - private readonly Dictionary>> modulesWithTargetId = new(); + private readonly Dictionary>> _modulesWithTargetId = new(); - private readonly Dictionary> modulesWithoutTargetId = new(); + private readonly Dictionary> _modulesWithoutTargetId = new(); /// /// Adds a module to the tree @@ -28,10 +28,10 @@ public void AddModule(TTokenType tokenType, TModule module) if (module is null) throw new ArgumentNullException(nameof(module)); - if (!this.modulesWithoutTargetId.TryGetValue(tokenType, out List? list)) + if (!_modulesWithoutTargetId.TryGetValue(tokenType, out var list)) { list = new List(); - this.modulesWithoutTargetId[tokenType] = list; + _modulesWithoutTargetId[tokenType] = list; } list.Add(module); @@ -43,7 +43,7 @@ public void AddModule(TTokenType tokenType, TModule module) /// /// /// - public void AddModule(TTokenType tokenType, String id, TModule module) + public void AddModule(TTokenType tokenType, string id, TModule module) { if (tokenType is null) throw new ArgumentNullException(nameof(tokenType)); @@ -52,13 +52,13 @@ public void AddModule(TTokenType tokenType, String id, TModule module) if (module is null) throw new ArgumentNullException(nameof(module)); - if (!this.modulesWithTargetId.TryGetValue(tokenType, out Dictionary>? dict)) + if (!_modulesWithTargetId.TryGetValue(tokenType, out var dict)) { - dict = new Dictionary>(StringComparer.Ordinal); - this.modulesWithTargetId[tokenType] = dict; + dict = new Dictionary>(StringComparer.Ordinal); + _modulesWithTargetId[tokenType] = dict; } - if (!dict.TryGetValue(id, out List? list)) + if (!dict.TryGetValue(id, out var list)) { list = new List(); dict[id] = list; @@ -77,18 +77,18 @@ public IEnumerable GetSortedCandidates(ITokenReader reader) if (reader is null) throw new ArgumentNullException(nameof(reader)); - Token peeked = reader.Lookahead(); + var peeked = reader.Lookahead(); - if (this.modulesWithTargetId.TryGetValue(peeked.Type, out Dictionary>? dict) - && dict.TryGetValue(peeked.Id, out List? candidates)) + if (_modulesWithTargetId.TryGetValue(peeked.Type, out var dict) + && dict.TryGetValue(peeked.Id, out var candidates)) { - foreach (TModule candidate in candidates) + foreach (var candidate in candidates) yield return candidate; } - if (this.modulesWithoutTargetId.TryGetValue(peeked.Type, out candidates)) + if (_modulesWithoutTargetId.TryGetValue(peeked.Type, out candidates)) { - foreach (TModule candidate in candidates) + foreach (var candidate in candidates) yield return candidate; } }