|
1 | 1 | using SER.Code.ContextSystem.BaseContexts; |
| 2 | +using SER.Code.ContextSystem.Contexts; |
2 | 3 | using SER.Code.ContextSystem.Contexts.Control; |
3 | 4 | using SER.Code.ContextSystem.Interfaces; |
4 | 5 | using SER.Code.Extensions; |
@@ -138,23 +139,82 @@ List<RunnableContext> contexts |
138 | 139 |
|
139 | 140 | if (firstToken is not IContextableToken contextable) |
140 | 141 | { |
141 | | - return $"'{firstToken.RawRep}' is not a valid way to start a line. Perhaps you made a typo?"; |
| 142 | + return rs + $"'{firstToken.RawRep}' is not a valid way to start a line. Perhaps you made a typo?"; |
142 | 143 | } |
143 | 144 |
|
144 | 145 | var context = contextable.GetContext(scr); |
145 | 146 | if (context is null) return context; |
146 | | - |
147 | | - foreach (var token in tokens.Skip(1)) |
| 147 | + |
| 148 | + bool endLineContexting = false; |
| 149 | + for (var index = 1; index < tokens.Length; index++) |
148 | 150 | { |
149 | | - if (HandleCurrentContext(token, context, out var endLineContexting).HasErrored(out var errorMsg)) |
150 | | - return rs + errorMsg; |
| 151 | + var token = tokens[index]; |
| 152 | + rs = $"Cannot add token {token} to {context}"; |
| 153 | + if (AttemptsInlineWithKeyword(token, context)) |
| 154 | + { |
| 155 | + if (HandleInlineWithKeyword(tokens.Skip(index), context, scr).HasErrored(out var error)) |
| 156 | + { |
| 157 | + return rs + error; |
| 158 | + } |
| 159 | + |
| 160 | + break; |
| 161 | + } |
| 162 | + |
| 163 | + if (token is CommentToken) |
| 164 | + { |
| 165 | + return context; |
| 166 | + } |
151 | 167 |
|
| 168 | + if (HandleCurrentContext(token, context, out endLineContexting).HasErrored(out var errorMsg)) |
| 169 | + return rs + errorMsg; |
| 170 | + |
152 | 171 | if (endLineContexting) break; |
153 | 172 | } |
154 | 173 |
|
155 | 174 | return context; |
156 | 175 | } |
157 | 176 |
|
| 177 | + private static bool AttemptsInlineWithKeyword(BaseToken token, Context currentContext) |
| 178 | + { |
| 179 | + return token is IContextableToken contextable |
| 180 | + && contextable.GetContext(token.Script) is WithKeyword |
| 181 | + && currentContext is StatementContext; |
| 182 | + } |
| 183 | + |
| 184 | + private static Result HandleInlineWithKeyword(IEnumerable<BaseToken> enumTokens, RunnableContext context, Script scr) |
| 185 | + { |
| 186 | + var tokens = enumTokens.ToArray(); |
| 187 | + |
| 188 | + if (tokens.First() is not IContextableToken contextable2 |
| 189 | + || contextable2.GetContext(scr) is not WithKeyword |
| 190 | + || context is not StatementContext statement) |
| 191 | + { |
| 192 | + return $"{context.FriendlyName} does not accept {tokens.First()}"; |
| 193 | + } |
| 194 | + |
| 195 | + if (ContextLine(tokens, null, scr).HasErrored(out var contextError, out var contextResult)) |
| 196 | + { |
| 197 | + return contextError; |
| 198 | + } |
| 199 | + |
| 200 | + if (contextResult is not WithKeyword with) |
| 201 | + { |
| 202 | + return $"{contextResult.FriendlyName} does not accept {tokens.First()}"; |
| 203 | + } |
| 204 | + |
| 205 | + if (with.AcceptStatement(statement).HasErrored(out var acceptError)) |
| 206 | + { |
| 207 | + return acceptError; |
| 208 | + } |
| 209 | + |
| 210 | + if (with.VerifyCurrentState().HasErrored(out var verifyError)) |
| 211 | + { |
| 212 | + return verifyError; |
| 213 | + } |
| 214 | + |
| 215 | + return true; |
| 216 | + } |
| 217 | + |
158 | 218 | private static Result HandleCurrentContext(BaseToken token, RunnableContext context, out bool endLineContexting) |
159 | 219 | { |
160 | 220 | Result rs = $"Cannot add '{token.RawRep}' to {context}"; |
|
0 commit comments