Yarn Parser

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
SiENcE
Party member
Posts: 806
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Yarn Parser

Post 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.
Last edited by SiENcE on Tue Oct 22, 2024 4:57 pm, edited 1 time in total.
User avatar
Hugues Ross
Party member
Posts: 112
Joined: Fri Oct 22, 2021 9:18 pm
Location: Quebec
Contact:

Re: Yarn Parser

Post 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:
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Yarn Parser

Post by pgimeno »

@SiENcE maybe you can include a link to your repo? :nyu:
User avatar
SiENcE
Party member
Posts: 806
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: Yarn Parser

Post by SiENcE »

@pgimeno Right, thx ... sure I'll make it more explicit :-)
User avatar
SiENcE
Party member
Posts: 806
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: Yarn Parser

Post 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()
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests