Skip to content

Commit

Permalink
A Practical Approach dotnet#2
Browse files Browse the repository at this point in the history
  • Loading branch information
elderlyscrolls committed Aug 10, 2024
1 parent 05201a1 commit 2883f46
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 125 deletions.
26 changes: 13 additions & 13 deletions src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,7 @@ internal CompilationUnitSyntax ParseCompilationUnit()
{
return ParseWithStackGuard(
static @this => @this.ParseCompilationUnitCore(),
static @this => SyntaxFactory.CompilationUnit(
new SyntaxList<ExternAliasDirectiveSyntax>(),
new SyntaxList<UsingDirectiveSyntax>(),
new SyntaxList<AttributeListSyntax>(),
new SyntaxList<MemberDeclarationSyntax>(),
SyntaxFactory.Token(SyntaxKind.EndOfFileToken)));
static @this => SyntaxFactory.CompilationUnit(endOfFileToken: SyntaxKind.EndOfFileToken));
}

internal CompilationUnitSyntax ParseCompilationUnitCore()
Expand Down Expand Up @@ -6213,7 +6208,7 @@ private void ParseTypeArgumentList(out SyntaxToken open, SeparatedSyntaxListBuil
if (isOpenName)
{
// NOTE: trivia will be attached to comma, not omitted type argument
var omittedTypeArgumentInstance = _syntaxFactory.OmittedTypeArgument(SyntaxFactory.Token(SyntaxKind.OmittedTypeArgumentToken));
var omittedTypeArgumentInstance = _syntaxFactory.OmittedTypeArgument(SyntaxKind.OmittedTypeArgumentToken);
types.Add(omittedTypeArgumentInstance);
while (this.CurrentToken.Kind == SyntaxKind.CommaToken)
{
Expand Down Expand Up @@ -7521,7 +7516,7 @@ private ArrayRankSpecifierSyntax ParseArrayRankSpecifier(out bool sawNonOmittedS
var open = this.EatToken(SyntaxKind.OpenBracketToken);
var list = _pool.AllocateSeparated<ExpressionSyntax>();

var omittedArraySizeExpressionInstance = _syntaxFactory.OmittedArraySizeExpression(SyntaxFactory.Token(SyntaxKind.OmittedArraySizeExpressionToken));
var omittedArraySizeExpressionInstance = _syntaxFactory.OmittedArraySizeExpression(SyntaxKind.OmittedArraySizeExpressionToken);
int lastTokenPosition = -1;
while (IsMakingProgress(ref lastTokenPosition) && this.CurrentToken.Kind != SyntaxKind.CloseBracketToken)
{
Expand Down Expand Up @@ -11580,16 +11575,21 @@ private ExpressionSyntax ParsePostFixExpression(ExpressionSyntax expr)
//
// It's far more likely the member access expression is simply incomplete and
// there is a new declaration on the next line.
if (this.CurrentToken.TrailingTrivia.Any((int)SyntaxKind.EndOfLineTrivia) &&
this.PeekToken(1).Kind == SyntaxKind.IdentifierToken &&
this.PeekToken(2).ContextualKind == SyntaxKind.IdentifierToken)

var dotToken = this.EatToken(SyntaxKind.DotToken);
var excToken = NoTriviaBetween(dotToken, CurrentToken) ? this.TryEatToken(SyntaxKind.ExclamationToken) : null;
SyntaxToken syntaxToken = excToken is null ? dotToken : NoTriviaBetween(excToken, dotToken) ? SyntaxFactory.Merge(SyntaxKind.DotExcalamationToken, dotToken, excToken) : dotToken;

if (syntaxToken.TrailingTrivia.Any((int)SyntaxKind.EndOfLineTrivia) &&
this.CurrentToken.Kind == SyntaxKind.IdentifierToken &&
this.PeekToken(1).ContextualKind == SyntaxKind.IdentifierToken)
{
return _syntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression, expr, this.EatToken(),
SyntaxKind.SimpleMemberAccessExpression, expr, syntaxToken,
this.AddError(this.CreateMissingIdentifierName(), ErrorCode.ERR_IdentifierExpected));
}

expr = _syntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, expr, this.EatToken(), this.ParseSimpleName(NameOptions.InExpression));
expr = _syntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, expr, syntaxToken, this.ParseSimpleName(NameOptions.InExpression));
continue;

case SyntaxKind.QuestionToken:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,15 @@ namespace Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax
[DebuggerDisplay("{GetDebuggerDisplay(), nq}")]
internal abstract class CSharpSyntaxNode : GreenNode
{
internal CSharpSyntaxNode(SyntaxKind kind)
: base((ushort)kind)
{
GreenStats.NoteGreen(this);
}

internal CSharpSyntaxNode(SyntaxKind kind, int fullWidth)
: base((ushort)kind, fullWidth)
{
GreenStats.NoteGreen(this);
}

internal CSharpSyntaxNode(SyntaxKind kind, DiagnosticInfo[] diagnostics)
: base((ushort)kind, diagnostics)
{
GreenStats.NoteGreen(this);
}

internal CSharpSyntaxNode(SyntaxKind kind, DiagnosticInfo[] diagnostics, int fullWidth)
: base((ushort)kind, diagnostics, fullWidth)
{
GreenStats.NoteGreen(this);
}
internal CSharpSyntaxNode(SyntaxKind kind, int fullWidth = 0)
: base((ushort)kind, fullWidth) { }

internal CSharpSyntaxNode(SyntaxKind kind, DiagnosticInfo[] diagnostics, SyntaxAnnotation[] annotations)
: base((ushort)kind, diagnostics, annotations)
{
GreenStats.NoteGreen(this);
}
internal CSharpSyntaxNode(SyntaxKind kind, DiagnosticInfo[] diagnostics, int fullWidth = 0)
: base((ushort)kind, diagnostics, fullWidth) { }

internal CSharpSyntaxNode(SyntaxKind kind, DiagnosticInfo[] diagnostics, SyntaxAnnotation[] annotations, int fullWidth)
: base((ushort)kind, diagnostics, annotations, fullWidth)
{
GreenStats.NoteGreen(this);
}
internal CSharpSyntaxNode(SyntaxKind kind, DiagnosticInfo[] diagnostics, SyntaxAnnotation[] annotations, int fullWidth = 0)
: base((ushort)kind, diagnostics, annotations, fullWidth) { }

public override string Language
{
Expand Down Expand Up @@ -213,30 +187,6 @@ private static void GetDirectives(GreenNode node, List<DirectiveTriviaSyntax> di
}
}

/// <summary>
/// Should only be called during construction.
/// </summary>
/// <remarks>
/// This should probably be an extra constructor parameter, but we don't need more constructor overloads.
/// </remarks>
protected void SetFactoryContext(SyntaxFactoryContext context)
=> SetFlags(context.IsInAsync ? NodeFlags.FactoryContextIsInAsync : context.IsInQuery ? NodeFlags.FactoryContextIsInQuery : null);

internal static NodeFlags SetFactoryContext(NodeFlags flags, SyntaxFactoryContext context)
{
if (context.IsInAsync)
{
flags |= NodeFlags.FactoryContextIsInAsync;
}

if (context.IsInQuery)
{
flags |= NodeFlags.FactoryContextIsInQuery;
}

return flags;
}

public sealed override CodeAnalysis.SyntaxToken CreateSeparator(SyntaxNode element)
{
return CSharp.SyntaxFactory.Token(SyntaxKind.CommaToken);
Expand Down
65 changes: 9 additions & 56 deletions src/Compilers/Core/Portable/Syntax/GreenNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ namespace Microsoft.CodeAnalysis
[DebuggerDisplay("{GetDebuggerDisplay(), nq}")]
internal abstract partial class GreenNode
{
private string GetDebuggerDisplay()
{
return this.GetType().Name + " " + this.KindText + " " + this.ToString();
}

internal const int ListKind = 1;

// Pack the kind, node-flags, slot-count, and full-width into 64bits. Note: if we need more bits in the future
Expand All @@ -36,7 +31,7 @@ private string GetDebuggerDisplay()
protected const int SlotCountTooLarge = 0b0000000000001111;

private readonly ushort _kind;
private NodeFlagsAndSlotCount _nodeFlagsAndSlotCount;
internal NodeFlagsAndSlotCount _nodeFlagsAndSlotCount;
private int _fullWidth;

private static readonly ConditionalWeakTable<GreenNode, DiagnosticInfo[]> s_diagnosticsTable =
Expand All @@ -49,55 +44,18 @@ private string GetDebuggerDisplay()
private static readonly SyntaxAnnotation[] s_noAnnotations = Array.Empty<SyntaxAnnotation>();
private static readonly IEnumerable<SyntaxAnnotation> s_noAnnotationsEnumerable = SpecializedCollections.EmptyEnumerable<SyntaxAnnotation>();

protected GreenNode(ushort kind)
{
_kind = kind;
}

protected GreenNode(ushort kind, int fullWidth)
{
_kind = kind;
_fullWidth = fullWidth;
}

protected GreenNode(ushort kind, DiagnosticInfo[]? diagnostics, int fullWidth)
{
_kind = kind;
_fullWidth = fullWidth;
if (diagnostics?.Length > 0)
{
SetFlags(NodeFlags.ContainsDiagnostics);
s_diagnosticsTable.Add(this, diagnostics);
}
}

protected GreenNode(ushort kind, DiagnosticInfo[]? diagnostics)
{
_kind = kind;
protected GreenNode(ushort kind, int fullWidth = 0) => (this._kind, this._fullWidth) = (kind, fullWidth);
protected GreenNode(ushort kind, DiagnosticInfo[]? diagnostics, int fullWidth = 0)
: this(kind, fullWidth) {
if (diagnostics?.Length > 0)
{
SetFlags(NodeFlags.ContainsDiagnostics);
s_diagnosticsTable.Add(this, diagnostics);
}
}

protected GreenNode(ushort kind, DiagnosticInfo[]? diagnostics, SyntaxAnnotation[]? annotations) :
this(kind, diagnostics)
{
if (annotations?.Length > 0)
{
foreach (var annotation in annotations)
{
if (annotation == null) throw new ArgumentException(paramName: nameof(annotations), message: "" /*CSharpResources.ElementsCannotBeNull*/);
}

SetFlags(NodeFlags.HasAnnotationsDirectly | NodeFlags.ContainsAnnotations);
s_annotationsTable.Add(this, annotations);
}
}

protected GreenNode(ushort kind, DiagnosticInfo[]? diagnostics, SyntaxAnnotation[]? annotations, int fullWidth) :
this(kind, diagnostics, fullWidth)
protected GreenNode(ushort kind, DiagnosticInfo[]? diagnostics, SyntaxAnnotation[]? annotations, int fullWidth = 0)
: this(kind, diagnostics, fullWidth)
{
if (annotations?.Length > 0)
{
Expand Down Expand Up @@ -293,16 +251,11 @@ internal NodeFlags Flags
get { return this._nodeFlagsAndSlotCount.NodeFlags; }
}

internal void SetFlags(NodeFlags? flags)
{
if (flags is not null)
_nodeFlagsAndSlotCount.NodeFlags |= flags.Value;
}
internal void SetFlags(NodeFlags flags)
=> _nodeFlagsAndSlotCount.NodeFlags |= flags;

internal void ClearFlags(NodeFlags flags)
{
_nodeFlagsAndSlotCount.NodeFlags &= ~flags;
}
=> _nodeFlagsAndSlotCount.NodeFlags &= ~flags;

internal bool IsMissing
{
Expand Down

0 comments on commit 2883f46

Please sign in to comment.