Skip to content

Commit

Permalink
feat(AST): add YAML CST -> AST transformation for all common types
Browse files Browse the repository at this point in the history
Only currently missing transformation is YAML Directives.

Refs #1
  • Loading branch information
char0n committed Sep 30, 2020
1 parent 8a84320 commit e2e1e1b
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 8 deletions.
5 changes: 3 additions & 2 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlKeyValuePair.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import stampit from 'stampit';

import Node from '../../Node';
import YamlStyleModel from './YamlStyle';

interface YamlKeyValuePair extends Node {
interface YamlKeyValuePair extends Node, YamlStyleModel {
type: 'keyValuePair';
}

const YamlKeyValuePair: stampit.Stamp<YamlKeyValuePair> = stampit(Node, {
const YamlKeyValuePair: stampit.Stamp<YamlKeyValuePair> = stampit(Node, YamlStyleModel, {
statics: {
type: 'keyValuePair',
},
Expand Down
10 changes: 6 additions & 4 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlStream.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import stampit from 'stampit';
import { either } from 'ramda';
import { isArray } from 'ramda-adjunct';

import Node from '../../Node';
import YamlDocument from './YamlDocument';
import { isDocument } from './predicates';
import YamlComment from './YamlComment';
import { isComment, isDocument } from './predicates';

interface YamlStream extends Node {
type: 'stream';
readonly content: Array<YamlDocument>;
readonly content: Array<YamlDocument | YamlComment>;
children: Array<YamlDocument>;
}

Expand All @@ -16,8 +18,8 @@ const YamlStream: stampit.Stamp<YamlStream> = stampit(Node, {
type: 'stream',
},
methods: {
get content(): Array<YamlDocument> {
return isArray(this.children) ? this.children.filter(isDocument) : [];
get content(): Array<YamlDocument | YamlComment> {
return isArray(this.children) ? this.children.filter(either(isDocument, isComment)) : [];
},
},
});
Expand Down
16 changes: 16 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlStyle.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stampit from 'stampit';

export enum YamlStyle {
Plain = 'Plain',
SingleQuoted = 'SingleQuoted',
Expand All @@ -14,3 +16,17 @@ export enum YamlStyleGroup {
Flow = 'Flow',
Block = 'Block',
}

interface YamlStyleModel {
styleGroup: YamlStyleGroup | null;
style: YamlStyle | null;
}

const YamlStyleModel: stampit.Stamp<YamlStyleModel> = stampit({
props: {
styleGroup: null,
style: null,
},
});

export default YamlStyleModel;
2 changes: 2 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export const isScalar = isNodeType('scalar');
export const isAlias = isNodeType('alias');

export const isDirective = isNodeType('directive');

export const isComment = isNodeType('comment');
121 changes: 120 additions & 1 deletion apidom/packages/apidom-ast/src/transformers/tree-sitter-yaml.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stampit from 'stampit';
import { either, flatten, lensProp, over } from 'ramda';
import { isArray, isFunction, isFalse } from 'ramda-adjunct';
import { isArray, isFalse, isFunction } from 'ramda-adjunct';
import { SyntaxNode, Tree } from 'tree-sitter';

import YamlStream from '../nodes/yaml/YamlStream';
Expand All @@ -11,6 +11,7 @@ import YamlKeyValuePair from '../nodes/yaml/YamlKeyValuePair';
import YamlTag, { YamlNodeKind } from '../nodes/yaml/YamlTag';
import YamlAnchor from '../nodes/yaml/YamlAnchor';
import YamlScalar from '../nodes/yaml/YamlScalar';
import YamlComment from '../nodes/yaml/YamlComment';
import { YamlStyle, YamlStyleGroup } from '../nodes/yaml/YamlStyle';
import ParseResult from '../ParseResult';
import Position, { Point } from '../Position';
Expand Down Expand Up @@ -185,6 +186,38 @@ const Visitor = stampit({
return YamlKeyValuePair({
children: node.children,
position,
styleGroup: YamlStyleGroup.Block,
isMissing: node.isMissing(),
});
},
};

this.flow_mapping = {
enter(node: SyntaxNode) {
const position = toPosition(node);
const tag = toTag(node);
const anchor = toAnchor(node);

return YamlMapping({
children: node.children,
position,
anchor,
tag,
styleGroup: YamlStyleGroup.Flow,
style: YamlStyle.Explicit,
isMissing: node.isMissing(),
});
},
};

this.flow_pair = {
enter(node: SyntaxNode) {
const position = toPosition(node);

return YamlKeyValuePair({
children: node.children,
position,
styleGroup: YamlStyleGroup.Flow,
isMissing: node.isMissing(),
});
},
Expand All @@ -196,6 +229,29 @@ const Visitor = stampit({
},
};

this.block_sequence = {
enter(node: SyntaxNode) {
const position = toPosition(node);
const tag = toTag(node);
const anchor = toAnchor(node);

return YamlSequence({
children: node.children,
position,
anchor,
tag,
styleGroup: YamlStyleGroup.Block,
style: YamlStyle.NextLine,
});
},
};

this.block_sequence_item = {
enter(node: SyntaxNode) {
return node.children;
},
};

this.flow_sequence = {
enter(node: SyntaxNode) {
const position = toPosition(node);
Expand Down Expand Up @@ -235,6 +291,69 @@ const Visitor = stampit({
});
},
};

this.single_quote_scalar = {
enter(node: SyntaxNode) {
const position = toPosition(node);
const tag = toTag(node);
const anchor = toAnchor(node);

return YamlScalar({
content: node.text,
anchor,
tag,
position,
styleGroup: YamlStyleGroup.Flow,
style: YamlStyle.SingleQuoted,
});
},
};

this.double_quote_scalar = {
enter(node: SyntaxNode) {
const position = toPosition(node);
const tag = toTag(node);
const anchor = toAnchor(node);

return YamlScalar({
content: node.text,
anchor,
tag,
position,
styleGroup: YamlStyleGroup.Flow,
style: YamlStyle.DoubleQuoted,
});
},
};

this.block_scalar = {
enter(node: SyntaxNode) {
const position = toPosition(node);
const tag = toTag(node);
const anchor = toAnchor(node);
// eslint-disable-next-line no-nested-ternary
const style = node.text.startsWith('|')
? YamlStyle.Literal
: node.text.startsWith('>')
? YamlStyle.Folded
: null;

return YamlScalar({
content: node.text,
anchor,
tag,
position,
styleGroup: YamlStyleGroup.Block,
style,
});
},
};

this.comment = {
enter(node: SyntaxNode) {
return YamlComment({ content: node.text });
},
};
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('tree-sitter-yaml', function () {
const parser = new Parser();
parser.setLanguage(YAMLLanguage);

const jsonString = '[1, null]';
const jsonString = '# comment\ntest';
cst = parser.parse(jsonString);
ast = transform(cst);
});
Expand Down

0 comments on commit e2e1e1b

Please sign in to comment.