🦕 Deno port of Microsoft/vscode-textmate: A library that helps tokenize text using TextMate grammars.

Overview

TextMate

A library that helps tokenize text using TextMate grammars.

Deno port of Microsoft/vscode-textmate. See original license here.

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. Cross-grammar injections are currently not supported.

You can import this module from

Tests:

deno test -A

For any other information: https://github.com/Microsoft/vscode-textmate

Example

import {
  INITIAL,
  parseRawGrammar,
  Registry,
} from "https://ghc.deno.dev/dcdunkan/deno_textmate/mod.ts";
import {
  loadWASM,
  OnigScanner,
  OnigString,
} from "https://esm.sh/[email protected]";

// https://github.com/microsoft/vscode-oniguruma/blob/main/out/onig.wasm
const wasmFile = await Deno.readFile("./onig.wasm");
const wasmBin = wasmFile.buffer;
const vscodeOnigurumaLib = loadWASM(wasmBin).then(() => {
  return {
    createOnigScanner(patterns: string[]) {
      return new OnigScanner(patterns);
    },
    createOnigString(str: string) {
      return new OnigString(str);
    },
  };
});

// Create a registry that can create a grammar from a scope name.
const registry = new Registry({
  onigLib: vscodeOnigurumaLib,
  loadGrammar: async (scopeName) => {
    if (scopeName === "source.js") {
      // https://github.com/textmate/javascript.tmbundle/blob/master/Syntaxes/JavaScript.plist
      const grammarFileContent = await Deno.readTextFile("./JavaScript.plist");
      return parseRawGrammar(grammarFileContent);
    }

    console.log(`Unknown scope name: ${scopeName}`);
  },
});

// Load the JavaScript grammar and any other grammars included by it async.
const grammar = await registry.loadGrammar("source.js");

const text = [
  `function sayHello(name) {`,
  `\treturn "Hello, " + name;`,
  `}`,
];
let ruleStack = 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;
}

Output:

Unknown scope name: source.js.regexp

Tokenizing 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.js

Tokenizing 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.js

Tokenizing line: }
 - token from 0 to 1 (}) with scopes source.js, punctuation.section.scope.end.js

Run the example using Deno CLI:

deno run --allow-env=VSCODE_TEXTMATE_DEBUG --allow-read --allow-net https://ghc.deno.dev/dcdunkan/deno_textmate/example.ts

Credits and copyright of the code goes to Microsoft.

You might also like...

Proofie is an experimental proof-reader for VSCode that helps you write better.

Proofie is an experimental proof-reader for VSCode that helps you write better.

Proofie Proofie is an experimental proof-reader for VSCode that helps you write better. Install You can install proofie from the VSCode Marketplace. O

Jul 25, 2022

VSCode extension for managing text selection.

Selection Manager This Visual Studio Code extension will allow you to manage selected text. The aim of this extension is to increase productivity by u

Apr 12, 2022

VSCode extension to paste text as a string array

VSCode extension to paste text as a string array

VSCode extension to paste text as a string array. This is useful when copying command line into launch.json args

Dec 28, 2022

Boost is a Microsoft Excel Add-in to help developers import large excel workbooks into their database using SQL queries

Boost is a Microsoft Excel Add-in to help developers import large excel workbooks into their database using SQL queries

Microsoft Excel Add-in for Developers About Us Boost is a Microsoft Excel Add-in to help developers import large excel workbooks into their database u

Sep 30, 2022

A facial recognition/detection app with Angular using Microsoft's Face API

A facial recognition/detection app with Angular using Microsoft's Face API

Deja-Vu Overview Deja-Vu is a client side application generated with Angular CLI version 13. The core function of this application is to detect, recog

Jul 30, 2022

This is a simple boilerplate for a Deno website, deployed with Deno Deploy.

Simple Deno Website Boilerplate This is a simple website boilerplate built using Deno and deployed using Deno Deploy. Demo at simple-deno-website-boil

Dec 3, 2022

TypeSafe MongoDB Atlas Data API SDK for Deno & Deno Deploy

Atlas SDK atlas_sdk is a TypeSafe MongoDB Atlas Data API SDK for Deno & Deno Deploy Links Docs Import Replace LATEST_VERSION with current latest versi

Dec 26, 2022

🛣️ A tiny and fast http request router designed for use with deno and deno deploy

Rutt Rutt is a tiny http router designed for use with deno and deno deploy. It is written in about 200 lines of code and is pretty fast, using an exte

Dec 10, 2022

deno-ja (Deno Japanese community) showcase

Showcase Deno本家よりも気軽に作ったものを公開できるようなShowcaseです。 スクリーンショットの撮影方法 短めのidを決めていただいて、下記のようにスクリプトを実行してください。 deno task screenshot [url] [id] ※エラーが出る場合は、下記を実行してみ

Oct 28, 2022
Owner
Dunkan
I love @grammyjs and 3446!
Dunkan
solid material ui port (ported from blazor port)

solid-material-ui solid material ui port (porting from blazor port) In preparation for solid hack Turbo Mono-repository is used for component package

skclusive 18 Apr 30, 2022
Opinionated collection of TypeScript definitions and utilities for Deno and Deno Deploy. With complete types for Deno/NPM/TS config files, constructed from official JSON schemas.

Schemas Note: You can also import any type from the default module, ./mod.ts deno.json import { type DenoJson } from "https://deno.land/x/[email protected]

deno911 2 Oct 12, 2022
Improved Deno port of GramJS — a MTProto API Telegram client library.

Warning Considered as unstable. But, most of the commonly used features are working as expected. Grm Grm is an improved Deno port of GramJS, written i

Dunkan 26 Dec 31, 2022
Fluent for Deno. Port of @the-moebius/fluent.

Fluent for Deno [better_fluent] Deno port of the-moebius/fluent Better Fluent integration for TypeScript/JavaScript. See the original repository for m

Dunkan 5 May 29, 2022
Grm is an improved Deno port of GramJS, written in TypeScript

Grm is an improved Deno port of GramJS, written in TypeScript. GramJS is a popular MTProto API Telegram client library written in JavaScript for Node.js and browsers, with its core being based on Telethon.

Dunkan 26 Dec 31, 2022
Unofficial port of the Sentry SDK for JavaScript to Deno.

Sentry_deno This is an unofficial port of the Sentry SDK (@sentry/browser) to Deno. import * as Sentry from "https://deno.land/x/sentry_deno/main.ts";

Geert-Jan Zwiers 11 Aug 11, 2022
Deno port of Gnome libraries (such as Gtk).

Deno GI Deno port of Gnome libraries (such as Gtk). Early Stage and Unstable Usage You must specify --allow-ffi and --unstable flags to run your progr

Ahmadreza Gilak 20 Dec 24, 2022
This package will help parse OData strings (only the Microsoft Dataverse subset). It can be used as a validator, or you can build some javascript library which consumes the output of this library.

@albanian-xrm/dataverse-odata This package will help parse OData strings (only the Microsoft Dataverse subset). It can be used as a validator, or you

AlbanianXrm 3 Oct 22, 2022
A small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity

Wren Wren is a small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity. convenient aliases for HTTP responses au

Jakub Neander 69 Dec 12, 2022
Deno bindings for yoga, using Deno FFI.

deno_yoga Deno bindings for yoga, using Deno FFI. Usage flags: --allow-ffi: Requires ffi access to "yogacore.dll", "libyogacore.so", "libyogacore.dyli

迷渡 6 Feb 11, 2022