Skip to content

Commit

Permalink
Simpler implementation of withSameLineComment
Browse files Browse the repository at this point in the history
  • Loading branch information
npasserini committed Oct 11, 2024
1 parent 7017aa6 commit f74b5e2
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ const __ = optional(key(';').or(Parsimmon.regex(/\s/)))

const comment = (position: 'start' | 'end' | 'inner') => lazy('comment', () => regex(/\/\*(.|[\r\n])*?\*\/|\/\/.*/)).map(text => new Annotation('comment', { text, position }))
const sameLineComment: Parser<Annotation> = comment('end')
const withSameLineComment = <T extends Node>(result: T): Parser<T> =>
optional(sameLineComment).map(comment => comment
? result.copy({ metadata: result.metadata.concat(comment) })
: result)

export const sanitizeWhitespaces = (originalFrom: SourceIndex, originalTo: SourceIndex, input: string): [SourceIndex, SourceIndex] => {
const EOL = input.includes('\r\n') ? '\r\n' : '\n'
Expand Down Expand Up @@ -453,7 +457,7 @@ const prefixMessageChain: Parser<ExpressionNode> = lazy(() =>
// TODO sumar messageChain.
// TODO cloures
const postfixMessageChain: Parser<ExpressionNode & { problems?: List<BaseProblem> }> = lazy(() =>
withSameLineComment(alt(
alt(
obj({
receiver: primaryExpression,
message: key('.').then(name),
Expand All @@ -475,15 +479,8 @@ const postfixMessageChain: Parser<ExpressionNode & { problems?: List<BaseProblem
message,
problems: [new ParseError(MALFORMED_MESSAGE_SEND, buildSourceMap(start, end))]
}))
))
).chain(withSameLineComment)
)

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

0 comments on commit f74b5e2

Please sign in to comment.