add slider

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9306]
This commit is contained in:
parrt 2011-11-13 11:35:11 -08:00
parent f420f4c327
commit 324884585b
14 changed files with 96 additions and 49 deletions

View File

@ -156,6 +156,10 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
*/ */
protected Object getCurrentInputSymbol() { return null; } protected Object getCurrentInputSymbol() { return null; }
public void notifyListeners(String msg) {
notifyListeners((Token)getCurrentInputSymbol(), msg, null);
}
public void notifyListeners(Token offendingToken, String msg, public void notifyListeners(Token offendingToken, String msg,
@Nullable RecognitionException e) @Nullable RecognitionException e)
{ {

View File

@ -183,7 +183,6 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
NoViableAltException e) NoViableAltException e)
throws RecognitionException throws RecognitionException
{ {
// TODO: subclass this class for treeparsers
TokenStream tokens = (TokenStream)recognizer.getInputStream(); TokenStream tokens = (TokenStream)recognizer.getInputStream();
String input = tokens.toString(e.startToken, e.offendingToken); String input = tokens.toString(e.startToken, e.offendingToken);
String msg = "no viable alternative at input "+escapeWSAndQuote(input); String msg = "no viable alternative at input "+escapeWSAndQuote(input);
@ -205,11 +204,7 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
{ {
String ruleName = recognizer.getRuleNames()[recognizer._ctx.getRuleIndex()]; String ruleName = recognizer.getRuleNames()[recognizer._ctx.getRuleIndex()];
String msg = "rule "+ruleName+" "+e.msg; String msg = "rule "+ruleName+" "+e.msg;
recognizer.notifyListeners(curToken(recognizer), msg, e); recognizer.notifyListeners((Token)recognizer.getCurrentInputSymbol(), msg, e);
}
private Token curToken(BaseRecognizer recognizer) {
return ((TokenStream)recognizer.getInputStream()).LT(1);
} }
public void reportUnwantedToken(BaseRecognizer recognizer) { public void reportUnwantedToken(BaseRecognizer recognizer) {

View File

@ -32,7 +32,7 @@ package org.antlr.v4.runtime;
import org.antlr.v4.runtime.tree.*; import org.antlr.v4.runtime.tree.*;
import org.antlr.v4.runtime.tree.gui.TreeViewer; import org.antlr.v4.runtime.tree.gui.TreeViewer;
public class DefaultANTLRTreeGrammarErrorStrategy implements ANTLRErrorStrategy { public class DefaultANTLRTreeGrammarErrorStrategy extends DefaultANTLRErrorStrategy {
@Override @Override
public void beginErrorCondition(BaseRecognizer recognizer) { public void beginErrorCondition(BaseRecognizer recognizer) {
} }
@ -41,18 +41,22 @@ public class DefaultANTLRTreeGrammarErrorStrategy implements ANTLRErrorStrategy
public void reportError(BaseRecognizer recognizer, RecognitionException e) public void reportError(BaseRecognizer recognizer, RecognitionException e)
throws RecognitionException throws RecognitionException
{ {
super.reportError(recognizer, e);
Object root = ((TreeParser)recognizer).getInputStream().getTreeSource(); Object root = ((TreeParser)recognizer).getInputStream().getTreeSource();
if ( root instanceof Tree ) { if ( root instanceof Tree ) {
TreeViewer viewer = new TreeViewer(recognizer, (Tree)root); TreeViewer viewer = new TreeViewer(recognizer, (Tree)root);
viewer.open(); viewer.open();
// viewer.addHighlightedNodes();
// TODO: highlight error node // TODO: highlight error node
} }
recognizer.notifyListeners(e.offendingToken, e.getMessage(), e); // recognizer.notifyListeners(e.offendingToken, e.getMessage(), e);
} }
@Override @Override
public Object recoverInline(BaseRecognizer recognizer) throws RecognitionException { public Object recoverInline(BaseRecognizer recognizer) throws RecognitionException {
throw new InputMismatchException(recognizer); InputMismatchException e = new InputMismatchException(recognizer);
reportError(recognizer, e);
throw e;
} }
@Override @Override

View File

@ -1,11 +1,20 @@
package org.antlr.v4.runtime; package org.antlr.v4.runtime;
import org.antlr.v4.runtime.tree.AST;
/** This signifies any kind of mismatched input exceptions such as /** This signifies any kind of mismatched input exceptions such as
* when the current input does not match the expected token or tree node. * when the current input does not match the expected token or tree node.
*/ */
public class InputMismatchException extends RecognitionException { public class InputMismatchException extends RecognitionException {
public InputMismatchException(BaseRecognizer recognizer) { public InputMismatchException(BaseRecognizer recognizer) {
super(recognizer, recognizer.getInputStream(), recognizer._ctx); super(recognizer, recognizer.getInputStream(), recognizer._ctx);
this.offendingToken = (Token)recognizer.getCurrentInputSymbol(); Object la = recognizer.getCurrentInputSymbol();
if ( la instanceof AST ) {
this.offendingNode = la;
this.offendingToken = ((AST)la).getPayload();
}
else {
this.offendingToken = (Token)la;
}
} }
} }

View File

@ -30,6 +30,7 @@ package org.antlr.v4.runtime;
import org.antlr.v4.runtime.atn.ATNConfig; import org.antlr.v4.runtime.atn.ATNConfig;
import org.antlr.v4.runtime.misc.OrderedHashSet; import org.antlr.v4.runtime.misc.OrderedHashSet;
import org.antlr.v4.runtime.tree.AST;
/** The parser could not decide which path in the decision to take based /** The parser could not decide which path in the decision to take based
* upon the remaining input. * upon the remaining input.
@ -44,6 +45,7 @@ public class NoViableAltException extends RecognitionException {
* buffer all of the tokens but later we might not have access to those.) * buffer all of the tokens but later we might not have access to those.)
*/ */
public Token startToken; public Token startToken;
public Object startNode;
public NoViableAltException(BaseRecognizer recognizer) { // LL(1) error public NoViableAltException(BaseRecognizer recognizer) { // LL(1) error
this(recognizer,recognizer.getInputStream(), this(recognizer,recognizer.getInputStream(),
@ -63,6 +65,12 @@ public class NoViableAltException extends RecognitionException {
super(recognizer, input, ctx); super(recognizer, input, ctx);
this.deadEndConfigs = deadEndConfigs; this.deadEndConfigs = deadEndConfigs;
this.startToken = startToken; this.startToken = startToken;
Object la = recognizer.getCurrentInputSymbol();
if ( la instanceof AST) {
this.offendingNode = la;
this.offendingToken = ((AST)la).getPayload();
}
this.offendingToken = offendingToken; this.offendingToken = offendingToken;
} }

View File

@ -59,8 +59,8 @@ public class RecognitionException extends RuntimeException {
/** If this is a tree parser exception, node is set to the node with /** If this is a tree parser exception, node is set to the node with
* the problem. * the problem.
public Object offendingNode;
*/ */
protected Object offendingNode;
protected int offendingState; protected int offendingState;

View File

@ -208,9 +208,9 @@ public abstract class Recognizer<ATNInterpreter> {
public List<ANTLRErrorListener> getListeners() { return _listeners; } public List<ANTLRErrorListener> getListeners() { return _listeners; }
public ANTLRErrorStrategy getErrHandler() { return _errHandler; } public ANTLRErrorStrategy getErrorHandler() { return _errHandler; }
public void setErrHandler(ANTLRErrorStrategy h) { this._errHandler = h; } public void setErrorHandler(ANTLRErrorStrategy h) { this._errHandler = h; }
// subclass needs to override these if there are sempreds or actions // subclass needs to override these if there are sempreds or actions
// that the ATN interp needs to execute // that the ATN interp needs to execute

View File

@ -35,6 +35,7 @@ import org.antlr.v4.runtime.BaseRecognizer;
import org.antlr.v4.runtime.tree.*; import org.antlr.v4.runtime.tree.*;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
@ -216,37 +217,39 @@ public class TreeViewer extends JComponent {
final Container contentPane = new JPanel(); final Container contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0,0)); contentPane.setLayout(new BorderLayout(0,0));
contentPane.setBackground(Color.white); contentPane.setBackground(Color.white);
// contentPane.setPreferredSize(new Dimension(200, 200));
dialog.setContentPane(contentPane); dialog.setContentPane(contentPane);
// Wrap viewer in scroll pane // Wrap viewer in scroll pane
JScrollPane scrollPane = new JScrollPane(viewer); JScrollPane scrollPane = new JScrollPane(viewer);
// Make it tree size up to width/height of viewer // Make it tree size up to width/height of viewer
Dimension scaledTreeSize = viewer.setPreferredSizeToScaledTree();
viewer.treeLayout.getBounds().getBounds().getSize();
scaledTreeSize = new Dimension((int)(scaledTreeSize.width*viewer.scale),
(int)(scaledTreeSize.height*viewer.scale));
if ( scaledTreeSize.width < viewer.width ) {
viewer.setWidth(scaledTreeSize.width);
}
if ( scaledTreeSize.height < viewer.height ) {
viewer.setHeight(scaledTreeSize.height);
}
scrollPane.setPreferredSize(new Dimension(viewer.width,viewer.height));
contentPane.add(scrollPane, BorderLayout.CENTER); contentPane.add(scrollPane, BorderLayout.CENTER);
// Add button to bottom // Add button to bottom
JPanel wrapper = new JPanel(new BorderLayout(0,0));
contentPane.add(wrapper, BorderLayout.SOUTH);
JButton ok = new JButton("OK"); JButton ok = new JButton("OK");
ok.addActionListener( ok.addActionListener(
new ActionListener() { new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
dialog.dispose(); dialog.dispose();
}
}
);
wrapper.add(ok, BorderLayout.SOUTH);
// Add scale slider
final JSlider scaleSlider = new JSlider(JSlider.HORIZONTAL,
0,1000,1);
scaleSlider.addChangeListener(
new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int v = scaleSlider.getValue();
viewer.setScale(v / 1000.0 + 1.0);
} }
} }
); );
JPanel wrapper = new JPanel(new FlowLayout(FlowLayout.CENTER)); wrapper.add(scaleSlider, BorderLayout.CENTER);
wrapper.add(ok);
contentPane.add(wrapper, BorderLayout.SOUTH);
// make viz // make viz
dialog.pack(); dialog.pack();
@ -254,9 +257,23 @@ public class TreeViewer extends JComponent {
dialog.setVisible(true); dialog.setVisible(true);
} }
protected void setPreferredSizeToScaledTree() {
Dimension scaledTreeSize =
treeLayout.getBounds().getBounds().getSize();
scaledTreeSize = new Dimension((int)(scaledTreeSize.width*scale),
(int)(scaledTreeSize.height*scale));
if ( scaledTreeSize.width < width ) {
setWidth(scaledTreeSize.width);
}
if ( scaledTreeSize.height < height ) {
setHeight(scaledTreeSize.height);
}
setPreferredSize(new Dimension(width,height));
}
public void open() { public void open() {
final TreeViewer viewer = this; final TreeViewer viewer = this;
viewer.setScale(10.5); viewer.setScale(2.0);
javax.swing.SwingUtilities.invokeLater(new Runnable() { javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { public void run() {
showInDialog(viewer); showInDialog(viewer);
@ -397,6 +414,7 @@ public class TreeViewer extends JComponent {
public void setScale(double scale) { public void setScale(double scale) {
this.scale = scale; this.scale = scale;
repaint();
} }
public int getHeight() { public int getHeight() {

View File

@ -1,4 +1,6 @@
grammar T; grammar T;
options {output=AST;}
s : i=ifstat {System.out.println(_input.toString(0,_input.index()-1));} ; s : i=ifstat {System.out.println(_input.toString(0,_input.index()-1));} ;
ifstat : 'if' '(' expr ')' assign ; ifstat : 'if' '(' expr ')' assign ;

View File

@ -47,7 +47,7 @@ public class TestT {
System.out.println(tree.toStringTree(p)); System.out.println(tree.toStringTree(p));
TreeViewer v = new TreeViewer(p, tree); TreeViewer v = new TreeViewer(p, tree);
v.setHighlightedBoxColor(TreeViewer.LIGHT_RED); v.setHighlightedBoxColor(TreeViewer.LIGHT_RED);
v.addHighlightNodes(new ArrayList<Tree>() {{ v.addHighlightedNodes(new ArrayList<Tree>() {{
ParseTree c0 = tree.getChild(0); ParseTree c0 = tree.getChild(0);
add(c0); add(c0);
add(c0.getChild(0)); add(c0.getChild(0));

View File

@ -188,10 +188,12 @@ RuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAc
<postamble; separator="\n"> <postamble; separator="\n">
<namedActions.after> <namedActions.after>
} }
<if(currentRule.catch_)>
catch (RecognitionException re) { catch (RecognitionException re) {
_errHandler.reportError(this, re); _errHandler.reportError(this, re);
_errHandler.recover(this, re); _errHandler.recover(this, re);
} }
<endif>
finally { finally {
<finallyAction> <finallyAction>
exitRule(RULE_<currentRule.name>); exitRule(RULE_<currentRule.name>);

View File

@ -52,6 +52,7 @@ public class RuleFunction extends OutputModelObject {
public int index; public int index;
public Collection<Attribute> args = null; public Collection<Attribute> args = null;
public Rule rule; public Rule rule;
public boolean catch_;
@ModelElement public List<SrcOp> code; @ModelElement public List<SrcOp> code;
@ModelElement public OrderedHashSet<Decl> locals; // TODO: move into ctx? @ModelElement public OrderedHashSet<Decl> locals; // TODO: move into ctx?
@ -73,6 +74,8 @@ public class RuleFunction extends OutputModelObject {
index = r.index; index = r.index;
catch_ = !r.g.isTreeGrammar();
ruleCtx = r.g.isTreeGrammar() ? ruleCtx = r.g.isTreeGrammar() ?
new TreeParserStructDecl(factory, r) : new TreeParserStructDecl(factory, r) :
new StructDecl(factory, r); new StructDecl(factory, r);

View File

@ -253,13 +253,6 @@ optionsSpec
option option
: id ASSIGN^ optionValue : id ASSIGN^ optionValue
/*
{
if ( $id.text.equals("output") ) {
if ( $optionValue.text.equals("AST") ) buildAST = true;
}
}
*/
; ;
// ------------ // ------------

View File

@ -30,6 +30,7 @@
package org.antlr.v4.tool.ast; package org.antlr.v4.tool.ast;
import org.antlr.runtime.Token; import org.antlr.runtime.Token;
import org.antlr.v4.misc.CharSupport;
import java.util.*; import java.util.*;
@ -48,14 +49,22 @@ public abstract class GrammarASTWithOptions extends GrammarAST {
public void setOption(String key, GrammarAST node) { public void setOption(String key, GrammarAST node) {
if ( options==null ) options = new HashMap<String, GrammarAST>(); if ( options==null ) options = new HashMap<String, GrammarAST>();
// if ( value.startsWith("'") || value.startsWith("\"") ) {
// value = CharSupport.getStringFromGrammarStringLiteral(value);
// }
options.put(key, node); options.put(key, node);
} }
public String getOptionString(String key) { public String getOptionString(String key) {
return null; GrammarAST value = getOption(key);
if ( value == null ) return null;
if ( value instanceof ActionAST ) {
return value.getText();
}
else {
String v = value.getText();
if ( v.startsWith("'") || v.startsWith("\"") ) {
v = CharSupport.getStringFromGrammarStringLiteral(v);
}
return v;
}
} }
public GrammarAST getOption(String key) { public GrammarAST getOption(String key) {