use of org.eclipse.wst.jsdt.core.dom.JavaScriptUnit in project webtools.sourceediting by eclipse.
the class JSDTSourceValidator method validateFile.
/**
* @param result
*/
private void validateFile(IValidationContext helper, IReporter reporter, IFile file, ValidationResult result) {
if ((reporter != null) && (reporter.isCancelled() == true)) {
throw new OperationCanceledException();
}
if (!shouldValidate(file)) {
return;
}
char[] source = fDocument.get().toCharArray();
if (source == null) {
return;
}
parser.setSource(source);
parser.setProject(JavaScriptCore.create(file.getProject()));
JavaScriptUnit unit = (JavaScriptUnit) parser.createAST(new NullProgressMonitor());
if (unit == null) {
Message valMessage = new // $NON-NLS-1$
Message(// $NON-NLS-1$
JavaScriptUI.ID_PLUGIN, // $NON-NLS-1$
IMessage.HIGH_SEVERITY, // $NON-NLS-1$
JavaScriptUI.ID_PLUGIN + ".problem") {
/**
* @see IMessage#getText(Locale, ClassLoader)
*/
public java.lang.String getText(Locale locale, ClassLoader classLoader) {
// $NON-NLS-1$
return "AST couldn't be created due to the fatal error";
}
};
valMessage.setOffset(0);
valMessage.setLength(0);
valMessage.setLineNo(0);
reporter.addMessage(this, valMessage);
} else if (unit.getProblems().length > 0) {
for (IProblem problem : unit.getProblems()) {
final String msg = problem.getMessage();
String[] arguments = problem.getArguments();
int severity = problem.isError() ? IMessage.HIGH_SEVERITY : IMessage.ERROR_AND_WARNING;
int lineNumber = problem.getSourceLineNumber();
int sourceStart = problem.getSourceStart();
int sourceEnd = problem.getSourceEnd();
int columnNumber = (problem instanceof DefaultProblem) ? ((DefaultProblem) problem).getSourceColumnNumber() : -1;
Message valMessage = new Message(JavaScriptUI.ID_PLUGIN, severity, JavaScriptUI.ID_PLUGIN + ".problem") {
/**
* @see IMessage#getText(Locale, ClassLoader)
*/
public java.lang.String getText(Locale locale, ClassLoader classLoader) {
return msg;
}
};
Position position = sourceEnd == -1 ? calcPosition(source, sourceStart) : new Position(sourceStart, sourceEnd - sourceStart);
valMessage.setOffset(position.getOffset());
valMessage.setLength(position.getLength());
valMessage.setLineNo(lineNumber);
// System.out.println(getClass().getName() + ": " + valMessage.getLineNumber() +
// "[" + valMessage.getOffset() + ":" + valMessage.getLength() + "] : " +
// valMessage.getText() +
// "==>>>" + String.copyValueOf(source, position.getOffset(), position.getLength()) + "<<<==");
reporter.addMessage(this, valMessage);
}
}
}
use of org.eclipse.wst.jsdt.core.dom.JavaScriptUnit in project compiler by boalang.
the class AbstractCommit method parseJavaScriptFile.
private boolean parseJavaScriptFile(final String path, final ChangedFile.Builder fb, final String content, final String compliance, final int astLevel, final boolean storeOnError, Writer astWriter, String key) {
try {
//System.out.println("parsing=" + (++count) + "\t" + path);
final org.eclipse.wst.jsdt.core.dom.ASTParser parser = org.eclipse.wst.jsdt.core.dom.ASTParser.newParser(astLevel);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setResolveBindings(true);
parser.setSource(content.toCharArray());
final Map options = JavaCore.getOptions();
JavaCore.setComplianceOptions(compliance, options);
parser.setCompilerOptions(options);
JavaScriptUnit cu;
try {
cu = (JavaScriptUnit) parser.createAST(null);
} catch (java.lang.IllegalArgumentException ex) {
return false;
}
final JavaScriptErrorCheckVisitor errorCheck = new JavaScriptErrorCheckVisitor();
cu.accept(errorCheck);
if (!errorCheck.hasError || storeOnError) {
final ASTRoot.Builder ast = ASTRoot.newBuilder();
// final CommentsRoot.Builder comments =
// CommentsRoot.newBuilder();
final JavaScriptVisitor visitor = new JavaScriptVisitor(content);
try {
ast.addNamespaces(visitor.getNamespaces(cu));
// for (final String s : visitor.getImports())
// ast.addImports(s);
/*
* for (final Comment c : visitor.getComments())
* comments.addComments(c);
*/
} catch (final UnsupportedOperationException e) {
return false;
} catch (final Exception e) {
if (debug)
System.err.println("Error visiting: " + path);
//e.printStackTrace();
return false;
}
if (astWriter != null) {
try {
// System.out.println("writing=" + count + "\t" + path);
astWriter.append(new Text(key), new BytesWritable(ast.build().toByteArray()));
} catch (IOException e) {
e.printStackTrace();
}
} else
fb.setAst(ast);
// fb.setComments(comments);
}
return !errorCheck.hasError;
} catch (final Exception e) {
e.printStackTrace();
return false;
}
}
use of org.eclipse.wst.jsdt.core.dom.JavaScriptUnit in project webtools.sourceediting by eclipse.
the class RunCodeHandler method getObjectLiteralField.
static ObjectLiteralField getObjectLiteralField(String source) {
String js = "x.prototype = {" + source + "}";
JavaScriptUnit jsu = getAST(js);
ExpressionStatement stmt = (ExpressionStatement) jsu.statements().get(0);
ObjectLiteral lit = (ObjectLiteral) ((Assignment) stmt.getExpression()).getRightHandSide();
ObjectLiteralField field = (ObjectLiteralField) lit.fields().get(0);
ASTNode node = ASTNode.copySubtree(jsu.getAST(), field);
return (ObjectLiteralField) node;
}
use of org.eclipse.wst.jsdt.core.dom.JavaScriptUnit in project webtools.sourceediting by eclipse.
the class RunCodeHandler method getAST.
static JavaScriptUnit getAST(String source) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(source.toCharArray());
ASTNode ast = parser.createAST(null);
return (JavaScriptUnit) ast;
}
Aggregations