[jira] [Created] (GROOVY-8546) Parrot Parser: multiple Reader instances opened from SourceUnit; many left open

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Created] (GROOVY-8546) Parrot Parser: multiple Reader instances opened from SourceUnit; many left open

JIRA jira@apache.org
Eric Milles created GROOVY-8546:
-----------------------------------

             Summary: Parrot Parser: multiple Reader instances opened from SourceUnit; many left open
                 Key: GROOVY-8546
                 URL: https://issues.apache.org/jira/browse/GROOVY-8546
             Project: Groovy
          Issue Type: Improvement
          Components: parser
            Reporter: Eric Milles


{{Antlr4ParserPlugin}} makes very inefficient use of {{ReaderSource}}/{{SourceUnit}}.  {{parseCST}} is passed a {{Reader}} with the idea that it is the source of source character data and is closed from outside.  It ignores this and mines the passed {{SourceUnit}}, which seems unnecessary since the same reference is passed again in {{buildAST}}.  Both {{parseCST}} and {{buildAST}} call {{getReader}}, which opens a buffered reader on the source unit file and never closes them.  Lastly, {{AstBuilder}} calls {{getReader}} as well to create a {{CharStream}} and never closes this reader.

Doing this in a long-running process (like an IDE) is causing numerous problems due to open file handles.

{code:java}
public class Antlr4ParserPlugin implements ParserPlugin {
    private ReaderSource readerSource;

    @Override
    public Reduction parseCST(SourceUnit sourceUnit, java.io.Reader reader) throws CompilationFailedException {
        try {
            ReaderSource readerSource = sourceUnit.getSource();
            if (null != readerSource && null != readerSource.getReader()) {
                this.readerSource = readerSource;
            } else {
                this.readerSource = new StringReaderSource(IOGroovyMethods.getText(reader), sourceUnit.getConfiguration());
            }
        } catch (IOException e) {
            throw new GroovyBugError("Failed to create StringReaderSource instance", e);
        }

        return null;
    }

    @Override
    public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduction cst) throws ParserException {
        try {
            ReaderSource readerSource = sourceUnit.getSource();
            if (null == readerSource || null == readerSource.getReader()) {
                sourceUnit.setSource(this.readerSource);
            }
        } catch (IOException e) {
            sourceUnit.setSource(this.readerSource);
        }

        AstBuilder builder = new AstBuilder(sourceUnit);

        return builder.buildAST();
    }
}
{code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)