wanted_modules icon indicating copy to clipboard operation
wanted_modules copied to clipboard

ast parser

Open lambtron opened this issue 3 years ago • 3 comments

deno parse to get the ast of html, js, xml any

origin: https://discord.com/channels/684898665143206084/769285344809451572/769323040324452352

for reference: https://www.npmjs.com/package/abstract-syntax-tree

lambtron avatar Mar 29 '22 02:03 lambtron

  • https://deno.land/x/swc (SWC parser/transformer compiled to WASM)
  • https://deno.land/x/css (CSS parser; it will use Servo's CSS parser via WASM soon)

littledivy avatar Mar 29 '22 09:03 littledivy

Recast works if you import it from jspm and use the TypeScript parser:

import { parse } from "https://jspm.dev/npm:recast";
import TSParser from "https://jspm.dev/npm:recast/parsers/typescript";

// Let's turn this function declaration into a variable declaration.
const code = [
  "function add(a: number, b: number): number {",
  "  return a +",
  "    // Weird formatting, huh?",
  "    b;",
  "}"
].join("\n");

// Parse the code using an interface similar to require("esprima").parse.
const ast = parse(code, { parser: TSParser });
console.log(ast);

tjosepo avatar May 30 '22 08:05 tjosepo

https://github.com/syntax-tree/unist ... an excellent ecosystem of syntax trees, and utilities (in the wider Unified ecosystem).

Mostly focused around markup languages (html, xml, markdown) rather than code, but there isn't anything to prevent unist based ASTs being created for programming languages.

I've found many of these libraries already work really well in Deno imported via esm.sh

jollytoad avatar Aug 29 '23 14:08 jollytoad