Skip to content

Commit

Permalink
perf(complier): assertion collection and group indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Demali-876 committed Nov 13, 2024
1 parent 4d926f0 commit dffd422
Showing 1 changed file with 58 additions and 7 deletions.
65 changes: 58 additions & 7 deletions src/Compiler.mo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module {
type Transition = Types.Transition;

public class Compiler() {

let assertionBuffer = Buffer.Buffer<Types.Assertion>(8);
public func compile(ast : Types.ASTNode) : Result.Result<Types.CompiledRegex, Types.RegexError> {
let startState : State = 0;
switch (buildNFA(ast, startState)) {
Expand All @@ -39,9 +39,10 @@ module {
#ok({
states = Array.tabulate<State>(maxState + 1, func(i) = i);
transitions = transitionTable;
transitionsByState = transitionsByState;
transitionTable = transitionsByState;
startState = startState;
acceptStates = acceptStates
acceptStates = acceptStates;
assertions = Buffer.toArray(assertionBuffer)
})
}
}
Expand Down Expand Up @@ -306,11 +307,61 @@ module {
}
}
};
case (#Group({subExpr; modifier = _; captureIndex = _})) {
// Just process the subexpression - grouping is handled by matcher
buildNFA(subExpr, startState)
case (#Group({subExpr; modifier; captureIndex})) {
switch(modifier) {
case (?#PositiveLookahead) {
assertionBuffer.add({
assertion = #Lookaround({
expr = subExpr;
isPositive = true;
isAhead = true;
});
position = startState;
captureIndex = captureIndex;
});
#ok([] : [Transition], [startState])
};
case (?#NegativeLookahead) {
assertionBuffer.add({
assertion = #Lookaround({
expr = subExpr;
isPositive = false;
isAhead = true;
});
position = startState;
captureIndex = captureIndex;
});
#ok([] : [Transition], [startState])
};
case (?#PositiveLookbehind) {
assertionBuffer.add({
assertion = #Lookaround({
expr = subExpr;
isPositive = true;
isAhead = false;
});
position = startState;
captureIndex = captureIndex;
});
#ok([] : [Transition], [startState])
};
case (?#NegativeLookbehind) {
assertionBuffer.add({
assertion = #Lookaround({
expr = subExpr;
isPositive = false;
isAhead = false;
});
position = startState;
captureIndex = captureIndex;
});
#ok([] : [Transition], [startState])
};
case (?#NonCapturing) buildNFA(subExpr, startState);
case (null) buildNFA(subExpr, startState);
}
}
}
}
}
}
};

0 comments on commit dffd422

Please sign in to comment.