01: /*
02: * AsmFormatter.java
03: *
04: * Copyright (C) 2003 Peter Graves
05: * $Id: AsmFormatter.java,v 1.3 2003/12/31 19:41:06 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j;
23:
24: import gnu.regexp.RE;
25: import gnu.regexp.REMatch;
26: import gnu.regexp.UncheckedRE;
27:
28: public final class AsmFormatter extends Formatter {
29: private static final UncheckedRE labelRE = new UncheckedRE(
30: "^[_a-zA-z0-9]+:");
31:
32: private static final int ASM_FORMAT_TEXT = 0;
33: private static final int ASM_FORMAT_COMMENT = 1;
34: private static final int ASM_FORMAT_LABEL = 2;
35:
36: public AsmFormatter(Buffer buffer) {
37: this .buffer = buffer;
38: }
39:
40: public LineSegmentList formatLine(Line line) {
41: clearSegmentList();
42: final String text = getDetabbedText(line);
43: if (text.length() > 0) {
44: int start = 0;
45: int index = text.indexOf(':');
46: if (index > 0) {
47: REMatch match = labelRE.getMatch(text);
48: if (match != null) {
49: index = match.getEndIndex();
50: addSegment(text, 0, index, ASM_FORMAT_LABEL);
51: start = index;
52: }
53: }
54: index = text.indexOf(';', start);
55: if (index >= 0) {
56: addSegment(text, start, index, ASM_FORMAT_TEXT);
57: addSegment(text, index, ASM_FORMAT_COMMENT);
58: } else
59: addSegment(text, start, ASM_FORMAT_TEXT);
60: } else
61: addSegment(text, ASM_FORMAT_TEXT);
62: return segmentList;
63: }
64:
65: public FormatTable getFormatTable() {
66: if (formatTable == null) {
67: formatTable = new FormatTable(null);
68: formatTable.addEntryFromPrefs(ASM_FORMAT_TEXT, "text");
69: formatTable
70: .addEntryFromPrefs(ASM_FORMAT_COMMENT, "comment");
71: formatTable.addEntryFromPrefs(ASM_FORMAT_LABEL, "function");
72: }
73: return formatTable;
74: }
75: }
|