Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
Improve documentation and code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
GGG-KILLER committed Jul 28, 2019
1 parent 2e23dee commit d8b996a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
16 changes: 13 additions & 3 deletions GParse/Parsing/IPrattParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ public interface IPrattParser<TokenTypeT, ExpressionNodeT>
ITokenReader<TokenTypeT> TokenReader { get; }

/// <summary>
/// Attempts to parse an expression
/// Attempts to parse an expression with a minimum precedence of
/// <paramref name="minPrecedence" />.
/// </summary>
/// <param name="precedence"></param>
/// <remarks>
/// The minimum precedence is used to enforce the precedence of operators as well as
/// associativity.
///
/// The <see cref="Parselets.SingleTokenInfixOperatorParselet{TokenTypeT, ExpressionNodeT}" />
/// uses the <paramref name="minPrecedence" /> parameter to implement associativity by passing in
/// the associativity of the operator subtracted by one so that the operator itself is in the set
/// of possible parselets.
/// </remarks>
/// <param name="minPrecedence"></param>
/// <param name="expression"></param>
/// <returns></returns>
Boolean TryParseExpression ( Int32 precedence, out ExpressionNodeT expression );
Boolean TryParseExpression ( Int32 minPrecedence, out ExpressionNodeT expression );

/// <summary>
/// Attempts to parse an expression
Expand Down
17 changes: 15 additions & 2 deletions GParse/Parsing/Parselets/SingleTokenInfixOperatorParselet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,21 @@ public Boolean TryParse ( IPrattParser<TokenTypeT, ExpressionNodeT> parser, Expr
{
parsedExpression = default;
Token<TokenTypeT> op = parser.TokenReader.Consume ( );
return parser.TryParseExpression ( this.isRightAssociative ? this.Precedence - 1 : this.Precedence, out ExpressionNodeT nextExpr )
&& this.factory ( expression, op, nextExpr, out parsedExpression );

// 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;
else
minPrecedence = this.Precedence;

if ( parser.TryParseExpression ( minPrecedence, out ExpressionNodeT nextExpr ) )
return this.factory ( expression, op, nextExpr, out parsedExpression );
else
return false;
}
}
}
6 changes: 4 additions & 2 deletions GParse/Parsing/Parselets/SingleTokenPrefixOperatorParselet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ public Boolean TryParse ( IPrattParser<TokenTypeT, ExpressionNodeT> parser, IPro
{
parsedExpression = default;
Token<TokenTypeT> prefix = parser.TokenReader.Consume ( );
return parser.TryParseExpression ( this.precedence, out ExpressionNodeT expression )
&& this.factory ( prefix, expression, out parsedExpression );
if ( parser.TryParseExpression ( this.precedence, out ExpressionNodeT expression ) )
return this.factory ( prefix, expression, out parsedExpression );
else
return false;
}
}
}
23 changes: 6 additions & 17 deletions GParse/Parsing/PrattParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@ namespace GParse.Parsing
public class PrattParser<TokenTypeT, ExpressionNodeT> : IPrattParser<TokenTypeT, ExpressionNodeT>
{
/// <summary>
/// The registered <see cref="IPrefixParselet{TokenTypeT, ExpressionNodeT}" />
/// The this holds the tree of <see cref="IPrefixParselet{TokenTypeT, ExpressionNodeT}" /> to be used while parsing expressions.
/// </summary>
protected readonly PrattParserModuleTree<TokenTypeT, IPrefixParselet<TokenTypeT, ExpressionNodeT>> prefixModuleTree;

/// <summary>
/// The registered <see cref="IInfixParselet{TokenTypeT, ExpressionNodeT}" />
/// This holds the tree of <see cref="IInfixParselet{TokenTypeT, ExpressionNodeT}"/> to be used while parsing expressions.
/// </summary>
protected readonly PrattParserModuleTree<TokenTypeT, IInfixParselet<TokenTypeT, ExpressionNodeT>> infixModuleTree;

/// <summary>
/// The <see cref="Diagnostic" /> emitter provided to the constructor
/// This is the <see cref="IProgress{T}"/> reporter to which the parser should send <see cref="Diagnostic">Diagnostics</see> to.
/// </summary>
protected readonly IProgress<Diagnostic> diagnosticReporter;

/// <summary>
/// The parser's token reader
/// </summary>
/// <inheritdoc />
public ITokenReader<TokenTypeT> TokenReader { get; }

/// <summary>
Expand All @@ -50,13 +48,8 @@ protected internal PrattParser ( ITokenReader<TokenTypeT> tokenReader, PrattPars

#region ParseExpression

/// <summary>
/// <inheritdoc />
/// </summary>
/// <param name="precedence"></param>
/// <param name="expression"></param>
/// <returns></returns>
public virtual Boolean TryParseExpression ( Int32 precedence, out ExpressionNodeT expression )
public virtual Boolean TryParseExpression ( Int32 minPrecedence, out ExpressionNodeT expression )
{
expression = default;
var foundExpression = false;
Expand All @@ -81,7 +74,7 @@ public virtual Boolean TryParseExpression ( Int32 precedence, out ExpressionNode
foreach ( IInfixParselet<TokenTypeT, ExpressionNodeT> module in this.infixModuleTree.GetSortedCandidates ( this.TokenReader ) )
{
SourceLocation start = this.TokenReader.Location;
if ( precedence < module.Precedence
if ( minPrecedence < module.Precedence
&& module.TryParse ( this, expression, this.diagnosticReporter, out ExpressionNodeT tmpExpr ) )
{
couldParse = true;
Expand All @@ -97,11 +90,7 @@ public virtual Boolean TryParseExpression ( Int32 precedence, out ExpressionNode
return true;
}

/// <summary>
/// <inheritdoc />
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public virtual Boolean TryParseExpression ( out ExpressionNodeT expression ) =>
this.TryParseExpression ( 0, out expression );

Expand Down

0 comments on commit d8b996a

Please sign in to comment.