|
3 | 3 | import com.scriptbasic.context.Context; |
4 | 4 | import com.scriptbasic.executors.commands.CommandSelect; |
5 | 5 | import com.scriptbasic.interfaces.AnalysisException; |
| 6 | +import com.scriptbasic.interfaces.BasicSyntaxException; |
6 | 7 | import com.scriptbasic.interfaces.ScriptBasicKeyWords; |
7 | 8 | import com.scriptbasic.spi.Command; |
| 9 | +import com.scriptbasic.syntax.BasicSyntaxAnalyzer; |
8 | 10 |
|
9 | 11 | public class CommandAnalyzerSelect |
10 | | - extends AbstractCommandAnalyzer |
11 | | -{ |
12 | | - |
13 | | - public CommandAnalyzerSelect(Context ctx) |
14 | | - { |
15 | | - super(ctx); |
16 | | - } |
17 | | - |
18 | | - @Override |
19 | | - public Command analyze() throws AnalysisException { |
20 | | - final var lexicalElement = ctx.lexicalAnalyzer.peek(); |
21 | | - // consume optional case statement |
22 | | - if(lexicalElement.isSymbol(ScriptBasicKeyWords.KEYWORD_CASE)) |
23 | | - ctx.lexicalAnalyzer.get(); |
24 | | - // read expression till end of line |
25 | | - final var expression = analyzeExpression(); |
26 | | - consumeEndOfLine(); |
27 | | - |
28 | | - final var node = new CommandSelect(); |
29 | | - node.setExpression(expression); |
30 | | - pushNode(node); |
31 | | - |
32 | | - return node; |
33 | | - } |
| 12 | + extends AbstractCommandAnalyzer { |
| 13 | + |
| 14 | + public CommandAnalyzerSelect(Context ctx) { |
| 15 | + super(ctx); |
| 16 | + } |
| 17 | + |
| 18 | + @Override |
| 19 | + public Command analyze() throws AnalysisException { |
| 20 | + var lexicalElement = ctx.lexicalAnalyzer.peek(); |
| 21 | + // consume optional case statement |
| 22 | + if (lexicalElement.isSymbol(ScriptBasicKeyWords.KEYWORD_CASE)) |
| 23 | + ctx.lexicalAnalyzer.get(); |
| 24 | + // read expression till end of line |
| 25 | + final var expression = analyzeExpression(); |
| 26 | + consumeEndOfLine(); |
| 27 | + |
| 28 | + final var node = new CommandSelect(); |
| 29 | + node.setExpression(expression); |
| 30 | + pushNode(node); |
| 31 | + |
| 32 | + // next command has to be 'case' |
| 33 | + // first skip any comments |
| 34 | + lexicalElement = ctx.lexicalAnalyzer.peek(); |
| 35 | + while (lexicalElement != null && BasicSyntaxAnalyzer.lineToIgnore(lexicalElement.getLexeme())) { |
| 36 | + ctx.lexicalAnalyzer.get(); |
| 37 | + BasicSyntaxAnalyzer.consumeIgnoredLine(ctx.lexicalAnalyzer, lexicalElement.getLexeme()); |
| 38 | + lexicalElement = ctx.lexicalAnalyzer.peek(); |
| 39 | + } |
| 40 | + if (lexicalElement == null) { |
| 41 | + throw new BasicSyntaxException("Preliminary end of file"); |
| 42 | + } |
| 43 | + if (!lexicalElement.isSymbol(ScriptBasicKeyWords.KEYWORD_CASE)) { |
| 44 | + throw new BasicSyntaxException("Expected case statement, but found: " + lexicalElement.getLexeme()); |
| 45 | + } |
| 46 | + |
| 47 | + return node; |
| 48 | + } |
34 | 49 |
|
35 | 50 | } |
0 commit comments