|
| 1 | +#include "input_action_script_grammar.hxx" |
| 2 | + |
| 3 | +namespace ice::asl::grammar |
| 4 | +{ |
| 5 | + |
| 6 | + auto Condition::internal_condition_matcher( |
| 7 | + arctic::SyntaxRule const&, |
| 8 | + arctic::MatchContext& ctx |
| 9 | + ) noexcept -> arctic::ParseState |
| 10 | + { |
| 11 | + using arctic::MatchContext; |
| 12 | + using arctic::ParseState; |
| 13 | + using arctic::SyntaxNode; |
| 14 | + using arctic::Token; |
| 15 | + |
| 16 | + static constexpr SyntaxRule cond_when{ WhenCondition, MatchAll }; |
| 17 | + static constexpr SyntaxRule cond_orand{ AndOrCondition, MatchAll }; |
| 18 | + static constexpr SyntaxRule cond_flags{ ConditionFlags, MatchAll }; |
| 19 | + static constexpr SyntaxRule steps{ ActionStep::Rules, MatchAll }; |
| 20 | + |
| 21 | + ParseState result = ParseState::Success; |
| 22 | + SyntaxNode parent = ctx.node; |
| 23 | + SyntaxNode condition{ }; |
| 24 | + MatchContext local_ctx = ctx; |
| 25 | + |
| 26 | + if (ctx.token.type != TokenType::ASL_KW_When) |
| 27 | + { |
| 28 | + return ParseState::Error_UnexpectedToken; |
| 29 | + } |
| 30 | + // We require 'when' conditions to be follwing a newline token. |
| 31 | + if (ctx.prev_token.type != TokenType::ST_EndOfLine) |
| 32 | + { |
| 33 | + return ParseState::Error_UnexpectedToken; |
| 34 | + } |
| 35 | + |
| 36 | + // Add a new child node and store the condition type. |
| 37 | + local_ctx.node = condition = parent.append_child(SyntaxNode<LayerActionCondition>{ local_ctx.alloc }); |
| 38 | + result = cond_when.execute(local_ctx); |
| 39 | + |
| 40 | + do |
| 41 | + { |
| 42 | + Token tok = local_ctx.token; |
| 43 | + if (tok.type == TokenType::ASL_KW_WhenOr |
| 44 | + || tok.type == TokenType::ASL_KW_WhenAnd) |
| 45 | + { |
| 46 | + // Add a new sibling node and store the condition type. |
| 47 | + local_ctx.node = condition = condition.append_sibling(SyntaxNode<LayerActionCondition>{ local_ctx.alloc }); |
| 48 | + result = cond_orand.execute(local_ctx); |
| 49 | + } |
| 50 | + else if (tok.type == TokenType::CT_Dot) |
| 51 | + { |
| 52 | + MatchContext stepsctx = local_ctx; |
| 53 | + while (result == ParseState::Success && tok.type == TokenType::CT_Dot) |
| 54 | + { |
| 55 | + stepsctx.node = condition.append_child(SyntaxNode<LayerActionStep>{ local_ctx.alloc }); |
| 56 | + result = steps.execute(stepsctx); |
| 57 | + tok = stepsctx.token; |
| 58 | + } |
| 59 | + |
| 60 | + local_ctx.token = stepsctx.token; |
| 61 | + local_ctx.prev_token = stepsctx.prev_token; |
| 62 | + } |
| 63 | + else if (tok.type == TokenType::CT_Comma) |
| 64 | + { |
| 65 | + result = cond_flags.execute(local_ctx); |
| 66 | + } |
| 67 | + else if (tok.type == TokenType::ST_EndOfLine) |
| 68 | + { |
| 69 | + local_ctx.prev_token = ice::exchange(local_ctx.token, local_ctx.lexer.next()); |
| 70 | + switch (local_ctx.token.type) |
| 71 | + { |
| 72 | + case TokenType::ASL_KW_WhenOr: // For additional conditions |
| 73 | + case TokenType::ASL_KW_WhenAnd: |
| 74 | + case TokenType::CT_Comma: |
| 75 | + case TokenType::CT_Dot: break; // For steps |
| 76 | + case TokenType::ASL_KW_When: |
| 77 | + case TokenType::ASL_KW_Modifier: |
| 78 | + default: result = ParseState::Error_UnexpectedToken; break; |
| 79 | + } |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + break; |
| 84 | + } |
| 85 | + } while (result == ParseState::Success); |
| 86 | + |
| 87 | + ctx.token = local_ctx.token; |
| 88 | + ctx.prev_token = local_ctx.prev_token; |
| 89 | + return result; |
| 90 | + } |
| 91 | + |
| 92 | +} // namespace ice::asl::grammar |
0 commit comments