From: <dal...@us...> - 2014-12-22 04:06:59
|
Revision: 23800 http://sourceforge.net/p/jedit/svn/23800 Author: daleanson Date: 2014-12-22 04:06:51 +0000 (Mon, 22 Dec 2014) Log Message: ----------- Updates to json sidekick. Modified Paths: -------------- plugins/XML/trunk/sidekick/json/JsonParser.java plugins/XML/trunk/sidekick/json/parser/JSON.g4 plugins/XML/trunk/sidekick/json/parser/JSON.tokens plugins/XML/trunk/sidekick/json/parser/JSONLexer.java plugins/XML/trunk/sidekick/json/parser/JSONLexer.tokens plugins/XML/trunk/sidekick/json/parser/JSONNode.java plugins/XML/trunk/sidekick/json/parser/JSONParser.java plugins/XML/trunk/sidekick/json/parser/JSONSideKickListener.java plugins/XML/trunk/sidekick/json/test.json Modified: plugins/XML/trunk/sidekick/json/JsonParser.java =================================================================== --- plugins/XML/trunk/sidekick/json/JsonParser.java 2014-12-21 07:58:06 UTC (rev 23799) +++ plugins/XML/trunk/sidekick/json/JsonParser.java 2014-12-22 04:06:51 UTC (rev 23800) @@ -105,11 +105,8 @@ // build the tree JSONNode parserRoot = listener.getRoot(); - parserRoot.setStartPosition(ElementUtil.createStartPosition(buffer, parserRoot)); - parserRoot.setEndPosition(ElementUtil.createEndPosition(buffer, parserRoot)); - DefaultMutableTreeNode jsonNode = new DefaultMutableTreeNode(parserRoot); - root.add( jsonNode ); - addChildren(jsonNode, buffer); + root.setUserObject( parserRoot ); + addChildren(root, buffer); } catch ( Exception e ) { e.printStackTrace(); } finally { @@ -124,9 +121,6 @@ if ( children != null && children.size() > 0 ) { // add the children as tree nodes for ( JSONNode child : children ) { - child.setStartPosition( ElementUtil.createStartPosition( buffer, child ) ); - child.setEndPosition( ElementUtil.createEndPosition( buffer, child ) ); - // create a tree node for the child and recursively add the childs children DefaultMutableTreeNode childNode = new DefaultMutableTreeNode( child ); node.add( childNode ); @@ -145,5 +139,4 @@ errorSource.addError( ErrorSource.ERROR, buffer.getPath(), pe.getLineNumber(), pe.getColumn(), pe.getColumn() + pe.getLength(), pe.getMessage() ); } } - } Modified: plugins/XML/trunk/sidekick/json/parser/JSON.g4 =================================================================== --- plugins/XML/trunk/sidekick/json/parser/JSON.g4 2014-12-21 07:58:06 UTC (rev 23799) +++ plugins/XML/trunk/sidekick/json/parser/JSON.g4 2014-12-22 04:06:51 UTC (rev 23800) @@ -35,7 +35,7 @@ | array // recursion ; -STRING : '"' (ESC | ~["\\])* '"' ; +STRING : DQUOTE (ESC | ~["\\])* DQUOTE ; fragment ESC : '\\' (["\\/bfnrt] | UNICODE) ; fragment UNICODE : 'u' HEX HEX HEX HEX ; @@ -59,3 +59,4 @@ RBRACE : '}' ; LSQUARE : '[' ; RSQUARE : ']' ; +DQUOTE : '"' ; Modified: plugins/XML/trunk/sidekick/json/parser/JSON.tokens =================================================================== --- plugins/XML/trunk/sidekick/json/parser/JSON.tokens 2014-12-21 07:58:06 UTC (rev 23799) +++ plugins/XML/trunk/sidekick/json/parser/JSON.tokens 2014-12-22 04:06:51 UTC (rev 23800) @@ -1,3 +1,4 @@ +DQUOTE=10 WS=5 LSQUARE=8 RBRACE=7 @@ -7,6 +8,7 @@ LBRACE=6 STRING=3 NUMBER=4 +'"'=10 '}'=7 ':'=2 '['=8 Modified: plugins/XML/trunk/sidekick/json/parser/JSONLexer.java =================================================================== --- plugins/XML/trunk/sidekick/json/parser/JSONLexer.java 2014-12-21 07:58:06 UTC (rev 23799) +++ plugins/XML/trunk/sidekick/json/parser/JSONLexer.java 2014-12-22 04:06:51 UTC (rev 23800) @@ -20,18 +20,18 @@ new PredictionContextCache(); public static final int T__1=1, T__0=2, STRING=3, NUMBER=4, WS=5, LBRACE=6, RBRACE=7, LSQUARE=8, - RSQUARE=9; + RSQUARE=9, DQUOTE=10; public static String[] modeNames = { "DEFAULT_MODE" }; public static final String[] tokenNames = { "'\\u0000'", "'\\u0001'", "'\\u0002'", "'\\u0003'", "'\\u0004'", "'\\u0005'", - "'\\u0006'", "'\\u0007'", "'\b'", "'\t'" + "'\\u0006'", "'\\u0007'", "'\b'", "'\t'", "'\n'" }; public static final String[] ruleNames = { "T__1", "T__0", "STRING", "ESC", "UNICODE", "HEX", "NUMBER", "INT", "EXP", - "WS", "LBRACE", "RBRACE", "LSQUARE", "RSQUARE" + "WS", "LBRACE", "RBRACE", "LSQUARE", "RSQUARE", "DQUOTE" }; @@ -59,39 +59,41 @@ public ATN getATN() { return _ATN; } public static final String _serializedATN = - "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2\13\177\b\1\4\2\t"+ + "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2\f\u0083\b\1\4\2\t"+ "\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13"+ - "\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\3\2\3\2\3\3\3\3\3\4\3\4\3\4"+ - "\7\4\'\n\4\f\4\16\4*\13\4\3\4\3\4\3\5\3\5\3\5\5\5\61\n\5\3\6\3\6\3\6\3"+ - "\6\3\6\3\6\3\7\3\7\3\b\5\b<\n\b\3\b\3\b\3\b\6\bA\n\b\r\b\16\bB\3\b\5\b"+ - "F\n\b\3\b\5\bI\n\b\3\b\3\b\3\b\3\b\5\bO\n\b\3\b\3\b\3\b\3\b\3\b\3\b\3"+ - "\b\3\b\3\b\3\b\3\b\3\b\3\b\3\b\5\b_\n\b\3\t\3\t\3\t\7\td\n\t\f\t\16\t"+ - "g\13\t\5\ti\n\t\3\n\3\n\5\nm\n\n\3\n\3\n\3\13\6\13r\n\13\r\13\16\13s\3"+ - "\13\3\13\3\f\3\f\3\r\3\r\3\16\3\16\3\17\3\17\2\2\20\3\3\5\4\7\5\t\2\13"+ - "\2\r\2\17\6\21\2\23\2\25\7\27\b\31\t\33\n\35\13\3\2\n\4\2$$^^\n\2$$\61"+ - "\61^^ddhhppttvv\5\2\62;CHch\3\2\62;\3\2\63;\4\2GGgg\4\2--//\5\2\13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2KL\3\2\2\2LM\5"+ + "\21\t\2MN\5\23\n\2Na\3\2\2\2OQ\7/\2\2PO\3\2\2\2PQ\3\2\2\2QR\3\2\2\2Ra"+ + "\5\21\t\2ST\7v\2\2TU\7t\2\2UV\7w\2\2Va\7g\2\2WX\7h\2\2XY\7c\2\2YZ\7n\2"+ + "\2Z[\7u\2\2[a\7g\2\2\\]\7p\2\2]^\7w\2\2^_\7n\2\2_a\7n\2\2`=\3\2\2\2`J"+ + "\3\2\2\2`P\3\2\2\2`S\3\2\2\2`W\3\2\2\2`\\\3\2\2\2a\20\3\2\2\2bk\7\62\2"+ + "\2cg\t\6\2\2df\t\5\2\2ed\3\2\2\2fi\3\2\2\2ge\3\2\2\2gh\3\2\2\2hk\3\2\2"+ + "\2ig\3\2\2\2jb\3\2\2\2jc\3\2\2\2k\22\3\2\2\2ln\t\7\2\2mo\t\b\2\2nm\3\2"+ + "\2\2no\3\2\2\2op\3\2\2\2pq\5\21\t\2q\24\3\2\2\2rt\t\t\2\2sr\3\2\2\2tu"+ + "\3\2\2\2us\3\2\2\2uv\3\2\2\2vw\3\2\2\2wx\b\13\2\2x\26\3\2\2\2yz\7}\2\2"+ + "z\30\3\2\2\2{|\7\177\2\2|\32\3\2\2\2}~\7]\2\2~\34\3\2\2\2\177\u0080\7"+ + "_\2\2\u0080\36\3\2\2\2\u0081\u0082\7$\2\2\u0082 \3\2\2\2\20\2(*\62=DG"+ + "JP`gjnu\3\b\2\2"; public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); static { Modified: plugins/XML/trunk/sidekick/json/parser/JSONLexer.tokens =================================================================== --- plugins/XML/trunk/sidekick/json/parser/JSONLexer.tokens 2014-12-21 07:58:06 UTC (rev 23799) +++ plugins/XML/trunk/sidekick/json/parser/JSONLexer.tokens 2014-12-22 04:06:51 UTC (rev 23800) @@ -1,3 +1,4 @@ +DQUOTE=10 WS=5 LSQUARE=8 RBRACE=7 @@ -7,6 +8,7 @@ LBRACE=6 STRING=3 NUMBER=4 +'"'=10 '}'=7 ':'=2 '['=8 Modified: plugins/XML/trunk/sidekick/json/parser/JSONNode.java =================================================================== --- plugins/XML/trunk/sidekick/json/parser/JSONNode.java 2014-12-21 07:58:06 UTC (rev 23799) +++ plugins/XML/trunk/sidekick/json/parser/JSONNode.java 2014-12-22 04:06:51 UTC (rev 23800) @@ -13,8 +13,11 @@ public class JSONNode extends Asset implements Comparable, SideKickElement { private boolean isArray = false; + private boolean isObject = false; + private boolean isPair = false; + private boolean isNumberOrString = false; private Icon icon = null; - private Set<JSONNode> children = null; + private TreeSet<JSONNode> children = null; private Location startLocation = new Location(); private Location endLocation = new Location(); private Position startPosition = new Position() { @@ -36,14 +39,38 @@ super(name); } - public void setIsArray(boolean b) { - isArray = b; - } + public boolean isArray() { + return isArray; + } + + public void setIsArray(boolean isArray) { + this.isArray = isArray; + } + + public boolean isObject() { + return isObject; + } + + public void setIsObject(boolean isObject) { + this.isObject = isObject; + } + + public boolean isPair() { + return isPair; + } + + public void setIsPair(boolean isPair) { + this.isPair = isPair; + } + + public boolean isNumberOrString() { + return isNumberOrString; + } + + public void setIsNumberOrString(boolean b) { + this.isNumberOrString = b; + } - public boolean isArray() { - return isArray; - } - public void addChild(JSONNode child) { if (child == null) { return; @@ -54,9 +81,21 @@ children.add(child); } + // this is useful when it is known that the node can have only one child + public JSONNode getFirstChild() { + if (children == null || children.isEmpty()) { + return null; + } + return children.first(); + } + public Set<JSONNode> getChildren() { return children; } + + public void removeChildren() { + children = null; + } public void setStartLocation(Location start) { startLocation = start; @@ -109,7 +148,16 @@ } public String getLongString() { - return name + ": " + getStartLocation() + ":" + getEndLocation(); + String type = null; + if (isObject()) + type = "object"; + else if (isArray()) + type = "array"; + else if (isPair()) + type = "pair"; + else if (isNumberOrString()) + type = "atom"; + return name + ": type=" + type + ":" + getStartLocation() + ":" + getEndLocation() + ":" + getStartPosition().getOffset() + ":" + getEndPosition().getOffset(); } public int compareTo(Object o) { Modified: plugins/XML/trunk/sidekick/json/parser/JSONParser.java =================================================================== --- plugins/XML/trunk/sidekick/json/parser/JSONParser.java 2014-12-21 07:58:06 UTC (rev 23799) +++ plugins/XML/trunk/sidekick/json/parser/JSONParser.java 2014-12-22 04:06:51 UTC (rev 23800) @@ -20,10 +20,10 @@ new PredictionContextCache(); public static final int T__1=1, T__0=2, STRING=3, NUMBER=4, WS=5, LBRACE=6, RBRACE=7, LSQUARE=8, - RSQUARE=9; + RSQUARE=9, DQUOTE=10; public static final String[] tokenNames = { "<INVALID>", "','", "':'", "STRING", "NUMBER", "WS", "'{'", "'}'", "'['", - "']'" + "']'", "'\"'" }; public static final int RULE_json = 0, RULE_object = 1, RULE_pair = 2, RULE_array = 3, RULE_value = 4; @@ -358,7 +358,7 @@ } public static final String _serializedATN = - "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\3\139\4\2\t\2\4\3\t"+ + "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\3\f9\4\2\t\2\4\3\t"+ "\3\4\4\t\4\4\5\t\5\4\6\t\6\3\2\3\2\5\2\17\n\2\3\3\3\3\3\3\3\3\7\3\25\n"+ "\3\f\3\16\3\30\13\3\3\3\3\3\3\3\3\3\5\3\36\n\3\3\4\3\4\3\4\3\4\3\5\3\5"+ "\3\5\3\5\7\5(\n\5\f\5\16\5+\13\5\3\5\3\5\3\5\3\5\5\5\61\n\5\3\6\3\6\3"+ Modified: plugins/XML/trunk/sidekick/json/parser/JSONSideKickListener.java =================================================================== --- plugins/XML/trunk/sidekick/json/parser/JSONSideKickListener.java 2014-12-21 07:58:06 UTC (rev 23799) +++ plugins/XML/trunk/sidekick/json/parser/JSONSideKickListener.java 2014-12-22 04:06:51 UTC (rev 23800) @@ -9,9 +9,11 @@ import java.util.ArrayDeque; import java.util.Deque; import javax.swing.ImageIcon; +import javax.swing.text.Position; import eclipseicons.EclipseIconsPlugin; +// Locations and Positions for all assets are set here public class JSONSideKickListener extends JSONBaseListener { Deque<JSONNode> stack = new ArrayDeque<JSONNode>(); @@ -25,83 +27,137 @@ public JSONNode getRoot() { return root; } - @Override public void enterPair( @NotNull JSONParser.PairContext ctx ) { - JSONNode node = new JSONNode( ctx.STRING().getText() ); - node.setStartLocation( getStartLocation( ctx ) ); - node.setEndLocation( getEndLocation( ctx ) ); - node.setIcon(pairIcon); - stack.push( node ); - } - @Override public void exitPair( @NotNull JSONParser.PairContext ctx ) { - JSONNode node = stack.pop(); - JSONNode parent = stack.peek(); - parent.addChild( node ); - } + @Override public void enterJson( @NotNull JSONParser.JsonContext ctx ) { root = new JSONNode(); root.setStartLocation( getStartLocation( ctx ) ); root.setEndLocation( getEndLocation( ctx ) ); stack.push( root ); } + @Override public void exitJson( @NotNull JSONParser.JsonContext ctx ) { - stack.pop(); - } - @Override public void enterValue( @NotNull JSONParser.ValueContext ctx ) { - // if the value is a number, which is defined as a number, true, false, or null, - // then go ahead and add it to the name of the parent node. If it is an object - // or an array, go ahead and recurse. - JSONNode node = new JSONNode(); - TerminalNode number = ctx.NUMBER(); - if ( number != null ) { - JSONNode parent = stack.peek(); - parent.setName(parent.getName() + ": " + number.getText()); + stack.pop(); // stack is empty at this point + JSONNode child = root.getFirstChild(); + Position start; + Position end; + if (child != null) { + start = child.getStartPosition(); + end = child.getEndPosition(); } else { - number = ctx.STRING(); - if ( number != null ) { - JSONNode parent = stack.peek(); - parent.setName(parent.getName() + ": " + number.getText()); - } + start = new SideKickPosition(0); + end = start; } - node.setStartLocation( getStartLocation( ctx ) ); - node.setEndLocation( getEndLocation( ctx ) ); - stack.push( node ); + root.setStartPosition(start); + root.setEndPosition(end); } - @Override public void exitValue( @NotNull JSONParser.ValueContext ctx ) { - JSONNode node = stack.pop(); - String name = node.getName(); - // numbers and strings are already part of the parent node name, this - // next 'if' adds object and array nodes to the parent. - if (name == null || name.isEmpty()) { - JSONNode parent = stack.peek(); - parent.addChild( node ); - } - } + @Override public void enterObject( @NotNull JSONParser.ObjectContext ctx ) { JSONNode node = new JSONNode(); + node.setIsObject(true); node.setStartLocation( getStartLocation( ctx ) ); node.setEndLocation( getEndLocation( ctx ) ); - node.setIcon(objectIcon); + node.setStartPosition(new SideKickPosition(ctx.LBRACE().getSymbol().getStartIndex())); + node.setEndPosition(new SideKickPosition(ctx.RBRACE().getSymbol().getStopIndex())); stack.push( node ); } + @Override public void exitObject( @NotNull JSONParser.ObjectContext ctx ) { JSONNode node = stack.pop(); JSONNode parent = stack.peek(); - parent.addChild( node ); + for (JSONNode newKid : node.getChildren()) { + parent.addChild(newKid); + } } + @Override public void enterArray( @NotNull JSONParser.ArrayContext ctx ) { JSONNode node = new JSONNode(); node.setIsArray(true); node.setStartLocation( getStartLocation( ctx ) ); node.setEndLocation( getEndLocation( ctx ) ); - node.setIcon(arrayIcon); + node.setStartPosition(new SideKickPosition(ctx.LSQUARE().getSymbol().getStartIndex())); + node.setEndPosition(new SideKickPosition(ctx.RSQUARE().getSymbol().getStopIndex())); stack.push( node ); } + @Override public void exitArray( @NotNull JSONParser.ArrayContext ctx ) { JSONNode node = stack.pop(); JSONNode parent = stack.peek(); + for (JSONNode newKid : node.getChildren()) { + parent.addChild(newKid); + } + } + + @Override public void enterPair( @NotNull JSONParser.PairContext ctx ) { + TerminalNode string = ctx.STRING(); + JSONNode node = new JSONNode( string.getText() ); + node.setIsPair(true); + node.setStartLocation( getStartLocation( ctx ) ); + node.setEndLocation( getEndLocation( ctx ) ); + node.setStartPosition(new SideKickPosition(string.getSymbol().getStartIndex())); + stack.push( node ); + } + + @Override public void exitPair( @NotNull JSONParser.PairContext ctx ) { + JSONNode node = stack.pop(); + JSONNode child = node.getFirstChild(); + node.setEndPosition(child.getEndPosition()); + node.removeChildren(); + if (child != null ) { + if (child.isNumberOrString()) { + node.setName(node.getName() + ": " + child.getName()); + node.setIcon(pairIcon); + } else if (child.isObject()) { + for (JSONNode newKid : child.getChildren()) { + node.addChild(newKid); + } + node.setIcon(objectIcon); + } else if (child.isArray()) { + for (JSONNode newKid : child.getChildren()) { + node.addChild(newKid); + } + node.setIcon(arrayIcon); + } + } + JSONNode parent = stack.peek(); parent.addChild( node ); } - + + @Override public void enterValue( @NotNull JSONParser.ValueContext ctx ) { + JSONNode node = new JSONNode(); + if (ctx.NUMBER() != null) { + TerminalNode value = ctx.NUMBER(); + node.setName(value.getText()); + node.setIsNumberOrString(true); + node.setStartPosition(new SideKickPosition(value.getSymbol().getStartIndex())); + node.setEndPosition(new SideKickPosition(value.getSymbol().getStopIndex())); + } else if (ctx.STRING() != null) { + TerminalNode value = ctx.STRING(); + node.setName(value.getText()); + node.setIsNumberOrString(true); + node.setStartPosition(new SideKickPosition(value.getSymbol().getStartIndex())); + node.setEndPosition(new SideKickPosition(value.getSymbol().getStopIndex())); + } else if (ctx.object() != null) { + JSONParser.ObjectContext value = ctx.object(); + node.setIsObject(true); + node.setStartPosition(new SideKickPosition(value.LBRACE().getSymbol().getStartIndex())); + node.setEndPosition(new SideKickPosition(value.RBRACE().getSymbol().getStopIndex())); + } else if (ctx.array() != null) { + JSONParser.ArrayContext value = ctx.array(); + node.setIsArray(true); + node.setStartPosition(new SideKickPosition(value.LSQUARE().getSymbol().getStartIndex())); + node.setEndPosition(new SideKickPosition(value.RSQUARE().getSymbol().getStopIndex())); + } + node.setStartLocation( getStartLocation( ctx ) ); + node.setEndLocation( getEndLocation( ctx ) ); + stack.push( node ); + } + + @Override public void exitValue( @NotNull JSONParser.ValueContext ctx ) { + JSONNode node = stack.pop(); + JSONNode parent = stack.peek(); + parent.addChild(node); + } + // return a Location representing the start of the rule context private Location getStartLocation( ParserRuleContext ctx ) { int line = ctx.getStart().getLine(); @@ -115,5 +171,4 @@ int col = ctx.getStop().getCharPositionInLine(); return new Location( line, col ); } - } \ No newline at end of file Modified: plugins/XML/trunk/sidekick/json/test.json =================================================================== --- plugins/XML/trunk/sidekick/json/test.json 2014-12-21 07:58:06 UTC (rev 23799) +++ plugins/XML/trunk/sidekick/json/test.json 2014-12-22 04:06:51 UTC (rev 23800) @@ -1,20 +1,31 @@ + { - "glossary": { + "glossary": + { "title": "example glossary", - "GlossDiv": { + "GlossDiv": + { "title": "S", - "GlossList": { - "GlossEntry": { + "array": [ + { + "one": "one", + "two": "two" + }, "three", "four"], + "GlossList": + { + "GlossEntry": + { "ID": "SGML", - "SortAs": "SGML", - "GlossTerm": "Standard Generalized Markup Language", - "Acronym": "SGML", - "Abbrev": "ISO 8879:1986", - "GlossDef": { + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": + { "para": "A meta-markup language, used to create markup languages such as DocBook.", - "GlossSeeAlso": ["GML", "XML"] + "GlossSeeAlso": ["GML", "XML"] }, - "GlossSee": "markup" + "GlossSee": "markup" } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |