Skip to content

Commit e39dee9

Browse files
authored
Merge pull request #1553 from EvilBeaver/feature/1545-comment-after-directive
fixes #1545 Разрешено иметь комментарии в строке с директивами аннотации модуля
2 parents 90d26e4 + d8dfe79 commit e39dee9

14 files changed

Lines changed: 269 additions & 219 deletions

File tree

src/OneScript.Language/ErrorPositionInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace OneScript.Language
1010
public class ErrorPositionInfo
1111
{
1212
public const int OUT_OF_TEXT = -1;
13-
13+
1414
public ErrorPositionInfo()
1515
{
1616
LineNumber = OUT_OF_TEXT;

src/OneScript.Language/LexicalAnalysis/DefaultLexer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ This Source Code Form is subject to the terms of the
77

88
namespace OneScript.Language.LexicalAnalysis
99
{
10+
/// <summary>
11+
/// Лексер, который выдает все, кроме комментариев.
12+
/// </summary>
1013
public class DefaultLexer : FullSourceLexer
1114
{
1215
public override Lexem NextLexem()

src/OneScript.Language/LexicalAnalysis/ExpressionBasedLexer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ namespace OneScript.Language.LexicalAnalysis
1212
{
1313
public class ExpressionBasedLexer : ILexer
1414
{
15-
private readonly Func<char, LexerState> _selector;
15+
private readonly Func<SourceCodeIterator, char, LexerState> _selector;
1616

17-
internal ExpressionBasedLexer(Func<char, LexerState> selector)
17+
internal ExpressionBasedLexer(Func<SourceCodeIterator, char, LexerState> selector)
1818
{
1919
_selector = selector;
2020
}
@@ -23,7 +23,7 @@ public Lexem NextLexem()
2323
{
2424
if (Iterator.MoveToContent())
2525
{
26-
var state = _selector(Iterator.CurrentSymbol);
26+
var state = _selector(Iterator, Iterator.CurrentSymbol);
2727
if (state == default)
2828
{
2929
throw new SyntaxErrorException(Iterator.GetErrorPosition(),

src/OneScript.Language/LexicalAnalysis/LexerBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public ILexer Build()
4141
}
4242

4343

44-
var lambda = Expression.Lambda<Func<char, LexerState>>(expr, charParam);
44+
var lambda = Expression.Lambda<Func<SourceCodeIterator, char, LexerState>>(expr, iteratorParam, charParam);
4545
var func = lambda.Compile();
4646

4747
return new ExpressionBasedLexer(func);

src/OneScript.Language/SyntaxAnalysis/ImportDirectivesHandler.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ public class ImportDirectivesHandler : ModuleAnnotationDirectiveHandler
1717
public ImportDirectivesHandler(IErrorSink errorSink) : base(errorSink)
1818
{
1919
var builder = new LexerBuilder();
20-
builder.Detect((cs, i) => !char.IsWhiteSpace(cs))
21-
.HandleWith(new NonWhitespaceLexerState());
20+
builder
21+
.DetectComments()
22+
.Detect((cs, i) => !char.IsWhiteSpace(cs))
23+
.HandleWith(new NonWhitespaceLexerState());
2224

2325
_importClauseLexer = builder.Build();
2426
}
@@ -46,7 +48,7 @@ protected override void ParseAnnotationInternal(
4648
parserContext.AddChild(node);
4749

4850
lex = _importClauseLexer.NextLexemOnSameLine();
49-
if (lex.Type != LexemType.EndOfText)
51+
if (lex.Type != LexemType.EndOfText && lex.Type != LexemType.Comment)
5052
{
5153
ErrorSink.AddError(LocalizedErrors.UnexpectedOperation());
5254
return;

src/OneScript.Language/SyntaxAnalysis/SingleWordModuleAnnotationHandler.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,25 @@ public class SingleWordModuleAnnotationHandler : ModuleAnnotationDirectiveHandle
2323

2424
public SingleWordModuleAnnotationHandler(ISet<string> knownNames, IErrorSink errorSink) : base(errorSink)
2525
{
26-
var builder = new LexerBuilder();
27-
builder.Detect((cs, i) => !char.IsWhiteSpace(cs))
28-
.HandleWith(new WordLexerState());
26+
var builder = SetupLexerBuilder();
2927

3028
_allLineContentLexer = builder.Build();
3129
_knownNames = knownNames;
3230
}
33-
34-
public SingleWordModuleAnnotationHandler(ISet<BilingualString> knownNames, IErrorSink errorSink) : base(errorSink)
31+
32+
private static LexerBuilder SetupLexerBuilder()
3533
{
3634
var builder = new LexerBuilder();
37-
builder.Detect((cs, i) => !char.IsWhiteSpace(cs))
35+
builder
36+
.DetectComments()
37+
.Detect((cs, i) => !char.IsWhiteSpace(cs))
3838
.HandleWith(new WordLexerState());
39+
return builder;
40+
}
41+
42+
public SingleWordModuleAnnotationHandler(ISet<BilingualString> knownNames, IErrorSink errorSink) : base(errorSink)
43+
{
44+
var builder = SetupLexerBuilder();
3945

4046
_allLineContentLexer = builder.Build();
4147

@@ -61,7 +67,7 @@ protected override void ParseAnnotationInternal(ref Lexem lastExtractedLexem, IL
6167
// после ничего не должно находиться
6268
var nextLexem = _allLineContentLexer.NextLexemOnSameLine();
6369
lastExtractedLexem = lexer.NextLexem(); // сдвиг основного лексера
64-
if (nextLexem.Type != LexemType.EndOfText)
70+
if (nextLexem.Type != LexemType.EndOfText && nextLexem.Type != LexemType.Comment)
6571
{
6672
var err = LocalizedErrors.ExpressionSyntax();
6773
err.Position = new ErrorPositionInfo

src/OneScript.Language/SyntaxErrorException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public SyntaxErrorException(ErrorPositionInfo codeInfo, string message):base(cod
1414

1515
}
1616

17-
internal SyntaxErrorException(CodeError error) : base(error.Position, error.Description)
17+
internal SyntaxErrorException(CodeError error) : base(error.Position ?? new ErrorPositionInfo(), error.Description)
1818
{
1919

2020
}

src/Tests/DocumenterTests/DocumenterTests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
2+
<Import Project="$(MSBuildProjectDirectory)/../../oscommon.targets" />
33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>$(TargetFrameworkVersion)</TargetFramework>
55
<IsPackable>false</IsPackable>
66
<IsTestProject>true</IsTestProject>
77
<LangVersion>8</LangVersion>

src/Tests/OneScript.Language.Tests/ParserTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,46 @@ public void Check_AstDirectives_Creation()
958958
batch.NoMoreChildren();
959959
}
960960

961+
[Fact]
962+
public void CanHaveCommentAfterUseStatement()
963+
{
964+
var code = @"
965+
#Использовать Библиотека // Подключаем библиотеку
966+
#Использовать ""Библиотека с кавычками"" // Подключаем библиотеку с кавычками
967+
";
968+
969+
var defaultLex = new DefaultLexer();
970+
defaultLex.Iterator = SourceCodeHelper.FromString(code).CreateIterator();
971+
972+
var lexer = new PreprocessingLexer(defaultLex);
973+
974+
lexer.Handlers = new PreprocessorHandlers(
975+
new[] {new ImportDirectivesHandler(new ThrowingErrorSink())});
976+
977+
var parser = new DefaultBslParser(
978+
lexer,
979+
new ListErrorSink(),
980+
lexer.Handlers);
981+
982+
var moduleNode = parser.ParseStatefulModule();
983+
moduleNode.Should().NotBeNull();
984+
parser.Errors.Should().BeEmpty("the valid code is passed");
985+
var firstChild = new SyntaxTreeValidator(new TestAstNode(moduleNode.Children.First()));
986+
987+
var batch = new SyntaxTreeValidator(new TestAstNode(firstChild.CurrentNode.RealNode.Parent));
988+
989+
var node = batch.NextChild();
990+
node.Is(NodeKind.Import)
991+
.Equal("Использовать");
992+
node.NextChild().Is(NodeKind.AnnotationParameter)
993+
.Equal("Библиотека");
994+
995+
node = batch.NextChild();
996+
node.Is(NodeKind.Import)
997+
.Equal("Использовать");
998+
node.NextChild().Is(NodeKind.AnnotationParameter)
999+
.Equal("\"Библиотека с кавычками\"");
1000+
}
9611001

9621002
[Fact]
9631003
public void Check_No_Semicolon_In_If()

src/Tests/OneScript.Language.Tests/TestAstNode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public TestAstNode(BslSyntaxNode node)
3131
BinaryOperationNode binary => binary.Operation.ToString(),
3232
UnaryOperationNode unary => unary.Operation.ToString(),
3333
PreprocessorDirectiveNode preproc => preproc.DirectiveName,
34+
AnnotationParameterNode annParam => annParam.Value.Content,
3435
_ => nonTerm.ToString()
3536
};
3637
}

0 commit comments

Comments
 (0)