hylyh / bondage.js

Javascript-based parser for the Yarn dialogue tree markup language
https://www.npmjs.com/package/bondage
MIT License
56 stars 21 forks source link

"->" leads to parsing error and text fails toload #31

Open blurymind opened 6 years ago

blurymind commented 6 years ago

I was using the attached example from yarn's website and found out that the 'Start' card will fail to load, because it contains '->' I get Error: Parse error on line 4: Unexpected 'Text'

You can experience this if you test it with http://hayley.zone/bondage.js/

Yarn example:

[
    {
        "title": "Start",
        "tags": "",
        "body": "A: Hey, I'm a character in a script!\nB: And I am too! You are talking to me!\n-> What's going on\n    A: Why this is a demo of the script system!\n    B: And you're in it!\n-> Um ok\nA: How delightful!\nB: What would you prefer to do next?\n[[Leave|Leave]]\n[[Learn more|LearnMore]]",
        "position": {
            "x": 592,
            "y": 181
        },
        "colorID": 0
    },
    {
        "title": "Leave",
        "tags": "",
        "body": "A: Oh, goodbye!\nB: You'll be back soon!",
        "position": {
            "x": 387,
            "y": 487
        },
        "colorID": 0
    },
    {
        "title": "LearnMore",
        "tags": "rawText",
        "body": "A: HAHAHA",
        "position": {
            "x": 763,
            "y": 472
        },
        "colorID": 0
    }
]
hylyh commented 6 years ago

Ah, good catch. This is an error with the following syntax:

-> What's going on
    A: Why this is a demo of the script system!
    B: And you're in it!
-> Um ok

The parser breaks when it tries to handle an -> with no tabbed-in text following it.

I'll try to find some time to look into this, I'm worried it may require a significant change to the parser to handle the potential of having or not having text after an option

blurymind commented 5 years ago

Interesting, the indent/dedent tokens are both nulls, so that seems to be handled elsewhere - the parser/states? There is a token for -> https://github.com/jhayley/bondage.js/blob/master/src/lexer/tokens.js#L32 ShortcutOption: /->/, But it's regex doesnt incorporate optional tab-in. It would be simple if we could handle that in the token like /(?: |\t|)->/

hylyh commented 5 years ago

Indents are handled specially within the lexer (see lexer.js) because they're used to mark block scope and can be nested arbitrarily, so using actual regex tokens to process them isn't sufficient. The lexer state for shortcut expressions is set to track a following indentation to mark its scope (https://github.com/jhayley/bondage.js/blob/master/src/lexer/states.js#L15).

The solution here might be to add a parameter to setTrackNextIndentation to make it be optional, but I'm not sure how implementing the "optional" part of that would work. Probably somewhere around here: https://github.com/jhayley/bondage.js/blob/master/src/lexer/lexer.js#L96

blurymind commented 5 years ago

not sure how this can be fixed, but your explanation is a very helpful start :)

How did yarnspinner solve this?

blurymind commented 4 years ago

I might fix that at some point, now that I better understand the lexer