From ca4988beadf0e8761a8a5b050b8ffa84a1a89de1 Mon Sep 17 00:00:00 2001 From: ivojawer Date: Tue, 21 Nov 2023 14:04:26 -0300 Subject: [PATCH] comments at the end of message chains --- src/parser.ts | 7 ++++--- test/parser.test.ts | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index c3c7e2e1..29df8e7f 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -84,7 +84,7 @@ const key = (str: T): Parser => ( 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 @@ -395,9 +395,10 @@ const messageChain = (receiver: Parser, message: Parser, 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) ) ) diff --git a/test/parser.test.ts b/test/parser.test.ts index 20f04a27..3d52b6fd 100644 --- a/test/parser.test.ts +++ b/test/parser.test.ts @@ -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) })