Skip to content

// within string #23

Open
Open

Description

@jbfaden

"//" within a string isn't handled properly, as if I do some pre-processing which doesn't consider the case:

String vapurl= "https://github.com/autoplot/dev/blob/master/demos/2024/20240907/kodalith.vap";

and returns

vapurl = 'https://github.com/autoplot/dev/blob/master/demos/2024/20240907/kodalith.vap'
#github.com/autoplot/dev/blob/master/demos/2024/20240907/kodalith.vap";

This needs to be explored. Here's the entire Java code:

package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import org.autoplot.ApplicationModel;
import org.autoplot.datasource.DataSetURI;
import org.autoplot.dom.Application;
import org.das2.graph.DasCanvas;
import org.das2.util.monitor.NullProgressMonitor;

/**
 * The SimpleServlet isn't resolving PWD references.
 * @author jbf
 */
public class TestServletPWD {
    public static void main( String[] args ) throws IOException {
        String vapurl= "https://github.com/autoplot/dev/blob/master/demos/2024/20240907/kodalith.vap";
        File openable = DataSetURI.getFile(vapurl,new NullProgressMonitor());
        ApplicationModel appmodel = new ApplicationModel();
        appmodel.addDasPeersToAppAndWait();
        LinkedHashMap deltas = new LinkedHashMap<>();
        //deltas.put("PWD", "https://github.com/autoplot/dev/blob/master/demos/2024/20240907/");
        appmodel.doOpenVap( openable, deltas );
        
        Application dom= appmodel.getDom();
        int width = appmodel.getDom().getCanvases(0).getWidth();
        int height = appmodel.getDom().getCanvases(0).getHeight();
        DasCanvas c = dom.getController().getCanvas().getController().getDasCanvas();
        c.prepareForOutput(width, height);

        dom.getController().waitUntilIdle();

        File fout= new File("/tmp/ap/kodalith.vap.png");
        fout.getParentFile().mkdirs();
        FileOutputStream fos= new FileOutputStream(fout);
        
        appmodel.getCanvas().writeToPng( fos, width, height );
        fos.close();
    }
}

Activity

  1. jbfaden commented on May 12, 2025

    @jbfaden
    OwnerAuthor

    I ran into this again.

  2. fadend commented on May 12, 2025

    @fadend
    Contributor

    So, the "//" in the URL is getting interpreted as a comment but the processing of the String literal is also working? Interesting that a single line is yielding two output lines. Weird that JavaParser is even getting to see the "//" as a comment... I'd expect its string literal parsing to take care of skipping over that.

    I've been trying to reproduce the issue in unit tests but haven't succeeded so far. "Unfortunately", the following both pass:

    % git diff
    diff --git a/src/test/java/com/cottagesystems/convert/ConvertJavaToPythonTest.java b/src/test/java/com/cottagesystems/convert/ConvertJavaToPythonTest.java
    index e79129e..9cb3fbb 100644
    --- a/src/test/java/com/cottagesystems/convert/ConvertJavaToPythonTest.java
    +++ b/src/test/java/com/cottagesystems/convert/ConvertJavaToPythonTest.java
    @@ -32,6 +32,11 @@ class ConvertJavaToPythonTest {
             assertEquals("# bye ", converter.doConvert("/* bye */").replace("\n", ""));
         }
     
    +    @Test
    +    void testSlashSlashInString() throws ParseException {
    +        assertEquals("x = 'https://autoplog.org/'", converter.doConvert("String x = \"https://autoplog.org/\";"));
    +    }
    +
         @Test
         void testArithmeticOps() throws ParseException {
             assertEquals("-x * y + z / 2 - 5", converter.doConvert("-x*y+z/2-5"));
    @@ -119,5 +124,24 @@ class ConvertJavaToPythonTest {
                 "    return result");
             assertLinesMatch(expectedPython, Arrays.asList(converter.doConvert(javaProgram).split("\n+")));
         }
    +
    +    @Test
    +    void testFullClassWithSlashSlashInString() throws ParseException {
    +        String javaProgram = "package test;\n" +
    +        "public class TestServletPWD {\n" +
    +        "  public static void main( String[] args ) {\n" +
    +        "    String vapurl= \"https://github.com/autoplot/dev/blob/master/demos/2024/20240907/kodalith.vap\";\n" +
    +        "    System.out.println(vapurl);\n" +
    +        "  }\n" +
    +        "}";
    +        // Currently strips the class definition
    +        List<String> expectedPython = Arrays.asList("",
    +            "def main(args):",
    +            "    vapurl = 'https://github.com/autoplot/dev/blob/master/demos/2024/20240907/kodalith.vap'",
    +            "    print(vapurl)",
    +            "if __name__ == '__main__':",
    +            "    main([])");
    +        assertLinesMatch(expectedPython, Arrays.asList(converter.doConvert(javaProgram).split("\n+")));
    +    }
     }
    
  3. jbfaden commented on May 13, 2025

    @jbfaden
    OwnerAuthor

    Indeed--the test code (containing "kodalith") above translates fine. Now I wish I could remember what I was translating yesterday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions