6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.javafx.css.parser;
27
28 import java.io.IOException;
29 import java.io.Reader;
30 import java.util.HashMap;
31 import java.util.Map;
32
33
34 final class CSSLexer {
35
36 /* Lazy instantiation */
37 private static class InstanceHolder {
38 final static CSSLexer INSTANCE = new CSSLexer();
39 }
40
41 public static CSSLexer getInstance() {
42 return InstanceHolder.INSTANCE;
43 }
44
45 final static int STRING = 10;
46 final static int IDENT = 11;
47 final static int FUNCTION = 12;
48 final static int NUMBER = 13;
49 final static int CM = 14;
50 final static int EMS = 15;
51 final static int EXS = 16;
52 final static int IN = 17;
53 final static int MM = 18;
54 final static int PC = 19;
55 final static int PT = 20;
56 final static int PX = 21;
57 final static int PERCENTAGE = 22;
58 final static int DEG = 23;
59 final static int GRAD = 24;
60 final static int RAD = 25;
61 final static int TURN = 26;
62 final static int GREATER = 27;
63 final static int LBRACE = 28;
64 final static int RBRACE = 29;
337 // from trailingDigitsState, next state must be another digit or units
338 map.put(
339 trailingDigitsState,
340 new LexerState[] {
341 trailingDigitsState,
342 unitsState,
343 }
344 );
345
346 // UnitsState stays in UnitsState
347 map.put(
348 unitsState,
349 new LexerState[] {
350 unitsState
351 }
352 );
353
354 return map;
355 }
356
357 CSSLexer() {
358 this.stateMap = createStateMap();
359 this.text = new StringBuilder(64);
360 this.currentState = initState;
361 }
362
363 public void setReader(Reader reader) {
364 this.reader = reader;
365 lastc = -1;
366 pos = offset = 0;
367 line = 1;
368 this.currentState = initState;
369 this.token = null;
370 try {
371 this.ch = readChar();
372 } catch (IOException ioe) {
373 token = Token.EOF_TOKEN;
374 }
375 }
376
377 private Token scanImportant() throws IOException{
378 // CSS 2.1 grammar for important_sym
379 // "!"({w}|{comment})*{I}{M}{P}{O}{R}{T}{A}{N}{T}
380 final Recognizer[] important_sym =
381 new Recognizer[] { I, M, P, O, R, T, A, N, T };
382 int current = 0;
383
735
736 private int readChar() throws IOException {
737
738 int c = reader.read();
739
740 // only reset line and pos counters after having read a NL since
741 // a NL token is created after the readChar
742 if (lastc == '\n' || (lastc == '\r' && c != '\n')) {
743 // set pos to 1 since we've already read the first char of the new line
744 pos = 1;
745 offset = 0;
746 line++;
747 } else {
748 pos++;
749 }
750
751 lastc = c;
752 return c;
753 }
754
755 public Token nextToken() {
756
757 Token tok = null;
758 if (token != null) {
759 tok = token;
760 if (token.getType() != Token.EOF) token = null;
761 } else {
762 do {
763 tok = getToken();
764 } while (tok != null &&
765 // tok.getType() != Token.EOF &&
766 Token.SKIP_TOKEN.equals(tok));
767 }
768
769 // reset text buffer and currentState
770 text.delete(0,text.length());
771 currentState = initState;
772
773 return tok;
774 }
775
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javafx.css;
27
28 import java.io.IOException;
29 import java.io.Reader;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import com.sun.javafx.css.parser.LexerState;
34 import com.sun.javafx.css.parser.Recognizer;
35 import com.sun.javafx.css.parser.Token;
36
37
38 final class CssLexer {
39 final static int STRING = 10;
40 final static int IDENT = 11;
41 final static int FUNCTION = 12;
42 final static int NUMBER = 13;
43 final static int CM = 14;
44 final static int EMS = 15;
45 final static int EXS = 16;
46 final static int IN = 17;
47 final static int MM = 18;
48 final static int PC = 19;
49 final static int PT = 20;
50 final static int PX = 21;
51 final static int PERCENTAGE = 22;
52 final static int DEG = 23;
53 final static int GRAD = 24;
54 final static int RAD = 25;
55 final static int TURN = 26;
56 final static int GREATER = 27;
57 final static int LBRACE = 28;
58 final static int RBRACE = 29;
331 // from trailingDigitsState, next state must be another digit or units
332 map.put(
333 trailingDigitsState,
334 new LexerState[] {
335 trailingDigitsState,
336 unitsState,
337 }
338 );
339
340 // UnitsState stays in UnitsState
341 map.put(
342 unitsState,
343 new LexerState[] {
344 unitsState
345 }
346 );
347
348 return map;
349 }
350
351 CssLexer() {
352 this.stateMap = createStateMap();
353 this.text = new StringBuilder(64);
354 this.currentState = initState;
355 }
356
357 void setReader(Reader reader) {
358 this.reader = reader;
359 lastc = -1;
360 pos = offset = 0;
361 line = 1;
362 this.currentState = initState;
363 this.token = null;
364 try {
365 this.ch = readChar();
366 } catch (IOException ioe) {
367 token = Token.EOF_TOKEN;
368 }
369 }
370
371 private Token scanImportant() throws IOException{
372 // CSS 2.1 grammar for important_sym
373 // "!"({w}|{comment})*{I}{M}{P}{O}{R}{T}{A}{N}{T}
374 final Recognizer[] important_sym =
375 new Recognizer[] { I, M, P, O, R, T, A, N, T };
376 int current = 0;
377
729
730 private int readChar() throws IOException {
731
732 int c = reader.read();
733
734 // only reset line and pos counters after having read a NL since
735 // a NL token is created after the readChar
736 if (lastc == '\n' || (lastc == '\r' && c != '\n')) {
737 // set pos to 1 since we've already read the first char of the new line
738 pos = 1;
739 offset = 0;
740 line++;
741 } else {
742 pos++;
743 }
744
745 lastc = c;
746 return c;
747 }
748
749 Token nextToken() {
750
751 Token tok = null;
752 if (token != null) {
753 tok = token;
754 if (token.getType() != Token.EOF) token = null;
755 } else {
756 do {
757 tok = getToken();
758 } while (tok != null &&
759 // tok.getType() != Token.EOF &&
760 Token.SKIP_TOKEN.equals(tok));
761 }
762
763 // reset text buffer and currentState
764 text.delete(0,text.length());
765 currentState = initState;
766
767 return tok;
768 }
769
|