|
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 | + { |
43 | 41 | 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 | +} |
0 commit comments