From 324884585bb7927e0df1bfdcaade2e4b912bbc14 Mon Sep 17 00:00:00 2001 From: parrt Date: Sun, 13 Nov 2011 11:35:11 -0800 Subject: [PATCH] add slider [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9306] --- .../org/antlr/v4/runtime/BaseRecognizer.java | 4 ++ .../v4/runtime/DefaultANTLRErrorStrategy.java | 7 +-- .../DefaultANTLRTreeGrammarErrorStrategy.java | 10 +++- .../v4/runtime/InputMismatchException.java | 11 +++- .../v4/runtime/NoViableAltException.java | 8 +++ .../v4/runtime/RecognitionException.java | 2 +- .../src/org/antlr/v4/runtime/Recognizer.java | 4 +- .../antlr/v4/runtime/tree/gui/TreeViewer.java | 56 ++++++++++++------- tool/playground/T.g | 2 + tool/playground/TestT.java | 2 +- .../v4/tool/templates/codegen/Java/Java.stg | 2 + .../antlr/v4/codegen/model/RuleFunction.java | 3 + tool/src/org/antlr/v4/parse/ANTLRParser.g | 17 ++---- .../v4/tool/ast/GrammarASTWithOptions.java | 17 ++++-- 14 files changed, 96 insertions(+), 49 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java b/runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java index 3d30ada59..a85c02c24 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/BaseRecognizer.java @@ -156,6 +156,10 @@ public abstract class BaseRecognizer extends Recognizer { */ 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) { diff --git a/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRErrorStrategy.java index 04648ed4a..1d3231f1b 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRErrorStrategy.java @@ -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) { diff --git a/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRTreeGrammarErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRTreeGrammarErrorStrategy.java index a33b6a036..70a1e6214 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRTreeGrammarErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DefaultANTLRTreeGrammarErrorStrategy.java @@ -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 diff --git a/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java b/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java index d7c6a7103..a8af6751a 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java @@ -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; + } } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java b/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java index 885f2f631..f3d6fb305 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java @@ -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; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java b/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java index 3feeb114f..81376348d 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java @@ -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; diff --git a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java index e32fd27c5..45c68b1a3 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java @@ -208,9 +208,9 @@ public abstract class Recognizer { public List 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 diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java index f5af05497..a31dd8674 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java @@ -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() { diff --git a/tool/playground/T.g b/tool/playground/T.g index 20aacece9..a67e4c623 100644 --- a/tool/playground/T.g +++ b/tool/playground/T.g @@ -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 ; diff --git a/tool/playground/TestT.java b/tool/playground/TestT.java index 816ba4cb0..eacc8d83b 100644 --- a/tool/playground/TestT.java +++ b/tool/playground/TestT.java @@ -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() {{ + v.addHighlightedNodes(new ArrayList() {{ ParseTree c0 = tree.getChild(0); add(c0); add(c0.getChild(0)); diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index da230f6f3..11a2c6101 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -188,10 +188,12 @@ RuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAc } + catch (RecognitionException re) { _errHandler.reportError(this, re); _errHandler.recover(this, re); } + finally { exitRule(RULE_); diff --git a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java index ac51a46ce..bdba7c761 100644 --- a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java +++ b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java @@ -52,6 +52,7 @@ public class RuleFunction extends OutputModelObject { public int index; public Collection args = null; public Rule rule; + public boolean catch_; @ModelElement public List code; @ModelElement public OrderedHashSet 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); diff --git a/tool/src/org/antlr/v4/parse/ANTLRParser.g b/tool/src/org/antlr/v4/parse/ANTLRParser.g index 168d6718a..7a880f526 100644 --- a/tool/src/org/antlr/v4/parse/ANTLRParser.g +++ b/tool/src/org/antlr/v4/parse/ANTLRParser.g @@ -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[$DOT] elementOptions?)) -> ^(WILDCARD[$DOT] elementOptions?) ; - + // -------------------- // Inverted element set // diff --git a/tool/src/org/antlr/v4/tool/ast/GrammarASTWithOptions.java b/tool/src/org/antlr/v4/tool/ast/GrammarASTWithOptions.java index 469f90450..557619e32 100644 --- a/tool/src/org/antlr/v4/tool/ast/GrammarASTWithOptions.java +++ b/tool/src/org/antlr/v4/tool/ast/GrammarASTWithOptions.java @@ -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(); -// 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) {