Package: VdmCharRule

VdmCharRule

nameinstructionbranchcomplexitylinemethod
VdmCharRule()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
evaluate(ICharacterScanner)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
evaluate(ICharacterScanner, boolean)
M: 54 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 18 C: 0
0%
M: 1 C: 0
0%
getSuccessToken()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isAcceptedEscapeSequence(ICharacterScanner)
M: 29 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
isValdChar(int)
M: 16 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
resetScanner(ICharacterScanner, int)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.ui
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.ui.editor.partitioning;
23:
24: import org.eclipse.jface.text.rules.ICharacterScanner;
25: import org.eclipse.jface.text.rules.IPredicateRule;
26: import org.eclipse.jface.text.rules.IToken;
27: import org.eclipse.jface.text.rules.Token;
28:
29: public class VdmCharRule implements IPredicateRule
30: {
31:         private static final char QUOTE = '\'';
32:         private static final char BACK_SLASH = '\\';
33:
34:         @Override
35:         public IToken evaluate(ICharacterScanner scanner)
36:         {
37:                 return evaluate(scanner, true);
38:         }
39:
40:         @Override
41:         public IToken getSuccessToken()
42:         {
43:                 return new Token(IVdmPartitions.STRING);
44:         }
45:
46:         @Override
47:         public IToken evaluate(ICharacterScanner scanner, boolean resume)
48:         {
49:                 // Examples of chars '\uffff' , '\xff', 'a'
50:                 int r = scanner.read();
51:                 int reads = 1;
52:
53:•                if (r == ICharacterScanner.EOF && r != QUOTE)
54:                 {
55:                         resetScanner(scanner, reads);
56:                         return Token.UNDEFINED;
57:                 }
58:
59:•                if (r == QUOTE)
60:                 {
61:                         r = scanner.read();
62:                         reads++;
63:
64:                         // Is the char using escape characters? Could be '\uffff' or '\xff'
65:•                        if (r == BACK_SLASH)
66:                         {
67:•                                if (isAcceptedEscapeSequence(scanner))
68:                                 {
69:                                         return getSuccessToken();
70:                                 }
71:                         } else
72:                         // The char is not using escape characters
73:                         {
74:•                                if (isValdChar(r))
75:                                 {
76:                                         r = scanner.read();
77:                                         reads++;
78:
79:•                                        if (r == QUOTE)
80:                                         {
81:                                                 return getSuccessToken();
82:                                         }
83:                                 }
84:                         }
85:                 }
86:
87:                 resetScanner(scanner, reads);
88:                 return Token.UNDEFINED;
89:         }
90:
91:         private void resetScanner(ICharacterScanner scanner, int unreads)
92:         {
93:•                for (int i = 0; i < unreads; i++)
94:                 {
95:                         scanner.unread();
96:                 }
97:         }
98:
99:         private boolean isValdChar(int c)
100:         {
101:•                return c != ICharacterScanner.EOF && c != '\n' && c != '\t'
102:•                                && c != '\r';
103:         }
104:
105:         private boolean isAcceptedEscapeSequence(ICharacterScanner scanner)
106:         {
107:                 int r;
108:                 int reads = 0;
109:•                do
110:                 {
111:
112:                         r = scanner.read();
113:                         reads++;
114:
115:•                        if (r == QUOTE)
116:                         {
117:                                 return true;
118:                         }
119:
120:                 }
121:                 // Keep it simple and assume the maximum char sequence
122:•                while (isValdChar(r) && reads <= 5);
123:
124:                 // Not a valid sequence so unread what has been read
125:•                for (int i = 0; i < reads; i++)
126:                 {
127:                         scanner.unread();
128:                 }
129:
130:                 return false;
131:         }
132: }