KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > AsmFormatter


1 /*
2  * AsmFormatter.java
3  *
4  * Copyright (C) 2003 Peter Graves
5  * $Id: AsmFormatter.java,v 1.3 2003/12/31 19:41:06 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 {
30     private static final UncheckedRE labelRE = new UncheckedRE("^[_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     {
38         this.buffer = buffer;
39     }
40
41     public LineSegmentList formatLine(Line line)
42     {
43         clearSegmentList();
44         final String JavaDoc text = getDetabbedText(line);
45         if (text.length() > 0) {
46             int start = 0;
47             int index = text.indexOf(':');
48             if (index > 0) {
49                 REMatch match = labelRE.getMatch(text);
50                 if (match != null) {
51                     index = match.getEndIndex();
52                     addSegment(text, 0, index, ASM_FORMAT_LABEL);
53                     start = index;
54                 }
55             }
56             index = text.indexOf(';', start);
57             if (index >= 0) {
58                 addSegment(text, start, index, ASM_FORMAT_TEXT);
59                 addSegment(text, index, ASM_FORMAT_COMMENT);
60             } else
61                 addSegment(text, start, ASM_FORMAT_TEXT);
62         } else
63             addSegment(text, ASM_FORMAT_TEXT);
64         return segmentList;
65     }
66
67     public FormatTable getFormatTable()
68     {
69         if (formatTable == null) {
70             formatTable = new FormatTable(null);
71             formatTable.addEntryFromPrefs(ASM_FORMAT_TEXT, "text");
72             formatTable.addEntryFromPrefs(ASM_FORMAT_COMMENT, "comment");
73             formatTable.addEntryFromPrefs(ASM_FORMAT_LABEL, "function");
74         }
75         return formatTable;
76     }
77 }
78
Popular Tags