Skip to content

Commit

Permalink
Add same line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
npasserini committed Oct 11, 2024
1 parent a8b9e98 commit 7017aa6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,9 @@ const prefixMessageChain: Parser<ExpressionNode> = lazy(() =>
)

// TODO sumar messageChain.
// TODO comments
// TODO cloures
const postfixMessageChain: Parser<ExpressionNode & { problems?: List<BaseProblem> }> = lazy(() =>
alt(
withSameLineComment(alt(
obj({
receiver: primaryExpression,
message: key('.').then(name),
Expand All @@ -476,8 +475,15 @@ const postfixMessageChain: Parser<ExpressionNode & { problems?: List<BaseProblem
message,
problems: [new ParseError(MALFORMED_MESSAGE_SEND, buildSourceMap(start, end))]
}))
)
))
)

const withSameLineComment = <T extends Node>(parser: Parser<T>): Parser<T> =>
seq(parser, optional(sameLineComment)).map(([result, comment]) =>
comment
? result.copy({ metadata: result.metadata.concat(comment) })
: result
)

const messageChain = (receiver: Parser<ExpressionNode>, message: Parser<Name>, args: Parser<List<ExpressionNode>>): Parser<ExpressionNode> => lazy(() =>
seq(
Expand Down
11 changes: 11 additions & 0 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ describe('Wollok parser', () => {
}))
})

it('comments after malformed sends should be parsed', () => {
'vola() //some comment'
.should.be.parsedBy(parse.Send)
.recoveringFrom(parse.MALFORMED_MESSAGE_SEND, 0, 4)
.into(new Send({
receiver: new Literal({ value: null }),
message: 'vola',
metadata: [new Annotation('comment', { text: '//some comment', position: 'end' })],
}))
})

it('comments after variable should be parsed', () => {
'const a = 1 //some comment'
.should.be.parsedBy(parse.Variable).into(new Variable({
Expand Down

0 comments on commit 7017aa6

Please sign in to comment.