1 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 33 public class Parser { 34 35 public static final String 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 ,Pattern<TokenType>> stateToPattern = new HashMap<Integer ,Pattern<TokenType>> (); 51 private Map<String ,Integer > nameToState = new HashMap<String ,Integer > (); 52 private Map<Integer ,String > stateToName = new HashMap<Integer ,String > (); 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 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 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 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 ,Pattern> patterns = new HashMap<String ,Pattern> (); 108 109 110 public String toString () { 111 StringBuffer sb = new StringBuffer (); 112 Iterator<String > it = patterns.keySet ().iterator (); 113 while (it.hasNext ()) { 114 String state = it.next (); 115 sb.append (state).append (":").append (patterns.get (state)); 116 } 117 return sb.toString (); 118 } 119 120 121 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 |