Command Tokenizer (beta)

An asset by winston
The page banner background of a mountain and forest
Command Tokenizer (beta) thumbnail image
Command Tokenizer (beta) thumbnail image
Command Tokenizer (beta) hero image

Quick Information

0 ratings
Command Tokenizer (beta) icon image
winston
Command Tokenizer (beta)

A small godot project with a (very simple) tokenizer written in GDScript. Supports bash like commands and command chains/pipes. BETA: I have not tested this really well yet. Please use with care.

Supported Engine Version
3.2
Version String
1.0.0
License Version
MIT
Support Level
community
Modified Date
2 years ago
Git URL
Issue URL

GD-Tokenizer

A small godot project with a (very simple) tokenizer written in GDScript.

Screenshots

README Screenshot showing grep working with piped output README Screenshot showing base64 in piped commands

Details

This project includes:

  • a terminal to input bash like commands
  • a very naive and simple tokenizer
  • a method to execute the result of the tokenizer

Features:

  • string support (double and single quoted)
  • bash like commands
  • multiple commands in one line (seperator: ;)
  • command piping with | (reuse command output as the input for the next command)

What this currently can not do:

  • flow control structures like if, for or while
  • subshells

The tokenizer is seperated from the execution/parsing. It should be relatively easy to implement your own execution method. If you want to support things like if, for or while you could implement these in the execution method, there is no need to change the tokenizing method. You may need to define a few more tokens though.

Files

Some important files/directories:

./cmd
./cmd/BashLikeCommands.gd (class that provides a few example bash commands)
./cmd/CommandParser.gd (class containing the tokenizer and a method to execute results)
./fonts
./fonts/fira-code (directory containing the monospace font I use for the terminal)
./ExampleTerminal.tscn (scene with a minimalistic terminal setup)
./ExampleTerminal.gd (implements the behaviour of the terminal scene)

Usage

Minimal Example

extends Node

var parser := CommandParser.new()
var commands := BashLikeCommands.new()

func _ready():

    # Parse and execute one of the bash like commands: echo
    var result := parser.tokenize("echo 'Hello world!'")
    var stdout := parser.execute(result, [self, commands], "%s")
    print(stdout)
    
    # Parse and execute the command 'hello' defined below
    var result := parser.tokenize("hello 'godot'")
    var stdout := parser.execute(result, [self, commands], "%s")
    print(stdout)

func cmd_hello(args: Array, stdin: String):
    if args.size() == 0:
        return "Hello unknown person!"
    elif args.size() == 1:
        return "Hello %s!"
    else:
        return "Error: too many arguments"

Tokenizer and executor method

The most important file is ./cmd/CommandParser.gd. This is where the tokenization and execution methods are defined.

To use the class file you need to create a parser object with var parser := CommandParser.new().

It provides two important methods:

CommandParser.tokenize(input)

Arguments:

  • input: String

This method will tokenize your input.

Returns: object of type TokenizedResult

CommandParser.execute()

Arguments:

  • tr: TokenResult
  • providers: Array of Objects
  • err_tpl: String (where %s represents the error)
  • pre: String

This method takes a TokenizedResult and tries to execute it. You need to call execute() using a list of command providers and an error template. The template is used to format errors, the simplest possible of which is '%s' (which simply represents only the error itself). A command provider can implement commands by defining methods that start with cmd_. This prefix can be changed with an optional parameter. If you want to use the bash like commands, you can add an instance of BashLikeCommands to the command provider list.

License

All files (except the fonts in ./fonts/fira-code) are licensed by the MIT License. Please see the License File for more details.

The font used in the Terminal is named "fira-code". Please see the License File for more details.

Links to other projects

All links in this section point to amazing external projects. These are not created by me.

TODO (Add to this readme later)

  • how to define your own tokens
  • how to write your own executor (maybe?)

A small godot project with a (very simple) tokenizer written in GDScript. Supports bash like commands and command chains/pipes. BETA: I have not tested this really well yet. Please use with care.

Reviews

0 ratings

Your Rating

Headline must be at least 3 characters but not more than 50
Review must be at least 5 characters but not more than 500
Please sign in to add a review

Quick Information

0 ratings
Command Tokenizer (beta) icon image
winston
Command Tokenizer (beta)

A small godot project with a (very simple) tokenizer written in GDScript. Supports bash like commands and command chains/pipes. BETA: I have not tested this really well yet. Please use with care.

Supported Engine Version
3.2
Version String
1.0.0
License Version
MIT
Support Level
community
Modified Date
2 years ago
Git URL
Issue URL

Open Source

Released under the AGPLv3 license

Plug and Play

Browse assets directly from Godot

Community Driven

Created by developers for developers