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; }
public void notifyListeners(String msg) {
notifyListeners((Token)getCurrentInputSymbol(), msg, null);
}
public void notifyListeners(Token offendingToken, String msg,
@Nullable RecognitionException e)
{

View File

@ -183,7 +183,6 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
NoViableAltException e)
throws RecognitionException
{
// TODO: subclass this class for treeparsers
TokenStream tokens = (TokenStream)recognizer.getInputStream();
String input = tokens.toString(e.startToken, e.offendingToken);
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 msg = "rule "+ruleName+" "+e.msg;
recognizer.notifyListeners(curToken(recognizer), msg, e);
}
private Token curToken(BaseRecognizer recognizer) {
return ((TokenStream)recognizer.getInputStream()).LT(1);
recognizer.notifyListeners((Token)recognizer.getCurrentInputSymbol(), msg, e);
}
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.gui.TreeViewer;
public class DefaultANTLRTreeGrammarErrorStrategy implements ANTLRErrorStrategy {
public class DefaultANTLRTreeGrammarErrorStrategy extends DefaultANTLRErrorStrategy {
@Override
public void beginErrorCondition(BaseRecognizer recognizer) {
}
@ -41,18 +41,22 @@ public class DefaultANTLRTreeGrammarErrorStrategy implements ANTLRErrorStrategy
public void reportError(BaseRecognizer recognizer, RecognitionException e)
throws RecognitionException
{
super.reportError(recognizer, e);
Object root = ((TreeParser)recognizer).getInputStream().getTreeSource();
if ( root instanceof Tree ) {
TreeViewer viewer = new TreeViewer(recognizer, (Tree)root);
viewer.open();
// viewer.addHighlightedNodes();
// TODO: highlight error node
}
recognizer.notifyListeners(e.offendingToken, e.getMessage(), e);
// recognizer.notifyListeners(e.offendingToken, e.getMessage(), e);
}
@Override
public Object recoverInline(BaseRecognizer recognizer) throws RecognitionException {
throw new InputMismatchException(recognizer);
InputMismatchException e = new InputMismatchException(recognizer);
reportError(recognizer, e);
throw e;
}
@Override

View File

@ -1,11 +1,20 @@
package org.antlr.v4.runtime;
import org.antlr.v4.runtime.tree.AST;
/** This signifies any kind of mismatched input exceptions such as
* when the current input does not match the expected token or tree node.
*/
public class InputMismatchException extends RecognitionException {
public InputMismatchException(BaseRecognizer recognizer) {
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.misc.OrderedHashSet;
import org.antlr.v4.runtime.tree.AST;
/** The parser could not decide which path in the decision to take based
* 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.)
*/
public Token startToken;
public Object startNode;
public NoViableAltException(BaseRecognizer recognizer) { // LL(1) error
this(recognizer,recognizer.getInputStream(),
@ -63,6 +65,12 @@ public class NoViableAltException extends RecognitionException {
super(recognizer, input, ctx);
this.deadEndConfigs = deadEndConfigs;
this.startToken = startToken;
Object la = recognizer.getCurrentInputSymbol();
if ( la instanceof AST) {
this.offendingNode = la;
this.offendingToken = ((AST)la).getPayload();
}
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
* the problem.
public Object offendingNode;
*/
protected Object offendingNode;
protected int offendingState;

View File

@ -208,9 +208,9 @@ public abstract class Recognizer<ATNInterpreter> {
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
// 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 javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
@ -216,37 +217,39 @@ public class TreeViewer extends JComponent {
final Container contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0,0));
contentPane.setBackground(Color.white);
// contentPane.setPreferredSize(new Dimension(200, 200));
dialog.setContentPane(contentPane);
// Wrap viewer in scroll pane
JScrollPane scrollPane = new JScrollPane(viewer);
// Make it tree size up to width/height of viewer
Dimension scaledTreeSize =
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));
viewer.setPreferredSizeToScaledTree();
contentPane.add(scrollPane, BorderLayout.CENTER);
// Add button to bottom
JPanel wrapper = new JPanel(new BorderLayout(0,0));
contentPane.add(wrapper, BorderLayout.SOUTH);
JButton ok = new JButton("OK");
ok.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.dispose();
new ActionListener() {
public void actionPerformed(ActionEvent e) {
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(ok);
contentPane.add(wrapper, BorderLayout.SOUTH);
wrapper.add(scaleSlider, BorderLayout.CENTER);
// make viz
dialog.pack();
@ -254,9 +257,23 @@ public class TreeViewer extends JComponent {
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() {
final TreeViewer viewer = this;
viewer.setScale(10.5);
viewer.setScale(2.0);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
showInDialog(viewer);
@ -397,6 +414,7 @@ public class TreeViewer extends JComponent {
public void setScale(double scale) {
this.scale = scale;
repaint();
}
public int getHeight() {

View File

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

View File

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

View File

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

View File

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

View File

@ -93,7 +93,7 @@ tokens {
ST_RESULT; // distinguish between ST and tree rewrites
RESULT;
ALT_REWRITE; // indicate ALT is rewritten
DOWN_TOKEN; // AST node representing DOWN node in tree parser code gen
UP_TOKEN;
}
@ -253,13 +253,6 @@ optionsSpec
option
: id ASSIGN^ optionValue
/*
{
if ( $id.text.equals("output") ) {
if ( $optionValue.text.equals("AST") ) buildAST = true;
}
}
*/
;
// ------------
@ -543,12 +536,12 @@ ruleBlock
ruleAltList
: labeledAlt (OR labeledAlt)* -> labeledAlt+
;
labeledAlt
: alternative (POUND id {((AltAST)$alternative.tree).altLabel=$id.tree;})?
-> alternative
;
altList
: alternative (OR alternative)* -> alternative+
;
@ -715,7 +708,7 @@ atom
input.LT(2).getCharPositionInLine()+1==input.LT(3).getCharPositionInLine()
}?
id DOT ruleref -> ^(DOT id ruleref)
|
*/
range (ROOT^ | BANG^)? // Range x..y - only valid in lexers
@ -742,7 +735,7 @@ wildcard
-> {astop!=null}? ^($astop ^(WILDCARD<TerminalAST>[$DOT] elementOptions?))
-> ^(WILDCARD<TerminalAST>[$DOT] elementOptions?)
;
// --------------------
// Inverted element set
//

View File

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