1 21 22 package org.armedbear.j; 23 24 import java.util.ArrayList ; 25 26 public final class PythonTagger extends Tagger 27 { 28 private static final int STATE_NEUTRAL = 0; 30 private static final int STATE_CLASS_NAME = 1; 31 private static final int STATE_FUNCTION_NAME = 2; 32 private static final int STATE_SINGLE_QUOTE = 3; 33 private static final int STATE_DOUBLE_QUOTE = 4; 34 private static final int STATE_TRIPLE_SINGLE = 5; 35 private static final int STATE_TRIPLE_DOUBLE = 6; 36 37 private static final PythonMode mode = PythonMode.getMode(); 38 39 public PythonTagger(SystemBuffer buffer) 40 { 41 super(buffer); 42 } 43 44 public void run() 45 { 46 ArrayList tags = new ArrayList (); 47 Position pos = new Position(buffer.getFirstLine(), 0); 48 int state = STATE_NEUTRAL; 49 while (!pos.atEnd()) { 50 char c = pos.getChar(); 51 if (Character.isWhitespace(c)) { 52 pos.skipWhitespace(); 53 continue; 54 } 55 if (c == '\\') { 56 if (pos.getOffset() < pos.getLineLength()-1) { 58 pos.skip(2); 59 } else { 60 pos.setLine(pos.getNextLine()); 61 pos.setOffset(0); 62 } 63 continue; 64 } 65 if (state == STATE_SINGLE_QUOTE) { 66 if (c == '\'') 67 state = STATE_NEUTRAL; 68 pos.next(); 69 continue; 70 } 71 if (state == STATE_DOUBLE_QUOTE) { 72 if (c == '"') 73 state = STATE_NEUTRAL; 74 pos.next(); 75 continue; 76 } 77 if (state == STATE_TRIPLE_SINGLE) { 78 if (c == '\'' && pos.lookingAt("'''")) { 79 pos.skip(3); 80 state = STATE_NEUTRAL; 81 } else 82 pos.next(); 83 continue; 84 } 85 if (state == STATE_TRIPLE_DOUBLE) { 86 if (c == '"' && pos.lookingAt("\"\"\"")) { 87 pos.skip(3); 88 state = STATE_NEUTRAL; 89 } else 90 pos.next(); 91 continue; 92 } 93 if (c == '\'') { 94 if (pos.lookingAt("'''")) { 95 state = STATE_TRIPLE_SINGLE; 96 pos.skip(3); 97 } else { 98 state = STATE_SINGLE_QUOTE; 99 pos.next(); 100 } 101 continue; 102 } 103 if (c == '"') { 104 if (pos.lookingAt("\"\"\"")) { 105 state = STATE_TRIPLE_DOUBLE; 106 pos.skip(3); 107 } else { 108 state = STATE_DOUBLE_QUOTE; 109 pos.next(); 110 } 111 continue; 112 } 113 if (c == '#') { 114 pos.setLine(pos.getNextLine()); 116 pos.setOffset(0); 117 continue; 118 } 119 if (mode.isIdentifierStart(c)) { 120 Position start = pos.copy(); 121 String token = gatherToken(pos); 122 if (state == STATE_CLASS_NAME) { 123 LocalTag tag = new PythonTag("class " + token, start, TAG_CLASS); 124 tags.add(tag); 125 state = STATE_NEUTRAL; 126 } else if (state == STATE_FUNCTION_NAME) { 127 LocalTag tag = new PythonTag(token, start, TAG_FUNCTION); 128 tags.add(tag); 129 state = STATE_NEUTRAL; 130 } else if (token.equals("class")) { 131 state = STATE_CLASS_NAME; 132 } else if (token.equals("def")) { 133 state = STATE_FUNCTION_NAME; 134 } 135 continue; 136 } 137 pos.next(); 138 } 139 buffer.setTags(tags); 140 } 141 142 private static String gatherToken(Position pos) 143 { 144 FastStringBuffer sb = new FastStringBuffer(); 145 char c; 146 while (mode.isIdentifierPart(c = pos.getChar())) { 147 sb.append(c); 148 if (!pos.next()) 149 break; 150 } 151 return sb.toString(); 152 } 153 } 154 | Popular Tags |