KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > test > state > StateLexer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.lexer.test.state;
21
22 import org.netbeans.api.lexer.InputAttributes;
23 import org.netbeans.api.lexer.Token;
24 import org.netbeans.spi.lexer.Lexer;
25 import org.netbeans.spi.lexer.LexerInput;
26 import org.netbeans.spi.lexer.LexerRestartInfo;
27 import org.netbeans.spi.lexer.TokenFactory;
28
29 /**
30  * Simple implementation a lexer.
31  *
32  * @author mmetelka
33  */

34 final class StateLexer implements Lexer<StateTokenId> {
35
36     // Copy of LexerInput.EOF
37
private static final int EOF = LexerInput.EOF;
38
39     static final Object JavaDoc AFTER_A = "after_a";
40     static final Object JavaDoc AFTER_B = "after_b";
41     
42     static final Integer JavaDoc AFTER_A_INT = 1;
43     static final Integer JavaDoc AFTER_B_INT = 2;
44
45     private boolean useIntStates;
46     
47     private Object JavaDoc state;
48     
49     private LexerInput input;
50
51     private TokenFactory<StateTokenId> tokenFactory;
52     
53     private LexerRestartInfo<StateTokenId> info;
54     
55     private InputAttributes inputAttributes;
56     
57     StateLexer(LexerRestartInfo<StateTokenId> info) {
58         this.input = info.input();
59         this.tokenFactory = info.tokenFactory();
60         this.state = info.state();
61         this.info = info;
62
63         this.useIntStates = Boolean.TRUE.equals(info.getAttributeValue("states"));
64         Object JavaDoc expectedRestartState = info.getAttributeValue("restartState");
65         if (expectedRestartState != null && !expectedRestartState.equals(state)) {
66             throw new IllegalStateException JavaDoc("Expected restart state " + expectedRestartState + ", but real is " + state);
67         }
68     }
69
70     public Object JavaDoc state() {
71         return state;
72     }
73
74     public Token<StateTokenId> nextToken() {
75         boolean returnNullToken = Boolean.TRUE.equals(info.getAttributeValue("returnNullToken"));
76         while (true) {
77             int c = input.read();
78             if (returnNullToken) // Test early return of null token
79
return null;
80             switch (c) {
81                 case 'a':
82                     state = useIntStates ? AFTER_A_INT : AFTER_A;
83                     return token(StateTokenId.A);
84
85                 case 'b':
86                     while (input.read() == 'b') {}
87                     input.backup(1);
88                     state = useIntStates ? AFTER_B_INT : AFTER_B;
89                     return token(StateTokenId.BMULTI);
90
91                 case EOF: // no more chars on the input
92
return null; // the only legal situation when null can be returned
93

94                 default:
95                     // Invalid char
96
state = null;
97                     return token(StateTokenId.ERROR);
98             }
99         }
100     }
101         
102     private Token<StateTokenId> token(StateTokenId id) {
103         return tokenFactory.createToken(id);
104     }
105     
106     public void release() {
107         InputAttributes attrs = info.inputAttributes();
108         if (attrs != null) {
109             attrs.setValue(StateTokenId.language(), "lexerRelease", Boolean.TRUE, false);
110         }
111     }
112
113 }
114
Popular Tags