Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nearley #5

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions interfaces/interpolate.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ declare namespace Expand {
type Key = '$' | '=' | '^';
type Open = '{' | '(';
type Terminal = '}' | ')' | ' ' | '__null__';
type Op = 'v' | 's' | 'e' | 'f';
type Op = 'v' | 's' | 'e' | 'f' | 'n';

interface State {
detecting?: Key
header?: Key
op?: Op
terminal?: Terminal;
terminal?: Terminal
dirty?: boolean
escape?: boolean
sourceMap: number[]
escaped?: string
sourceMap?: number[]
}

interface Elem {
state: State
raw: any[]
interface Elem extends State {
out: any[]
source: any[]
subst: any[]
}

export interface Options {
dereferenceSync?: (sub: string, sourceMap?: number[]) => any
dereferenceSync?: (sub: string, sourceMap?: number[]) => any
dereference?: (sub: string, sourceMap?: number[]) => any
call?: (sub: any, sourceMap?: number[]) => any
fetch?: (sub: any, sourceMap?: number[]) => any
Expand Down
2 changes: 2 additions & 0 deletions interfaces/moss.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference types="typed-json-transform" />
/// <reference path="schema.d.ts" />

interface MossError {
name: 'MossError',
Expand All @@ -20,6 +21,7 @@ declare namespace Moss {
auto?: any
stack?: any
selectors?: any
schema?: any
merge?: {
operator: Merge.Operator,
precedence: { [x: string]: number }
Expand Down
18 changes: 18 additions & 0 deletions interfaces/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
declare namespace Moss {
namespace Schema {
// interface Options {
// scalarType?: string
// singleType?: Schema.Options
// multiType?: { [x: string]: Schema.Options } | Schema.Options[]
// isArray?: boolean
// isMap?: boolean
// }
interface Options {
type: string,
properties?: {[x: string]: Options},
items?: Options[],
$id?: string
}
type Description = Options | [Options] | string
}
}
34 changes: 23 additions & 11 deletions src/async.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="../interfaces/moss.d.ts" />

import { merge, mergeArray, mergeObject, amap as map, aokmap as okmap, arrayify, extend, check, clone, each, union, difference, sum, valueForKeyPath, all, isEqual, unflatten, flatObject, unsetKeyPath, setValueForKeyPath, mergeOrReturnAssignment } from 'typed-json-transform';
import { interpolateAsync as __interpolate } from './interpolate';
import { merge, mergeArray, mergeObject, okmap as okmapSync, amap as map, aokmap as okmap, arrayify, extend, check, clone, each, union, difference, sum, valueForKeyPath, all, isEqual, unflatten, flatObject, unsetKeyPath, setValueForKeyPath, mergeOrReturnAssignment, contains } from 'typed-json-transform';
import { interpolateAsync as __interpolate, reservedKeys } from './interpolate';
import { cascadeAsync as _cascade, shouldConstruct, select, parseSelectors } from './cascade';
import * as yaml from 'js-yaml';

Expand All @@ -17,6 +17,7 @@ import {

import { handleError } from './util';
import { Sync } from './sync';
import { parseDescription } from './schema';

export namespace Async {
type Functions = Moss.Async.Functions;
Expand All @@ -35,7 +36,9 @@ export namespace Async {
const target = state.target || current.data;

let res;
for (const _key of Object.keys(source)) {
const keys = Object.keys(source);
console.log('evaluate keys', keys);
for (const _key of keys) {
if (!_key) {
continue;
}
Expand Down Expand Up @@ -70,14 +73,11 @@ export namespace Async {
} else {
let val = source[_key];
if (_key[0] === '$') {
key = <any>(await interpolate(current, _key)).data;
} else if (_key[0] == '\\') {
key = key.slice(1);
} else if (_key.indexOf('.') != -1) {
const [first, ...kp] = _key.split('.')
key = first;
val = {};
setValueForKeyPath(source[_key], kp.join('.'), val);
if (!contains(reservedKeys, _key)) {
key = <any>(await interpolate(current, _key)).data;
} else {
key = _key;
}
} else {
key = _key;
}
Expand All @@ -87,10 +87,14 @@ export namespace Async {
if (key) {
state.auto[key] = res;
state.autoMap[key] = currentErrorPath(state).path.join('.');

target[key] = res;
}
currentErrorPath(state).path.pop();
}
if (current.state.schema) {
// check
}
return current;
}

Expand Down Expand Up @@ -209,6 +213,14 @@ export namespace Async {
$: async (current: Moss.ReturnValue, args: any) => {
await parseNextStructure(current, args);
},
schema: async (current: Moss.ReturnValue, args: any) => {
const description = await continueWithNewFrame(current, args);
current.state.schema = parseDescription(description.data);
current.data = current.state.schema;
},
validate: async (current: Moss.ReturnValue, args: any) => {
const schema = await continueWithNewFrame(current, args);
},
extend: async (parent: Moss.ReturnValue, args: any) => {
const layer = await continueWithNewFrame(parent, args);
const { data } = layer;
Expand Down
Loading