Skip to content

Commit

Permalink
Merge pull request #399 from Luthetus/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
Luthetus authored Dec 23, 2024
2 parents ee287c1 + 23e3a95 commit 8c7a292
Show file tree
Hide file tree
Showing 28 changed files with 949 additions and 277 deletions.

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions Source/Lib/CompilerServices/CSharp/BinderCase/CSharpBinder.Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ public void BindStringVerbatimExpression(
DecorationByte = (byte)GenericDecorationKind.StringLiteral,
}), compilationUnit);
}

public void BindDiscard(
IdentifierToken identifierToken,
CSharpCompilationUnit compilationUnit)
{
AddSymbolReference(new DiscardSymbol(identifierToken.TextSpan with
{
DecorationByte = (byte)GenericDecorationKind.None,
}), compilationUnit);
}

public void BindFunctionDefinitionNode(
FunctionDefinitionNode functionDefinitionNode,
Expand Down Expand Up @@ -1777,5 +1787,13 @@ public void OnBoundScopeCreatedAndSetAsCurrent(ICodeBlockOwner codeBlockOwner, C
compilationUnit.Binder.BindVariableDeclarationNode(argument.VariableDeclarationNode, compilationUnit);
}
}
else if (codeBlockOwner.SyntaxKind == SyntaxKind.LambdaExpressionNode)
{
var lambdaExpressionNode = (LambdaExpressionNode)codeBlockOwner;
foreach (var variableDeclarationNode in lambdaExpressionNode.VariableDeclarationNodeList)
{
compilationUnit.Binder.BindVariableDeclarationNode(variableDeclarationNode, compilationUnit);
}
}
}
}
215 changes: 89 additions & 126 deletions Source/Lib/CompilerServices/CSharp/LexerCase/CSharpLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ public static CSharpLexerOutput Lex(ResourceUri resourceUri, string sourceText)
case '+':
if (stringWalker.PeekCharacter(1) == '+')
{
LexPlusPlusToken(ref lexerOutput, ref stringWalker);
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new PlusPlusToken(textSpan));
}
else
{
Expand All @@ -131,7 +135,11 @@ public static CSharpLexerOutput Lex(ResourceUri resourceUri, string sourceText)
case '-':
if (stringWalker.PeekCharacter(1) == '-')
{
LexMinusMinusToken(ref lexerOutput, ref stringWalker);
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new MinusMinusToken(textSpan));
}
else
{
Expand All @@ -144,8 +152,20 @@ public static CSharpLexerOutput Lex(ResourceUri resourceUri, string sourceText)
case '=':
if (stringWalker.PeekCharacter(1) == '=')
{
LexEqualsEqualsToken(ref lexerOutput, ref stringWalker);
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new EqualsEqualsToken(textSpan));
}
else if (stringWalker.PeekCharacter(1) == '>')
{
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new EqualsCloseAngleBracketToken(textSpan));
}
else
{
var entryPositionIndex = stringWalker.PositionIndex;
Expand All @@ -157,7 +177,11 @@ public static CSharpLexerOutput Lex(ResourceUri resourceUri, string sourceText)
case '?':
if (stringWalker.PeekCharacter(1) == '?')
{
LexQuestionMarkQuestionMarkToken(ref lexerOutput, ref stringWalker);
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new QuestionMarkQuestionMarkToken(textSpan));
}
else
{
Expand Down Expand Up @@ -211,10 +235,21 @@ public static CSharpLexerOutput Lex(ResourceUri resourceUri, string sourceText)
}
case '!':
{
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new BangToken(textSpan));
if (stringWalker.PeekCharacter(1) == '=')
{
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new BangEqualsToken(textSpan));
}
else
{
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new BangToken(textSpan));
}
break;
}
case ';':
Expand Down Expand Up @@ -259,18 +294,40 @@ public static CSharpLexerOutput Lex(ResourceUri resourceUri, string sourceText)
}
case '<':
{
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new OpenAngleBracketToken(textSpan));
if (stringWalker.PeekCharacter(1) == '=')
{
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new OpenAngleBracketEqualsToken(textSpan));
}
else
{
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new OpenAngleBracketToken(textSpan));
}
break;
}
case '>':
{
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new CloseAngleBracketToken(textSpan));
if (stringWalker.PeekCharacter(1) == '=')
{
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new CloseAngleBracketEqualsToken(textSpan));
}
else
{
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new CloseAngleBracketToken(textSpan));
}
break;
}
case '[':
Expand Down Expand Up @@ -329,16 +386,30 @@ public static CSharpLexerOutput Lex(ResourceUri resourceUri, string sourceText)
}
else
{
LexDollarSignToken(ref lexerOutput, ref stringWalker);
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new DollarSignToken(textSpan));
break;
}
break;
case '@':
if (stringWalker.NextCharacter == '"')
{
LexString(ref lexerOutput, ref stringWalker, ref decorationByteLastEscapeCharacter, countDollarSign: 0, useVerbatim: true);
}
else if (stringWalker.PeekCharacter(1) == '$' && stringWalker.PeekCharacter(2) == '"')
{
LexString(ref lexerOutput, ref stringWalker, ref decorationByteLastEscapeCharacter, countDollarSign: 1, useVerbatim: true);
}
else
LexAtToken(ref lexerOutput, ref stringWalker);
{
var entryPositionIndex = stringWalker.PositionIndex;
stringWalker.ReadCharacter();
var textSpan = new TextEditorTextSpan(entryPositionIndex, stringWalker.PositionIndex, (byte)GenericDecorationKind.None, stringWalker.ResourceUri, stringWalker.SourceText);
lexerOutput.SyntaxTokenList.Add(new AtToken(textSpan));
break;
}
break;
case ':':
{
Expand Down Expand Up @@ -827,114 +898,6 @@ public static void LexCommentMultiLineToken(ref CSharpLexerOutput lexerOutput, r
lexerOutput.SyntaxTokenList.Add(new CommentMultiLineToken(textSpan));
}

public static void LexPlusPlusToken(ref CSharpLexerOutput lexerOutput, ref StringWalkerStruct stringWalker)
{
var entryPositionIndex = stringWalker.PositionIndex;

// First '+'
stringWalker.ReadCharacter();
// Second '+'
stringWalker.ReadCharacter();

var textSpan = new TextEditorTextSpan(
entryPositionIndex,
stringWalker.PositionIndex,
(byte)GenericDecorationKind.None,
stringWalker.ResourceUri,
stringWalker.SourceText);

lexerOutput.SyntaxTokenList.Add(new PlusPlusToken(textSpan));
}

public static void LexMinusMinusToken(ref CSharpLexerOutput lexerOutput, ref StringWalkerStruct stringWalker)
{
var entryPositionIndex = stringWalker.PositionIndex;

// First '-'
stringWalker.ReadCharacter();
// Second '-'
stringWalker.ReadCharacter();

var textSpan = new TextEditorTextSpan(
entryPositionIndex,
stringWalker.PositionIndex,
(byte)GenericDecorationKind.None,
stringWalker.ResourceUri,
stringWalker.SourceText);

lexerOutput.SyntaxTokenList.Add(new MinusMinusToken(textSpan));
}

public static void LexEqualsEqualsToken(ref CSharpLexerOutput lexerOutput, ref StringWalkerStruct stringWalker)
{
var entryPositionIndex = stringWalker.PositionIndex;

// First '='
stringWalker.ReadCharacter();
// Second '='
stringWalker.ReadCharacter();

var textSpan = new TextEditorTextSpan(
entryPositionIndex,
stringWalker.PositionIndex,
(byte)GenericDecorationKind.None,
stringWalker.ResourceUri,
stringWalker.SourceText);

lexerOutput.SyntaxTokenList.Add(new EqualsEqualsToken(textSpan));
}

public static void LexQuestionMarkQuestionMarkToken(ref CSharpLexerOutput lexerOutput, ref StringWalkerStruct stringWalker)
{
var entryPositionIndex = stringWalker.PositionIndex;

// First '?'
stringWalker.ReadCharacter();
// Second '?'
stringWalker.ReadCharacter();

var textSpan = new TextEditorTextSpan(
entryPositionIndex,
stringWalker.PositionIndex,
(byte)GenericDecorationKind.None,
stringWalker.ResourceUri,
stringWalker.SourceText);

lexerOutput.SyntaxTokenList.Add(new QuestionMarkQuestionMarkToken(textSpan));
}

public static void LexDollarSignToken(ref CSharpLexerOutput lexerOutput, ref StringWalkerStruct stringWalker)
{
var entryPositionIndex = stringWalker.PositionIndex;

stringWalker.ReadCharacter();

var textSpan = new TextEditorTextSpan(
entryPositionIndex,
stringWalker.PositionIndex,
(byte)GenericDecorationKind.None,
stringWalker.ResourceUri,
stringWalker.SourceText);

lexerOutput.SyntaxTokenList.Add(new DollarSignToken(textSpan));
}

public static void LexAtToken(ref CSharpLexerOutput lexerOutput, ref StringWalkerStruct stringWalker)
{
var entryPositionIndex = stringWalker.PositionIndex;

stringWalker.ReadCharacter();

var textSpan = new TextEditorTextSpan(
entryPositionIndex,
stringWalker.PositionIndex,
(byte)GenericDecorationKind.None,
stringWalker.ResourceUri,
stringWalker.SourceText);

lexerOutput.SyntaxTokenList.Add(new AtToken(textSpan));
}

public static void LexPreprocessorDirectiveToken(ref CSharpLexerOutput lexerOutput, ref StringWalkerStruct stringWalker)
{
var entryPositionIndex = stringWalker.PositionIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ public static FunctionArgumentsListingNode HandleFunctionArguments(CSharpCompila
{
break;
}
else if (parserModel.TokenWalker.Current.SyntaxKind == SyntaxKind.EqualsToken &&
parserModel.TokenWalker.Next.SyntaxKind == SyntaxKind.CloseAngleBracketToken)
else if (parserModel.TokenWalker.Current.SyntaxKind == SyntaxKind.EqualsCloseAngleBracketToken)
{
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public static void ParsePreprocessorDirectiveToken(

public static void ParseIdentifierToken(CSharpCompilationUnit compilationUnit, ref CSharpParserModel parserModel)
{
if (parserModel.TokenWalker.Current.TextSpan.Length == 1 &&
parserModel.TokenWalker.Current.TextSpan.GetText() == "_")
{
if (!compilationUnit.Binder.TryGetVariableDeclarationHierarchically(
compilationUnit,
compilationUnit.BinderSession.ResourceUri,
compilationUnit.BinderSession.CurrentScopeIndexKey,
parserModel.TokenWalker.Current.TextSpan.GetText(),
out _))
{
compilationUnit.Binder.BindDiscard((IdentifierToken)parserModel.TokenWalker.Current, compilationUnit);
_ = parserModel.TokenWalker.Consume();
return;
}
}

var originalTokenIndex = parserModel.TokenWalker.Index;

parserModel.TryParseExpressionSyntaxKindList.Add(SyntaxKind.TypeClauseNode);
Expand Down Expand Up @@ -71,6 +87,21 @@ public static void ParseIdentifierToken(CSharpCompilationUnit compilationUnit, r
MoveToHandleVariableDeclarationNode((VariableDeclarationNode)expressionNode, compilationUnit, ref parserModel);
return;
case SyntaxKind.VariableReferenceNode:

var isQuestionMarkMemberAccessToken = parserModel.TokenWalker.Current.SyntaxKind == SyntaxKind.QuestionMarkToken &&
parserModel.TokenWalker.Next.SyntaxKind == SyntaxKind.MemberAccessToken;

if ((parserModel.TokenWalker.Current.SyntaxKind == SyntaxKind.MemberAccessToken || isQuestionMarkMemberAccessToken) &&
originalTokenIndex == parserModel.TokenWalker.Index - 1)
{
_ = parserModel.TokenWalker.Backtrack();
expressionNode = ParseOthers.ParseExpression(compilationUnit, ref parserModel);
parserModel.StatementBuilder.ChildList.Add(expressionNode);
return;
}

parserModel.StatementBuilder.ChildList.Add(expressionNode);
return;
case SyntaxKind.FunctionInvocationNode:
case SyntaxKind.ConstructorInvocationExpressionNode:
parserModel.StatementBuilder.ChildList.Add(expressionNode);
Expand All @@ -96,8 +127,7 @@ public static void MoveToHandleVariableDeclarationNode(IVariableDeclarationNode
var variableKind = VariableKind.Local;

if (parserModel.TokenWalker.Current.SyntaxKind == SyntaxKind.OpenBraceToken ||
(parserModel.TokenWalker.Current.SyntaxKind == SyntaxKind.EqualsToken &&
parserModel.TokenWalker.Next.SyntaxKind == SyntaxKind.CloseAngleBracketToken))
parserModel.TokenWalker.Current.SyntaxKind == SyntaxKind.EqualsCloseAngleBracketToken)
{
variableKind = VariableKind.Property;
}
Expand All @@ -117,8 +147,7 @@ public static void MoveToHandleVariableDeclarationNode(IVariableDeclarationNode
{
ParsePropertyDefinition(compilationUnit, variableDeclarationNode, ref parserModel);
}
else if (parserModel.TokenWalker.Current.SyntaxKind == SyntaxKind.EqualsToken &&
parserModel.TokenWalker.Next.SyntaxKind == SyntaxKind.CloseAngleBracketToken)
else if (parserModel.TokenWalker.Current.SyntaxKind == SyntaxKind.EqualsCloseAngleBracketToken)
{
ParsePropertyDefinition_ExpressionBound(compilationUnit, ref parserModel);
}
Expand Down Expand Up @@ -202,8 +231,7 @@ public static void ParsePropertyDefinition(CSharpCompilationUnit compilationUnit

public static void ParsePropertyDefinition_ExpressionBound(CSharpCompilationUnit compilationUnit, ref CSharpParserModel parserModel)
{
var equalsToken = (EqualsToken)parserModel.TokenWalker.Consume();
var closeAngleBracketToken = (CloseAngleBracketToken)parserModel.TokenWalker.Consume();
var equalsCloseAngleBracketToken = (EqualsCloseAngleBracketToken)parserModel.TokenWalker.Consume();

var expressionNode = ParseOthers.ParseExpression(compilationUnit, ref parserModel);
var statementDelimiterToken = (StatementDelimiterToken)parserModel.TokenWalker.Match(SyntaxKind.StatementDelimiterToken);
Expand Down
Loading

0 comments on commit 8c7a292

Please sign in to comment.