Skip to content

Commit

Permalink
feat(AST): introduce YAML AST
Browse files Browse the repository at this point in the history
Refs #1
  • Loading branch information
char0n committed Sep 25, 2020
1 parent dfaa005 commit a99ebbc
Show file tree
Hide file tree
Showing 22 changed files with 320 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ast/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"stampit": "=4.3.1"
},
"devDependencies": {
"tree-sitter": "=0.16.2",
"tree-sitter": "=0.17.0",
"tree-sitter-json": "=0.16.0"
}
}
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ast/src/nodes/json/JsonArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { anyPass } from 'ramda';
import JsonNode from './JsonNode';
import { isFalse, isTrue, isNull, isNumber, isString, isArray, isObject } from './predicates';

type JsonArray = Node;
type JsonArray = JsonNode;

const JsonArray: stampit.Stamp<JsonArray> = stampit(JsonNode, {
statics: {
Expand Down
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ast/src/nodes/json/JsonDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { head } from 'ramda';

import JsonNode from './JsonNode';

interface JsonDocument extends Node {
interface JsonDocument extends JsonNode {
child: unknown | null;
}

Expand Down
3 changes: 2 additions & 1 deletion apidom/packages/apidom-ast/src/nodes/json/JsonObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import stampit from 'stampit';

import JsonNode from './JsonNode';
import JsonProperty from './JsonProperty';
import { isProperty } from './predicates';

type JsonObject = JsonNode;
Expand All @@ -10,7 +11,7 @@ const JsonObject: stampit.Stamp<JsonObject> = stampit(JsonNode, {
type: 'object',
},
methods: {
get properties(): unknown[] {
get properties(): Array<JsonProperty> {
// @ts-ignore
return this.children.filter(isProperty);
},
Expand Down
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ast/src/nodes/json/JsonProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
isTrue,
} from './predicates';

interface JsonProperty extends Node {
interface JsonProperty extends JsonNode {
key: JsonKey;
value: unknown;
}
Expand Down
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ast/src/nodes/json/JsonString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import JsonStringContent from './JsonStringContent';
import JsonEscapeSequence from './JsonEscapeSequence';
import { isEscapeSequence, isStringContent } from './predicates';

interface JsonString extends Node {
interface JsonString extends JsonNode {
value: string;
}

Expand Down
51 changes: 51 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlBlockScalar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import stampit from 'stampit';
import { test } from 'ramda';
import { isString } from 'ramda-adjunct';

import YamlScalar from './YamlScalar';

enum BlockStyleIndicator {
LITERAL = 'LITERAL',
FOLDED = 'FOLDED',
}

enum BlockChompingIndicator {
CLIP = 'CLIP',
STRIP = 'STRIP',
KEEP = 'KEEP',
}

interface YamlBlockScalar extends YamlScalar {
readonly style: BlockStyleIndicator;
readonly chomping: BlockChompingIndicator;
}

const YamlBlockScalar: stampit.Stamp<YamlBlockScalar> = stampit(YamlScalar, {
statics: {
type: 'blockScalar',
},
props: {
style: BlockStyleIndicator.LITERAL,
chomping: BlockChompingIndicator.CLIP,
},
methods: {
get style(): BlockStyleIndicator {
return isString(this.value) && test(/^|/, this.value)
? BlockStyleIndicator.LITERAL
: BlockStyleIndicator.FOLDED;
},
get chomping(): BlockChompingIndicator {
/* eslint-disable no-nested-ternary */
return !isString(this.value)
? BlockChompingIndicator.CLIP
: test(/^(>|\|)\+/, this.value)
? BlockChompingIndicator.KEEP
: test(/^(>|\|)-/, this.value)
? BlockChompingIndicator.STRIP
: BlockChompingIndicator.CLIP;
/* eslint-enable */
},
},
});

export default YamlBlockScalar;
21 changes: 21 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import stampit from 'stampit';

import YamlNode from './YamlNode';

interface YamlComment extends YamlNode {
value: string | null;
}

const YamlComment: stampit.Stamp<YamlComment> = stampit(YamlNode, {
statics: {
type: 'comment',
},
props: {
value: null,
},
init({ value = null } = {}) {
this.value = value;
},
});

export default YamlComment;
23 changes: 23 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlDocument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import stampit from 'stampit';
import { head } from 'ramda';

import YamlNode from './YamlNode';

interface YamlDocument extends YamlNode {
child: unknown | null;
}

const YamlDocument: stampit.Stamp<YamlDocument> = stampit(YamlNode, {
statics: {
type: 'document',
},
methods: {
// @ts-ignore
get child(): unknown {
// @ts-ignore
return head(this.children);
},
},
});

export default YamlDocument;
13 changes: 13 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlDoubleQuoteScalar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import stampit from 'stampit';

import YamlScalar from './YamlScalar';

type YamlDoubleQuoteScalar = YamlScalar;

const YamlDoubleQuoteScalar: stampit.Stamp<YamlDoubleQuoteScalar> = stampit(YamlScalar, {
statics: {
type: 'doubleQuoteScalar',
},
});

export default YamlDoubleQuoteScalar;
23 changes: 23 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import stampit from 'stampit';

import YamlNode from './YamlNode';
import YamlPair from './YamlPair';
import { isPair } from './predicates';

interface YamlMap extends YamlNode {
readonly entries: Array<YamlPair>;
}

const YamlStream: stampit.Stamp<YamlMap> = stampit(YamlNode, {
statics: {
type: 'map',
},
methods: {
get entries(): Array<YamlPair> {
// @ts-ignore
return this.children.filter(isPair);
},
},
});

export default YamlStream;
18 changes: 18 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import stampit from 'stampit';

import Node from '../../Node';

interface YamlNode extends Node {
tag: unknown | null;
}

const YamlNode: stampit.Stamp<YamlNode> = stampit(Node, {
props: {
tag: null,
},
init({ tag = null } = {}) {
this.tag = tag;
},
});

export default YamlNode;
31 changes: 31 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlPair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import stampit from 'stampit';
import { anyPass } from 'ramda';

import YamlNode from './YamlNode';
import { isScalar, isMap, isSeq } from './predicates';
import YamlScalar from './YamlScalar';

interface YamlPair extends YamlNode {
readonly key: unknown;
readonly value: unknown;
}

const YamlPair: stampit.Stamp<YamlPair> = stampit(YamlNode, {
statics: {
type: 'pair',
},
methods: {
// @ts-ignore
get key(): YamlScalar {
// @ts-ignore
return this.children.find(isScalar);
},
// @ts-ignore
get value(): unknown {
// @ts-ignore
return this.children.find(anyPass([isMap, isSeq, isScalar]));
},
},
});

export default YamlPair;
13 changes: 13 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlPlainScalar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import stampit from 'stampit';

import YamlScalar from './YamlScalar';

type YamlPlainScalar = YamlScalar;

const YamlPlainScalar: stampit.Stamp<YamlPlainScalar> = stampit(YamlScalar, {
statics: {
type: 'plainScalar',
},
});

export default YamlPlainScalar;
21 changes: 21 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlScalar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import stampit from 'stampit';

import YamlNode from './YamlNode';

interface YamlScalar extends YamlNode {
value: string | null;
}

const YamlScalar: stampit.Stamp<YamlScalar> = stampit(YamlNode, {
statics: {
type: 'scalar',
},
props: {
value: null,
},
init({ value = null } = {}) {
this.value = value;
},
});

export default YamlScalar;
21 changes: 21 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlSeq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import stampit from 'stampit';

import YamlNode from './YamlNode';

interface YamlSeq extends YamlNode {
readonly items: Array<unknown>;
}

const YamlSeq: stampit.Stamp<YamlSeq> = stampit(YamlNode, {
statics: {
type: 'seq',
},
methods: {
get items(): Array<unknown> {
// @ts-ignore
return this.children;
},
},
});

export default YamlSeq;
13 changes: 13 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlSingleQuoteScalar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import stampit from 'stampit';

import YamlScalar from './YamlScalar';

type YamlSingleQuoteScalar = YamlScalar;

const YamlSingleQuoteScalar: stampit.Stamp<YamlSingleQuoteScalar> = stampit(YamlScalar, {
statics: {
type: 'singleQuoteScalar',
},
});

export default YamlSingleQuoteScalar;
13 changes: 13 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import stampit from 'stampit';

import YamlNode from './YamlNode';

type YamlStream = YamlNode;

const YamlStream: stampit.Stamp<YamlStream> = stampit(YamlNode, {
statics: {
type: 'stream',
},
});

export default YamlStream;
21 changes: 21 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import stampit from 'stampit';

import YamlNode from './YamlNode';

interface YamlTag extends YamlNode {
value: string | null;
}

const YamlTag: stampit.Stamp<YamlTag> = stampit(YamlNode, {
statics: {
type: 'tag',
},
props: {
value: null,
},
init({ value = null } = {}) {
this.value = value;
},
});

export default YamlTag;
29 changes: 29 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/predicates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { anyPass } from 'ramda';
import { isNodeType } from '../../predicates';

export const isStream = isNodeType('stream');

export const isDocument = isNodeType('document');

export const isMap = isNodeType('map');

export const isSeq = isNodeType('seq');

export const isPair = isNodeType('pair');

export const isTag = isNodeType('tag');

export const isPlainScalar = isNodeType('plainScalar');

export const isSingleQuoteScalar = isNodeType('singleQuoteScalar');

export const isDoubleQuoteScalar = isNodeType('doubleQuoteScalar');

export const isBlockScalar = isNodeType('blockScalar');

export const isScalar = anyPass([
isPlainScalar,
isSingleQuoteScalar,
isDoubleQuoteScalar,
isBlockScalar,
]);
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const ValueVisitor = stampit(SpecificationVisitor, {
},

null(nullNode: JsonNull) {
const nullElement = new this.namespace.elements.Null(nullNode.value);
const nullElement = new this.namespace.elements.Null();
this.element = this.maybeAddSourceMap(nullNode, nullElement);
return BREAK;
},
Expand Down
Loading

0 comments on commit a99ebbc

Please sign in to comment.