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

[JLINE-730] Support for comments in DefaultParser #731

Merged
merged 3 commits into from
Nov 8, 2021

Conversation

snuyanzin
Copy link
Contributor

@snuyanzin snuyanzin commented Oct 21, 2021

The PR makes it possible to set multiline comment start and end.
Also it allows to specify several one-line comments as several engines support several one-line comments

fixes #730

@mattirn
Copy link
Collaborator

mattirn commented Oct 27, 2021

There are some minor things I would like to be fixed before merge.

  1. 'one line comments' and 'multi-line comments' are better known as 'line comments' and 'block comments', see for example Comment (computer programming). We should use these names to construct method and variable names.
  2. The setters of line comment delimiters should be renamed as
    setLineCommentDelims( ... ) and lineCommentDelims( ... )
  3. IMHO we should not set individually block comment delimiters. We should have methods that set block comment delimiters as:
    setBlockCommentDelims(new BlockCommentDelims(<start>, <end>))
    The class BlockCommentDelims we can implement as an inner class in DefaultParser. BlockCommentDelims constructor should throw InvalidArgumentException if either of the comment delimiters is null/empty or if delimiters are equal.
  4. We do not need the boolean field eofOnUnclosedBlockComment in DefaultParser. If the block comment delimiters are set we will always throw EofError on not closed block comments.
  5. The method isLineStartsWith( ... ) should be renamed as isCommentDelim( ... ).
  6. The method isOneLineCommentStarted( ... ) should be renamed as isLineCommentDelim( ... ) and its scope should be private.
  7. The methods isMultiLineCommentStarted( ... ) and isMultiLineCommentClosed( ... ) can be removed. We can call directly the method isCommentDelim( ... )

@snuyanzin
Copy link
Contributor Author

Hi @mattirn
thanks for the feedback.
I've made changes based on it.

Besides that I also noticed a failing test case like hello/*comment*/world produced only one word ['helloworld'] while in fact there should be ['hello', 'world']. I fixed that and added this to tests.

@@ -333,6 +407,17 @@ public boolean isDelimiter(final CharSequence buffer, final int pos) {
return !isQuoted(buffer, pos) && !isEscaped(buffer, pos) && isDelimiterChar(buffer, pos);
}

private int handleDelimiterAndGetRawWordLength(StringBuilder current, List<String> words, int rawWordStart, int rawWordCursor, int rawWordLength, int pos) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extracted this into a method as now this code block is used in 2 places.
In case you have a better method name suggestion I would consider that

Copy link
Collaborator

@mattirn mattirn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. BlockCommentDelims constructor should throw InvalidArgumentException if either of start or end argument is null/empty or if they are equal:
    throw new InvalidArgumentException("Bad block comment delimiter!")

  2. I would rename also private field lineComments as lineCommentDelims .

    private String[] lineComments = null;

  3. On missing block comment delimiter the error message should be "Missing closing block comment delimiter"

    throw new EOFError(-1, -1, "Missing closing block comment",

Please, could you squash the commits in a single commit when finished.

Thanks.

@snuyanzin
Copy link
Contributor Author

ok, thanks, done

Copy link
Collaborator

@mattirn mattirn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. IllegalArgumentException should thrown also when start.isEmpty() || end.isEmpty()

    if (start == null || end == null || start.equals(end)) {

  2. same test executed twice

    parser.setBlockCommentDelims(new DefaultParser.BlockCommentDelims("/*", null));

    and
    parser.setBlockCommentDelims(new DefaultParser.BlockCommentDelims("/*", null));

@snuyanzin
Copy link
Contributor Author

Sorry, had to check it before pushing...
Fixed it.
Please let me know if I can squash commits

@mattirn
Copy link
Collaborator

mattirn commented Oct 29, 2021

NP. I will do some more testing next week.... I let you know if I find something.

@snuyanzin
Copy link
Contributor Author

Probably also need to check that block start and end differ from one line comments and from brackets and quotes

@mattirn
Copy link
Collaborator

mattirn commented Nov 6, 2021

Probably also need to check that block start and end differ from one line comments and from brackets and quotes

Partially true... brackets can be used as block comment delimiters if they do not have any other use in CLI. I would not add any other validity checks for comment delimiters. IMHO checks would just make the code more complicate to follow without bringing much more value for implementation. Anyway I guess that the 'standard' comment delimiters (like //, #, /*, */, ...) will be mostly adopted in Jline CLIs

Copy link
Collaborator

@mattirn mattirn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should throw EOFError also for missing opening block comment:

            if (startBlockCommentMissing) {
                throw new EOFError(-1, -1, "Missing opening block comment delimiter",
                        "missing: " + blockCommentStart);
            }

Please, change also the line

throw new EOFError(-1, -1, "Missing closing block comment delimiter",

to

                throw new EOFError(-1, -1, "Missing closing block comment delimiter",
                        "add: " + blockCommentEnd);

@mattirn mattirn merged commit 1315fc0 into jline:master Nov 8, 2021
@mattirn mattirn mentioned this pull request Jan 26, 2022
@gnodet gnodet added this to the 3.22.0 milestone Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for comments in DefaultParser
3 participants