Custom Matcher

To implement your own token matcher, you need to implement the IMatcher interface with the Match and Build methods. The Match method is responsible for recognizing if this matcher should be executed or not, and the Build method constructs the final token.

Below is a simple example of a matcher that recognizes the boolean keywords true and false and builds a token with a boolean type. The implicit cast operator is overloaded on symbol to simply convert strings to symbols. There are also many predefined symbols here

public class BooleanMatcher(bool ignoreCasing = false) : IMatcher
{
    public bool Match(Lexer lexer, char c)
    {
        return lexer.IsMatch("true", ignoreCasing) || lexer.IsMatch("false", ignoreCasing);
    }

    public Token Build(Lexer lexer, ref int index, ref int column, ref int line)
    {
        var oldColumn = column;
        var oldIndex = index;

        if (lexer.IsMatch("true", ignoreCasing))
        {
            lexer.Advance("true".Length);
        }
        else if (lexer.IsMatch("false", ignoreCasing))
        {
            lexer.Advance("false".Length);
        }

        return new(PredefinedSymbols.Boolean, lexer.Document.Source[oldIndex..index], line, oldColumn);
    }
}

Last updated