View Javadoc

1   /*
2    * $Id: NestedConstructRule.java,v 1.3 2004/09/08 12:42:40 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.editor.rules;
10  
11  import org.eclipse.jface.text.rules.ICharacterScanner;
12  import org.eclipse.jface.text.rules.IToken;
13  import org.eclipse.jface.text.rules.MultiLineRule;
14  import org.eclipse.jface.text.rules.Token;
15  
16  /***
17   * A pattern-based multiple line rule that will ignore nested subsequences
18   * matching a secondary pattern.
19   * @author jlerner
20   */
21  public class NestedConstructRule extends MultiLineRule
22  {
23     private char[] _acStart;
24     private char[] _acEnd;
25     
26     /***
27      * Creates a new NestedConstructRule based on specified patterns.
28      * @param sStartSequence Start sequence for MultiLineRule
29      * @param sEndSequence End sequence for MultieLineRule
30      * @param token Success token
31      * @param sStartIgnoreSequence Starting sequence for the subsequence to be ignored
32      * @param sEndIgnoreSequence Ending sequence for the subsequence to be ignored
33      */
34     public NestedConstructRule(String sStartSequence, 
35                                String sEndSequence, 
36                                IToken token,
37                                String sStartIgnoreSequence,
38                                String sEndIgnoreSequence)
39     {
40        super(sStartSequence, sEndSequence, token);
41  
42        _acStart = sStartIgnoreSequence.toCharArray();
43        _acEnd = sEndIgnoreSequence.toCharArray();
44     }
45     
46     /***
47      * Locates the end of the rule's pattern by examining the characters in the
48      * provided scanner.
49      * @param scanner The scanner being used by this rule
50      * @return <code>true</code> if pattern end is found, <code>false</code> if
51      *         not
52      */
53     protected boolean findEndSequence(ICharacterScanner scanner)
54     {
55        int c;
56        boolean bIgnore = false;
57        while ((c = scanner.read()) != ICharacterScanner.EOF)
58        {
59           if (bIgnore)
60           {
61              if (c == _acEnd[0] && sequenceDetected(scanner, _acEnd, false))
62              {
63                 bIgnore = false;
64                 //make sure an end-of-ignore doesn't also register as
65                 //end-of-sequence
66                 continue;
67              }
68              else
69              {
70                 continue;
71              }
72           }
73           if (c == fEndSequence[0])
74           {
75              if (sequenceDetected(scanner, fEndSequence, false))
76              {
77                 return true;
78              }
79           }
80           else if (c == _acStart[0])
81           {
82              if (sequenceDetected(scanner, _acStart, false))
83              {
84                 bIgnore = true;
85              }
86           }
87        }
88        scanner.unread();
89        return false;
90     }
91  
92     /* (non-Javadoc)
93      * @see org.eclipse.jface.text.rules.IPredicateRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner, boolean)
94      */
95     public IToken evaluate(ICharacterScanner scanner, boolean resume)
96     {
97        if (!resume)
98        {
99           int c = scanner.read();
100          if (c == this.fStartSequence[0])
101          {
102             if (sequenceDetected(scanner, fStartSequence, false))
103             {
104                if (findEndSequence(scanner))
105                {
106                   return fToken;
107                }
108             }
109          }
110          scanner.unread();
111          return Token.UNDEFINED;
112       }
113       else if (findEndSequence(scanner))
114       {
115          return fToken;
116       }
117       return Token.UNDEFINED;
118    }
119 
120    /* (non-Javadoc)
121     * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
122     */
123    public IToken evaluate(ICharacterScanner scanner)
124    {
125       return evaluate(scanner, false);
126    }
127 
128 }