Skip to content

Commit

Permalink
fix(parser): update optional whitespace limitations
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgw committed Jan 4, 2025
1 parent c6a4111 commit 97cf99c
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 77 deletions.
72 changes: 72 additions & 0 deletions src/parser/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class BaseParser extends EmbeddedActionsParser {
*/
this.RULE('tag', () => {
this.CONSUME(Tokens.LeftParenthesis)
this.OPTION(() => this.SUBRULE(this.nodeSpace))
const tag = this.SUBRULE(this.string)
this.OPTION1(() => this.SUBRULE1(this.nodeSpace))
this.CONSUME(Tokens.RightParenthesis)
return tag
})
Expand Down Expand Up @@ -206,6 +208,76 @@ class BaseParser extends EmbeddedActionsParser {

return String.fromCharCode(codepoint)
})

/**
* Consume node space
* @method #nodeSpace
* @memberof module:kdljs.parser.kdl.BaseParser
*/
this.RULE('nodeSpace', () => {
this.AT_LEAST_ONE(() => this.OR([
{ ALT: () => this.SUBRULE(this.whiteSpace) },
{ ALT: () => this.SUBRULE(this.lineContinuation) }
]))
})

/**
* Consume a line continuation
* @method #lineContinuation
* @memberof module:kdljs.parser.kdl.BaseParser
*/
this.RULE('lineContinuation', () => {
this.CONSUME(Tokens.EscLine)
this.OPTION(() => this.SUBRULE(this.whiteSpace))
this.OR([
{ ALT: () => this.SUBRULE(this.lineComment) },
{ ALT: () => this.CONSUME(Tokens.NewLine) },
{ ALT: () => this.CONSUME(Tokens.EOF) }
])
})

/**
* Consume a line comment
* @method #lineComment
* @memberof module:kdljs.parser.kdl.BaseParser
*/
this.RULE('lineComment', () => {
this.CONSUME(Tokens.LineComment)
this.OR([
{ ALT: () => this.CONSUME(Tokens.NewLine) },
{ ALT: () => this.CONSUME(Tokens.EOF) }
])
})

/**
* Consume a multiline comment
* @method #multilineComment
* @memberof module:kdljs.parser.kdl.BaseParser
*/
this.RULE('multilineComment', () => {
this.CONSUME(Tokens.OpenMultiLineComment)
this.MANY(() => {
this.OR([
{ ALT: () => this.CONSUME(Tokens.MultiLineCommentContent) },
{ ALT: () => this.SUBRULE(this.multilineComment) }
])
})
this.CONSUME(Tokens.CloseMultiLineComment)
})

/**
* Consume whitespace
* @method #whiteSpace
* @memberof module:kdljs.parser.kdl.BaseParser
*/
this.RULE('whiteSpace', () => {
this.AT_LEAST_ONE(() => {
this.OR([
{ ALT: () => this.CONSUME(Tokens.WhiteSpace) },
{ ALT: () => this.SUBRULE(this.multilineComment) }
])
})
})
}

/**
Expand Down
105 changes: 28 additions & 77 deletions src/parser/kdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ class KdlParser extends BaseParser {
{
ALT: () => {
this.CONSUME(Tokens.BlockComment)
this.OPTION1(() => this.SUBRULE(this.nodeSpace))
this.OPTION1(() => this.SUBRULE(this.lineSpace))
this.SUBRULE(this.node)
}
},
{ ALT: () => this.SUBRULE(this.lineComment) },
{ ALT: () => this.SUBRULE(this.whiteSpace) },
{ ALT: () => this.CONSUME(Tokens.NewLine) },
{ ALT: () => this.SUBRULE1(this.lineSpace) },
{ ALT: () => nodes.push(this.SUBRULE1(this.node)) }
]))

Expand All @@ -104,7 +102,11 @@ class KdlParser extends BaseParser {
*/
this.RULE('node', () => {
const tags = {
name: this.OPTION(() => this.SUBRULE(this.tag)),
name: this.OPTION(() => {
const tag = this.SUBRULE(this.tag)
this.OPTION1(() => this.SUBRULE(this.nodeSpace))
return tag
}),
properties: {},
values: []
}
Expand All @@ -113,11 +115,11 @@ class KdlParser extends BaseParser {
const values = []

this.MANY(() => {
this.SUBRULE(this.nodeSpace)
this.SUBRULE1(this.nodeSpace)

const commented = this.OPTION1(() => {
const commented = this.OPTION2(() => {
this.CONSUME(Tokens.BlockComment)
this.OPTION2(() => this.SUBRULE1(this.nodeSpace))
this.OPTION3(() => this.SUBRULE(this.lineSpace))
return true
})

Expand Down Expand Up @@ -150,7 +152,7 @@ class KdlParser extends BaseParser {
this.SUBRULE(this.nodeChildrenSlashdash)
})

const children = this.OPTION3(() => {
const children = this.OPTION4(() => {
this.SUBRULE3(this.nodeSpace)
const children = this.SUBRULE(this.nodeChildren)
return children
Expand All @@ -161,7 +163,7 @@ class KdlParser extends BaseParser {
this.SUBRULE1(this.nodeChildrenSlashdash)
})

this.OPTION4(() => this.SUBRULE5(this.nodeSpace))
this.OPTION5(() => this.SUBRULE5(this.nodeSpace))

if (this.LA(1).tokenType !== Tokens.RightBrace) {
this.SUBRULE(this.nodeTerminator)
Expand All @@ -177,19 +179,21 @@ class KdlParser extends BaseParser {
*/
this.RULE('nodeChildrenSlashdash', () => {
this.CONSUME(Tokens.BlockComment)
this.OPTION(() => this.SUBRULE(this.nodeSpace))
this.OPTION(() => this.SUBRULE(this.lineSpace))
this.SUBRULE(this.nodeChildren)
})

/**
* Consume a property
* @method #property
* @memberof module:kdljs.parser.kdl.KdlParser
* @return {Array<string,module:kdljs~Value>} key-value pair
* @return {[string, module:kdljs~Value>, string]} key-value-type tuple
*/
this.RULE('property', () => {
const key = this.SUBRULE(this.string)
this.OPTION(() => this.SUBRULE(this.nodeSpace))
this.CONSUME(Tokens.Equals)
this.OPTION1(() => this.SUBRULE1(this.nodeSpace))
const parts = this.SUBRULE(this.taggedValue)
return [key, parts[0], parts[1]]
})
Expand All @@ -198,10 +202,14 @@ class KdlParser extends BaseParser {
* Consume a tagged value
* @method #taggedValue
* @memberof module:kdljs.parser.kdl.KdlParser
* @return {module:kdljs~Value}
* @return {[module:kdljs~Value, string]} value-type tuple
*/
this.RULE('taggedValue', () => {
const tag = this.OPTION(() => this.SUBRULE(this.tag))
const tag = this.OPTION(() => {
const tag = this.SUBRULE(this.tag)
this.OPTION1(() => this.SUBRULE(this.nodeSpace))
return tag
})
const value = this.SUBRULE(this.value)
return [value, tag]
})
Expand All @@ -220,30 +228,16 @@ class KdlParser extends BaseParser {
})

/**
* Consume node space
* @method #nodeSpace
* Consume line space
* @method #lineSpace
* @memberof module:kdljs.parser.kdl.KdlParser
*/
this.RULE('nodeSpace', () => {
this.RULE('lineSpace', () => {
this.AT_LEAST_ONE(() => this.OR([
{ ALT: () => this.SUBRULE(this.whiteSpace) },
{ ALT: () => this.SUBRULE(this.lineContinuation) }
]))
})

/**
* Consume a line continuation
* @method #lineContinuation
* @memberof module:kdljs.parser.kdl.KdlParser
*/
this.RULE('lineContinuation', () => {
this.CONSUME(Tokens.EscLine)
this.OPTION(() => this.SUBRULE1(this.whiteSpace))
this.OPTION1(() => this.CONSUME(Tokens.LineComment))
this.OR([
{ ALT: () => this.SUBRULE(this.nodeSpace) },
{ ALT: () => this.CONSUME(Tokens.NewLine) },
{ ALT: () => this.CONSUME(Tokens.EOF) }
])
{ ALT: () => this.SUBRULE(this.lineComment) }
]))
})

/**
Expand All @@ -260,49 +254,6 @@ class KdlParser extends BaseParser {
])
})

/**
* Consume a line comment
* @method #lineComment
* @memberof module:kdljs.parser.kdl.KdlParser
*/
this.RULE('lineComment', () => {
this.CONSUME(Tokens.LineComment)
this.OR([
{ ALT: () => this.CONSUME(Tokens.NewLine) },
{ ALT: () => this.CONSUME(Tokens.EOF) }
])
})

/**
* Consume a multiline comment
* @method #multilineComment
* @memberof module:kdljs.parser.kdl.KdlParser
*/
this.RULE('multilineComment', () => {
this.CONSUME(Tokens.OpenMultiLineComment)
this.MANY(() => {
this.OR([
{ ALT: () => this.CONSUME(Tokens.MultiLineCommentContent) },
{ ALT: () => this.SUBRULE(this.multilineComment) }
])
})
this.CONSUME(Tokens.CloseMultiLineComment)
})

/**
* Consume whitespace
* @method #whiteSpace
* @memberof module:kdljs.parser.kdl.KdlParser
*/
this.RULE('whiteSpace', () => {
this.AT_LEAST_ONE(() => {
this.OR([
{ ALT: () => this.CONSUME(Tokens.WhiteSpace) },
{ ALT: () => this.SUBRULE(this.multilineComment) }
])
})
})

this.performSelfAnalysis()
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/kdl/node_spaces.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(\
tag )\
/*

*/ node prop /*

*/\
= (\
tag )\ // comment
/*

*/ value
14 changes: 14 additions & 0 deletions test/suite.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,19 @@
"name": "node",
"children": [{ "name": "foo" }, { "name": "bar" }]
}
],
"node_spaces": [
{
"name": "node",
"properties": {
"prop": "value"
},
"tags": {
"name": "tag",
"properties": {
"prop": "tag"
}
}
}
]
}

0 comments on commit 97cf99c

Please sign in to comment.