Skip to content

Commit 1a8ccab

Browse files
committed
Finished refactoring the action script grammar.
* Condition rules are more strict * Definitions should be much more readable.
1 parent 2ae48be commit 1a8ccab

File tree

4 files changed

+234
-175
lines changed

4 files changed

+234
-175
lines changed

source/code/systems/input_action_system/private/input_action_script.cxx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ namespace ice::asl
6464
.location = location
6565
};
6666

67+
ICE_LOG(LogSeverity::Debug, LogTag::Core, "{}", word.value[0] == '\n' ? "<nl>" : word.value);
68+
6769
ice::ucount idx;
6870
ice::asl::TokenDefinition const needle{ .value = word.value };
6971
if (ice::binary_search(ice::span::from_std_const(Constant_TokenDefinitions), needle, idx))
@@ -95,7 +97,7 @@ namespace ice::asl
9597
);
9698

9799
std::unique_ptr<arctic::Parser> parser = arctic::create_default_parser(
98-
{ .rules = ice::asl::grammar::Rule_GlobalRules }
100+
{ .rules = ice::asl::grammar::ScriptRules }
99101
);
100102

101103
arctic::SyntaxVisitor* visitors[]{ &handler };
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)