Skip to content

Commit

Permalink
Лексер для меток
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilBeaver committed Oct 19, 2023
1 parent 9deefae commit 8fa327f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 9 deletions.
2 changes: 0 additions & 2 deletions src/OneScript.Language/LanguageDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ static LanguageDef()
AddToken(Token.Equal, "=");
AddToken(Token.Semicolon, ";");
AddToken(Token.Question, "?");
AddToken(Token.Tilde, "~");
AddToken(Token.Colon, ":");

#endregion

Expand Down
5 changes: 5 additions & 0 deletions src/OneScript.Language/LexicalAnalysis/FullSourceLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class FullSourceLexer : ILexer
private readonly LexerState _commentState = new CommentLexerState();
private readonly LexerState _annotationState = new AnnotationLexerState();
private readonly LexerState _directiveState = new PreprocessorDirectiveLexerState();
private readonly LexerState _labelState = new LabelLexerState();

private readonly FixedLexerState _fixedState = new FixedLexerState();

Expand Down Expand Up @@ -119,6 +120,10 @@ private void SelectState()
{
_state = _annotationState;
}
else if (cs == SpecialChars.Tilde)
{
_state = _labelState;
}
else
{
var cp = _iterator.GetErrorPosition();
Expand Down
39 changes: 39 additions & 0 deletions src/OneScript.Language/LexicalAnalysis/LabelLexerState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Diagnostics;
using OneScript.Localization;

namespace OneScript.Language.LexicalAnalysis
{
public class LabelLexerState : LexerState
{
private static BilingualString MESSAGE_NAME_EXPECTED = new BilingualString(
"Ожидается имя метки",
"Label name expected"
);

WordLexerState _wordExtractor = new WordLexerState();

public override Lexem ReadNextLexem(SourceCodeIterator iterator)
{
Debug.Assert(iterator.CurrentSymbol == SpecialChars.Tilde);

var start = new CodeRange(iterator.CurrentLine, iterator.CurrentColumn);
iterator.MoveNext();
if (!iterator.MoveToContent())
throw CreateExceptionOnCurrentLine(MESSAGE_NAME_EXPECTED.ToString(), iterator);

if (!char.IsLetter(iterator.CurrentSymbol))
throw CreateExceptionOnCurrentLine(MESSAGE_NAME_EXPECTED.ToString(), iterator);

var result = _wordExtractor.ReadNextLexem(iterator);
result.Type = LexemType.LabelRef;
if (iterator.CurrentSymbol == SpecialChars.Colon)
{
result.Type = LexemType.Label;
iterator.MoveNext();
}

result.Location = start;
return result;
}
}
}
2 changes: 2 additions & 0 deletions src/OneScript.Language/LexicalAnalysis/LexemType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public enum LexemType
PreprocessorDirective,
Annotation,
Comment,
Label,
LabelRef,
EndOfText
}
}
4 changes: 1 addition & 3 deletions src/OneScript.Language/LexicalAnalysis/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public enum Token
RemoveHandler,
Async,
Await,
Tilde,
Colon,


// operators
Plus,
Minus,
Expand Down
2 changes: 2 additions & 0 deletions src/OneScript.Language/SpecialChars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public static class SpecialChars
public const char QuestionMark = '?';
public const char Preprocessor = '#';
public const char Annotation = '&';
public const char Tilde = '~';
public const char Colon = ':';

public static bool IsOperatorChar(char symbol)
{
Expand Down
24 changes: 20 additions & 4 deletions src/Tests/OneScript.Language.Tests/LexerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,12 +586,12 @@ public void Syntax_Error_Handling()
Assert.Equal("Б", lex.Content);
}

[Fact(Skip = "Рефакторинг")]
[Fact]
public void New_Exception_Shows_Negative_Line_And_Column()
{
//var e = new ScriptException();
//Assert.True(e.LineNumber == -1);
//Assert.True(e.ColumnNumber == -1);
var e = new ScriptException("fake");
Assert.True(e.LineNumber == -1);
Assert.True(e.ColumnNumber == -1);
}

[Fact]
Expand Down Expand Up @@ -633,6 +633,22 @@ public void Lexer_Ignores_Comments()
Assert.Equal("value", lex.Content);
}

[Fact]
public void Leer_Extracts_Labels()
{
string code = "~ImALabel: ~LabelRef;";
var lexer = GetLexerForCode(code);

var lex = lexer.NextLexem();

Assert.Equal(LexemType.Label, lex.Type);
Assert.Equal("ImALabel", lex.Content);

lex = lexer.NextLexem();
Assert.Equal(LexemType.LabelRef, lex.Type);
Assert.Equal("LabelRef", lex.Content);
}

private ILexer GetLexerForCode(string code)
{
return new FullSourceLexer
Expand Down

0 comments on commit 8fa327f

Please sign in to comment.