Skip to content

Commit

Permalink
Merge branch 'release/nested-dsl'
Browse files Browse the repository at this point in the history
  • Loading branch information
mistlog committed Feb 23, 2020
2 parents ede8519 + 0443e11 commit 984719f
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 45 deletions.
33 changes: 29 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "typedraft",
"version": "0.1.10",
"version": "0.1.12",
"description": "TypeDraft is a superset of typescript with built-in support for DSL extension and literate programming.",
"keywords": [
"literate programming",
Expand Down Expand Up @@ -56,11 +56,12 @@
"@types/node": "^12.12.14",
"@types/toposort": "2.0.3",
"concurrently": "^5.0.2",
"draft-dsl-match": "0.0.3",
"jest": "^24.9.0",
"source-view": "^0.0.5",
"ts-jest": "^24.0.2",
"ts-node": "^8.3.0",
"typedraft": "^0.1.9",
"typedraft": "^0.1.11",
"typescript": "^3.6.2"
}
}
36 changes: 21 additions & 15 deletions source-view/code-object/module.md
Expand Up @@ -58,20 +58,24 @@ export class Foo {
As we are only interested in the draft part of a module, then we need a way to return this "view" of module code.

```typescript
<ModuleCode /> +
function ToDraft(this: ModuleCode) {
let draft: Draft = [];
const that = this;
const visitor: Visitor = {
Program(path) {
that.m_Path = path;
path.get("body").forEach(path => {
<CreateDraft />;
});
}
};
traverse(this.m_File, visitor);
return draft;
<ModuleCode /> +
function ToDraft(this: ModuleCode) {
// refresh and update bindings
this.m_File = ToFile(ToString(this.m_File));

//
let draft: Draft = [];
const that = this;
const visitor: Visitor = {
Program(path) {
that.m_Path = path;
path.get("body").forEach(path => {
<CreateDraft />;
});
}
};
traverse(this.m_File, visitor);
return draft;
};
```

Expand Down Expand Up @@ -124,12 +128,14 @@ export function IsLocalContext(path: NodePath<Node>): path is NodePath<FunctionD
if (!path.isFunctionDeclaration()) {
return false;
}
const [directive] = path.node.body.directives;
const has_context = Boolean(directive);
const name = path.node.id.name;
const binding = path.scope.parent.getBinding(name);
const is_local_context = binding.referencePaths.some(path => {
const used_as_jsx = path.parentPath?.parentPath?.isJSXElement();
const used_as_statement = path.parentPath?.parentPath?.parentPath?.isExpressionStatement();
return used_as_jsx && used_as_statement;
return (has_context && used_as_jsx) || (used_as_jsx && used_as_statement);
});
return is_local_context;
}
Expand Down
31 changes: 21 additions & 10 deletions source-view/core/transcriber.md
Expand Up @@ -35,6 +35,7 @@ A transcriber just likes a container, it's a collection of plugins. For example,
```typescript
export interface ITranscriber {
Preprocess: () => void;
RefreshDraft: () => void;
}
```

Expand All @@ -58,6 +59,19 @@ When we init a transcriber, we will preprocess code to build some maps for looku
};
```

```typescript
<Transcriber /> +
function RefreshDraft(this: Transcriber & ITranscriber) {
//
this.m_ClassMap.clear();
this.m_MethodMap.clear();
this.m_ContextMap.clear();

//
this.Preprocess();
};
```

# Utility

Other methods are just utility.
Expand Down Expand Up @@ -95,26 +109,26 @@ export interface ITranscriber {

```typescript
export interface ITranscriber {
TraverseLocalContext: (callback: ITraverseLocalContextCallback) => void;
TraverseLocalContext: (callback: TraverseLocalContextCallback) => void;
GetLocalContext: (name: string) => LocalContext;
}
```

```typescript
export type ITraverseLocalContextCallback = (context: LocalContext, name: string) => void;
export type TraverseLocalContextCallback = (context: LocalContext, name: string) => void;
```

## Class

```typescript
export interface ITranscriber {
GetClass: (name: string) => ExportClassCode;
TraverseMethod: (callback: ITraverseMethodCallback) => void;
TraverseMethod: (callback: TraverseMethodCallback) => void;
}
```

```typescript
export type ITraverseMethodCallback = (methods: Array<MethodCode>, class_name: string) => void;
export type TraverseMethodCallback = (methods: Array<MethodCode>, class_name: string) => void;
```

# Implementation
Expand Down Expand Up @@ -151,7 +165,7 @@ export type ITraverseMethodCallback = (methods: Array<MethodCode>, class_name: s
* 3. add methods to class
* 4. remove redundant code
*/
this.m_Plugins = [new DSLPlugin(this), new LocalContextPlugin(this), new ClassPlugin(this), new FilterPlugin(this)];
this.m_Plugins = [new RefreshDraftPlugin(this), new DSLPlugin(this), new RefreshDraftPlugin(this), new LocalContextPlugin(this), new ClassPlugin(this), new FilterPlugin(this)];
};
```

Expand All @@ -173,7 +187,7 @@ export type ITraverseMethodCallback = (methods: Array<MethodCode>, class_name: s

```typescript
<Transcriber /> +
function TraverseLocalContext(this: Transcriber, callback: ITraverseLocalContextCallback) {
function TraverseLocalContext(this: Transcriber, callback: TraverseLocalContextCallback) {
this.m_ContextMap.forEach((context, name) => callback(context, name));
};
```
Expand All @@ -189,7 +203,7 @@ export type ITraverseMethodCallback = (methods: Array<MethodCode>, class_name: s

```typescript
<Transcriber /> +
function TraverseMethod(this: Transcriber, callback: ITraverseMethodCallback) {
function TraverseMethod(this: Transcriber, callback: TraverseMethodCallback) {
this.m_MethodMap.forEach((methods, class_name) => callback(methods, class_name));
};
```
Expand All @@ -211,8 +225,5 @@ export type ITraverseMethodCallback = (methods: Array<MethodCode>, class_name: s
//
this.PrepareDSLs();
this.PreparePlugins();

//
this.Preprocess();
};
```
11 changes: 11 additions & 0 deletions source-view/plug-in/draft-plugin-refresh.md
@@ -0,0 +1,11 @@
```typescript
export class RefreshDraftPlugin implements IPlugin {
m_Transcriber: ITranscriber;
constructor(transcriber: ITranscriber) {
this.m_Transcriber = transcriber;
}
Transcribe() {
this.m_Transcriber.RefreshDraft();
}
}
```
12 changes: 10 additions & 2 deletions src/code-object/module.tsx
Expand Up @@ -5,6 +5,7 @@ import { LocalContext } from "./local-context";
import { Visitor } from "@babel/core";
import { ToFile } from "../common/utility";
import traverse, { NodePath, Node } from "@babel/traverse";
import { ToString } from "typedraft";

/*
# Draft
Expand Down Expand Up @@ -68,8 +69,12 @@ export class Foo {
As we are only interested in the draft part of a module, then we need a way to return this "view" of module code.
*/
<ModuleCode /> + function ToDraft(this: ModuleCode)
< ModuleCode /> + function ToDraft(this: ModuleCode)
{
// refresh and update bindings
this.m_File = ToFile(ToString(this.m_File));

//
let draft: Draft = [];

const that = this;
Expand Down Expand Up @@ -152,13 +157,16 @@ export function IsLocalContext(path: NodePath<Node>): path is NodePath<FunctionD
return false;
}

const [directive] = path.node.body.directives;
const has_context = Boolean(directive);

const name = path.node.id.name;
const binding = path.scope.parent.getBinding(name);
const is_local_context = binding.referencePaths.some(path =>
{
const used_as_jsx = path.parentPath?.parentPath?.isJSXElement();
const used_as_statement = path.parentPath?.parentPath?.parentPath?.isExpressionStatement();
return used_as_jsx && used_as_statement;
return (has_context && used_as_jsx) || (used_as_jsx && used_as_statement);
});
return is_local_context;
}
Expand Down
30 changes: 20 additions & 10 deletions src/core/transcriber.tsx
Expand Up @@ -9,6 +9,7 @@ import { LocalContext } from "../code-object/local-context";
import { DSLPlugin } from "../plug-in/draft-plugin-dsl";
import { ToString } from "../common/utility";
import { NodePath } from "@babel/traverse";
import { RefreshDraftPlugin } from "../plug-in/draft-plugin-refresh";

/*
# Transcriber
Expand Down Expand Up @@ -45,10 +46,10 @@ A transcriber just likes a container, it's a collection of plugins. For example,
/*
# Preprocess
*/

export interface ITranscriber
{
Preprocess: () => void;
RefreshDraft: () => void;
};

/*
Expand Down Expand Up @@ -76,6 +77,16 @@ When we init a transcriber, we will preprocess code to build some maps for looku
})
};

<Transcriber /> + function RefreshDraft(this: Transcriber & ITranscriber)
{
//
this.m_ClassMap.clear();
this.m_MethodMap.clear();
this.m_ContextMap.clear();

//
this.Preprocess();
};
/*
# Utility
Expand Down Expand Up @@ -115,11 +126,11 @@ export interface ITranscriber
*/
export interface ITranscriber
{
TraverseLocalContext: (callback: ITraverseLocalContextCallback) => void;
TraverseLocalContext: (callback: TraverseLocalContextCallback) => void;
GetLocalContext: (name: string) => LocalContext;
}

export type ITraverseLocalContextCallback = (context: LocalContext, name: string) => void;
export type TraverseLocalContextCallback = (context: LocalContext, name: string) => void;

/*
## Class
Expand All @@ -128,11 +139,11 @@ export interface ITranscriber
{

GetClass: (name: string) => ExportClassCode;
TraverseMethod: (callback: ITraverseMethodCallback) => void;
TraverseMethod: (callback: TraverseMethodCallback) => void;
};


export type ITraverseMethodCallback = (methods: Array<MethodCode>, class_name: string) => void;
export type TraverseMethodCallback = (methods: Array<MethodCode>, class_name: string) => void;

/*
# Implementation
Expand Down Expand Up @@ -169,7 +180,9 @@ export type ITraverseMethodCallback = (methods: Array<MethodCode>, class_name: s
* 4. remove redundant code
*/
this.m_Plugins = [
new RefreshDraftPlugin(this),
new DSLPlugin(this),
new RefreshDraftPlugin(this),
new LocalContextPlugin(this),
new ClassPlugin(this),
new FilterPlugin(this)
Expand All @@ -189,7 +202,7 @@ export type ITraverseMethodCallback = (methods: Array<MethodCode>, class_name: s
return this.m_ContextMap.get(name);
};

<Transcriber /> + function TraverseLocalContext(this: Transcriber, callback: ITraverseLocalContextCallback)
<Transcriber /> + function TraverseLocalContext(this: Transcriber, callback: TraverseLocalContextCallback)
{
this.m_ContextMap.forEach((context, name) => callback(context, name));
};
Expand All @@ -202,7 +215,7 @@ export type ITraverseMethodCallback = (methods: Array<MethodCode>, class_name: s
return this.m_ClassMap.get(name);
};

<Transcriber /> + function TraverseMethod(this: Transcriber, callback: ITraverseMethodCallback)
<Transcriber /> + function TraverseMethod(this: Transcriber, callback: TraverseMethodCallback)
{
this.m_MethodMap.forEach((methods, class_name) => callback(methods, class_name));
};
Expand All @@ -225,8 +238,5 @@ export type ITraverseMethodCallback = (methods: Array<MethodCode>, class_name: s
//
this.PrepareDSLs();
this.PreparePlugins();

//
this.Preprocess();
};

1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -9,5 +9,6 @@ export * from "./plug-in/draft-plugin-class";
export * from "./plug-in/draft-plugin-dsl";
export * from "./plug-in/draft-plugin-filter";
export * from "./plug-in/draft-plugin-local-context";
export * from "./plug-in/draft-plugin-refresh";

export * from "./common/utility";

0 comments on commit 984719f

Please sign in to comment.