/
InstructionTokenizer.java
95 lines (71 loc) · 2.59 KB
/
InstructionTokenizer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.isk.jvmhardcore.pjba.parser.tokenizer;
import org.isk.jvmhardcore.pjba.instruction.meta.MetaInstruction;
import org.isk.jvmhardcore.pjba.instruction.meta.MetaInstructions;
import org.isk.jvmhardcore.pjba.parser.core.Reader;
import org.isk.jvmhardcore.pjba.parser.core.Tokenizer;
import org.isk.jvmhardcore.pjba.parser.core.util.StringGenerator;
import org.isk.jvmhardcore.pjba.util.Ascii;
public class InstructionTokenizer extends Tokenizer {
final private StringGenerator generator;
public InstructionTokenizer(String filename, Reader reader) {
super(filename, reader);
this.generator = new StringGenerator();
}
public MetaInstruction getMetaInstruction() {
this.mark();
int character = Ascii.NULL;
while ((character = this.next()) != Ascii.SPACE
&& character != Ascii.TAB
&& character != Ascii.LF
&& this.isInstructionOrLabelChar(character)) {
this.generator.appendChar(character);
}
final String instructionStr = this.generator.toString();
final MetaInstruction metaInstruction = MetaInstructions.getMetaInstruction(instructionStr);
if (metaInstruction != null) {
this.rewind();
return metaInstruction;
}
this.reset();
throw new ParserException("Unknown instruction. Got: " + instructionStr, false);
}
public String getLabelAsArg() {
int character = 0;
while (this.isInstructionOrLabelChar(character = this.next())) {
this.generator.appendChar(character);
}
if (character == Ascii.COLON) {
throw new ParserException("A label following an instruction should not end with a colon.");
}
this.rewind();
return this.generator.toString();
}
public String getLabel() {
int character = 0;
while (this.isInstructionOrLabelChar(character = this.next())) {
this.generator.appendChar(character);
}
if (character != Ascii.COLON) {
throw new ParserException("Excepted colon and got: " + character);
}
final String label = this.generator.toString();
if (label.isEmpty()) {
throw new ParserException("Empty Label.");
}
return label;
}
private boolean isInstructionOrLabelChar(int character) {
return this.isAsciiLetter(character) || this.isDigit(character) || character == Ascii.UNDERSCORE;
}
public boolean isLabel() {
this.mark();
int character = 0;
while (this.isInstructionOrLabelChar(character = this.next())) {}
this.reset();
if (character == Ascii.COLON) {
return true;
} else {
return false;
}
}
}