All Projects â†’ cst â†’ Cst

cst / Cst

Licence: mit
🌿 JavaScript Concrete Syntax Tree

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Cst

Luaparse
A Lua parser written in JavaScript
Stars: ✭ 309 (-25.9%)
Mutual labels:  ast
Ratel Core
High performance JavaScript to JavaScript compiler with a Rust core
Stars: ✭ 367 (-11.99%)
Mutual labels:  ast
Php Parser
🌿 NodeJS PHP Parser - extract AST or tokens (PHP5 and PHP7)
Stars: ✭ 400 (-4.08%)
Mutual labels:  ast
Reshape
💠 transform html with javascript plugins
Stars: ✭ 314 (-24.7%)
Mutual labels:  ast
Hast
Hypertext Abstract Syntax Tree format
Stars: ✭ 344 (-17.51%)
Mutual labels:  ast
Node Dependency Tree
Get the dependency tree of a module
Stars: ✭ 383 (-8.15%)
Mutual labels:  ast
Exprtk
C++ Mathematical Expression Parsing And Evaluation Library
Stars: ✭ 301 (-27.82%)
Mutual labels:  ast
Orgajs
parse org-mode content into AST
Stars: ✭ 417 (+0%)
Mutual labels:  ast
Solhint
Solhint is an open source project created by https://protofire.io. Its goal is to provide a linting utility for Solidity code.
Stars: ✭ 363 (-12.95%)
Mutual labels:  ast
Astexplorer
A web tool to explore the ASTs generated by various parsers.
Stars: ✭ 4,330 (+938.37%)
Mutual labels:  ast
Compodoc
📔 The missing documentation tool for your Angular, Nest & Stencil application
Stars: ✭ 3,567 (+755.4%)
Mutual labels:  ast
Reinforced.typings
Converts C# classes to TypeScript interfaces (and many more) within project build. 0-dependency, minimal, gluten-free
Stars: ✭ 341 (-18.23%)
Mutual labels:  ast
Detective
Find all calls to require() no matter how deeply nested using a proper walk of the AST
Stars: ✭ 387 (-7.19%)
Mutual labels:  ast
Astroid
A common base representation of python source code for pylint and other projects
Stars: ✭ 310 (-25.66%)
Mutual labels:  ast
Remark
remark is a popular tool that transforms markdown with plugins. These plugins can inspect and change your markup. You can use remark on the server, the client, CLIs, deno, etc.
Stars: ✭ 4,746 (+1038.13%)
Mutual labels:  ast
Awesome Graal
A curated list of awesome resources for Graal, GraalVM, Truffle and related topics
Stars: ✭ 302 (-27.58%)
Mutual labels:  ast
Javaparser
Java 1-15 Parser and Abstract Syntax Tree for Java, including preview features to Java 13
Stars: ✭ 3,972 (+852.52%)
Mutual labels:  ast
Elm Analyse
A tool that allows you to analyse your Elm code, identify deficiencies and apply best practices.
Stars: ✭ 418 (+0.24%)
Mutual labels:  ast
Rector
Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
Stars: ✭ 4,739 (+1036.45%)
Mutual labels:  ast
I18nize React
Internationalize react apps within a lunch break
Stars: ✭ 389 (-6.71%)
Mutual labels:  ast

Build Status

JavaScript CST implementation

CST

Check out code samples and rest of the wiki for more.

CST means Concrete Syntax Tree. Unlike an AST (Abstract Syntax Tree), a CST contains all the information from the JavaScript source file: whitespace, punctuators, comments. This information is extremely useful for code style checkers and other code linters. CST is also useful for cases when you need to apply modifications to existing JavaScript files while preserving the initial file formatting.

This CST implementation is designed to be 100% compatible with JS AST (https://github.com/estree/estree).

Main principles:

  • CST contains all the information from a parsed file (including whitespace and comments).
  • Compatible with AST (https://github.com/estree/estree).
  • Requires tokens to modify CST structure.
  • The tree is always valid (it protects itself against breaking changes).
  • CST can be rendered to valid JS at any time.

Let's see an example:

x = 0;
if (x) x++;

The CST for this example:

  • Blue text — CST Tokens.
  • White text in blue blocks — CST Nodes (their structure is equal to an AST).
  • Blue lines — CST Structure.
  • Red lined — AST Links.

Classes

Element

Element is the base class for Node and Token.

declare class Element {

  // traversal for children
  childElements: Array<Element>;
  firstChild: ?Element;
  lastChild: ?Element;

  // traversal for parent
  parentElement: ?Element;

  // traversing between siblings
  nextSibling: ?Element;
  previousSibling: ?Element;

  // traversing to first/last tokens (not only direct tokens)
  getFirstToken(): ?Token;
  getLastToken(): ?Token;

  // traversing to next/previous tokens (not only siblings)
  getNextToken(): ?Token;
  getPreviousToken(): ?Token;

  // Code properties
  type: string;
  isToken: boolean;
  isNode: boolean;
  isExpression: boolean;
  isStatement: boolean;
  isWhitespace: boolean;
  isFragment: boolean;
  isModuleDeclaration: boolean;
  isModuleSpecifier: boolean;

  // Code methods
  getSourceCode(): string;
  getSourceCodeLength(): number;

  // Mutation methods

  // appends child to the end of the `Element`
  appendChild(newElement: Element): void;
  // prepends child to the end of the `Element`
  prependChild(newElement: Element): void;
  // inserts child before `referenceChild`
  insertChildBefore(newElement: Element, referenceChild: Element): void;
  // replaces specified child interval (from `firstChildRef` to lastChildRef`) with specified child.
  replaceChildren(newElement: Element, firstRefChild: Element, lastRefChild: Element): void;

  // Location methods
  getRange(): Range;
  getLoc(): Location;
}

declare class Token extends Element {
  // token value
  value: string;
}

type Range = [
    start: number;
    end: number;
];

type Position = {
  line: number,
  column: number
};

type Location = {
  start: Position,
  end: Position
};

Node

Node extends Element. The Nodes are the "AST part of a CST". If you drop everything but Nodes from a CST, you will get a pure AST from the Node structure. So it is fair to say that Nodes provide the AST logic for a CST. Currently only Nodes can contain children.

The Node property isNode always returns true.

Token

Token extends Element. The purpose of a CST is to have tokens in the tree. By only manipulating tokens, we can change code formatting without any effect on the behaviour.

The Token property isToken always returns true.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].