# 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.&#x20;

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](https://github.com/furesoft/Silverfly/blob/main/Source/Silverfly/PredefinedSymbols.cs)

```csharp
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);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://furesoft.gitbook.io/silverfly/basics/lexer/markdown/custom-matcher.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
