Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Sep 21, 2024
1 parent 856c2a5 commit f4196bb
Show file tree
Hide file tree
Showing 29 changed files with 87 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void testMatchImports() throws Exception {
+ "from a.b.c.d import *\n" //rename a.b.c with wild import
+ "");
IPythonNature nature = new PythonNatureStub();
ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, nature));
ParseOutput obj = PyParser.parseFull(new PyParser.ParserInfo(doc, nature));
SourceModule module = (SourceModule) AbstractModule.createModule((SimpleNode) obj.ast, null, "z", null);

MatchImportsVisitor visitor = new MatchImportsVisitor(nature, "a.b.c", module, null);
Expand All @@ -44,7 +44,7 @@ public void testMatchImports2() throws Exception {
+ "import a.b\n"
+ "");
IPythonNature nature = new PythonNatureStub();
ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, nature));
ParseOutput obj = PyParser.parseFull(new PyParser.ParserInfo(doc, nature));
SourceModule module = (SourceModule) AbstractModule.createModule((SimpleNode) obj.ast, null, "z", null);

MatchImportsVisitor visitor = new MatchImportsVisitor(nature, "a.b.c", module, null);
Expand All @@ -68,7 +68,7 @@ public int getGrammarVersion() {
return IPythonNature.GRAMMAR_PYTHON_VERSION_3_5;
}
};
ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, nature));
ParseOutput obj = PyParser.parseFull(new PyParser.ParserInfo(doc, nature));
SourceModule module = (SourceModule) AbstractModule.createModule((SimpleNode) obj.ast, null, "a.g", null);

MatchImportsVisitor visitor = new MatchImportsVisitor(nature, "a.b.c", module, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.python.pydev.parser.jython.ast.argumentsType;
import org.python.pydev.parser.jython.ast.exprType;
import org.python.pydev.parser.jython.ast.factory.AdapterPrefs;
import org.python.pydev.parser.prettyprinterv2.Rewriter;
import org.python.pydev.shared_core.string.StringUtils;

public class FunctionArgAdapter extends AbstractNodeAdapter<argumentsType> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package org.python.pydev.ast.adapters.visitors;

import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.python.pydev.ast.adapters.AbstractNodeAdapter;
import org.python.pydev.ast.adapters.AbstractScopeNode;
import org.python.pydev.ast.adapters.ModuleAdapter;
import org.python.pydev.ast.adapters.context.AbstractContextVisitor;
import org.python.pydev.core.IGrammarVersionProvider;
import org.python.pydev.core.MisconfigurationException;
import org.python.pydev.parser.PyParser;
import org.python.pydev.parser.PyParser.ParserInfo;
import org.python.pydev.parser.jython.ParseException;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.parser.jython.TokenMgrError;
import org.python.pydev.parser.jython.ast.Module;
import org.python.pydev.parser.jython.ast.VisitorIF;
import org.python.pydev.parser.jython.ast.exprType;
import org.python.pydev.shared_core.parsing.BaseParser.ParseOutput;
import org.python.pydev.shared_core.string.ICoreTextSelection;

public class VisitorFactory {
public final class VisitorFactory {

private VisitorFactory() {
}

/**
* Unchecked (because if doing Class.cast, it does not work in java 1.4)
Expand Down Expand Up @@ -62,41 +56,4 @@ public static <T extends VisitorIF> T createVisitor(Class<T> visitorClass, Simpl
return visitor;
}

public static <T extends VisitorIF> T createVisitor(Class<T> visitorClass, String source,
IGrammarVersionProvider versionProvider) throws Throwable {
return createVisitor(visitorClass, VisitorFactory.getRootNodeFromString(source, versionProvider));
}

public static Module getRootNode(IDocument doc, IGrammarVersionProvider versionProvider) throws ParseException,
MisconfigurationException {
ParseOutput objects = PyParser.reparseDocument(new PyParser.ParserInfo(doc, versionProvider));
Throwable exception = objects.error;

if (exception != null) {
/* We try to get rid of the 'Throwable' exception, if possible */
if (exception instanceof ParseException) {
throw (ParseException) exception;
} else if (exception instanceof TokenMgrError) {
/* Error from Lexer */
throw new ParseException(exception.toString());
} else {
throw new RuntimeException(exception);
}
}

if (objects.error != null) {
throw new RuntimeException(objects.error);
}
return (Module) objects.ast;
}

public static IDocument getDocumentFromString(String source) {
return new Document(source);
}

public static SimpleNode getRootNodeFromString(String source, IGrammarVersionProvider versionProvider)
throws ParseException, MisconfigurationException {
return getRootNode(getDocumentFromString(source), versionProvider);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ public TokensList getCompletionsForToken(IDocument doc, ICompletionState state)
TokensList completionsForModule;
try {
ParseOutput obj = PyParser
.reparseDocument(new PyParser.ParserInfo(doc, state.getNature()));
.parseFull(new PyParser.ParserInfo(doc, state.getNature()));
SimpleNode n = (SimpleNode) obj.ast;
IModule module = AbstractModule.createModule(n, state.getNature());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ public AdditionalGrammarVersionsToCheck getAdditionalGrammarVersions()
return null;
}
};
ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, provider,
ParseOutput obj = PyParser.parseFull(new PyParser.ParserInfo(doc, provider,
"_django_manager_body", managerBody));
Module ast2 = (Module) obj.ast;
newBody = ast2.body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public AdditionalGrammarVersionsToCheck getAdditionalGrammarVersions()
return null;
}
};
ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, provider,
ParseOutput obj = PyParser.parseFull(new PyParser.ParserInfo(doc, provider,
finalName, predefinedModule));
if (obj.error != null) {
if (lastModified == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,12 @@ public static SourceModule createModuleFromDoc(String name, File f, IDocument do

if (f != null) {
if (!checkForPath || PythonPathHelper.isValidSourceFile(f.getName())) {
ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, grammarVersionProvider, name,
ParseOutput obj = PyParser.parseFull(new PyParser.ParserInfo(doc, grammarVersionProvider, name,
f));
return new SourceModule(name, f, (SimpleNode) obj.ast, obj.error, nature);
}
} else {
ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, grammarVersionProvider, name, f));
ParseOutput obj = PyParser.parseFull(new PyParser.ParserInfo(doc, grammarVersionProvider, name, f));
return new SourceModule(name, f, (SimpleNode) obj.ast, obj.error, nature);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension4;
import org.eclipse.jface.text.IRegion;
Expand Down Expand Up @@ -82,7 +83,7 @@
*/

@SuppressWarnings("restriction")
public class PyParser extends BaseParser implements IPyParser {
public final class PyParser extends BaseParser implements IPyParser {

/**
* Just for tests: show whenever we're not able to parse some file.
Expand Down Expand Up @@ -291,7 +292,7 @@ public ParseOutput reparseDocument(Object... argsToReparse) {
version = IGrammarVersionProvider.LATEST_GRAMMAR_PY3_VERSION;
}
long documentTime = System.currentTimeMillis();
ParseOutput obj = reparseDocument(new ParserInfo(document, version, true, additionalGrammarsToCheck));
ParseOutput obj = parseFull(new ParserInfo(document, version, true, additionalGrammarsToCheck));

IFile original = null;
IAdaptable adaptable = null;
Expand Down Expand Up @@ -367,6 +368,34 @@ public ParseOutput reparseDocument(Object... argsToReparse) {

//static methods that can be used to get the ast (and error if any) --------------------------------------

public static SimpleNode parseSimple(String source, IGrammarVersionProvider versionProvider)
throws ParseException, MisconfigurationException {
return parseSimple(new Document(source), versionProvider);
}

public static Module parseSimple(IDocument doc, IGrammarVersionProvider versionProvider) throws ParseException,
MisconfigurationException {
ParseOutput objects = parseFull(new ParserInfo(doc, versionProvider));
Throwable exception = objects.error;

if (exception != null) {
/* We try to get rid of the 'Throwable' exception, if possible */
if (exception instanceof ParseException) {
throw (ParseException) exception;
} else if (exception instanceof TokenMgrError) {
/* Error from Lexer */
throw new ParseException(exception.toString());
} else {
throw new RuntimeException(exception);
}
}

if (objects.error != null) {
throw new RuntimeException(objects.error);
}
return (Module) objects.ast;
}

public final static class ParserInfo implements IGrammarVersionProvider {
public IDocument document;

Expand Down Expand Up @@ -550,7 +579,7 @@ public static Tuple<SimpleNode, IGrammar> reparseDocumentInternal(IDocument doc,
* @return a tuple with the SimpleNode root(if parsed) and the error (if any).
* if we are able to recover from a reparse, we have both, the root and the error.
*/
public static ParseOutput reparseDocument(ParserInfo info) {
public static ParseOutput parseFull(ParserInfo info) {
if (info.grammarVersion == IPythonNature.GRAMMAR_PYTHON_VERSION_CYTHON) {
return createCythonAst(info);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void formatAll(IDocument doc, IPyFormatStdProvider edit, IFile f, boolean
throws SyntaxErrorException {
ParseOutput objects;
try {
objects = PyParser.reparseDocument(new PyParser.ParserInfo(doc, edit.getPythonNature()));
objects = PyParser.parseFull(new PyParser.ParserInfo(doc, edit.getPythonNature()));
} catch (MisconfigurationException e1) {
Log.log(e1);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
* Contributors:
* Fabio Zadrozny <fabiofz@gmail.com> - initial API and implementation
******************************************************************************/
package org.python.pydev.refactoring.ast.visitors.rewriter;
package org.python.pydev.parser.prettyprinterv2;

import org.python.pydev.core.IGrammarVersionProvider;
import org.python.pydev.core.IIndentPrefs;
import org.python.pydev.core.autoedit.DefaultIndentPrefs;
import org.python.pydev.parser.PyParser;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.parser.jython.ast.factory.AdapterPrefs;
import org.python.pydev.parser.prettyprinterv2.PrettyPrinterPrefsV2;
import org.python.pydev.parser.prettyprinterv2.PrettyPrinterV2;

/**
* This class just provides an interface for using the rewriter.
Expand All @@ -26,7 +25,7 @@ public final class Rewriter {

public static String reparsed(String source, AdapterPrefs adapterPrefs) {
try {
SimpleNode root = org.python.pydev.ast.adapters.visitors.VisitorFactory.getRootNodeFromString(source,
SimpleNode root = PyParser.parseSimple(source,
adapterPrefs.versionProvider);
return createSourceFromAST(root, adapterPrefs);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2207,7 +2207,7 @@ public static exprType[] extractElts(ISimpleNode node) {
public static exprType extractOptionalValueSubscript(exprType node) {
if (node instanceof Str) {
ParseOutput objects = PyParser
.reparseDocument(new ParserInfo(new Document(((Str) node).s),
.parseFull(new ParserInfo(new Document(((Str) node).s),
IPythonNature.LATEST_GRAMMAR_PY3_VERSION, null));
if (objects.error == null && objects.ast != null && objects.ast instanceof Module
&& ((Module) objects.ast).body.length == 1 && ((Module) objects.ast).body[0] != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testTryReparse() throws BadLocationException {
}

PyParser.ParserInfo parserInfo = new PyParser.ParserInfo(doc, IPythonNature.LATEST_GRAMMAR_PY2_VERSION, null);
ParseOutput reparseDocument = PyParser.reparseDocument(parserInfo);
ParseOutput reparseDocument = PyParser.parseFull(parserInfo);
assertTrue(reparseDocument.ast == null);
assertTrue(reparseDocument.error != null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected Throwable parseILegalDoc(IDocument doc) {
protected Throwable parseILegalDoc(IDocument doc, boolean generateTree) {
ParseOutput objects;
try {
objects = PyParser.reparseDocument(new ParserInfo(doc, parser.getGrammarVersion(), generateTree, null));
objects = PyParser.parseFull(new ParserInfo(doc, parser.getGrammarVersion(), generateTree, null));
} catch (MisconfigurationException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -162,7 +162,7 @@ protected static SimpleNode parseLegalDoc(IDocument doc, Object[] additionalErrI
*/
protected static SimpleNode parseLegalDoc(IDocument doc, Object[] additionalErrInfo, int grammarVersion,
boolean generateTree) {
ParseOutput objects = PyParser.reparseDocument(new ParserInfo(doc, grammarVersion,
ParseOutput objects = PyParser.parseFull(new ParserInfo(doc, grammarVersion,
generateTree, null));

Object err = objects.error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public AdditionalGrammarVersionsToCheck getAdditionalGrammarVersions() throws Mi
return null;
}
};
ParseOutput tuple = PyParser.reparseDocument(new PyParser.ParserInfo(new Document(contents),
ParseOutput tuple = PyParser.parseFull(new PyParser.ParserInfo(new Document(contents),
grammarProvider, false));

assertTrue("Found: " + tuple.error, tuple.error instanceof ParseException);

tuple = PyParser.reparseDocument(new PyParser.ParserInfo(new Document("a = 10"), grammarProvider, false));
tuple = PyParser.parseFull(new PyParser.ParserInfo(new Document("a = 10"), grammarProvider, false));

assertTrue("Found: " + tuple.error, tuple.error == null);
}
Expand Down
Loading

0 comments on commit f4196bb

Please sign in to comment.