Skip to content

Commit

Permalink
Merge pull request #9 from rusty-ecma/feat/smaller-ast
Browse files Browse the repository at this point in the history
Feat/smaller ast
  • Loading branch information
FreeMasen authored Jun 3, 2023
2 parents 63c6214 + 2646502 commit 40167a8
Show file tree
Hide file tree
Showing 14 changed files with 3,102 additions and 2,513 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "resast"
version = "0.5.0"
authors = ["rfm <r@robertmasen.pizza>"]
version = "0.6.0-alpha.1"
authors = ["rfm <r[email protected]>"]
edition = "2018"
description = "Rusty-ECMAScript Abstract Syntax Tree"
repository = "https://github.com/FreeMasen/resast"
repository = "https://github.com/rusty-ecma/resast"
license = "MIT"
keywords = ["JavaScript", "parsing", "JS", "ES", "ECMA"]
categories = ["parsing", "text-processing", "web-programming"]
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# RESAST

> Rusty ECMAScript Abstract Syntax Tree
[![crates.io](https://img.shields.io/crates/v/resast.svg)](https://crates.io/crates/resast)
[![API Documentation on docs.rs](https://docs.rs/resast/badge.svg)](https://docs.rs/resast)
[![last commit master](https://img.shields.io/github/last-commit/FreeMasen/resast.svg)](https://github.com/FreeMasen/resast/)
[![last commit main](https://img.shields.io/github/last-commit/FreeMasen/resast.svg)](https://github.com/FreeMasen/resast/)

## Contributing

If you are interested in contributing to RESAST know that I would be happy for the help!

Feel free to open issues and/or pull requests for anything that you see that might be an improvement.
Expand All @@ -15,4 +17,4 @@ I do not work on this full time, please be patient if I am not able to respond q
Before starting on any work, please comment on an existing issue or open a new issue in order to reduce
the chance that work would get duplicated.

[check out the issues](https://github.com/FreeMasen/resast/issues)
[check out the issues](https://github.com/FreeMasen/resast/issues)
81 changes: 33 additions & 48 deletions src/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@ use crate::{Class, Func, Ident};
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum Decl<'a> {
pub enum Decl<T> {
/// A variable declaration
/// ```js
/// var x, b;
/// let y, a = 0;
/// const q = 100
/// ```
Var(VarKind, Vec<VarDecl<'a>>),
Var(VarKind, Vec<VarDecl<T>>),
/// A function declaration
/// ```js
/// function thing() {}
/// ```
Func(Func<'a>),
Func(Func<T>),
/// A class declaration
/// ```js
/// class Thing {}
/// ```
Class(Class<'a>),
Class(Class<T>),
/// An import declaration
/// ```js
/// import * as moment from 'moment';
/// import Thing, {thing} from 'stuff';
/// ```
Import(Box<ModImport<'a>>),
Import(Box<ModImport<T>>),
/// An export declaration
/// ```js
/// export function thing() {}
/// ```
Export(Box<ModExport<'a>>),
Export(Box<ModExport<T>>),
}

/// The identifier and optional value of a variable declaration
Expand All @@ -48,19 +48,9 @@ pub enum Decl<'a> {
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub struct VarDecl<'a> {
pub id: Pat<'a>,
pub init: Option<Expr<'a>>,
}

/// A module declaration, This would only be available
/// in an ES Mod, it would be either an import or
/// export at the top level
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub enum ModDecl<'a> {
Import(ModImport<'a>),
Export(ModExport<'a>),
pub struct VarDecl<T> {
pub id: Pat<T>,
pub init: Option<Expr<T>>,
}

/// A declaration that imports exported
Expand All @@ -71,9 +61,9 @@ pub enum ModDecl<'a> {
/// ```
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct ModImport<'a> {
pub specifiers: Vec<ImportSpecifier<'a>>,
pub source: Lit<'a>,
pub struct ModImport<T> {
pub specifiers: Vec<ImportSpecifier<T>>,
pub source: Lit<T>,
}

/// The name of the thing being imported
Expand All @@ -83,35 +73,35 @@ pub struct ModImport<'a> {
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum ImportSpecifier<'a> {
pub enum ImportSpecifier<T> {
/// A specifier in curly braces, this might
/// have a local alias
///
/// ```js
/// import {Thing} from './stuff.js';
/// import {People as Persons} from './places.js';
/// ```
Normal(Vec<NormalImportSpec<'a>>),
Normal(Vec<NormalImportSpec<T>>),
/// A specifier that has been exported with the
/// default keyword, this should not be wrapped in
/// curly braces.
/// ```js
/// import DefaultThing from './stuff/js';
/// ```
Default(Ident<'a>),
Default(Ident<T>),
/// Import all exported members from a module
/// in a namespace.
///
/// ```js
/// import * as Moment from 'moment.js';
/// ```
Namespace(Ident<'a>),
Namespace(Ident<T>),
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct NormalImportSpec<'a> {
pub local: Ident<'a>,
pub imported: Ident<'a>,
pub struct NormalImportSpec<T> {
pub alias: Option<Ident<T>>,
pub imported: Ident<T>,
}

/// Something exported from this module
Expand All @@ -121,13 +111,13 @@ pub struct NormalImportSpec<'a> {
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum ModExport<'a> {
pub enum ModExport<T> {
/// ```js
/// export default function() {};
/// //or
/// export default 1;
/// ```
Default(DefaultExportDecl<'a>),
Default(DefaultExportDecl<T>),
///```js
/// export {foo} from 'mod';
/// //or
Expand All @@ -138,30 +128,25 @@ pub enum ModExport<'a> {
/// export function bar() {
/// }
/// ```
Named(NamedExportDecl<'a>),
Named(NamedExportDecl<T>),
/// ```js
/// export * from 'mod';
/// ```
All {
alias: Option<Ident<'a>>,
name: Lit<'a>,
alias: Option<Ident<T>>,
name: Lit<T>,
},
}

// pub struct NamedExportDecl<'a> {
// decl: Option<Box<Decl<'a>>>,
// specs: Vec<ExportSpecifier<'a>>,
// source: Option<Cow<'a, str>>
// }
/// An export that has a name
/// ```js
/// export function thing() {}
/// export {stuff} from 'place';
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub enum NamedExportDecl<'a> {
Decl(Decl<'a>),
Specifier(Vec<ExportSpecifier<'a>>, Option<Lit<'a>>),
pub enum NamedExportDecl<T> {
Decl(Decl<T>),
Specifier(Vec<ExportSpecifier<T>>, Option<Lit<T>>),
}

/// A default export
Expand All @@ -174,9 +159,9 @@ pub enum NamedExportDecl<'a> {
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum DefaultExportDecl<'a> {
Decl(Decl<'a>),
Expr(Expr<'a>),
pub enum DefaultExportDecl<T> {
Decl(Decl<T>),
Expr(Expr<T>),
}

/// The name of the thing being exported
Expand All @@ -193,7 +178,7 @@ pub enum DefaultExportDecl<'a> {
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub struct ExportSpecifier<'a> {
pub local: Ident<'a>,
pub exported: Ident<'a>,
pub struct ExportSpecifier<T> {
pub local: Ident<T>,
pub alias: Option<Ident<T>>,
}
Loading

0 comments on commit 40167a8

Please sign in to comment.