Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pycparser/c_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ def _handle_pppragma(self) -> List[Token]:
_bad_string_literal = '"' + _string_char + "*" + _bad_escape + _string_char + '*"'

# floating constants (K&R2: A.2.5.3)
_floating_suffix_opt = r"([FfLl][iIjJ]?|[iIjJ][FfLl]?)?"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Since we're following the standards now (rather than K&R2), could you ask you to follow the clauses listed in the linked doc?

I.e. break it to floating-suffix, real-floating-suffix, complex-suffix, etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Does this apply to whole floating-literal, or just floating-suffix?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Just the suffix

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I’m unsure whether and how to change the comment “# floating constants (K&R2: A.2.5.3)” in this case.

Another issue: The aforementioned standard also contains decimal literals (suffixes df, dd, dl, DF, DD, DL; orthogonal to complex suffixes). They were added to C23, i.e. before complex literals.

If following standards, it would be consistent to support them in the lexer as well. This could be introduced in the same or a separate commit. Alternatively, I can use the names of the clauses from the standards but omit the decimal suffixes for now.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I’m unsure whether and how to change the comment “# floating constants (K&R2: A.2.5.3)” in this case.

Feel free to omit it or point to the new draft PDF. Unfortunately the link to open-std.org almost always fails to open in my browser; are you aware of an alternative place where we can point?

Another issue: The aforementioned standard also contains decimal literals (suffixes df, dd, dl, DF, DD, DL; orthogonal to complex suffixes). They were added to C23, i.e. before complex literals.

This can be done in either the same PR or a separate PR, whatever you feel is right. My only request is that each PR on its own is self-contained.

_exponent_part = r"""([eE][-+]?[0-9]+)"""
_fractional_constant = r"""([0-9]*\.[0-9]+)|([0-9]+\.)"""
_floating_constant = (
Expand All @@ -546,7 +547,9 @@ def _handle_pppragma(self) -> List[Token]:
+ _exponent_part
+ "?)|([0-9]+"
+ _exponent_part
+ "))[FfLl]?)"
+ "))"
+ _floating_suffix_opt
+ ")"
)
_binary_exponent_part = r"""([pP][+-]?[0-9]+)"""
_hex_fractional_constant = (
Expand All @@ -561,7 +564,8 @@ def _handle_pppragma(self) -> List[Token]:
+ _hex_fractional_constant
+ ")"
+ _binary_exponent_part
+ "[FfLl]?)"
+ _floating_suffix_opt
+ ")"
)


Expand Down
15 changes: 15 additions & 0 deletions tests/test_c_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ def test_hexadecimal_floating_constants(self):
self.assertTokensTypes("0x.488641p0", ["HEX_FLOAT_CONST"])
self.assertTokensTypes("0X12.P0", ["HEX_FLOAT_CONST"])

def test_complex_constants(self):
self.assertTokensTypes("1.2i", ["FLOAT_CONST"])
self.assertTokensTypes("1.2I", ["FLOAT_CONST"])
self.assertTokensTypes("1.2j", ["FLOAT_CONST"])
self.assertTokensTypes("1.2J", ["FLOAT_CONST"])
self.assertTokensTypes("1.2fi", ["FLOAT_CONST"])
self.assertTokensTypes("1.2if", ["FLOAT_CONST"])

self.assertTokensTypes("0x1.2p3i", ["HEX_FLOAT_CONST"])
self.assertTokensTypes("0x1.2p3I", ["HEX_FLOAT_CONST"])
self.assertTokensTypes("0x1.2p3j", ["HEX_FLOAT_CONST"])
self.assertTokensTypes("0x1.2p3J", ["HEX_FLOAT_CONST"])
self.assertTokensTypes("0x1.2p3fi", ["HEX_FLOAT_CONST"])
self.assertTokensTypes("0x1.2p3if", ["HEX_FLOAT_CONST"])

def test_char_constants(self):
self.assertTokensTypes(r"""'x'""", ["CHAR_CONST"])
self.assertTokensTypes(r"""L'x'""", ["WCHAR_CONST"])
Expand Down