Skip to content

Instantly share code, notes, and snippets.

@KapitanOczywisty
Last active May 1, 2022 10:36
Show Gist options
  • Save KapitanOczywisty/ae65eb9809d847d00752896968339730 to your computer and use it in GitHub Desktop.
Save KapitanOczywisty/ae65eb9809d847d00752896968339730 to your computer and use it in GitHub Desktop.
Atom language spec generator
// paste into Init Script (.js) in Atom
function specBuild(code, skipSpaces = true) {
const _escape = (str) => str.replace(/\\/g, '\\\\');
code = code.replace(/^\s*\n|\n\s*$/g, '');
let tokens = atom.grammars.grammarForScopeName('source.php').tokenizeLines(code);
let spec = `it 'should tokenize ... correctly', ->\n `;
if ((code.match(/\n/g) || []).length) {
code = `\n ${code.replace(/\n/g, '\n ')}\n `;
}
const multiLine = tokens.length > 1;
spec += multiLine
? `lines = grammar.tokenizeLines '''${_escape(code)}'''\n\n`
: `{tokens} = grammar.tokenizeLine '${_escape(code).replace(/'/g,"\\'")}'\n\n`;
for (let i in tokens) {
for (let j in tokens[i]) {
const {scopes, value} = tokens[i][j];
if (skipSpaces && /^\s*$/.test(value))
continue;
let scopesList = JSON.stringify(scopes).replace(/","/g, "', '").replace(/"/g, "'");
let index = multiLine ? `lines[${i}][${j}]` : `tokens[${j}]`;
spec += ` expect(${index}).toEqual value: '${_escape(value).replace(/'/g,"\\'")}', scopes: ${scopesList}\n`
}
}
return `\n${spec}\n`;
}
// // Run in Developer Tools Console:
// specBuild('function() : \\Client {}');
// // or
// specBuild(`function() : \\Client {
// return 2;
// }`);
// // or
// specBuild(`
// function() : \\Client {
// return 2;
// }`);
// // be careful with indentations
// paste into Init Script (.js) in Atom
function specPretty(code) {
const _escape = (str) => str.replace(/\\/g, '\\\\');
let grammar = atom.grammars.grammarForScopeName('source.php');
let tokens = grammar.tokenizeLine(code).tokens;
let spec = `Test code: \`${_escape(code)}\`\n\n`;
for (let i in tokens) {
let scopes = JSON.stringify(tokens[i].scopes);
scopes = scopes.replace(/","/g, '" "').replace(/"/g, '`').replace(/\[|\]/g, '');
spec += `${i}: \`${_escape(tokens[i].value)}\` scopes: ${scopes}\n`
}
return spec;
}
// // Run in Developer Tools Console:
// specPretty('function() : \\Client {}');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment