Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matching line continuation in shell style comment #1509

Open
kevemueller opened this issue Jan 23, 2025 · 3 comments
Open

Matching line continuation in shell style comment #1509

kevemueller opened this issue Jan 23, 2025 · 3 comments
Labels

Comments

@kevemueller
Copy link

What is your question?

I am trying to build a grammar for bmake style Makefiles. In my first run I handled line continuation outside of the grammar using a generator and then fed the grammar only single lines. I would like to improve on that by handling line continuation in the grammar and feed the whole file to the grammar.

I am struggling with the most simple thing as the SH_COMMENT:

# this is a comment \
that spans multiple \
lines

I am working now refining this, but can't get the continuation handling right. Continuation is a backslash directly followed by new line. Both need to be consumed and the rest of the line appended to the comment.

start: bmake_comment

bmake_comment: SH_COMMENT ((CONT /[^\n]*\n/)+|NEWLINE)

CONT: "\\" NEWLINE

%import common.SH_COMMENT
%import common.NEWLINE
@erezsh
Copy link
Member

erezsh commented Jan 23, 2025

The comment should be one terminal. Meaning rename it to BMAKE_COMMENT.

@kevemueller
Copy link
Author

Hi Erez,
thank you for helping a beginner like me.

I have updated the grammar to be

start: BMAKE_COMMENT

BMAKE_COMMENT: SH_COMMENT (_CONT /[^\n\\]*/)* NEWLINE

_CONT: "\\" NEWLINE

SH_COMMENT: /#[^\n\\]*/
#%import common.SH_COMMENT
%import common.NEWLINE

But I had to add the \\ into the regexp of both the SH_COMMENT as well as the line coming after the continuation. This is not precise as the comment may have \ anywhere inside it not bearing any special meaning.
Without this, I could not get the CONT match, as any preceding regexp would eat the \. I tried using with (?!\\), but I could not get that right either.

How can I get rid of adding \\ to the regexp and allow \ to appear inside the comment, and treat it special only at the end of the line?

@kevemueller
Copy link
Author

kevemueller commented Jan 24, 2025

Ok. So I got the negative lookbehind in place:

start: BMAKE_COMMENT*

BMAKE_COMMENT: SH_COMMENT CONT* NEWLINE

CONT: /\\/ NEWLINE /[^\n]*(?<!\\)/
SH_COMMENT: /#[^\n]*(?<!\\)/

#%import common.SH_COMMENT
%import common.NEWLINE

This will not treat \ inside the comment any special.

Is this the best way to do it?

Bonus problem, how do I get the '\\n' out of the matched string. I tried _CONT and %ignore CONT, but both left the continuation characters in the parse result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants