A utility function for chunking code in Sense dashboards.
A utility function that makes it easier to write code chunkers for Sense engines.
Common code operations like parsing and syntax highlighting have strong library support. While writing Sense engines, however, we found that another code operation we call chunking is often awkward to implement. Code chunking is splitting code up into logical units such as comment and statement blocks, which are the top-level executable units of Sense's interactive dashboards. Chunking allows dashboards to display code, comments, results and output in an order that closely matches the source code. As such, the goal is stylistic rather than formal.
// Here are some line comments.// Successive line comments should be grouped together in a chunk.// These chunks are often markdown-formatted and displayed as documentation.
var a = 2;
var b = 2; var c = 3;
/*Here's a block comment. Block comments can be markdown-formatted and displayedas documentation too.*/
varreturn x * x;;
if trueconsole.logsquare3;elseconsole.log'Not displaying the answer.';
Most languages have parsers available that get you most of the way to a chunker. Parsers usually output a sequence of abstract syntax tree nodes, which correspond to the code chunks, and the line and column at which each chunk begins and ends. If you can find such a parser for your language, this package will help you create a chunker relatively quickly. If your parser doesn't make a record of comments, no problem- this module will go back through the code and pick them up.
var// You have to supply this function.// If a syntax error or other error occurs during parsing, it should callcb"the error message"// If the code parses, it should callcbfalsestart: line: 0 column: 0 end: line: 0 column: 3 // the type is "code" by default.start: line: 1 column: 0 end: line: 10 column: 4 type: "blockComment"// Note that the end line and columns are inclusive upper bounds and that// indexing begins at zero, so the statemnt"var x;"// would bestart: line: 0 column: 0 end: line: 0 column: 5var chunk = require'comment-chunk-helper'chunkparser: parserFunclineComment: "//"blockComment: left: "/*" right: "*/";chunkcode cb;// The single argument passed to the callback will be an array// of chunks, that is, an argument of the formtype: "code" or "error" or "comment" or "blockComment"value: "the code, comment, or error message"
Note that the chunker's callback does not take an error argument. If the parser passes an error argument to its callback, that argument will be wrapped in a single chunk of type 'error' and passed to the chunker's callback. The dashboard implementation can decide what to do with error chunks. Simply passing them to the engine as code is often a good idea.