Conversation
|
I could also not find an official syntax for a linemaker, but looked at https://git.cels.anl.gov/ksalapenades/llvm-project/-/blob/develop/clang/test/Preprocessor/line-directive.c (and did some compiler tests to verify what gets rejected) |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #498 +/- ##
=======================================
Coverage 92.21% 92.21%
=======================================
Files 87 87
Lines 13831 13845 +14
=======================================
+ Hits 12754 12767 +13
- Misses 1077 1078 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Checking the git log, it appears this code was contributed by @reuterbal - I wonder if you would have time to provide some feedback? Thanks! |
reuterbal
left a comment
There was a problem hiding this comment.
Happy to provide my 2ct here. I agree that linemarkers should be handled like preprocessor directives and the implementation itself looks good to me.
As for the need for the additional regex validation: I believe you only need to encode the full linemarker specification as a regex pattern to use in WORDClsBase.match - either manually or using the building blocks in pattern_tools - e.g., like here:
fparser/src/fparser/two/pattern_tools.py
Line 416 in 5e124e5
Without testing it, I would expect this would maybe need to look like something like this:
_pattern = r"^\s*#\s+" + digit_string + "\s+" + file_name
|
|
||
| # The match method will check that it is a valid linemarker, i.e. | ||
| # it has a line number, and file name in double quotes. | ||
| _pattern = pattern.Pattern("<linemarker>", r"^\s*#", value="#") |
There was a problem hiding this comment.
Note that most Fortran compilers don't accept white space in front of the # for preprocessor directives. Are they more forgiving for line markers?
But since fparser allows for white space in front of directives, it's probably wise to allow them for marker as well.
There was a problem hiding this comment.
I actually remember that I was wondering about the \s at the beginning (since I did some testing originally, and knew it's not accepted). But since all regex here started with #\s* .. I just followed the trend :) And I'll agree to leave it in for consistency (and because I am afraid of breaking something).
…er syntax. Extend match function to be able to return the fully matched string.
|
Hi Balthasar, thanks a lot for the quick review, really appreciated.
I am glad that I made the right decision here :)
I tried that, and thanks a lot for the example. Looking at the But then I am stuck with my original problem: I am using: But if I do this, the matching in Basically, the rest of the match is discarded (line number and filename) :( I tried to fix that using: So, I provide This passed nearly all tests, except it would not remove spaces between "#" and line number and line number and file name. Therefore, I adjusted one linemarker test (but the test still checks that leading spaces before the I am not sure if this is the right solution tbh :( Maybe it should not be based on @reuterbal , @arporter , @sergisiso - feedback appreciated! The modification of the |
reuterbal
left a comment
There was a problem hiding this comment.
Nice work, the details of the keyword/value returns were lost on me and your proposal seems sensible to me.
Overall I think that's indeed much better now, no further comments!
arporter
left a comment
There was a problem hiding this comment.
Looks good to me Joerg, thanks. I could do with a bit of help understanding some of the changes - probably because I know pretty much nothing about the Pattern class.
I think it would also be good to test the new functionality in context - i.e. provide some Fortran containing some linemarkers and check that things work OK.
|
|
||
| """C99 Preprocessor Syntax Rules.""" | ||
| """C99 Preprocessor Syntax Rules. It also supports linemarker statements | ||
| (which are technically not preprocessor directives, but are very close |
There was a problem hiding this comment.
Please also update the documentation (https://fparser.readthedocs.io/en/latest/fparser2.html#preprocessing-directives)
There was a problem hiding this comment.
Oops, I missed that, thanks.
|
|
||
| class Cpp_Linemarker_Stmt(WORDClsBase): # Linemarker | ||
| """ | ||
| Linemarker |
There was a problem hiding this comment.
A bit more detail would be good here. i.e. explain what a Linemarker is.
|
|
||
| :param str string: the string to match with as a line statement. | ||
|
|
||
| :return: a tuple of size 1 with the right hand side as a string, \ |
There was a problem hiding this comment.
No line-continuation and please update to use type hints.
| @staticmethod | ||
| def match(string): | ||
| """Implements the matching for a linemarker. | ||
| The right hand side of the directive is not matched any further |
There was a problem hiding this comment.
What does the "right hand side of the directive" mean?
| require_cls=False, | ||
| ) | ||
|
|
||
| def tostr(self): |
| Returns the line marker as string. Note that fparser accepts | ||
| spaces before the `#`, but it should remove the spaces, hence | ||
| we lstrip the result | ||
| :return: this linemarker as a string. |
There was a problem hiding this comment.
Blank line before :return: please.
| return None | ||
| line = string[len(my_match.group()) :] | ||
| pattern_value = keyword.value | ||
| # If no constant return value is defined, |
There was a problem hiding this comment.
I need a bit of help here: what does "no constant return value" mean?
There was a problem hiding this comment.
I've added a longer comment.
…ed when they are in a Fortran program.
…parsed in utils as part of classes, not as part of add_comments_includes_directives anymore).
|
I've resolved Balthasar's comments, and addressed all new issues. Back for next review. |
|
@arporter I just remembered that pylint flagged one issue, which looks wrong, but I just wanted to make sure it's not done on purpose before fixing. In Adding a The original (pre-black) line was: Which comes from Is it ok if I add the missing |
|
Nice catch, that was definitely a mistake when I expanded the test parameterisation. I'd say please add the missing |
|
Thanks for the confirmation. Updated to current master, ready for review. |
Fixes #390 by supporting linemarkers (
# 123 "test.f90"- indicating line number and file).I have used the existing preprocessor directive support, since linemarkers looks pretty much like a preprocessor directive (starting with
#). But I was unable to get it to verify properly out of the box - I feel like the existing implementation assumes that thevalueis constant (e.g.#ifdef), while in the case of a linemarker we don't have this (since it would need the line number and filename to verify correctness, and I couldn't work out how to do this). While it worked this way, it was unable to detect invalid linemarkers (# abc "xx.f90"or# 123 'no_single_quote_allowed.f90'). So, instead I added a simple regex check at the beginning, and only if that passes, it will use the established path.I am happy to reimplement (I know there is
pattern_tools.digit_stringandfile_name... I just couldn't figure out how to plug this all together.