Skip to content

Commit

Permalink
comments at the end of message chains
Browse files Browse the repository at this point in the history
  • Loading branch information
ivojawer committed Nov 21, 2023
1 parent 54a5bfa commit ca4988b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const key = <T extends string>(str: T): Parser<T> => (
const _ = optional(whitespace.atLeast(1))
const __ = optional(key(';').or(_))

const comment = (position: 'start'|'end') => lazy('comment', () => regex(/\/\*(.|[\r\n])*?\*\/|\/\/.*/)).map(text => new Annotation('comment', { text, position }))
const comment = (position: 'start' | 'end') => lazy('comment', () => regex(/\/\*(.|[\r\n])*?\*\/|\/\/.*/)).map(text => new Annotation('comment', { text, position }))
const endComment = alt(
optional(_).then(comment('end')), // same-line comment
comment('end').sepBy(_) // after-line comments
Expand Down Expand Up @@ -395,9 +395,10 @@ const messageChain = (receiver: Parser<ExpressionNode>, message: Parser<Name>, a
index,
receiver,
seq(message, args, index).many(),
).map(([start, initialReceiver, calls]) =>
endComment,
).map(([start, initialReceiver, calls, comments]) =>
calls.reduce((receiver, [message, args, end]) =>
new SendNode({ receiver, message, args, sourceMap: buildSourceMap(start, end) })
new SendNode({ receiver, message, args, sourceMap: buildSourceMap(start, end), metadata: Array.isArray(comments) ? comments : [comments] })
, initialReceiver)
)
)
Expand Down
9 changes: 9 additions & 0 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ describe('Wollok parser', () => {
.and.have.nested.property('entity').tracedTo(28, 29)
})

it('comments after sends should be parsed', () => {
'pepita.vola() //some comment'
.should.be.parsedBy(parse.Send).into(new Send({
receiver: new Reference({ name: 'pepita' }),
message: 'vola',
metadata: [new Annotation('comment', { text: 'some asa comment' })],
}))
})

it('should not parse elements inside line comment', () => {
'// import p'.should.not.be.parsedBy(parser)
})
Expand Down

0 comments on commit ca4988b

Please sign in to comment.