On Sat, Jul 26, 2008 at 00:03, Anders Dahnielson <anders@dahnielson.com> wrote:


On Wed, Jul 23, 2008 at 18:13, Anders Dahnielson <anders@dahnielson.com> wrote:

Here's a simple tokenizer for SFZ I wrote once upon a time in Python. Not sure if I got it completely right.

Nope. E.g. it matches lokey= twice (once as lokey= with a value and as key= without a value). *Blushes*. LOL.


Ok, this is now an old thread. But to not appear as a complete fool I will post some code that actually works. ;-)

[BEGIN PYTHON]

import sys

def parse(f):
    token_stack = []
    string_stack = []
    for line in f:
        # COMMENT
        if line.startswith('//'):
            continue
        # DEFINITION
        for token in line.split():
            if token.startswith('<') and token.endswith('>'):
                # HEAD
                if string_stack:
                    token_stack.append(" ".join(string_stack))
                    string_stack[:] = []
                string_stack.append(token)
            elif token.find('=') != -1:
                # HEAD
                if string_stack:
                    token_stack.append(" ".join(string_stack))
                    string_stack[:] = []
                string_stack.append(token)
            else:
                # TAIL
                string_stack.append(token)
        # EOL
        if string_stack:
            token_stack.append(" ".join(string_stack))
            string_stack[:] = []
    return token_stack

if __name__ == '__main__':
    sfzf = open(sys.argv[1], 'r')
    print parse(sfzf)
    sfzf.close()

[END PYTHON]

--
Anders Dahnielson
<anders@dahnielson.com>