From 7017aa6afd653008f5f766c6bfa2096d55de7d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Passerini?= Date: Fri, 11 Oct 2024 10:49:10 -0300 Subject: [PATCH] Add same line comments --- src/parser.ts | 12 +++++++++--- test/parser.test.ts | 11 +++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index a2dfb5b1..629c7bf5 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -451,10 +451,9 @@ const prefixMessageChain: Parser = lazy(() => ) // TODO sumar messageChain. -// TODO comments // TODO cloures const postfixMessageChain: Parser }> = lazy(() => - alt( + withSameLineComment(alt( obj({ receiver: primaryExpression, message: key('.').then(name), @@ -476,8 +475,15 @@ const postfixMessageChain: Parser(parser: Parser): Parser => + seq(parser, optional(sameLineComment)).map(([result, comment]) => + comment + ? result.copy({ metadata: result.metadata.concat(comment) }) + : result + ) const messageChain = (receiver: Parser, message: Parser, args: Parser>): Parser => lazy(() => seq( diff --git a/test/parser.test.ts b/test/parser.test.ts index a38c606e..54e856a5 100644 --- a/test/parser.test.ts +++ b/test/parser.test.ts @@ -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({