semantic highlights for rules
authorMarek Fukala <mfukala@netbeans.org>
Tue, 15 Jan 2013 11:00:41 +0100
changeset 17929f47cd4b5c58f
parent 17928 a9373da4c569
child 17930 5a5600b5542a
semantic highlights for rules
o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrCslLanguage.java
o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrSemanticAnalyzer.java
o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/Utils.java
o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/fontsColors.xml
     1.1 --- a/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrCslLanguage.java	Tue Jan 15 10:42:18 2013 +0100
     1.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrCslLanguage.java	Tue Jan 15 11:00:41 2013 +0100
     1.3 @@ -43,6 +43,7 @@
     1.4  
     1.5  import org.netbeans.core.spi.multiview.MultiViewElement;
     1.6  import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
     1.7 +import org.netbeans.modules.csl.api.SemanticAnalyzer;
     1.8  import org.netbeans.modules.csl.api.StructureScanner;
     1.9  import org.netbeans.modules.csl.spi.DefaultLanguageConfig;
    1.10  import org.netbeans.modules.csl.spi.LanguageRegistration;
    1.11 @@ -105,6 +106,10 @@
    1.12      public StructureScanner getStructureScanner() {
    1.13          return new AntlrStructureScanner();
    1.14      }
    1.15 -    
    1.16 +
    1.17 +    @Override
    1.18 +    public SemanticAnalyzer getSemanticAnalyzer() {
    1.19 +        return new AntlrSemanticAnalyzer();
    1.20 +    }
    1.21      
    1.22  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrSemanticAnalyzer.java	Tue Jan 15 11:00:41 2013 +0100
     2.3 @@ -0,0 +1,129 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * If you wish your version of this file to be governed by only the CDDL
    2.31 + * or only the GPL Version 2, indicate your decision by adding
    2.32 + * "[Contributor] elects to include this software in this distribution
    2.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.34 + * single choice of license, a recipient has the option to distribute
    2.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.36 + * to extend the choice of license to its licensees as provided above.
    2.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.38 + * Version 2 license, then the option applies only if the new code is
    2.39 + * made subject to such option by the copyright holder.
    2.40 + *
    2.41 + * Contributor(s):
    2.42 + *
    2.43 + * Portions Copyrighted 2013 Sun Microsystems, Inc.
    2.44 + */
    2.45 +package org.netbeans.modules.antlr.editor;
    2.46 +
    2.47 +import java.util.ArrayList;
    2.48 +import java.util.HashMap;
    2.49 +import java.util.List;
    2.50 +import java.util.Map;
    2.51 +import java.util.Set;
    2.52 +import org.antlr.runtime.CommonToken;
    2.53 +import org.antlr.runtime.tree.CommonTree;
    2.54 +import org.antlr.runtime.tree.TreeVisitor;
    2.55 +import org.antlr.runtime.tree.TreeVisitorAction;
    2.56 +import org.netbeans.modules.antlr.editor.gen.ANTLRv3Parser;
    2.57 +import org.netbeans.modules.csl.api.ColoringAttributes;
    2.58 +import org.netbeans.modules.csl.api.OffsetRange;
    2.59 +import org.netbeans.modules.csl.api.SemanticAnalyzer;
    2.60 +import org.netbeans.modules.csl.api.StructureItem;
    2.61 +import org.netbeans.modules.parsing.spi.Parser;
    2.62 +import org.netbeans.modules.parsing.spi.Scheduler;
    2.63 +import org.netbeans.modules.parsing.spi.SchedulerEvent;
    2.64 +import org.openide.filesystems.FileObject;
    2.65 +
    2.66 +/**
    2.67 + *
    2.68 + * @author marekfukala
    2.69 + */
    2.70 +public class AntlrSemanticAnalyzer extends SemanticAnalyzer {
    2.71 +
    2.72 +    private Map<OffsetRange, Set<ColoringAttributes>> semanticHighlights;
    2.73 +
    2.74 +    @Override
    2.75 +    public Map<OffsetRange, Set<ColoringAttributes>> getHighlights() {
    2.76 +        return semanticHighlights;
    2.77 +    }
    2.78 +
    2.79 +    @Override
    2.80 +    public void cancel() {
    2.81 +    }
    2.82 +
    2.83 +    @Override
    2.84 +    public void run(Parser.Result info, SchedulerEvent event) {
    2.85 +        resume();
    2.86 +
    2.87 +        NbAntlrParserResult result = (NbAntlrParserResult) info;
    2.88 +        semanticHighlights = new HashMap<OffsetRange, Set<ColoringAttributes>>();
    2.89 +        
    2.90 +        TreeVisitor visitor = new TreeVisitor();
    2.91 +        visitor.visit(result.getParseTree(), new TreeVisitorAction() {
    2.92 +            @Override
    2.93 +            public Object pre(Object o) {
    2.94 +                CommonTree t = (CommonTree) o;
    2.95 +                switch(t.getType()) {
    2.96 +                    case ANTLRv3Parser.RULE:
    2.97 +                        //get next ID rule
    2.98 +                        CommonTree id = (CommonTree) t.getChild(0);
    2.99 +                        assert id.getType() == ANTLRv3Parser.ID;
   2.100 +                        
   2.101 +                        CommonToken ct = (CommonToken)id.getToken();
   2.102 +                        OffsetRange range = Utils.getCommonTokenOffsetRange(ct);
   2.103 +
   2.104 +                        semanticHighlights.put(range, ColoringAttributes.METHOD_SET);
   2.105 +                        
   2.106 +                        break;
   2.107 +                }
   2.108 +                return t;
   2.109 +            }
   2.110 +
   2.111 +            @Override
   2.112 +            public Object post(Object o) {
   2.113 +                CommonTree t = (CommonTree) o;
   2.114 +                return t;
   2.115 +            }
   2.116 +        });
   2.117 +
   2.118 +    }
   2.119 +
   2.120 +    @Override
   2.121 +    public int getPriority() {
   2.122 +        return 500; //higher means less important
   2.123 +    }
   2.124 +
   2.125 +    @Override
   2.126 +    public Class<? extends Scheduler> getSchedulerClass() {
   2.127 +        return null;
   2.128 +    }
   2.129 +
   2.130 +    private void resume() {
   2.131 +    }
   2.132 +}
     3.1 --- a/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/Utils.java	Tue Jan 15 10:42:18 2013 +0100
     3.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/Utils.java	Tue Jan 15 11:00:41 2013 +0100
     3.3 @@ -45,6 +45,7 @@
     3.4  import org.antlr.runtime.CommonToken;
     3.5  import org.antlr.runtime.tree.Tree;
     3.6  import org.netbeans.modules.antlr.editor.gen.ANTLRv3Parser;
     3.7 +import org.netbeans.modules.csl.api.OffsetRange;
     3.8  
     3.9  /**
    3.10   *
    3.11 @@ -63,16 +64,16 @@
    3.12       * @return two members array - arr[0] is the start offset, arr[1] is the end
    3.13       * offset
    3.14       */
    3.15 -    public static int[] getCommonTokenOffsetRange(CommonToken token) {
    3.16 +    public static OffsetRange getCommonTokenOffsetRange(CommonToken token) {
    3.17          if (token.getType() == CommonToken.EOF) {
    3.18              //"eof token" points at the end offset of the source, with zero length
    3.19 -            return new int[]{token.getStartIndex(), token.getStopIndex()};
    3.20 +            return new OffsetRange(token.getStartIndex(), token.getStopIndex());
    3.21          } else {
    3.22 -            return new int[]{token.getStartIndex(), token.getStopIndex() + 1};
    3.23 +            return new OffsetRange(token.getStartIndex(), token.getStopIndex() + 1);
    3.24          }
    3.25 -
    3.26      }
    3.27      
    3.28 +    
    3.29       public static void dumpTree(Tree node) {
    3.30          PrintWriter pw = new PrintWriter(System.out);
    3.31          dumpTree(node, pw);
     4.1 --- a/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/fontsColors.xml	Tue Jan 15 10:42:18 2013 +0100
     4.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/fontsColors.xml	Tue Jan 15 11:00:41 2013 +0100
     4.3 @@ -54,10 +54,8 @@
     4.4      </fontcolor>
     4.5      
     4.6      <fontcolor name="rules" default="identifier">
     4.7 -        <font style="bold" />
     4.8      </fontcolor>
     4.9      
    4.10 -    
    4.11      <fontcolor name="strings" default="string"/>
    4.12      
    4.13      <fontcolor name="others" default="default"/>    
    4.14 @@ -67,4 +65,8 @@
    4.15      <fontcolor name="keywords" default="keyword"/>
    4.16      <fontcolor name="numbers" default="number"/>
    4.17      
    4.18 +    <fontcolor name="mod-method" default="identifier">
    4.19 +         <font style="bold" />
    4.20 +    </fontcolor>
    4.21 +    
    4.22  </fontscolors>