This question already has an answer here:

Usually when I write an answer I explain each section of my code separately and then add a link to a JSFiddle demo at the end. For example take a look at this answer on StackOverflow:

Jison ignores one of my rules

Now with the introduction of stack snippets I would like to replace my JSFiddle links with stack snippets. However it seems that you cannot split your stack snippets into multiple sections.

It would be nice if this were possible. It would be even better if you could arrange the sections in any order. The order in which the snippets should be rearranged could be specified in the language comment. For example:

<!-- language: lang-js snippet: 2 -->
<!-- language: lang-js snippet: 1 -->
<!-- language: lang-js snippet: 3 -->

Currently the only other way I can split my code into multiple sections and use stack snippets is to duplicate the code (and rearrange and merge the sections) into a stack snippet at the end of the answer, which is okay: I can say “Here is the code put together.”

However this method has the disadvantage that if I change some code in my section then I need to make the same change in my stack snippet. It would be nice if I only needed to make the change in one place.

marked as duplicate by Martijn Pieters discussion Sep 21 '14 at 9:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You can do like you normally would, but instead of separating out the instructions/explanations, put them into comments.

function match(expected) {
    var token = lexer.lex();
    if (token === expected) return lexer.yytext;
    else throw new Error("Unexpected token: " + token);

// Now all you need to do is call parse:

var output = parse("goto http://www.google.com/ then goto http://www.bing.com/");

alert(JSON.stringify(output, null, 4));

I would often do that even before Stack Snippets.

Not the answer you're looking for? Browse other questions tagged .