Clark
is a simple command-lexing package designed for chat systems such as Discord, Teamspeak, or Telegram. It is
therefore intended to facilitate the handling of user input and the parsing processes that follow.
To use a Go package such as clark
, you must of course have Go installed on your system.
It is assumed that you have already worked with the Go environment. If this is not the case, see this page first.
To use clark
as a Go package, you must have it installed on your current system. If this is not the case, run the
command below.
go get -u github.com/lukasl-dev/clark
Otherwise, if you want to use clark
inside your terminal, you need to have it installed as well. If this is not the
case, run the command below.
If you have no experience in this section, read this.
go get -u github.com/lukasl-dev/clark/cmd/clark
Create a Reader
As an example, we create a strings.Reader
here.
reader := strings.NewReader("!help")
Create a Lexer
lex, err := clark.NewLexer(reader)
You can configure the lexer using the variadic options.
See available Options.
lex, err := clark.NewLexer(
reader,
lexing.Prefixes("!"), lexing.Labels("help", "play", "help me"),
)
// handle error
Available Options
Function | Description |
---|---|
lexing.Prefixes |
Prefixes defines the prefixes used by the lexer. |
lexing.Labels |
Labels defines the labels used by the lexer. |
lexing.PrefixIgnoreCase |
PrefixIgnoreCase defines the prefixes case sensitivity. |
lexing.LabelIgnoreCase |
LabelIgnoreCase defines the labels case sensitivity. |
lexing.SkipPrefix |
SkipPrefix defines whether the prefix should be skipped. |
lexing.SkipLabel |
SkipLabel defines whether the label should be skipped. |
To execute the Lexer
, lex.Lex()
is executed as a goroutine. lex.Lex()
reads the passed reader lexicographically until io.EOF
and
returns the lexed tokens.
go lex.Lex()
Collect the lexed Tokens
During reading tokens are sent to the token
channel. This channel can be accessed via lex.Chan()
.
for token := range lex.Chan() {
// handle token
}
A Token has a Type and an optional value.
See supported types.