git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] userdiff: tighten cpp word regex

From
Johannes Sixt via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 7, 2021, 06:50 UTC
Message-ID
<a47ab9ba20ec931a9d7a844633319c486dd699a1.1633589461.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1054.git.1633589461.gitgitgadget@gmail.com>
From: Johannes Sixt <j6t@kdbg.org>

Generally, word regex can be written such that they match tokens liberally and need not model the actual syntax because it can be assumed that the regex will only be applied to syntactically correct text.

The regex for cpp (C/C++) is too liberal, though. It regards these sequences as single tokens:

   1+2
   1.5-e+2+f
and the following amalgams as one token:
   .l      as in str.length
   .f      as in str.find
Tighten the regex in the following way:
- Accept + and - only in one position in the exponent. + and - are no
  longer regarded as the sign of a number and are treated by the
  catcher-all that is not visible in the driver's regex.
- Accept a leading decimal point only when it is followed by a digit.
For readability, factor hex- and binary numbers into an own term.

As a drive-by, this fixes that floatingpoint numbers such as 12E5 (with upper-case E) were split into two tokens.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
 userdiff.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/userdiff.c b/userdiff.c
index d9b2ba752f0..ce2a9230703 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -54,8 +54,14 @@ PATTERNS("cpp",
 	 /* functions/methods, variables, and compounds at top level */
 	 "^((::[[:space:]]*)?[A-Za-z_].*)$",
 	 /* -- */
+	 /* identifiers and keywords */
 	 "[a-zA-Z_][a-zA-Z0-9_]*"
-	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
+	 /* decimal and octal integers as well as floatingpoint numbers */
+	 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
+	 /* hexadecimal and binary integers */
+	 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
+	 /* floatingpoint numbers that begin with a decimal point */
+	 "|\\.[0-9]+([Ee][-+]?[0-9]+)?[fFlL]?"
 	 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
 PATTERNS("csharp",
 	 /* Keywords */
-- 
gitgitgadget
Previous: Johannes Sixt via GitGitGadgetNext: Johannes Sixt via GitGitGadget
Message 2 of 24 in “Fun with cpp word regex”
  1. 0/3 Fun with cpp word regexJohannes Sixt via GitGitGadget, Oct 7, 2021
  2. 1/3 userdiff: tighten cpp word regexJohannes Sixt via GitGitGadget, Oct 7, 2021
  3. 2/3 userdiff: permit the digit-separating single-quote in numbersJohannes Sixt via GitGitGadget, Oct 7, 2021
  4. 3/3 userdiff: learn the C++ spaceship operatorJohannes Sixt via GitGitGadget, Oct 7, 2021
  5. Ævar Arnfjörð BjarmasonOct 7, 2021
  6. Johannes SixtOct 7, 2021
  7. 0/5 Fun with cpp word regexJohannes Sixt via GitGitGadget, Oct 8, 2021
  8. 1/5 t4034/cpp: actually test that operator tokens are not splitJohannes Sixt via GitGitGadget, Oct 8, 2021
  9. 2/5 t4034: add tests showing problematic cpp tokenizationsJohannes Sixt via GitGitGadget, Oct 8, 2021
  10. 3/5 userdiff-cpp: tighten word regexJohannes Sixt via GitGitGadget, Oct 8, 2021
  11. 4/5 userdiff-cpp: permit the digit-separating single-quote in numbersJohannes Sixt via GitGitGadget, Oct 8, 2021
  12. 5/5 userdiff-cpp: learn the C++ spaceship operatorJohannes Sixt via GitGitGadget, Oct 8, 2021
  13. Ævar Arnfjörð BjarmasonOct 8, 2021
  14. Johannes SixtOct 8, 2021
  15. Ævar Arnfjörð BjarmasonOct 9, 2021
  16. Johannes SixtOct 10, 2021
  17. 0/6 Fun with cpp word regexJohannes Sixt via GitGitGadget, Oct 10, 2021
  18. 1/6 t4034/cpp: actually test that operator tokens are not splitJohannes Sixt via GitGitGadget, Oct 10, 2021
  19. 2/6 t4034: add tests showing problematic cpp tokenizationsJohannes Sixt via GitGitGadget, Oct 10, 2021
  20. 4/6 userdiff-cpp: prepare test cases with yet unsupported featuresJohannes Sixt via GitGitGadget, Oct 10, 2021
  21. 3/6 userdiff-cpp: tighten word regexJohannes Sixt via GitGitGadget, Oct 10, 2021
  22. 5/6 userdiff-cpp: permit the digit-separating single-quote in numbersJohannes Sixt via GitGitGadget, Oct 10, 2021
  23. 6/6 userdiff-cpp: learn the C++ spaceship operatorJohannes Sixt via GitGitGadget, Oct 10, 2021
  24. 7/6 userdiff-cpp: back out the digit-separators in numbersJohannes Sixt, Oct 24, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.