From 63f00e7d3137c7de4cab6c33ee128f4f8f885cc2 Mon Sep 17 00:00:00 2001 From: Semenov Dmitry Date: Sun, 28 Jan 2024 14:49:43 +0400 Subject: [PATCH] Improved same line attributes parsing --- .../InvalidTokenTests.cs | 3 +- .../Resolvers/GDClassMembersResolver.cs | 70 +++++++------------ 2 files changed, 29 insertions(+), 44 deletions(-) diff --git a/src/GDShrapt.Reader.Tests/InvalidTokenTests.cs b/src/GDShrapt.Reader.Tests/InvalidTokenTests.cs index 2f1b56f..0ac3b94 100644 --- a/src/GDShrapt.Reader.Tests/InvalidTokenTests.cs +++ b/src/GDShrapt.Reader.Tests/InvalidTokenTests.cs @@ -52,7 +52,8 @@ extends ResourceFormatSaver @class.AllInvalidTokens.Select(x => x.ToString()).Should().BeEquivalentTo(new string[] { - "123H+=Ter^5r3_-ain-DataSaver" + "123", + "H+=Ter^5r3_-ain-DataSaver" }); } } diff --git a/src/GDShrapt.Reader/Resolvers/GDClassMembersResolver.cs b/src/GDShrapt.Reader/Resolvers/GDClassMembersResolver.cs index 80a93f4..909d70d 100644 --- a/src/GDShrapt.Reader/Resolvers/GDClassMembersResolver.cs +++ b/src/GDShrapt.Reader/Resolvers/GDClassMembersResolver.cs @@ -5,7 +5,7 @@ namespace GDShrapt.Reader { internal class GDClassMembersResolver : GDIntendedResolver { - bool _memberResolved; + bool _handleEndOfTheLineAsInvalid; readonly StringBuilder _sequenceBuilder = new StringBuilder(); @@ -22,7 +22,7 @@ public GDClassMembersResolver(IIntendedTokenReceiver owner, int l internal override void HandleCharAfterIntendation(char c, GDReadingState state) { - if (_memberResolved) + if (_handleEndOfTheLineAsInvalid) { if (IsSpace(c)) { @@ -83,7 +83,7 @@ internal override void HandleNewLineAfterIntendation(GDReadingState state) return; } - _memberResolved = false; + _handleEndOfTheLineAsInvalid = false; ResetIntendation(); state.PassNewLine(); } @@ -137,7 +137,7 @@ private void Complete(GDReadingState state, string sequence) return; } - _memberResolved = true; + _handleEndOfTheLineAsInvalid = true; void HandleStaticIfMet(ITokenReceiver spaceReceiver, Action push, bool invalid = true) { @@ -193,30 +193,38 @@ void HandleStaticIfMet(ITokenReceiver spaceReceiver, Action push, bool { case "@icon": { - Owner.HandleReceivedToken(state.Push(new GDCustomAttribute())); - for (int i = 0; i < sequence.Length; i++) - state.PassChar(sequence[i]); + var a = new GDCustomAttribute(); + HandleStaticIfMet(a, () => Owner.HandleReceivedToken(state.Push(a))); + a.Add(new GDAttribute().AddAt().Add(new GDIdentifier() { Sequence = "icon" })); + + _handleEndOfTheLineAsInvalid = false; } break; case "class_name": { - Owner.HandleReceivedToken(state.Push(new GDClassNameAttribute())); - for (int i = 0; i < sequence.Length; i++) - state.PassChar(sequence[i]); + var a = new GDClassNameAttribute(); + HandleStaticIfMet(a, () => Owner.HandleReceivedToken(state.Push(a))); + a.Add(new GDClassNameKeyword()); + + _handleEndOfTheLineAsInvalid = false; } break; case "extends": { - Owner.HandleReceivedToken(state.Push(new GDExtendsAttribute())); - for (int i = 0; i < sequence.Length; i++) - state.PassChar(sequence[i]); + var a = new GDExtendsAttribute(); + HandleStaticIfMet(a, () => Owner.HandleReceivedToken(state.Push(a))); + a.Add(new GDExtendsKeyword()); + + _handleEndOfTheLineAsInvalid = false; } break; case "tool": { - Owner.HandleReceivedToken(state.Push(new GDToolAttribute())); - for (int i = 0; i < sequence.Length; i++) - state.PassChar(sequence[i]); + var a = new GDToolAttribute(); + HandleStaticIfMet(a, () => Owner.HandleReceivedToken(state.Push(a))); + a.Add(new GDToolKeyword()); + + _handleEndOfTheLineAsInvalid = false; } break; case "signal": @@ -235,7 +243,7 @@ void HandleStaticIfMet(ITokenReceiver spaceReceiver, Action push, bool break; case "static": { - _memberResolved = false; + _handleEndOfTheLineAsInvalid = false; _sequenceBuilder.Clear(); if (_staticMet) @@ -259,30 +267,6 @@ void HandleStaticIfMet(ITokenReceiver spaceReceiver, Action push, bool m.Add(new GDFuncKeyword()); } break; - /* case "export": - { - var m = new GDMethodDeclaration(LineIntendationThreshold); - _memberResolved = false; - Owner.HandleReceivedToken(state.Push(new GDClassMemberAttributeDeclaration(LineIntendationThreshold))); - - if (sequence != null) - for (int i = 0; i < sequence.Length; i++) - state.PassChar(sequence[i]); - - return; - } - case "onready": - { - var m = new GDMethodDeclaration(LineIntendationThreshold); - _memberResolved = false; - Owner.HandleReceivedToken(state.Push(new GDClassMemberAttributeDeclaration(LineIntendationThreshold))); - - if (sequence != null) - for (int i = 0; i < sequence.Length; i++) - state.PassChar(sequence[i]); - - return; - }*/ case "const": { var m = new GDVariableDeclaration(LineIntendationThreshold); @@ -306,7 +290,7 @@ void HandleStaticIfMet(ITokenReceiver spaceReceiver, Action push, bool break; default: { - _memberResolved = false; + _handleEndOfTheLineAsInvalid = false; HandleStaticIfMet(null, () => Owner.HandleReceivedToken(state.Push(new GDInvalidToken(x => x.IsSpace() || x.IsNewLine())))); @@ -329,7 +313,7 @@ internal override void ForceComplete(GDReadingState state) return; } - if (!_memberResolved) + if (!_handleEndOfTheLineAsInvalid) SendIntendationTokensToOwner(); base.ForceComplete(state); }