Project

General

Profile

Bug #10028

FWD needs full support for unicode/octal escape sequences

Added by Octavian Adrian Gavril over 1 year ago. Updated about 7 hours ago.

Status:
Review
Priority:
Normal
Target version:
-
Start date:
Due date:
% Done:

100%

billable:
No
vendor_id:
GCD
case_num:
version_reported:
version_resolved:
reviewer:
production:
No
env_name:
topics:
4GL Preprocessor

Related issues

Related to Database - Bug #9969: Parsing query string error with \r character. WIP
Related to Conversion Tools - Feature #3971: add support for unicode escape sequences in the preprocessor WIP
Related to Conversion Tools - Feature #4389: escape support for supplementary unicode characters New
Related to Conversion Tools - Bug #5399: schema parsing failure when a string literal ends in a tilde New

History

#2 Updated by Octavian Adrian Gavril over 1 year ago

  • Related to Bug #9969: Parsing query string error with \r character. added

#3 Updated by Constantin Asofiei over 1 year ago

A thought about the case like define variable ch as character initial "~000doesntmatter":U no-undo..

In FWD, the character(String) constructor does javaSpacifyNull. I don't think we should do this at parse time with the literal - we should just escape the null char and let it emit as it was in the 4GL code.

This also helps the anti-parser to emit the similar 4GL code as the source.

#4 Updated by Greg Shah over 1 year ago

Agreed.

#5 Updated by Octavian Adrian Gavril over 1 year ago

I finished preprocessor testcases for printable ASCII characters (octal representation). I continue with code page-dependent characters and representative Unicode cases.

#6 Updated by Octavian Adrian Gavril 4 months ago

I’ve created branch 10028a to continue working on this. From what I recall, there are two sets of tests that I created for the testcases project: control_characters.xml and printableASCIICharacters.xml. I will start by getting these fixed first. Currently, both test sets are failing.

#7 Updated by Octavian Adrian Gavril 4 months ago

Some issues with the control_characters.p test set involved two aspects regarding how the null character (~000) is handled.

First, the current implementation calculates octal sequences and returns the results directly. For the NULL character, this resulted in returning a 0 character, which breaks the string. To match the OE behavior, we now return a new line when the octal result is zero.

The second issue affecting NULL character handling was the progressSpacifyNull method. This method is triggered by the STRING rule in text.g. While the logic was correct, it correctly transformed the input "This has a ~000 (null) char." into "This has a ", the entire process is occurring at runtime in OE. The actual OE preprocessor output is "This has a ~000 (null) char.". Therefore, I moved the progressSpacifyNull method call from the STRING rule to the progressToJavaString method, ensuring it executes before the string input is processed.

Another issue causing the control_characters.p test set to fail is how the \t character is handled. The current implementation replaces the tab character with spaces, and I am not entirely sure why this is necessary. The following method is responsible:

   /**
     * Expands tabs with spaces right in the ANTLRStringBuilder text.
     * This version is used here because according to experiments,
     * the tabs expansion for the preprocessor variables is immediate.
     */
   public void tab()
   {
      int c  = getColumn();
      int nc = (((c-1)/tabsize) + 1) * tabsize + 1;  // calculate tab stop
      setColumn(nc);

      text.setLength(text.length() - 1);    // replace '\t' in the buffer
      for (; c < nc; c ++)                  // add more spaces
         text.append(' ');
   }

While I understand the first part, calculating the new column number based on the tab size, it is confusing why we are replacing the tab with literal space characters.
If I remove that replacement logic from the tab method and apply the changes mentioned above for null characters, the control_characters.p tests pass.

#8 Updated by Octavian Adrian Gavril 4 months ago

I've committed new changes that fix all cases from printableASCIICharacters.p. These include:
  • removing the tab replacement with white spaces.
  • moving the progressSpacifyNull method call from STRING to progressToJavaString.
  • printing translated octal sequences if needed.
  • handling the octal sequences inside the CODE rule:
    • if we are at the beginning, we consume the octal sequence only:
           message ~042 test! ". // message " test! ".
           // visible preprocessor output should be: message ~042" test! ".
           // where ~042 is CODE and " is starting a STRING.      
      
    • if we are already inside a CODE rule processing, we consume the octal sequence and continue the CODE rule without the escaped character:
          def frame a~042. // def frame a".
          // visible preprocessor output should be: def frame a~042.
          // where a~042. is CODE.
      

The changes are committed in 10028a/16654.

#9 Updated by Octavian Adrian Gavril 4 months ago

I added printableASCIICharacters.p to the test plan, fixed the include file path from tests and adjusted the baseline output for the tests. All of these are committed in testcases/1856.

#10 Updated by Octavian Adrian Gavril 4 months ago

I added unicode_escape_sequences.p to cover several cases involving unicode sequences in different contexts. Based on these and the cases related to octal sequences, I'm preparing a solution that uses the source-charset parameter to handle escape sequences when needed. The latest tests and changes have been committed to testcases/1857.

#11 Updated by Octavian Adrian Gavril 11 days ago

Rebased branch 10028a with trunk/16843. The new revision is 16844.

#12 Updated by Octavian Adrian Gavril 5 days ago

  • Status changed from WIP to Review
  • % Done changed from 0 to 100
  • reviewer Greg Shah added

10028a/16845 makes FWD handle 4GL octal and Unicode escapes as OpenEdge 11.6 does, in the preprocessor, the converted code and the runtime. Every rule was measured on OpenEdge.

The main changes are these:
  • Octal runs and ~u/~U escapes decode in the code page, so ~303~251 is one é under UTF-8, an unrepresentable ~u keeps its text and a null is spacified only at runtime (#10028-3).
  • Escapes decode with CPINTERNAL, not the source code page (#10678), through a new p2j.cfg.xml parameter cpinternal. Without it nothing changes for existing projects.
  • &IF, ASC, MATCHES, BEGINS, WHERE literals and translation phrases use the same decoding.
  • A .df string keeps its stored text, since OpenEdge processes no escape there (#3028).
  • Runtime query values decode with the session CPINTERNAL and a null ends the value (#11597). A bare CR in a string is kept (#9969) and unix escapes are set per file (#9967).
  • Supplementary characters count as one in LENGTH, SUBSTRING and INDEX and keep their case, which also covers #4389 and #7770.
  • The -keeptildes listing matches OpenEdge byte by byte.

The tests are in testcases/1996 and testcases/1998 (15 escape_* preprocessor tests, EscapeLiterals.cls, TestPrepareEscapes.cls), plus JUnit tests in the branch for the UTF-8 cases. Both ABLUnit classes pass. The preprocessor targets need 11747a for -charset, so I ran the harness on 11747a/16794 with 10028a applied (conflicts resolved by hand, 11747a's octal bound removed): 131 of 161 pass against 119 on 11747a/16794 alone. 13 escape and octal tests pass only with 10028a.

The escape tests that still fail are control_characters, unicode_escape_sequences and tab_from_escape, which need #11837 (TABs from escapes), and the define_* tests, which fail on 11747a alone too. escape_unix_escapes_off fails because the targets always pass -unixescapes, and escape_include_charset only on the line break FWD adds at an include end.

Merging will conflict mainly with 11747a (ClearStream, Preprocessor), and in small blocks with 11837a, 3881b, 11741a, 10804b and 7169a. With 11747a, its U+D800 marker also makes a supplementary ~U escape in code fail under UTF-8, which whoever merges second has to resolve.

Text.progressSpacifyNull has no caller left. Greg, can it be removed?

Greg, please review 10028a/16845.

#13 Updated by Octavian Adrian Gavril 5 days ago

  • Related to Feature #3971: add support for unicode escape sequences in the preprocessor added

#15 Updated by Octavian Adrian Gavril 5 days ago

  • Related to Feature #4389: escape support for supplementary unicode characters added

#16 Updated by Octavian Adrian Gavril 5 days ago

  • Related to Bug #5399: schema parsing failure when a string literal ends in a tilde added

#17 Updated by Octavian Adrian Gavril 5 days ago

  • % Done changed from 100 to 90
  • Status changed from Review to WIP

10028a/16845 also covers the following tasks, which can be closed once it is merged into trunk.

  • #3971: an escape with a payload which is not hex, such as ~uxxxx, made the converted Java fail to compile ("illegal unicode escape"). It now converts and compiles, and ~u and ~U escapes decode in the code page.
  • #4389: trunk already converts ~UXXXXXX since 4069a/11450. 10028a adds the runtime part: a supplementary character counts as one character in LENGTH, SUBSTRING and INDEX and keeps its case in CAPS and LC.
  • #7770: ~u00bf decoded with the source code page. It now decodes with CPINTERNAL, through the new p2j.cfg.xml parameter cpinternal, as the task asks.
  • #9969: since #11597 (trunk/16713) the query no longer fails with error 133, but the CR is silently dropped from the quoted value, so the query finds no record or the wrong one. 10028a keeps the CR, and the query finds the same record as OpenEdge.
  • #10678: under a single-byte CPINTERNAL, ~u2591 became the character ░. It now keeps its text ~u2591, as in OpenEdge.
  • #3028: a .df string with a backslash before a doubled quote failed to import when unix escapes are on. A .df string now keeps its stored text, since OpenEdge processes no escape there.
  • #5399: a .df string ending in a tilde took the closing quote as escaped and failed to import. It now imports with the tilde as part of the text.

I found one more case where 10028a/16845 differs from OpenEdge: a ~u or ~U escape whose payload takes the closing quote of a string. I am moving this task back to WIP, and I will present the fix once it is ready.

#18 Updated by Octavian Adrian Gavril about 7 hours ago

  • % Done changed from 90 to 100
  • Status changed from WIP to Review
  • topics 4GL Preprocessor added

10028a/16846 fixes the case found in #10028-17 and makes FWD translate escapes inside include braces as OpenEdge does. Every rule was measured on OpenEdge.

The main changes are these:
  • A ~u escape takes the next 4 characters as its payload and ~U the next 6, whatever they are: a quote, a doubled quote, a line break or a TAB. So message "~unix". now fails as in OpenEdge, because the payload takes the closing quote.
  • The payload value is read as C strtol reads a hex number: leading white space and an 0x prefix are skipped, and no digit gives a null. This holds in strings, in code, in &IF and in runtime queries.
  • A line break taken by a payload in code no longer moves the following lines, so {&LINE-NUMBER} and the error lines match the source.
  • An escape inside braces is translated before the braces are read, in argument values and names, reference names and include file names, also inside a string. A translated space splits the arguments, a translated } closes the reference and a null ends the argument list. The -keeptildes listing shows only the translation, as the OpenEdge listing does.
  • The literal scan of a runtime query skips an escape as one unit, as the lexer does, so '~u41'. x' and 'a~~' end where OpenEdge ends them. With unix escapes, ~\ gives one backslash.
  • The first-level query cache is split by CPINTERNAL, because the literal values it keeps are decoded with it.

The tests are in testcases/2028 (3 new escape_braces_* preprocessor tests and new cases in EscapeLiterals.cls and TestPrepareEscapes.cls), plus JUnit tests in the branch. Both ABLUnit classes pass (35 tests). The preprocessor harness passes 112 of 164 tests against 109 on 10028a/16845, and the difference is the 3 new escape_braces_* tests.

When 10028a and 11747a are merged, the keepTildes reads in ClearStream.translateUnicode have to become keepTildes() as in 11747a/16733, so that a define value is listed as OpenEdge lists it under -keeptildes.

Greg, please review 10028a/16846.

Also available in: Atom PDF