natefaubion / sparkler

Native pattern matching for JavaScript
MIT License
693 stars 18 forks source link

Literal String Support #16

Closed Havvy closed 10 years ago

Havvy commented 10 years ago

I'm trying to dispatch based on a string value, but I get a parse error at the opening quote.

Here's my code:

const transitions = function {
    "initialized" => {
        if (state !== "initialized") {
            if (strictMode) crash();
            return;
        }

        state = "initialized";
        emitter.emit("initialized");
    },

    "unstartable" => {
        state = "unstartable";
        emitter.emit("unstartable");
    },

    "started" => {
        state = "started"
        emitter.emit("started");
        setState("running", false);
    },

    ("running", restarting) => {
        state = "running"
        emitter.emit("running", restarting);
    },

    ("dying", data) => {
        const killer = data[0];
        const reason = data[1];
        const deathData = data[2];

        state = "dying";
        emitter.emit("dying", killer, reason, deathData);
        setState(shouldRestart ? "restarting" : "dead");
    },

    "restarting" => {
        state = "restarting";
        emitter.emit("restarting");
        restart();
    },

    "dead" => {
        state = "dead";
        client._end();
        emitter.emit("dead");
    },

    "rehashing" => {
        state = "rehashing";
        emitter.emit("rehash");
        if (state !== "rehashing") return;
        emitter.emit("rehashed");
        setState("running", true);
    },

    ("crashed", previousState) => {
        emitter.emit("crash", previousState);
    }
};

Here's my shell command:

./node_modules/gulp-sweetjs/node_modules/.bin/sjs -m "sparkler/macros" -o out.js -r sparkler-test.sjs 

Here's the output of the shell command:

/home/havvy/tennu/tennu/node_modules/gulp-sweetjs/node_modules/sweet.js/lib/sweet.js:99
                    throw new SyntaxError(syn.printSyntaxError(source$2, err))
                          ^
SyntaxError: [sparkler] Unexpected token (expected case)
2:     "initialized" => {
       ^
    at expand$2 (/home/havvy/tennu/tennu/node_modules/gulp-sweetjs/node_modules/sweet.js/lib/sweet.js:99:27)
    at parse (/home/havvy/tennu/tennu/node_modules/gulp-sweetjs/node_modules/sweet.js/lib/sweet.js:135:29)
    at Object.compile (/home/havvy/tennu/tennu/node_modules/gulp-sweetjs/node_modules/sweet.js/lib/sweet.js:143:19)
    at doCompile (/home/havvy/tennu/tennu/node_modules/gulp-sweetjs/node_modules/sweet.js/lib/sjs.js:61:45)
    at Object.exports.run (/home/havvy/tennu/tennu/node_modules/gulp-sweetjs/node_modules/sweet.js/lib/sjs.js:74:9)
    at Object.<anonymous> (/home/havvy/tennu/tennu/node_modules/gulp-sweetjs/node_modules/sweet.js/bin/sjs:7:23)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

Am I reading the documentation incorrectly, or does Sparkler not support literal strings in patterns?

natefaubion commented 10 years ago

Are you using an old version? The error you are getting (expected case) doesn't exist anymore in the source code. In older versions (0.1.x), cases needed to be prefixed with an actual case keyword, which is what the error is indicating. There is a test that handles string literals here https://github.com/natefaubion/sparkler/blob/a8450ed378e41d41d26e3731fdb293cb56f7ac7d/test/patterns.sjs#L28

Havvy commented 10 years ago

Yes. Old version it is. Should have checked that first. Sorry.