danieldietrich / xtext-protected-regions

Xtext Protected Regions
danieldietrich.net
12 stars 6 forks source link

Java Parser fail #37

Closed ylemoigne closed 12 years ago

ylemoigne commented 12 years ago

Hi, thanks for xtext-protected-regions.

Since I migrate to 2.0.0, the parser fail on this class :

public class StringCodec {
    public final static String encode(String... arStr) {
        StringBuffer sb = new StringBuffer();

        boolean bFirst = true;
        for (String str : arStr) {
            if (!bFirst) {
                sb.append(",");
            }
            if (str == null) {
                str = "null";
            } else {
                str = str.replaceAll("\"", "\\\\\"");
                str = "\"" + str + "\"";
            }
            sb.append(str);
            bFirst = false;
        }
        return sb.toString();
    }

    public final static String[] decode(String str) {
        List<String> lstMatches = new ArrayList<String>();

        // Regexp  : [^\\]?"(.*?[^\\])",?
        // Test sur: "titi","to\"to","ta,ta","te\",\"te",null,"ty\ty"

        Pattern p = Pattern.compile("[^\\\\]?(?:(?:\"(.*?[^\\\\])\")|(?:null)),?");
        Matcher m = p.matcher(str);
        while (m.find()) {
            String strGroup = m.group(1);
            if(strGroup.equals("null")){
                strGroup=null;
            }
            lstMatches.add(strGroup);
        }

        return lstMatches.toArray(new String[lstMatches.size()]);
    }

    public static void main(String[] args) {
        System.out.println(encode("titi", "to\"to", "ta,ta", "te\",\"te",null, "ty\\ty"));
        String[] ar = decode("\"titi\",\"to\\\"to\",\"ta,ta\",\"te\\\",\\\"te\",null,\"ty\\ty\"");
        for(String str:ar){
            System.out.println("   =>"+str);
        }
    }
}
danieldietrich commented 12 years ago

Hi, thank you for your bug-report. I've found the error (string escape sequences are currently not parsed correctly). I work on a fix. Since then your can use ParserFactory.genericParser() instead of the Java parser as temporary workaround. Greets Daniel

danieldietrich commented 12 years ago

Please try the Java parser with the new version. There is now one update site for current releases:

http://danieldietrich.net/protectedregions/updates/releases/current

Old releases are also available under

http://danieldietrich.net/protectedregions/updates/releases/2.0.1 http://danieldietrich.net/protectedregions/updates/releases/2.0.0 http://danieldietrich.net/protectedregions/updates/releases/1.0.1

Greets

Daniel

ylemoigne commented 12 years ago

The new version fixe the problem, thanks :)

danieldietrich commented 12 years ago

Great :)