Skip to content

Commit 8ea8b74

Browse files
committed
вынесена проверка границы; доп. функция; мелочи
1 parent ab43599 commit 8ea8b74

File tree

5 files changed

+124
-103
lines changed

5 files changed

+124
-103
lines changed

src/OneScript.Language/LanguageDef.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ This Source Code Form is subject to the terms of the
99
using System;
1010
using System.Collections.Generic;
1111
using System.Runtime.CompilerServices;
12-
using static OneScript.Language.LanguageDef;
1312

1413
namespace OneScript.Language
1514
{

src/OneScript.Language/LexicalAnalysis/SourceCodeIterator.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public int CurrentColumn
7878
return OUT_OF_TEXT;
7979
}
8080

81-
int start = GetLineBound(CurrentLine);
81+
int start = GetLineBound(_lineCounter);
8282
return _index - start + 1;
8383
}
8484
}
@@ -95,8 +95,7 @@ public bool MoveNext()
9595
if (_currentSymbol == '\n')
9696
{
9797
_lineCounter++;
98-
if (_index < _codeLength)
99-
_lineBounds.Add(_index + 1);
98+
_lineBounds.Add(_index + 1);
10099
}
101100

102101
return true;
@@ -163,8 +162,26 @@ public bool SkipSpaces()
163162
}
164163

165164
return true;
165+
}
166+
167+
public char ReadNextChar()
168+
{
169+
while (Char.IsWhiteSpace(_currentSymbol))
170+
{
171+
if (_currentSymbol == '\n')
172+
{
173+
_onNewLine = true;
174+
}
175+
if (!MoveNext())
176+
{
177+
break;
178+
}
179+
}
180+
181+
return _currentSymbol;
166182
}
167183

184+
168185
public string ReadToLineEnd()
169186
{
170187
while (_currentSymbol != '\n' && MoveNext())
@@ -182,6 +199,9 @@ public string ReadToLineEnd()
182199
public string GetCodeLine(int lineNumber)
183200
{
184201
int start = GetLineBound(lineNumber);
202+
if (start >= _code.Length)
203+
return String.Empty;
204+
185205
int end = _code.IndexOf('\n', start);
186206
if (end >= 0)
187207
{
Lines changed: 95 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,96 @@
1-
/*----------------------------------------------------------
2-
This Source Code Form is subject to the terms of the
3-
Mozilla Public License, v.2.0. If a copy of the MPL
4-
was not distributed with this file, You can obtain one
5-
at http://mozilla.org/MPL/2.0/.
6-
----------------------------------------------------------*/
7-
8-
using System.Text;
9-
10-
namespace OneScript.Language.LexicalAnalysis
11-
{
12-
public class StringLexerState : LexerState
13-
{
14-
private void SkipSpacesAndComments(SourceCodeIterator iterator)
15-
{
16-
while (true)
17-
{ /* Пропускаем все пробелы и комментарии */
18-
iterator.SkipSpaces();
19-
20-
if (iterator.CurrentSymbol == '/')
21-
{
22-
if (!iterator.MoveNext())
23-
throw CreateExceptionOnCurrentLine("Некорректный символ", iterator);
24-
25-
if (iterator.CurrentSymbol != '/')
26-
throw CreateExceptionOnCurrentLine("Некорректный символ", iterator);
27-
28-
do
29-
{
30-
if (!iterator.MoveNext())
31-
break;
32-
33-
} while (iterator.CurrentSymbol != '\n');
34-
35-
}
36-
else
37-
break;
38-
}
39-
}
40-
41-
public override Lexem ReadNextLexem(SourceCodeIterator iterator)
42-
{
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
8+
using System.Text;
9+
10+
namespace OneScript.Language.LexicalAnalysis
11+
{
12+
public class StringLexerState : LexerState
13+
{
14+
private void SkipSpacesAndComments(SourceCodeIterator iterator)
15+
{
16+
while (true)
17+
{ /* Пропускаем все пробелы и комментарии */
18+
if (iterator.ReadNextChar() == '/')
19+
{
20+
if (!iterator.MoveNext())
21+
throw CreateExceptionOnCurrentLine("Некорректный символ", iterator);
22+
23+
if (iterator.CurrentSymbol != '/')
24+
throw CreateExceptionOnCurrentLine("Некорректный символ", iterator);
25+
26+
do
27+
{
28+
if (!iterator.MoveNext())
29+
break;
30+
31+
} while (iterator.CurrentSymbol != '\n');
32+
33+
}
34+
else
35+
break;
36+
}
37+
}
38+
39+
public override Lexem ReadNextLexem(SourceCodeIterator iterator)
40+
{
4341
StringBuilder contentBuilder = new StringBuilder();
44-
45-
while (iterator.MoveNext())
46-
{
47-
var cs = iterator.CurrentSymbol;
48-
49-
if (cs == SpecialChars.StringQuote)
50-
{
51-
if (iterator.MoveNext())
52-
{
53-
if (iterator.CurrentSymbol == SpecialChars.StringQuote)
54-
{
55-
/* Двойная кавычка */
56-
contentBuilder.Append("\"");
57-
continue;
58-
}
59-
60-
/* Завершение строки */
61-
SkipSpacesAndComments(iterator);
62-
63-
if (iterator.CurrentSymbol == SpecialChars.StringQuote)
64-
{
65-
/* Сразу же началась новая строка */
66-
contentBuilder.Append('\n');
67-
continue;
68-
}
69-
}
70-
71-
var lex = new Lexem
72-
{
73-
Type = LexemType.StringLiteral,
74-
Content = contentBuilder.ToString()
75-
};
76-
return lex;
77-
}
78-
79-
if (cs == '\n')
80-
{
81-
iterator.MoveNext();
82-
SkipSpacesAndComments(iterator);
83-
84-
if (iterator.CurrentSymbol != '|')
85-
throw CreateExceptionOnCurrentLine("Некорректный строковый литерал!", iterator);
86-
87-
contentBuilder.Append('\n');
88-
}
89-
else if(cs != '\r')
90-
contentBuilder.Append(cs);
91-
92-
}
93-
94-
throw CreateExceptionOnCurrentLine("Незавершённый строковой интервал!", iterator);
95-
}
96-
}
97-
}
42+
43+
while (iterator.MoveNext())
44+
{
45+
var cs = iterator.CurrentSymbol;
46+
47+
if (cs == SpecialChars.StringQuote)
48+
{
49+
if (iterator.MoveNext())
50+
{
51+
if (iterator.CurrentSymbol == SpecialChars.StringQuote)
52+
{
53+
/* Двойная кавычка */
54+
contentBuilder.Append('"');
55+
56+
continue;
57+
}
58+
59+
/* Завершение строки */
60+
SkipSpacesAndComments(iterator);
61+
62+
if (iterator.CurrentSymbol == SpecialChars.StringQuote)
63+
{
64+
/* Сразу же началась новая строка */
65+
contentBuilder.Append('\n');
66+
67+
continue;
68+
}
69+
}
70+
71+
return new Lexem
72+
{
73+
Type = LexemType.StringLiteral,
74+
Content = contentBuilder.ToString()
75+
};
76+
}
77+
78+
if (cs == '\n')
79+
{
80+
iterator.MoveNext();
81+
SkipSpacesAndComments(iterator);
82+
83+
if (iterator.CurrentSymbol != '|')
84+
throw CreateExceptionOnCurrentLine("Некорректный строковый литерал", iterator);
85+
86+
contentBuilder.Append('\n');
87+
}
88+
else if (cs != '\r')
89+
contentBuilder.Append(cs);
90+
91+
}
92+
93+
throw CreateExceptionOnCurrentLine("Незавершённый строковый литерал", iterator);
94+
}
95+
}
96+
}

src/OneScript.Language/LexicalAnalysis/WordLexerState.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public override Lexem ReadNextLexem(SourceCodeIterator iterator)
6666
var tok = LanguageDef.GetToken(content);
6767
if (LanguageDef.IsBuiltInFunction(tok))
6868
{
69-
iterator.SkipSpaces();
70-
if (iterator.CurrentSymbol != '(')
69+
if (iterator.ReadNextChar() != '(')
7170
{
7271
tok = Token.NotAToken;
7372
}

src/OneScript.Language/ScriptException.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ public override string Message
9898
{
9999
var sb = new StringBuilder(MessageWithoutCodeFragment);
100100
sb.AppendLine();
101-
var codeLine = Code?.Replace('\t', ' ').TrimEnd();
101+
var codeLine = Code?.Replace('\t', ' ')?.TrimEnd() ?? String.Empty;
102+
if (ColumnNumber > codeLine.Length)
103+
{
104+
ColumnNumber = codeLine.Length;
105+
}
102106

103107
if (ColumnNumber != ErrorPositionInfo.OUT_OF_TEXT)
104108
{

0 commit comments

Comments
 (0)