Skip to content

About

Syntax parser for Wheaties command invocations

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Blolol Chat Command Syntax

How Wheaties reads commands typed in chat, as a Ruby gem, blolol-chat_command_syntax. blolol-web's dispatcher runs chat's commands with it, and the command runner uses it for v2 commands that re-parse their arguments and for command tests, which go through the same steps as chat.

What it does

  1. Message.parse(event, nick:, discord_user_id:) finds what a chat event asks for: an Invocation (.weather Seattle, or !weather for a debug invocation) or an Assignment (Wheaties: foo is bar, or a Discord mention of Wheaties).
  2. Resolver#resolve(name, regexes: true) finds the command a name refers to: by its name, then a chat alias, then a name it had before a rename, then the first regex trigger, by priority, that matches the name. Resolver is the order. A subclass says where commands come from by implementing find_by_name, find_by_alias, find_by_previous_name, regex_triggers and regex_command: blolol-web looks them up in Postgres, and command tests in their own set of commands. Resolver.compile compiles a trigger's pattern with a timeout.
  3. Syntax.parse(text) parses a command's arguments into a tree, and Evaluator turns the tree into Tokens, calling its block to run each [command args], innermost first. The syntax is Wheaties v2's (the Harby gem's grammar): words, "double quotes" (with brackets run inside them), 'single quotes', numbers, /regexps/i, (lists) and [brackets]. It never fails: anything that doesn't parse is part of a word.
  4. Arguments.bind(tokens, schema, positionals: true) splits options (--name value, --name=value, -n value, --flag, -- to end them) from positional arguments by the command's signature, converts them to the types it asks for, and checks them against it with Signature#bind. Signature#usage(name) says how to call the command, like forecast [-u|--units <metric|imperial>] <place...>, for usage replies.

A signature can have subcommands (x-subcommands), each with its own signature, aliases (x-aliases) and description, plus a fallback for when the first word isn't one (x-fallback: main.rb or a subcommand) and Vim-style abbreviations (x-abbreviations). Signature#subcommand(name) is the signature a subcommand is called with, top-level options included, and Signature#usage(name) lists the subcommands, or shows one with subcommand:. Arguments.bind runs the subcommand the first bare word names (a name, an alias, then an abbreviation if they're on), or the fallback, and says which in its result's subcommand (and subcommand_word, the word that named it, or nil for the fallback); options before the subcommand's name consume their values. Code that invokes a command names its subcommand exactly, through Signature#target. Signature#usage_reply(name, result) is what chat is told when a result isn't valid.

A signature is a subset of JSON Schema with chat syntax in x- keywords (x-position, x-rest, x-short). blolol-web's Command::Signature defines the format and checks that a signature is valid when a command is saved.

Using it

It's a Git gem, pinned to a tag:

gem 'blolol-chat_command_syntax', git: 'https://github.com/blolol/chat-command-syntax.git', tag: 'v1.4.1'
require 'blolol/chat_command_syntax'

include Blolol::ChatCommandSyntax

request = Message.parse(event, nick: 'Wheaties')
nodes = Syntax.parse(request.arguments)
tokens = Evaluator.new { |name, args| run(name, args) }.evaluate(nodes)
result = Arguments.bind(tokens, signature)
result.valid? # => true, or see result.errors

Development

bundle install
bundle exec rake test

A change that changes what chat means changes Wheaties for everyone, so release it as a new tag and update blolol-web and the command runner together.

About

Syntax parser for Wheaties command invocations

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages