Parsing indention based language?

Philipp Lenssen lenssen at hitnet.rwth-aachen.de
Sat Apr 20 15:24:04 EDT 2002


There's QML (Quest Markup Language, XML-based) for which I just written an
early Python port. Now I created an additional format called QL (Quest
Language, text-based) which can then be converted to QML (which is the
better storing format for interpreters). The Python language which I got to
know through the port was part in inspiring me to do an indent-based scoping
in QL. Now that I've written a nicely working but relatively ugly hack to
convert QL to QML (I used VBScript/ VisualBasic, the results can be
downloaded at http://questml.com/download.htm ), I was wondering if anybody
knows any elegant approaches.

I'm asking in this group first and foremost out of general interest and
because Python itself has similar rules, not because I immediately want to
start porting the QL-to-QML to Python (maybe some day) -- but I'd be happy
to get any hints written in Python.


QL, in short, looks like this (see http://questml.com/tutorial/ql.htm for
language details):

--------------------- Start below
"Battle sample", Philipp Lenssen
// Being a smaller sample of QL

start
    ---
        There's a big monster in front of you
        blocking the path.
        *This could be dangerous!*

    ( monkey.gif )

    % player skill = 10
    % player stamina = 10

    % enemy skill = 10
    % enemy stamina = 10

    $ after win = new path

    --> battle
        Let's start the battle
        _ player started battle2

battle
    % player power = [player skill] + {random 2, 12}
    % enemy power = [enemy skill] + {random 2, 12}

    ? [player power] = [enemy power]
        ---
            Both of your swords miss the other.
            Stamina stays the same.
        --> battle
            Battle again
    ? [player power] > [enemy power]
        ---
            You hit the enemy and wound him.
        % enemy stamina = [enemy stamina] - 2
        --> battle result
            Continue
    ...
        ---
            The enemy hits you and it hurts.
        % player stamina = [player stamina] - 2
        --> battle result
            Continue
--------------------- End above

According to the rules the above has to be converted to the following QML
(see http://questml.com/tutorial/ ):

--------------------- Start below
<quest>

<about>
    <title>Battle sample</title>
    <author>Philipp Lenssen</author>
</about>

<station id="start">
    <text>There's a big monster in front of you blocking the path.
        <em>This could be dangerous.</em>
    </text>

    <number name="player skill" value="10"/>
    <number name="player stamina" value="10"/>

    <number name="enemy skill" value="10"/>
    <number name="enemy stamina" value="10"/>

    <string name="after win" value="new path"/>

    <choice station="battle">Let's start the battle</choice>
</station>

<station id="battle">
    <number name="player power" value="[player skill] + {random 2, 12}"/>
    <number name="enemy power" value="[enemy skill] + {random 2, 12}"/>

    <if check="[player power] = [enemy power]">
        <text>Both of your swords miss the other. Stamina stays the
                same.</text>
        <choice station="battle">Battle again</choice>
    </if>
    <if check="[player power] greater [enemy power]">
        <text>You hit the enemy and wound him.</text>
        <number name="enemy stamina" value="[enemy stamina] - 2"/>
        <choice station="battle result">Continue</choice>
    </if>
    <else>
        <text>The enemy hits you and it hurts.</text>
        <number name="player stamina" value="[player stamina] - 2"/>
        <choice station="battle result">Continue</choice>
    </else>
</station>

</quest>
--------------------- End above

Thanks!


On a side-note, converting the other way round which I'm scripting now seems
fairly straightforward (I can recursively iterate through child elements by
just using XML DOM/ XSL XPath interfaces).





More information about the Python-list mailing list