Skip to content

Commit

Permalink
FunctionWaiter: slight cleanup, update CONTRIBUTING
Browse files Browse the repository at this point in the history
  • Loading branch information
NotNite committed Oct 24, 2024
1 parent 39b0c09 commit 540e9b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ Most problems with contributions aren't hard blockers for merge, as they are usu
- Use LF line format.
- Use K&R braces.
- Use file-scoped namespaces.
- Use `this.` for fields/properties.
- Use `PascalCase` for namespaces, types, and public/static fields/properties. Use `camelCase` for private fields/properties. No prefixed underscore on private fields/properties.
- Use `var` where possible.
- Prefer fields to properties (unless in an interface). Fields can be `ref`d and are easier to deal with in the context of native code.
- Use `readonly` where possible.
- Prefer `const` over `static`.
- Use primary constructors where possible.
- Keep in mind the contract for modding and API breakage.
- Don't make changes to enums or interfaces that mods use.
- Classes that shouldn't be exposed to mods should be `internal` or `private`.
Expand Down
47 changes: 21 additions & 26 deletions GDWeave/Script/ModUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,47 @@ public interface IWaiter {
public bool Check(Token token);
}

public class FunctionWaiter : IWaiter {
private MultiTokenWaiter functionWaiter;
public class FunctionWaiter(string name, bool waitForReady = false) : IWaiter {
private readonly MultiTokenWaiter functionWaiter = new([
t => t.Type == TokenType.PrFunction,
t => t is IdentifierToken identifier && identifier.Name == name
]);

public bool Matched {get; private set;} = false;
public bool Ready {get; private set;} = true;

public FunctionWaiter(string name, bool waitForReady = false) {
functionWaiter = new([
t => t.Type == TokenType.PrFunction,
t => t is IdentifierToken identifier && identifier.Name == name,
]);
}
public bool Matched { get; private set; }
public bool Ready { get; private set; } = !waitForReady;

public void Reset() {
foundFunction = false;
foundColon = false;
this.foundFunction = false;
this.foundColon = false;
this.Matched = false;
this.Ready = !waitForReady;
this.functionWaiter.Reset();
}

public void SetReady() {
Ready = true;
this.Ready = true;
}

private bool foundFunction = false;
private bool foundColon = false;

public bool Check(Token token) {
if (!Ready) {
return false;
}
if (!this.Ready || this.Matched) return false;

if (foundColon) {
Matched = true;
if (this.foundColon && token.Type == TokenType.Newline) {
this.Matched = true;
return true;
}

if (foundFunction && token.Type == TokenType.Colon) {
foundColon = true;
if (this.foundFunction && token.Type == TokenType.Colon) {
this.foundColon = true;
return false;
}

if (!functionWaiter.Check(token)) {
foundFunction = true;
if (this.functionWaiter.Check(token)) {
this.foundFunction = true;
return false;
}
else {
functionWaiter.Reset();
}

return false;
}
Expand Down

0 comments on commit 540e9b7

Please sign in to comment.