Commit 99ed1e43 authored by Johannes Roith's avatar Johannes Roith

Various changes.

parent e56f6079
......@@ -18,6 +18,10 @@ public class CupParser
return result;
}
public boolean hasParseErrors() {
return context.getErrorManager().hasParseErrors();
}
public CupContext getContext() {
return context;
}
......
......@@ -100,7 +100,8 @@ public class CupSymbol {
static {
keywords = new int[] {
sym.IMPORT,
sym.IMPORT,
sym.CLASS,
sym.PARSER,
sym.WITH,
sym.SCAN,
......
......@@ -3,7 +3,7 @@ package de.in.tum.www2.cup;
public class DefaultErrorReporter implements IErrorReporter
{
public void report(ErrorType type, ErrorSource source, int errorCode,
public void report(ErrorType type, ErrorSource source, ErrorCode errorCode,
String message, Position start, Position end)
{
StringBuilder m = new StringBuilder();
......
......@@ -14,14 +14,16 @@ import de.in.tum.www2.cup.internal.emit;
class Demo
{
public static void main(String[] blah) throws Exception {
IErrorReporter er = new DefaultErrorReporter();
FileInputStream input = new FileInputStream("/Users/jroith/testcup/test.cup");
IErrorReporter er = new DefaultErrorReporter();
FileInputStream input = new FileInputStream("/Users/jroith/testcup/testspeed.cup");
CupParser parser = new CupParser(er, input);
ParserResult res = parser.parse();
CupContext context = parser.getContext();
System.out.println(res);
// System.out.println(res);
System.out.println("\nLocationPatchVisitor:");
long ms = System.currentTimeMillis();
......@@ -45,10 +47,12 @@ class Demo
System.out.println("QUESTION is a declared terminal! :-)");
System.out.println("\nComputing tables ...");
LALRResult lalrResult = LALRResult.Compute(context, context.start_production);
// System.out.println("\nComputing tables ...");
// LALRResult lalrResult = LALRResult.Compute(context, context.start_production);
// System.out.println(lalrResult.getActionTable());
// System.out.println(lalrResult.getReduceTable());
......
package de.in.tum.www2.cup;
public enum ErrorCode {
SymbolAlreadyDeclared
}
......@@ -9,6 +9,26 @@ public class ErrorManager
private IErrorReporter er;
private int error_count;
public boolean hasScanErrors (){
throw new RuntimeException("TODO: implement!");
}
public boolean hasParseErrors (){
throw new RuntimeException("TODO: implement!");
}
public boolean hasLALRErrors (){
throw new RuntimeException("TODO: implement!");
}
public boolean hasErrors (){
throw new RuntimeException("TODO: implement!");
}
public boolean hasWarnings (){
throw new RuntimeException("TODO: implement!");
}
public int getErrorCount() {
return error_count;
}
......@@ -34,7 +54,7 @@ public class ErrorManager
// }
public void Warning(String msg) {
er.report(ErrorType.Warning, ErrorSource.Unknown, -1, msg, null, null);
er.report(ErrorType.Warning, ErrorSource.Unknown, null, msg, null, null);
}
public void Warning(ErrorSource source, String msg) {
......@@ -46,7 +66,7 @@ public class ErrorManager
}
public void Warning(ErrorSource source, String msg, Position start, Position end) {
er.report(ErrorType.Warning, source, -1, msg, start, end);
er.report(ErrorType.Warning, source, null, msg, start, end);
}
public void Fatal(ErrorSource source, String msg, ComplexSymbol sym) {
......@@ -54,7 +74,7 @@ public class ErrorManager
}
public void Fatal(ErrorSource source, String msg) {
er.report(ErrorType.Fatal, source, -1, msg, null, null);
er.report(ErrorType.Fatal, source, null, msg, null, null);
}
public void Error(ErrorSource source, String msg, ComplexSymbol sym) {
......@@ -82,7 +102,7 @@ public class ErrorManager
}
public void Error(ErrorSource source, String msg, Position start, Position end) {
er.report(ErrorType.Error, source, -1, msg, start, end);
er.report(ErrorType.Error, source, null, msg, start, end);
error_count++;
}
}
......
......@@ -8,12 +8,12 @@ public interface IErrorReporter
*
* @param type
* @param source
* @param errorCode May be -1 if no code is known.
* @param errorCode May be null if no code is known.
* @param message May be null if no message is provided.
* @param start May be null if there is no line information.
* @param end May be null if the error is not localized on the line.
*/
void report(ErrorType type, ErrorSource source, int errorCode,
void report(ErrorType type, ErrorSource source, ErrorCode errorCode,
String message, Position start, Position end);
}
......@@ -11,7 +11,7 @@ public class MultiErrorReporter implements IErrorReporter
this.reporters = reporters;
}
public void report(ErrorType type, ErrorSource source, int errorCode,
public void report(ErrorType type, ErrorSource source, ErrorCode errorCode,
String message, Position start, Position end)
{
for (IErrorReporter reporter : reporters)
......
......@@ -3,7 +3,7 @@ package de.in.tum.www2.cup;
public class NoopErrorReporter implements IErrorReporter
{
public void report(ErrorType type, ErrorSource source, int errorCode,
public void report(ErrorType type, ErrorSource source, ErrorCode errorCode,
String message, Position start, Position end)
{
}
......
......@@ -37,6 +37,10 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
}
StringBuilder builder = new StringBuilder();
if (parent.getName() == null)
return;
builder.append (parent.getName().name);
builder.append ("::");
builder.append (parent.getRightHandSides().size());
......@@ -63,7 +67,8 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
public void postVisit (TerminalDeclaration node, Object data) {
if (node != null && node.getTerminals() != null) {
for (Terminal t : node.getTerminals())
result.getTerminals().add(t.getName().name);
if (t != null)
result.getTerminals().add(t.getNameStringOrNull());
}
}
......@@ -71,7 +76,8 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
public void postVisit (NonTerminalDeclaration node, Object data) {
if (node != null && node.getNonTerminals() != null) {
for (NonTerminal nt : node.getNonTerminals())
result.getNonTerminals().add(nt.getName().name);
if (nt != null)
result.getNonTerminals().add(nt.getNameStringOrNull());
}
}
......@@ -80,7 +86,8 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
// TODO: OAOO!
if (node != null && node.getNonTerminals() != null) {
for (NonTerminal nt : node.getNonTerminals())
result.getNonTerminals().add(nt.getName().name);
if (nt != null)
result.getNonTerminals().add(nt.getNameStringOrNull());
}
}
......@@ -89,7 +96,8 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
// TODO: OAOO!
if (node != null && node.getTerminals() != null) {
for (Terminal nt : node.getTerminals())
result.getNonTerminals().add(nt.getName().name);
if (nt != null)
result.getNonTerminals().add(nt.getNameStringOrNull());
}
}
......
......@@ -31,6 +31,8 @@ public class RefResolutionVisitor extends Visitor<Object>
}
private void resolveDeclarationFrom(String name, IHasDeclarationReference node) {
if (name == null)
return;
if (declarations.containsKey(name)) {
AbstractNode def = declarations.get(name);
node.setDeclaration(def);
......@@ -38,6 +40,8 @@ public class RefResolutionVisitor extends Visitor<Object>
}
private void resolveDefinitionFrom(String name, IHasDefinitionReference node) {
if (name == null)
return;
if (definitions.containsKey(name)) {
AbstractNode def = definitions.get(name);
node.setDefinition(def);
......@@ -50,9 +54,11 @@ public class RefResolutionVisitor extends Visitor<Object>
}
@Override public Object preVisit (StartWith node, Object data) {
if (node == null)
return data;
NonTerminal nt = node.getNonTerminal();
if (nt != null) {
String name = nt.getName().name;
String name = nt.getNameStringOrNull();
resolveDefinitionFrom(name, nt);
resolveDeclarationFrom(name, nt);
}
......@@ -60,8 +66,10 @@ public class RefResolutionVisitor extends Visitor<Object>
}
@Override public Object preVisit (NonTerminalDeclaration node, Object data) {
if (node == null)
return data;
for(NonTerminal nt : node.getNonTerminals()) {
String name = nt.getName().name;
String name = nt.getNameStringOrNull();
declarations.put(name, nt);
resolveDefinitionFrom(name, nt);
}
......@@ -69,14 +77,18 @@ public class RefResolutionVisitor extends Visitor<Object>
}
@Override public Object preVisit (TerminalDeclaration node, Object data) {
if (node == null)
return data;
for(Terminal t : node.getTerminals())
declarations.put(t.getName().name, t);
declarations.put(t.getNameStringOrNull(), t);
return data;
}
@Override public Object preVisit (TypedNonTerminalDeclaration node, Object data) {
if (node == null)
return data;
for(NonTerminal nt : node.getNonTerminals()) {
String name = nt.getName().name;
String name = nt.getNameStringOrNull();
declarations.put(name, nt);
resolveDefinitionFrom(name, nt);
}
......@@ -85,18 +97,20 @@ public class RefResolutionVisitor extends Visitor<Object>
@Override public Object preVisit (TypedTerminalDeclaration node, Object data) {
for(Terminal t : node.getTerminals())
declarations.put(t.getName().name, t);
declarations.put(t.getNameStringOrNull(), t);
return data;
}
@Override public Object preVisit (Precedence node, Object data) {
if (node == null)
return data;
for(Terminal t : node.getTerminals())
resolveDeclarationFrom(t.getName().name, t);
resolveDeclarationFrom(t.getNameStringOrNull(), t);
return data;
}
@Override public Object preVisit (Production node, Object data) {
String name = node.getName().name;
String name = node.getNameStringOrNull();
definitions.put(name, node);
resolveDeclarationFrom(name, node); // handles the LHS
......@@ -110,7 +124,7 @@ public class RefResolutionVisitor extends Visitor<Object>
}
@Override public Object preVisit (ProductionSymbolRef node, Object data) {
String name = node.getName().name;
String name = node.getNameStringOrNull();
resolveDefinitionFrom(name, node);
resolveDeclarationFrom(name, node);
return data;
......
......@@ -4,4 +4,11 @@ public interface IWithName {
Name getName();
default String getNameStringOrNull() {
Name name = getName();
if (name == null)
return null;
return name.name;
}
}
......@@ -12,7 +12,7 @@ public class NonTerminalDeclaration extends SymbolDeclaration {
public List<NonTerminal> getNonTerminals() {
return nonTerminals;
}
public NonTerminalDeclaration(List<NonTerminal> nonTerminals, Range range) {
super(range);
this.nonTerminals = nonTerminals;
......@@ -30,7 +30,8 @@ public class NonTerminalDeclaration extends SymbolDeclaration {
if (visitor.resetDescend()) {
if (nonTerminals != null) {
for (NonTerminal nt : nonTerminals)
nt.accept(visitor, childArg);
if (nt != null)
nt.accept(visitor, childArg);
}
}
visitor.postVisit(this, data);
......
......@@ -74,8 +74,9 @@ public class ParserResult extends AbstractNode {
if (pack != null)
pack.accept(visitor, childArg);
for (Import im : imports)
im.accept(visitor, childArg);
if (imports != null)
for (Import im : imports)
im.accept(visitor, childArg);
if (className != null)
className.accept(visitor, childArg);
......@@ -92,17 +93,20 @@ public class ParserResult extends AbstractNode {
if (scanCode != null)
scanCode.accept(visitor, childArg);
for (SymbolDeclaration s : symbols)
s.accept(visitor, childArg);
if (symbols != null)
for (SymbolDeclaration s : symbols)
s.accept(visitor, childArg);
for (Precedence p : precedences)
p.accept(visitor, childArg);
if (precedences != null)
for (Precedence p : precedences)
p.accept(visitor, childArg);
if (startWith != null)
startWith.accept(visitor, childArg);
for (Production p : productions)
p.accept(visitor, childArg);
if (productions != null)
for (Production p : productions)
p.accept(visitor, childArg);
}
visitor.postVisit(this, data);
......
......@@ -2,8 +2,8 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: CupPlugin
Bundle-SymbolicName: de.tum.in.www2.CupPlugin;singleton:=true
Bundle-Version: 0.0.0.qualifier
Bundle-Activator: cupplugin.Activator
Bundle-Version: 1.0
Bundle-Activator: de.tum.in.www2.cupplugin.Activator
Bundle-Vendor: de.tum.in.www2
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
......@@ -20,6 +20,7 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.debug.ui;bundle-version="3.10.1",
org.eclipse.jdt;bundle-version="3.10.0",
org.eclipse.jdt.debug;bundle-version="3.8.101",
org.eclipse.jdt.core;bundle-version="3.10.0",
org.eclipse.jdt.ui;bundle-version="3.10.1"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
......
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: CupPlugin
Bundle-SymbolicName: de.tum.in.www2.CupPlugin;singleton:=true
Bundle-Version: 1.0
Bundle-Activator: de.tum.in.www2.cupplugin.Activator
Bundle-Vendor: de.tum.in.www2
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.jface.text,
org.eclipse.core.resources,
org.eclipse.ui.editors,
org.eclipse.ui.ide,
org.eclipse.zest.core;bundle-version="1.5.100",
org.eclipse.zest.layouts;bundle-version="1.1.100",
CupReferencedLibraries;bundle-version="1.0.0",
org.eclipse.ui.views;bundle-version="3.7.0",
org.eclipse.core.expressions;bundle-version="3.4.600",
org.eclipse.debug.core;bundle-version="3.9.1",
org.eclipse.debug.ui;bundle-version="3.10.1",
org.eclipse.jdt.core;bundle-version="3.10.0",
org.eclipse.jdt.ui;bundle-version="3.10.1"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .
......@@ -3,5 +3,6 @@ output.. = bin/
bin.includes = plugin.xml,\
META-INF/,\
.,\
icons/
icons/,\
templates/
jars.compile.order = .
package cupplugin;
package de.tum.in.www2.cupplugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
......
package de.tum.in.www2.cupplugin;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import de.in.tum.www2.cup.ast.Name;
import de.in.tum.www2.cup.ast.ParserResult;
import de.tum.in.www2.cupplugin.model.Model;
public class GeneratedFileLocator {
private IFile currentFile;
private void scanResources(List<IFile> candidates,
String filename, IContainer parent) throws CoreException {
for (IResource res : parent.members()) {
if (res instanceof IContainer) {
// System.out.println("found container: " + res.getName());
scanResources(candidates, filename, (IContainer) res);
} else if (res instanceof IFile) {
IFile file = (IFile) res;
if (res.getName().equals(filename)) {
// The file should have the correct class name.
candidates.add(file);
}
}
}
}
private List<IFile> findCandidates(IFile cupFile, ParserResult result) {
if (result == null)
return null;
List<IFile> candidates = new ArrayList<IFile>();
IProject project = cupFile.getProject();
String className = "Parser";
if (result.className != null) {
Name name = result.className.getName();
if (name != null)
className = name.name;
}
String filename = className + ".java";
try {
scanResources(candidates, filename, project);
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return candidates;
}
public IFile find(IFile cupFile, ParserResult result) {
if (currentFile != null && currentFile.exists()) {
// System.out.println("cached!");
return currentFile;
} else {
currentFile = null;
}
List<IFile> candidates = findCandidates(cupFile, result);
// if (candidates.size() == 0)
// System.out.println("Found no candidates!");
for (IFile candidate : candidates) {
System.out.println("candidate: " + candidate.getName());
if (currentFile != null) {
// TODO: check if better than candidate.
// e.g. if package matches
}
currentFile = candidate;
}
return currentFile;
}
}
......@@ -20,13 +20,17 @@ import de.tum.in.www2.cupplugin.editors.MultiPageEditor;
public final class PluginUtility {
public static void showMessage(String message) {
MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getDisplay().getActiveShell());
mb.setMessage(message);
mb.open();
}
public static void fullSyntaxRefresh (ISourceViewer sourceViewer) {
System.out.println(">>>>>>>>>>>>>> FULL REFRESH OF SYNTAX HIGHLIGHTING! <<<<<<<<<<<<<<<");
sourceViewer.invalidateTextPresentation();
}
public static Range getRangeFromSelection (CupTextEditor editor)
{
ITextSelection textSelection = (ITextSelection) editor
......
......@@ -43,8 +43,11 @@ public class OpenDeclarationHandler extends AbstractHandler {
AbstractNode target = ((IHasDeclarationReference) parent).getDeclaration();
if (target != null && target instanceof IWithName) {
Position targetPosition = target.getBegin();
int length = ((IWithName) target).getName().name.length();
editor.selectAndReveal(targetPosition.getOffsetFromStart(), length);
String name = ((IWithName) target).getNameStringOrNull();
if (name != null) {
int length = name.length();
editor.selectAndReveal(targetPosition.getOffsetFromStart(), length);
}
}
}
......
......@@ -48,8 +48,11 @@ public class OpenDefinitionHandler extends AbstractHandler {
AbstractNode target = ((IHasDefinitionReference) parent).getDefinition();
if (target != null && target instanceof IWithName) {
Position targetPosition = target.getBegin();
int length = ((IWithName) target).getName().name.length();
editor.selectAndReveal(targetPosition.getOffsetFromStart(), length);
String name = ((IWithName) target).getNameStringOrNull();
if (name != null) {
int length = name.length();
editor.selectAndReveal(targetPosition.getOffsetFromStart(), length);
}
}
}
......
......@@ -122,14 +122,12 @@ public class DocumentDidChangeJob extends Job {
}
}
IFile file = ((FileEditorInput) myEditor.getEditorInput()).getFile();
CupEditorErrorReporter errorReporter = new CupEditorErrorReporter(file);
if (jobs.contains(JobsToDo.parseCode)) {
InputStream in = new ByteArrayInputStream(codeText.getBytes());
IFile file = ((FileEditorInput) myEditor.getEditorInput())
.getFile();
CupEditorErrorReporter errorReporter = new CupEditorErrorReporter(
file);
CupParser p = new CupParser(errorReporter, in);
result = null;
context = null;
......@@ -142,7 +140,8 @@ public class DocumentDidChangeJob extends Job {
e.printStackTrace();
}
errorReporter.pushToUIThread();
// TODO
// errorReporter.pushToUIThread();
if (result != null) {
// System.out.println(result.toString());
......@@ -154,16 +153,27 @@ public class DocumentDidChangeJob extends Job {
return Status.CANCEL_STATUS;
}
}
/*
if (jobs.contains(JobsToDo.buildTable)) {
lalrResult = null;
try {
lalrResult = LALRResult.Compute(context,
context.start_production);
} catch (internal_error e) {
// try {
// if (!p.hasParseErrors()) {
// lalrResult = LALRResult.Compute(context,
// context.start_production);
// }
// } catch (internal_error e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// e.printStackTrace();
// }
errorReporter.pushToUIThread();
if (lalrResult != null) {
LaLrResultJob resultLaLrModelJob = new LaLrResultJob(lalrResult, revNumber, context);
resultLaLrModelJob.setSystem(true);
......@@ -171,7 +181,7 @@ public class DocumentDidChangeJob extends Job {
} else {
return Status.CANCEL_STATUS;
}
}
}*/
System.out.println(jobs);
......@@ -183,6 +193,7 @@ public class DocumentDidChangeJob extends Job {
CallbackJob cb = new CallbackJob(myEditor);
cb.setSystem(true);
cb.schedule();
return Status.OK_STATUS;
}
......
package de.tum.in.www2.cupplugin.editors;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import de.in.tum.www2.cup.ast.Name;
import de.in.tum.www2.cup.ast.ParserResult;
import de.tum.in.www2.cupplugin.controller.Controller;
import de.tum.in.www2.cupplugin.model.Model;
import de.tum.in.www2.cupplugin.GeneratedFileLocator;
import de.tum.in.www2.cupplugin.PluginUtility;
class CTEDocumentListener implements IDocumentListener {
private IFile file;
private GeneratedFileLocator gfl;
public CTEDocumentListener(IFile file) {