Changeset 9 for trunk/src/org/modelica


Ignore:
Timestamp:
09/14/05 16:05:24 (19 years ago)
Author:
remar
Message:
  • Removed Peter Bunus code
  • Added code abducted from the PyDev? Eclipse plug-in
  • Made a correct list of Modelica keywords and types
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/modelica/mdt/editor/ModelicaRuleScanner.java

    r8 r9  
    77package org.modelica.mdt.editor;
    88
     9import java.util.ArrayList;
     10import java.util.List;
     11
    912import org.eclipse.jface.text.TextAttribute;
    1013import org.eclipse.jface.text.rules.EndOfLineRule;
    1114import org.eclipse.jface.text.rules.IRule;
    1215import org.eclipse.jface.text.rules.IToken;
     16import org.eclipse.jface.text.rules.IWhitespaceDetector;
     17import org.eclipse.jface.text.rules.IWordDetector;
    1318import org.eclipse.jface.text.rules.RuleBasedScanner;
    14 import org.eclipse.jface.text.rules.SingleLineRule;
    1519import org.eclipse.jface.text.rules.MultiLineRule;
     20import org.eclipse.jface.text.rules.WhitespaceRule;
    1621import org.eclipse.jface.text.rules.WordRule;
    1722import org.eclipse.jface.text.rules.Token;
     
    2328/**
    2429 * @author Peter Bunus
    25  *
    26  * TODO To change the template for this generated type comment go to
    27  * Window - Preferences - Java - Code Style - Code Templates
     30 * @author Andreas Remar, x05andre@ida.liu.se
     31 *
     32 * 2005-09-14, Andreas Remar
     33 *    Recoded from scratch, most code abducted from PyDev
    2834 */
    2935public class ModelicaRuleScanner extends RuleBasedScanner
    3036                                 implements ModelicaSyntax {
    3137
    32     //comments are shown in green 4 (0,139,0)
    3338    private static Color COMMENT_COLOR = new Color(Display.getCurrent(), new RGB(0, 139, 0));
    34     //Modelica reserved keywords are shown in red4 (139,0,0)
    3539    private static Color KEYWORD_COLOR = new Color(Display.getCurrent(), new RGB(139, 0, 0));
    36     //Modelica types  are shown in blue4 (0,0,139)
    37     private static Color TYPES_COLOR = new Color(Display.getCurrent(), new RGB(0, 0, 139));
     40    private static Color CODE_COLOR = new Color(Display.getCurrent(), new RGB(0, 0, 0));
     41    private static Color TYPE_COLOR = new Color(Display.getCurrent(), new RGB(0, 0, 139));
     42    private static Color NUMBER_COLOR = new Color(Display.getCurrent(), new RGB(139, 0, 139));
     43    private static Color ERROR_COLOR = new Color(Display.getCurrent(), new RGB(255, 0, 0));
    3844
     45    /**
     46     * Whitespace detector
     47     */
     48    static private class WhiteSpace implements IWhitespaceDetector {
     49        public boolean isWhitespace(char c) {return Character.isWhitespace(c);}
     50    }
     51   
     52    /**
     53     * Modelica keyword detector
     54     */
     55    static private class ModelicaKeywordDetector implements IWordDetector {
     56        // keywords list has to be alphabetized for this to work properly
     57        static public String[] keywords = {
     58            "algorithm","and","annotation","block","break",
     59            "class","connector","constant",
     60            "der","discrete",
     61            "each","elseif","elsewhen","encapsulated","end",
     62            "enumeration","equation",
     63            "extends","external","false","final",
     64            "flow","for","function",
     65            "if","import","in","initial","inner","input",
     66            "loop","model","not","or","outer",
     67            "output","package","parameter",
     68            "partial","protected","public","record",
     69            "redeclare","replacable","return",
     70            "then","time","true","type","when","while","within"
     71        };
     72
     73        public ModelicaKeywordDetector() {
     74        }
     75
     76        public boolean isWordStart(char c) {
     77            return Character.isJavaIdentifierStart(c);
     78        }
     79
     80        public boolean isWordPart(char c) {
     81            return Character.isJavaIdentifierPart(c);
     82        }
     83    }
     84
     85    /**
     86     * Modelica type detector
     87     */
     88    static private class ModelicaTypeDetector implements IWordDetector {
     89        // keywords list has to be alphabetized for this to work properly
     90        static public String[] keywords = {
     91            "Boolean","Integer","Real","String"
     92        };
     93
     94        public ModelicaTypeDetector() {
     95        }
     96
     97        public boolean isWordStart(char c) {
     98            return Character.isJavaIdentifierStart(c);
     99        }
     100
     101        public boolean isWordPart(char c) {
     102            return Character.isJavaIdentifierPart(c);
     103        }
     104    }
     105
     106    static public class NumberDetector implements IWordDetector{
     107        /**
     108         * @see org.eclipse.jface.text.rules.IWordDetector#isWordStart(char)
     109         */
     110        public boolean isWordStart(char c) {
     111            return Character.isDigit(c);
     112        }
     113
     114        /**
     115         * @see org.eclipse.jface.text.rules.IWordDetector#isWordPart(char)
     116         */
     117        public boolean isWordPart(char c) {
     118            return Character.isDigit(c) || c == 'e'  || c == '.';
     119        }
     120    }   
     121   
    39122    public ModelicaRuleScanner() {
     123        super();
    40124       
     125        setupRules();
     126    }
     127
     128    private void setupRules()
     129    {
    41130        IToken commentToken = new Token(new TextAttribute(COMMENT_COLOR,null,SWT.BOLD));
    42131        IToken keywordToken = new Token(new TextAttribute(KEYWORD_COLOR,null,SWT.BOLD));
    43         IToken typesToken   = new Token(new TextAttribute(TYPES_COLOR,null,SWT.BOLD));
    44            
    45         IRule[] rules = new IRule[4];
    46            
    47                
    48         // Add word rule for keywords, types, and constants.
    49         WordRule wordRule = new WordRule(new ModelicaKeywordDetector());
    50         for (int i = 0; i < reservedwords.length; i++)
    51               wordRule.addWord(reservedwords[i], keywordToken);
    52         for (int i = 0; i < types.length; i++)
    53               wordRule.addWord(types[i], typesToken);
    54         for (int i = 0; i < constants.length; i++)
    55               wordRule.addWord(constants[i], keywordToken);
    56         rules[0]=wordRule;
     132        IToken defaultToken = new Token(new TextAttribute(CODE_COLOR,null,SWT.BOLD));
     133        IToken typeToken = new Token(new TextAttribute(TYPE_COLOR,null,SWT.BOLD));
     134        IToken numberToken = new Token(new TextAttribute(NUMBER_COLOR,null,SWT.BOLD));
     135        IToken errorToken = new Token(new TextAttribute(ERROR_COLOR,null,SWT.BOLD));
     136
     137        setDefaultReturnToken(errorToken);
     138        List rules = new ArrayList();
    57139       
    58 //       this will match single line Modelica comments given between quotes "this is a comment"
     140        rules.add(new WhitespaceRule(new WhiteSpace()));
     141       
     142        WordRule wordRule = new WordRule(new ModelicaKeywordDetector(), defaultToken);
     143        for (int i=0; i<ModelicaKeywordDetector.keywords.length;i++) {
     144            wordRule.addWord(ModelicaKeywordDetector.keywords[i], keywordToken);
     145        }
     146        for (int i=0; i<ModelicaTypeDetector.keywords.length;i++) {
     147            wordRule.addWord(ModelicaTypeDetector.keywords[i], typeToken);
     148        }
     149        rules.add(wordRule);
    59150
    60         rules[1] = (new SingleLineRule( "\"", "\"", commentToken,'\\'));
    61         // this will match sinle line Modelica comments precceded by double backslash
    62         rules[2] = (new EndOfLineRule("//", commentToken,'\\'));
    63         //Add rules for multi-line comments
    64         rules[3] = (new MultiLineRule("/*", "*/", commentToken, (char) 0, true));
     151        rules.add(new MultiLineRule( "\"", "\"", commentToken,'\\'));
     152        rules.add(new EndOfLineRule("//", commentToken,'\\'));
     153        rules.add(new MultiLineRule("/*", "*/", commentToken, (char) 0, true));
     154        rules.add(new WordRule(new NumberDetector(), numberToken));
    65155       
    66         setRules(rules);
     156        IRule[] result = new IRule[rules.size()];
     157        rules.toArray(result);
     158        setRules(result);
    67159    }
    68 
    69160}
Note: See TracChangeset for help on using the changeset viewer.