add slider
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9306]
This commit is contained in:
parent
f420f4c327
commit
324884585b
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,26 +217,17 @@ 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() {
|
||||
|
@ -244,9 +236,20 @@ public class TreeViewer extends JComponent {
|
|||
}
|
||||
}
|
||||
);
|
||||
JPanel wrapper = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
||||
wrapper.add(ok);
|
||||
contentPane.add(wrapper, BorderLayout.SOUTH);
|
||||
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);
|
||||
}
|
||||
}
|
||||
);
|
||||
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() {
|
||||
|
|
|
@ -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 ;
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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>);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -253,13 +253,6 @@ optionsSpec
|
|||
|
||||
option
|
||||
: id ASSIGN^ optionValue
|
||||
/*
|
||||
{
|
||||
if ( $id.text.equals("output") ) {
|
||||
if ( $optionValue.text.equals("AST") ) buildAST = true;
|
||||
}
|
||||
}
|
||||
*/
|
||||
;
|
||||
|
||||
// ------------
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue