Page 1 of 1

Yarn Parser

Posted: Mon Oct 21, 2024 9:49 pm
by SiENcE
I've created a new Yarn parser library that converts Yarn Spinner dialogues into Lua.

https://github.com/SiENcE/yarnparser/

It fully supports the current Yarn Spinner syntax. Additionally, I reverse-engineered the eBNF since there was no official one available (which is unfortunate).

Features
- Parses Yarn scripts into structured node objects
- Groups related content (like choices and their responses)
- Can locate dialogue lines preceding choice groups
Supports:
  • Dialogue lines
  • Choices (including nested choices)
  • Conditional statements (if/else)
  • Variable assignments, declarations, and interpolation
  • Commands (jump, set, declare, etc.)
  • Comments (both single-line and multi-line)
It's not an interpreter ;) .

Previously, there was only this outdated parser, which is no longer compatible with the current yarn syntax.

Pull requests are welcome.

Re: Yarn Parser

Posted: Mon Oct 21, 2024 10:42 pm
by Hugues Ross
Oh nice, I'm going to need a better way of writing dialogue in my game and Yarn looks like it might fit the bill! I'll be keeping this in mind for later :awesome:

Re: Yarn Parser

Posted: Tue Oct 22, 2024 8:33 am
by pgimeno
@SiENcE maybe you can include a link to your repo? :nyu:

Re: Yarn Parser

Posted: Tue Oct 22, 2024 4:56 pm
by SiENcE
@pgimeno Right, thx ... sure I'll make it more explicit :-)

Re: Yarn Parser

Posted: Fri Oct 25, 2024 10:58 pm
by SiENcE
I added a sample implementation of an interpreter along with a Yarn test sample. The interpreter supports callbacks for every step.

https://github.com/SiENcE/yarnparser/bl ... r_test.lua

Code: Select all

local YarnParser = require("yarn_parser")
local YarnInterpreter = require("yarn_interpreter")

-- Parse your Yarn script
local nodes = YarnParser:parse(your_script)

-- Create a new interpreter instance
local interpreter = YarnInterpreter.new(nodes)

-- Define callbacks
local callbacks = {
    on_dialogue = function(text)
        print("Dialogue:", text)
    end,
    on_choice = function(choices, path)
        print("Choice path:", path or "root")
        for i, choice in ipairs(choices) do
            print(i .. ": " .. choice)
        end
        io.write("Select (1-" .. #choices .. "): ")
        return tonumber(io.read())
    end,
    on_variable = function(name, value)
        print("Variable changed:", name, "=", value)
    end,
    on_node_enter = function(title)
        print("Entering node:", title)
    end,
    on_node_exit = function(title)
        print("Exiting node:", title)
    end
}

-- Set the callbacks
interpreter:set_callbacks(callbacks)

-- Start the interpretation
interpreter:run()