KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > parser > Parser


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.languages.parser;
21
22 import org.netbeans.api.languages.CharInput;
23 import org.netbeans.api.languages.ASTToken;
24 import java.util.*;
25 import org.netbeans.modules.languages.Feature;
26 import org.netbeans.modules.languages.Language.TokenType;
27
28
29 /**
30  *
31  * @author Jan Jancura
32  */

33 public class Parser {
34     
35     public static final String JavaDoc DEFAULT_STATE = "DEFAULT";
36
37     private Parser () {
38     }
39     
40     public static Parser create (List rules) {
41         Parser p = new Parser ();
42         Iterator it = rules.iterator ();
43         while (it.hasNext ()) {
44             TokenType r = (TokenType) it.next ();
45             p.add (r);
46         }
47         return p;
48     }
49     
50     private Map<Integer JavaDoc,Pattern<TokenType>> stateToPattern = new HashMap<Integer JavaDoc,Pattern<TokenType>> ();
51     private Map<String JavaDoc,Integer JavaDoc> nameToState = new HashMap<String JavaDoc,Integer JavaDoc> ();
52     private Map<Integer JavaDoc,String JavaDoc> stateToName = new HashMap<Integer JavaDoc,String JavaDoc> ();
53     private int counter = 1;
54     {
55         nameToState.put (DEFAULT_STATE, -1);
56         stateToName.put (-1, DEFAULT_STATE);
57     }
58     
59     
60     private void add (TokenType tt) {
61         if (tt.getPattern () == null) return;
62         String JavaDoc startState = tt.getStartState ();
63         if (startState == null) startState = DEFAULT_STATE;
64         int state = 0;
65         if (nameToState.containsKey (startState))
66             state = nameToState.get (startState);
67         else {
68             state = counter++;
69             nameToState.put (startState, state);
70             stateToName.put (state, startState);
71         }
72         Pattern<TokenType> pattern = tt.getPattern ();
73         pattern.mark (tt.getPriority (), tt);
74         if (stateToPattern.containsKey (state))
75             stateToPattern.put (
76                 state,
77                 stateToPattern.get (state).merge (pattern)
78             );
79         else
80             stateToPattern.put (state, pattern);
81     }
82     
83     public ASTToken read (Cookie cookie, CharInput input, String JavaDoc mimeType) {
84         if (input.eof ()) return null;
85         int originalIndex = input.getIndex ();
86         Pattern pattern = stateToPattern.get (cookie.getState ());
87         if (pattern == null) return null;
88         TokenType tokenType = (TokenType) pattern.read (input);
89         if (tokenType == null) {
90             return null;
91         }
92         cookie.setProperties (tokenType.getProperties ());
93         String JavaDoc endState = tokenType.getEndState ();
94         int state = -1;
95         if (endState != null) {
96             state = nameToState.get (endState);
97         }
98         cookie.setState (state);
99         return ASTToken.create (
100             mimeType,
101             tokenType.getType (),
102             input.getString (originalIndex, input.getIndex ()),
103             originalIndex
104         );
105     }
106     
107     private Map<String JavaDoc,Pattern> patterns = new HashMap<String JavaDoc,Pattern> ();
108     
109     
110     public String JavaDoc toString () {
111         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
112         Iterator<String JavaDoc> it = patterns.keySet ().iterator ();
113         while (it.hasNext ()) {
114             String JavaDoc state = it.next ();
115             sb.append (state).append (":").append (patterns.get (state));
116         }
117         return sb.toString ();
118     }
119     
120     
121     // innerclasses ............................................................
122

123     public interface Cookie {
124         public abstract int getState ();
125         public abstract void setState (int state);
126         public abstract void setProperties (Feature tokenProperties);
127     }
128 }
129
Popular Tags