kach / nearley

📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.57k stars 229 forks source link

trying to parse Harlowe text adventure language #372

Open jnicho02 opened 6 years ago

jnicho02 commented 6 years ago

Hi,

i'm working on a parser for the Harlowe language which is used in Twine text adventures. Rules are coming on fairly well, but my overall structure is lacking. Could anyone get me to the next step please?

https://github.com/jnicho02/nearley-harlowe

tjvr commented 6 years ago

Can you be more specific? I'm afraid we don't have the time to write a grammar for you! 😊

jnicho02 commented 6 years ago

I thought that might be the case :) ...pls believe me when I say i'm not just a noob asking you to do my homework. Just some first steps at structure that I can build on. My grammar so far is https://github.com/jnicho02/nearley-harlowe/blob/master/harlowe.ne

A Twine text adventure is written in a wiki-like language (perhaps you know of a good example grammar for Wiki that you could point me at?) with extended language that represents variables etc., e.g.

"You have started (set: $status to "started") in a dark room you can [[go north|north]] or [[go south|south]]"

So, I have defined rules to match narrative "You have started", code (set: $status...) and link [[go north...

What is the simplest way to specify that narrative, code, and links can appear in any combination, zero-or-many times?

tjvr commented 6 years ago

Something like this perhaps?

A rule is one or more things; a thing is either a narrative, code, or a link.

rule -> rule thing {% ([xl, x]) => xl.concat([x]) %}
      | thing {% id %}

thing -> narrative {% id %} | code {% id %} | link {% id %}