A library that helps tokenize text using Text Mate grammars.
Перейти к файлу
Alexandru Dima 5b8bf6a305
Update README.md
2019-07-12 15:08:57 +02:00
.vscode extend benchmark 2018-06-21 17:06:37 +02:00
benchmark extend benchmark 2018-06-21 17:06:37 +02:00
release Make sure that even capture rules starting at position 0 are not checked for while conditions 2019-07-12 12:57:49 +02:00
scripts Merge remote-tracking branch 'origin/master' into LineEndRuleCnt 2019-07-12 14:40:09 +02:00
src Fix compilation errors 2019-07-12 14:53:59 +02:00
test-cases Simplify themes test running 2019-07-12 11:00:32 +02:00
typings tests, benchmarks & dispose OnigString 2018-06-14 10:21:01 +02:00
.gitignore Fixes #19: Bring back the /release/ folder to git 2016-08-22 10:24:33 +02:00
.npmignore Clean up dev dependencies 2019-03-01 15:02:30 +01:00
LICENSE.md Update LICENSE.md 2015-11-06 14:32:12 +01:00
README.md Update README.md 2019-07-12 15:08:57 +02:00
ThirdPartyNotices.txt Add ThirdPartyNotices.txt 2015-11-18 14:38:28 +01:00
azure-pipelines.yml Tweaks to the build scripts 2019-07-12 09:42:15 +02:00
build-template.yml Merge remote-tracking branch 'origin/master' into update_oniguruma 2019-07-12 10:01:01 +02:00
package-lock.json 4.2.0 2019-07-12 11:10:09 +02:00
package.json 4.2.0 2019-07-12 11:10:09 +02:00
tsconfig.json pluggable onig scanner 2018-06-08 17:16:23 +02:00
tslint.json Update dependencies 2019-07-12 11:07:51 +02:00

README.md

VSCode TextMate Build Status

An interpreter for grammar files as defined by TextMate. Supports loading grammar files from JSON or PLIST format. Cross - grammar injections are currently not supported.

Installing

npm install vscode-textmate

Using

const fs = require('fs');
const vsctm = require('vscode-textmate');

/**
 * Utility to read a file as a promise
 */
function readFile(path) {
    return new Promise((resolve, reject) => {
        fs.readFile(path, (error, data) => error ? reject(error) : resolve(data));
    })
}

// Create a registry that can create a grammar from a scope name.
const registry = new vsctm.Registry({
    loadGrammar: (scopeName) => {
        if (scopeName === 'source.js') {
            // https://github.com/textmate/javascript.tmbundle/blob/master/Syntaxes/JavaScript.plist
            return readFile('./JavaScript.plist').then(data => vsctm.parseRawGrammar(data.toString()))
        }
        console.log(`Unknown scope name: ${scopeName}`);
        return null;
    }
});

// Load the JavaScript grammar and any other grammars included by it async.
registry.loadGrammar('source.js').then(grammar => {
    const text = [
        `function sayHello(name) {`,
        `\treturn "Hello, " + name;`,
        `}`
    ];
    let ruleStack = vsctm.INITIAL;
    for (let i = 0; i < text.length; i++) {
        const line = text[i];
        const lineTokens = grammar.tokenizeLine(line, ruleStack);
        console.log(`\nTokenizing line: ${line}`);
        for (let j = 0; j < lineTokens.tokens.length; j++) {
            const token = lineTokens.tokens[j];
            console.log(` - token from ${token.startIndex} to ${token.endIndex} ` +
              `(${line.substring(token.startIndex, token.endIndex)}) ` +
              `with scopes ${token.scopes.join(', ')}`
            );
        }
        ruleStack = lineTokens.ruleStack;
    }
});



API doc

See the main.ts file

Developing

  • Clone the repository
  • Run npm install
  • Compile in the background with npm run watch
  • Run tests with npm test
  • Run benchmark with npm run benchmark
  • Troubleshoot a grammar with npm run inspect -- PATH_TO_GRAMMAR PATH_TO_FILE

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

License

MIT