From 739984a31c72d4de54e8be9a56890db6a6c2f799 Mon Sep 17 00:00:00 2001 From: Alexandru Dima Date: Fri, 12 Jul 2019 15:08:01 +0200 Subject: [PATCH] Update README.md --- README.md | 78 ++++++++++++++++++++++++++----------------------------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 10e6f4c..d889047 100644 --- a/README.md +++ b/README.md @@ -14,53 +14,49 @@ npm install vscode-textmate const fs = require('fs'); const vsctm = require('vscode-textmate'); -// Create a registry that can create a grammar from a scope name. -const registry = new vsctm.Registry({ - loadGrammar: (scopeName) => { - if (scopeName === 'source.js') { - // https://raw.githubusercontent.com/textmate/javascript.tmbundle/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; - } -}); - /** * Utility to read a file as a promise */ function readFile(path) { - return new Promise((resolve, reject) => { - fs.readFile(path, (error, data) => { - if (error) { - reject(error); - } else { - resolve(data); - } - }); - }) + 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