Avatar billede cvpcvp Nybegynder
08. november 2004 - 13:27 Der er 1 løsning

Eclipse udvikling IFile og error parser

Jeg er ved at lave en error parser til en compiler i Eclipse.

Jeg har kigget lidt i GCC´s eksempel(det ligger til sidst).

Mit problem er at når jeg vil kalde: eoParser.generateMarker(..);

Jeg kan godt kalde denne funktion hvis jeg sætter første parameter til 'null' men lige så snart jeg indsætter en IFile variabel kører det helt i skoven. funktionen bliver ikke længere kaldt. Jeg har prøvet at steppe igennem med debuggeren, og det kan jeg fint når jeg er der står null, men når jeg har indsat IFile variablen bliver funktionen slet ikke kaldt.

jeg generer IFilen med følgende parametre:

IFile file = eoParser.findFilePath("C:\programs\eclipse_3.1M2\runtime-EclipseApplication\test\src\test.c");


Håber i kan hjælpe...




import org.eclipse.cdt.core.ErrorParserManager;
import org.eclipse.cdt.core.IErrorParser;
import org.eclipse.cdt.core.IMarkerGenerator;
import org.eclipse.core.resources.IFile;

public class GCCErrorParser implements IErrorParser {
   
    public boolean processLine(String line, ErrorParserManager eoParser) {
        return processLine(line, eoParser, IMarkerGenerator.SEVERITY_ERROR_RESOURCE);
    }

    public boolean processLine(String line, ErrorParserManager eoParser, int inheritedSeverity) {
        // Known patterns.
        // (a)
        // filename:lineno: description
        //
        // (b)
        // filename:lineno:column: description
        //
        // (c)
        // In file included from b.h:2,
        //                from a.h:3,
        //                from hello.c:3:
        // c.h:2:15: missing ')' in macro parameter list
        //
        // (d)
        // In file included from hello.c:3:
        // c.h:2:15: missing ')' in macro parameter list
        //
        // (e)
        // h.c: In function `main':
        // h.c:41: `foo' undeclared (first use in this function)
        // h.c:41: (Each undeclared identifier is reported only once
        // h.c:41: for each function it appears in.)
        // h.c:41: parse error before `char'
        // h.c:75: `p' undeclared (first use in this function)

        int firstColon = line.indexOf(':');

        /* Guard against drive in Windows platform.  */
        if (firstColon == 1) {
            try {
                String os = System.getProperty("os.name"); //$NON-NLS-1$
                if (os != null && os.startsWith("Win")) { //$NON-NLS-1$
                    try {
                        if (Character.isLetter(line.charAt(0))) {
                            firstColon = line.indexOf(':', 2);
                        }
                    } catch (StringIndexOutOfBoundsException e) {
                    }
                }
            } catch (SecurityException e) {
            }
        }

        if (firstColon != -1) {
            try {
                int secondColon = -1;
                int    num  = -1;

                while ((secondColon = line.indexOf(':', firstColon + 1)) != -1) {
                    String lineNumber = line.substring(firstColon + 1, secondColon);
                    try {
                        num = Integer.parseInt(lineNumber);
                    } catch (NumberFormatException e) {
                        // Failed.
                    }
                    if (num != -1) {
                        break; // Find possible match.
                    }
                    firstColon = secondColon;
                }

                if (secondColon != -1) {
                    int col = -1;

                    String fileName = line.substring(0, firstColon);
                    String varName = null;
                    String desc = line.substring(secondColon + 1).trim();
                    /* Then check for the column  */
                    int thirdColon= line.indexOf(':', secondColon + 1);
                    if (thirdColon != -1) {
                        String columnNumber = line.substring(secondColon + 1, thirdColon);
                        try {
                            col = Integer.parseInt(columnNumber);
                        } catch (NumberFormatException e) {
                        }
                    }
                    if (col != -1) {
                        desc = line.substring(thirdColon + 1).trim();
                    }

                    // gnu c: filename:no: (Each undeclared identifier is reported
                    // only once. filename:no: for each function it appears in.)
                    if (desc.startsWith ("(Each undeclared")) { //$NON-NLS-1$
                        // Do nothing.
                        return false;
                    } else  {
                        String previous = eoParser.getPreviousLine();
                        if (desc.endsWith(")") //$NON-NLS-1$
                            && previous.indexOf("(Each undeclared") >= 0 ) { //$NON-NLS-1$
                            // Do nothing.
                            return false;
                        }
                    }

                    /* See if we can get a var name
                    * Look for:
                    * `foo' undeclared
                    * `foo' defined but not used
                    * conflicting types for `foo'
                    * previous declaration of `foo'
                    * parse error before `foo'
                    *
                    */
                    int s;
                    if((s = desc.indexOf("\' undeclared")) != -1) { //$NON-NLS-1$
                        int p = desc.indexOf("`"); //$NON-NLS-1$
                        if (p != -1) {
                            varName = desc.substring(p+1, s);
                            //System.out.println("undex varName "+ varName);
                        }
                    } else if((s = desc.indexOf("\' defined but not used")) != -1) { //$NON-NLS-1$
                        int p = desc.indexOf("`"); //$NON-NLS-1$
                        if (p != -1) {
                            varName = desc.substring(p+1, s);
                            //System.out.println("unused varName "+ varName);
                        }
                    } else if((s = desc.indexOf("conflicting types for `")) != -1) { //$NON-NLS-1$
                        int p = desc.indexOf("\'", s); //$NON-NLS-1$
                        if (p != -1) {
                            varName = desc.substring(desc.indexOf("`") + 1, p); //$NON-NLS-1$
                            //System.out.println("confl varName "+ varName);
                        }
                    } else if((s = desc.indexOf("previous declaration of `")) != -1) { //$NON-NLS-1$
                        int p = desc.indexOf("\'", s); //$NON-NLS-1$
                        if (p != -1) {
                            varName = desc.substring(desc.indexOf("`") + 1, p); //$NON-NLS-1$
                            //System.out.println("prev varName "+ varName);
                        }
                    } else if ((s = desc.indexOf("parse error before ")) != -1) { //$NON-NLS-1$
                        int p = desc.indexOf("\'", s); //$NON-NLS-1$
                        if (p != -1) {
                            varName = desc.substring(desc.indexOf("`") + 1, p); //$NON-NLS-1$
                            //System.out.println("prev varName "+ varName);
                        }
                    }

                    /*
                    *    In file included from hello.c:3:
                    *    c.h:2:15: missing ')' in macro parameter list
                    *
                    * We reconstruct the multiline gcc errors to multiple errors:
                    *    c.h:2:15: missing ')' in macro parameter list
                    *    hello.c:3:  in inclusion c.h:2:15
                    *   
                    */
                    if (line.startsWith("In file included from ")) { //$NON-NLS-1$
                        // We want the last error in the chain, so continue.
                        eoParser.appendToScratchBuffer(line);
                        return false;
                    }

                    /*
                    *    In file included from b.h:2,
                    *                    from a.h:3,
                    *                    from hello.c:3:
                    *    c.h:2:15: missing ')' in macro parameter list
                    *
                    * We reconstruct the multiline gcc errors to multiple errors:
                    *    c.h:2:15: missing ')' in macro parameter list
                    *    b.h:2:  in inclusion c.h:3:15
                    *    a.h:3:  in inclusion b.h:2
                    *    hello.c:3:  in inclusion a.h:3
                    *   
                    */
                    if (eoParser.getScratchBuffer().startsWith("In file included from ")) { //$NON-NLS-1$
                        if (line.startsWith("from ")) { //$NON-NLS-1$
                            // We want the last error in the chain, so continue.
                            eoParser.appendToScratchBuffer(line);
                            return false;
                        }
                        String buffer = eoParser.getScratchBuffer();
                        eoParser.clearScratchBuffer();
                        int from = -1;
                        String inclusionError = fileName + ":" + num; //$NON-NLS-1$
                        while ((from = buffer.indexOf("from ")) != -1) { //$NON-NLS-1$
                            int coma = buffer.indexOf(',', from);
                            String buf;
                            if (coma != -1) {
                                buf = buffer.substring(from + 5, coma) + ':';
                                buffer = buffer.substring(coma);
                            } else {
                                buf = buffer.substring(from + 5);
                                buffer = ""; //$NON-NLS-1$
                            }
                            String t = buf;
                            buf += " in inclusion " + inclusionError; //$NON-NLS-1$
                            inclusionError = t;
                            // Call the parsing process again.
                            processLine(buf, eoParser, extractSeverity(desc, inheritedSeverity));
                        }
                    }

                    IFile file = eoParser.findFilePath(fileName);

                    if (file == null) {
                        // Parse the entire project.
                        file = eoParser.findFileName(fileName);
                        if (file != null) {
                            // If there is a conflict set the error on the project.
                            if (eoParser.isConflictingName(fileName)) {
                                desc = "*" + desc; //$NON-NLS-1$
                                file = null;
                            }
                        }
                    }

                    int severity = extractSeverity(desc, inheritedSeverity);
                    if (desc.startsWith("warning") || desc.startsWith("Warning")) { //$NON-NLS-1$ //$NON-NLS-2$
                        // Remove the warning.
                        String d = desc.substring("warning".length()).trim(); //$NON-NLS-1$
                        if (d.startsWith(":")) { //$NON-NLS-1$
                            d = d.substring(1).trim();
                        }

                        if (d.length() != 0) {
                            desc = d;
                        }
                    }
                   
                    // Display the fileName.
                    if (file == null) {
                        desc = desc +"[" + fileName + "]"; //$NON-NLS-1$ //$NON-NLS-2$
                    }

                    eoParser.generateMarker(file, num, desc, severity, varName);
                } else {
                    if (line.startsWith("In file included from ")) { //$NON-NLS-1$
                        eoParser.appendToScratchBuffer(line);
                    } else if (line.startsWith("from ")) { //$NON-NLS-1$
                        eoParser.appendToScratchBuffer(line);
                    }
                }
            } catch (StringIndexOutOfBoundsException e) {
            } catch (NumberFormatException e) {
            }
        }
        return false;
    }
   
    private int extractSeverity(String desc, int defaultSeverity) {
        int severity = defaultSeverity;
        if (desc.startsWith("warning") || desc.startsWith("Warning")) { //$NON-NLS-1$ //$NON-NLS-2$
            severity = IMarkerGenerator.SEVERITY_WARNING;
        }
        return severity;
    }
}
Avatar billede cvpcvp Nybegynder
18. september 2008 - 13:13 #1
lukker
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester