A library that helps tokenize text using Text Mate grammars.
Перейти к файлу
Alex Dima 95bb619096 Add test for #22, remove old broken test 2019-04-04 17:00:17 +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 update release & version scripts 2018-06-19 16:23:35 +02:00
scripts path was grammer_paths[0] 2018-09-20 00:33:49 -05:00
src Add test for #22, remove old broken test 2019-04-04 17:00:17 +02:00
test-cases Add test for #22, remove old broken test 2019-04-04 17:00:17 +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 Fix tslint issues 2016-11-10 17:27:13 +01:00
.travis.yml travis: use node 8 2018-06-14 23:12:09 +02:00
LICENSE.md Update LICENSE.md 2015-11-06 14:32:12 +01:00
README.md update readme 2018-06-16 00:45:19 +02:00
ThirdPartyNotices.txt Add ThirdPartyNotices.txt 2015-11-18 14:38:28 +01:00
package-lock.json Revert "stash build changes" 2018-09-20 00:49:31 -05:00
package.json Revert "stash build changes" 2018-09-20 00:49:31 -05:00
tsconfig.json pluggable onig scanner 2018-06-08 17:16:23 +02:00
tslint.json Fix tslint issues 2016-11-10 17:27:13 +01:00
typings.json Adopt typings 2016-08-18 11:34:18 +02:00

README.md

VSCode TextMate Build Status Coverage 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

var vsctm = require('vscode-textmate');
var grammarPaths = {
	'source.js': './javascript.tmbundle/Syntaxes/JavaScript.plist'
};

var registry = new vsctm.Registry({
	loadGrammar: function (scopeName) {
		var path = grammarPaths[scopeName];
		if (path) {
			return new Promise((c,e) => {
				fs.readFile(path, (error, content) => {
					if (error) {
						e(error);
					} else {
						var rawGrammar = vsctm.parseRawGrammar(content.toString(), path);
						c(rawGrammar);
					}
				});
			});
		}
		return null;
	}
});

// Load the JavaScript grammar and any other grammars included by it async.
registry.loadGrammar('source.js').then(grammar => {
	// at this point `grammar` is available...
	var lineTokens = grammar.tokenizeLine('function add(a,b) { return a+b; }');
	for (var i = 0; i < lineTokens.tokens.length; i++) {
		var token = lineTokens.tokens[i];
		console.log('Token from ' + token.startIndex + ' to ' + token.endIndex + ' with scopes ' + token.scopes);
	}
});

Tokenizing multiple lines

To tokenize multiple lines, you must pass in the previous returned ruleStack.

var ruleStack = null;
for (var i = 0; i < lines.length; i++) {
	var r = grammar.tokenizeLine(lines[i], ruleStack);
	console.log('Line: #' + i + ', tokens: ' + r.tokens);
	ruleStack = r.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