Skip to content

Commit

Permalink
feat: fully support type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgw committed Apr 25, 2022
1 parent 1eaa488 commit 63a008f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
31 changes: 22 additions & 9 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,32 @@ function formatIdentifier (value, options) {
}
}

/**
* @access private
* @memberof module:kdljs.formatter
* @param {string} [value] - KDL tag
* @param {module:kdljs/formatter.ProcessedOptions} options - Formatting options
* @return {string}
*/
function formatTag (value, options) {
return value === undefined ? '' : '(' + formatIdentifier(value, options) + ')'
}

/**
* @access private
* @memberof module:kdljs.formatter
* @param {module:kdljs~Value} value - KDL value
* @param {string} [tag] - KDL tag
* @param {module:kdljs/formatter.ProcessedOptions} options - Formatting options
* @return {string}
*/
function formatValue (value, options) {
function formatValue (value, tag, options) {
if (typeof value === 'string') {
return formatString(value, options)
return formatTag(tag, options) + formatString(value, options)
} else if (typeof value === 'number' && options.exponentChar === 'E') {
return value.toString().toUpperCase()
return formatTag(tag, options) + value.toString().toUpperCase()
} else {
return value + ''
return formatTag(tag, options) + value
}
}

Expand All @@ -113,11 +125,12 @@ function formatValue (value, options) {
* @memberof module:kdljs.formatter
* @param {string} key
* @param {module:kdljs~Value} value - KDL value
* @param {string} [tag] - KDL tag
* @param {module:kdljs/formatter.ProcessedOptions} options - Formatting options
* @return {string}
*/
function formatProperty (key, value, options) {
return formatIdentifier(key, options) + '=' + formatValue(value, options)
function formatProperty (key, value, tag, options) {
return formatIdentifier(key, options) + '=' + formatValue(value, tag, options)
}

/**
Expand All @@ -141,9 +154,9 @@ function formatNode (node, options, indent) {

const currentIndent = options.indentChar.repeat(options.indent * indent)
const parts = [
currentIndent + formatIdentifier(node.name, options),
...values.map(value => formatValue(value, options)),
...properties.map(key => formatProperty(key, node.properties[key], options))
currentIndent + formatTag(node.tags.name) + formatIdentifier(node.name, options),
...values.map((value, index) => formatValue(value, node.tags.values[index], options)),
...properties.map(key => formatProperty(key, node.properties[key], node.tags.properties[key], options))
]

if (node.children.length) {
Expand Down
32 changes: 23 additions & 9 deletions src/parser/kdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ class KdlParser extends BaseParser {
* @return {module:kdljs~Node}
*/
this.RULE('node', () => {
this.OPTION1(() => this.SUBRULE(this.tag))
const tags = {
name: this.OPTION1(() => this.SUBRULE(this.tag)),
properties: {},
values: []
}
const name = this.SUBRULE(this.identifier)
const properties = {}
const values = []
Expand All @@ -103,13 +107,22 @@ class KdlParser extends BaseParser {
{
GATE: this.BACKTRACK(this.property),
ALT: () => {
const pair = this.SUBRULE(this.property)
properties[pair[0]] = pair[1]
const parts = this.SUBRULE(this.property)
properties[parts[0]] = parts[1]
if (parts[2] !== undefined) {
tags.properties[parts[0]] = parts[2]
}
}
},
{
GATE: this.BACKTRACK(this.taggedValue),
ALT: () => values.push(this.SUBRULE(this.taggedValue))
ALT: () => {
const parts = this.SUBRULE(this.taggedValue)
values.push(parts[0])
if (parts[1] !== undefined) {
tags.values.push(parts[1])
}
}
},
{
ALT: () => {
Expand Down Expand Up @@ -161,7 +174,7 @@ class KdlParser extends BaseParser {
}
])

return { name, properties, values, children }
return { name, properties, values, children, tags }
})

/**
Expand All @@ -173,8 +186,8 @@ class KdlParser extends BaseParser {
this.RULE('property', () => {
const key = this.SUBRULE(this.identifier)
this.CONSUME(Tokens.Equals)
const value = this.SUBRULE(this.taggedValue)
return [key, value]
const parts = this.SUBRULE(this.taggedValue)
return [key, parts[0], parts[1]]
})

/**
Expand All @@ -184,8 +197,9 @@ class KdlParser extends BaseParser {
* @return {module:kdljs~Value}
*/
this.RULE('taggedValue', () => {
this.OPTION(() => this.SUBRULE(this.tag))
return this.SUBRULE(this.value)
const tag = this.OPTION(() => this.SUBRULE(this.tag))
const value = this.SUBRULE(this.value)
return [value, tag]
})

/**
Expand Down

0 comments on commit 63a008f

Please sign in to comment.