- Notifications
You must be signed in to change notification settings - Fork118
A library that helps tokenize text using Text Mate grammars.
License
NotificationsYou must be signed in to change notification settings
microsoft/vscode-textmate
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
An interpreter for grammar files as defined by TextMate. TextMate grammars use the oniguruma dialect (https://github.com/kkos/oniguruma). Supports loading grammar files from JSON or PLIST format. This library is used in VS Code. Cross - grammar injections are currently not supported.
npm install vscode-textmate
constfs=require('fs');constpath=require('path');constvsctm=require('vscode-textmate');constoniguruma=require('vscode-oniguruma');/** * Utility to read a file as a promise */functionreadFile(path){returnnewPromise((resolve,reject)=>{fs.readFile(path,(error,data)=>error ?reject(error) :resolve(data));})}constwasmBin=fs.readFileSync(path.join(__dirname,'./node_modules/vscode-oniguruma/release/onig.wasm')).buffer;constvscodeOnigurumaLib=oniguruma.loadWASM(wasmBin).then(()=>{return{createOnigScanner(patterns){returnnewoniguruma.OnigScanner(patterns);},createOnigString(s){returnnewoniguruma.OnigString(s);}};});// Create a registry that can create a grammar from a scope name.constregistry=newvsctm.Registry({onigLib:vscodeOnigurumaLib,loadGrammar:(scopeName)=>{if(scopeName==='source.js'){// https://github.com/textmate/javascript.tmbundle/blob/master/Syntaxes/JavaScript.plistreturnreadFile('./JavaScript.plist').then(data=>vsctm.parseRawGrammar(data.toString()))}console.log(`Unknown scope name:${scopeName}`);returnnull;}});// Load the JavaScript grammar and any other grammars included by it async.registry.loadGrammar('source.js').then(grammar=>{consttext=[`function sayHello(name) {`,`\treturn "Hello, " + name;`,`}`];letruleStack=vsctm.INITIAL;for(leti=0;i<text.length;i++){constline=text[i];constlineTokens=grammar.tokenizeLine(line,ruleStack);console.log(`\nTokenizing line:${line}`);for(letj=0;j<lineTokens.tokens.length;j++){consttoken=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;}});/* OUTPUT:Unknown scope name: source.js.regexpTokenizing line: function sayHello(name) { - token from 0 to 8 (function) with scopes source.js, meta.function.js, storage.type.function.js - token from 8 to 9 ( ) with scopes source.js, meta.function.js - token from 9 to 17 (sayHello) with scopes source.js, meta.function.js, entity.name.function.js - token from 17 to 18 (() with scopes source.js, meta.function.js, punctuation.definition.parameters.begin.js - token from 18 to 22 (name) with scopes source.js, meta.function.js, variable.parameter.function.js - token from 22 to 23 ()) with scopes source.js, meta.function.js, punctuation.definition.parameters.end.js - token from 23 to 24 ( ) with scopes source.js - token from 24 to 25 ({) with scopes source.js, punctuation.section.scope.begin.jsTokenizing line: return "Hello, " + name; - token from 0 to 1 ( ) with scopes source.js - token from 1 to 7 (return) with scopes source.js, keyword.control.js - token from 7 to 8 ( ) with scopes source.js - token from 8 to 9 (") with scopes source.js, string.quoted.double.js, punctuation.definition.string.begin.js - token from 9 to 16 (Hello, ) with scopes source.js, string.quoted.double.js - token from 16 to 17 (") with scopes source.js, string.quoted.double.js, punctuation.definition.string.end.js - token from 17 to 18 ( ) with scopes source.js - token from 18 to 19 (+) with scopes source.js, keyword.operator.arithmetic.js - token from 19 to 20 ( ) with scopes source.js - token from 20 to 24 (name) with scopes source.js, support.constant.dom.js - token from 24 to 25 (;) with scopes source.js, punctuation.terminator.statement.jsTokenizing line: } - token from 0 to 1 (}) with scopes source.js, punctuation.section.scope.end.js*/
Seevscode-tmgrammar-test that can help you write unit tests against your grammar.
- 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
This project has adopted theMicrosoft Open Source Code of Conduct. For more information see theCode of Conduct FAQ or contactopencode@microsoft.com with any additional questions or comments.
About
A library that helps tokenize text using Text Mate grammars.
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published