Компиляторы 4 лаба (7) Scanner

 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
import java.util.ArrayList;
public class Scanner {
public String program;
private Compiler compiler;
private Position cur;
boolean flag;
private ArrayList<Fragment> comments;
public ArrayList<Fragment> GetComments(){
return comments;
}
public Scanner (String program, Compiler compiler){
this.compiler = compiler;
cur = new Position(this.program = program);
comments = new ArrayList<Fragment>();
}
public Token NextToken(){
while (cur.cP() != -1){
while (cur.isWhiteSpace())
cur.iterate();
Position start = cur.copyPos();
switch (cur.cP()){
case '<':
cur.iterate();
if (cur.cP() == '='){
cur.iterate();
return new OperToken("<=", start, cur);
}
if (cur.isDecimalDigit()){
long val = 0;
while(cur.isDecimalDigit()){
val *= 10;
val += cur.cP() - '0';
cur.iterate();
}
if (cur.cP() == '>' ) {
cur.iterate();
} else {
compiler.AddMessage(true, cur, " > expected ");
}
return new NumberToken(val, start, cur);
}
break;
case '=':
cur.iterate();
if (cur.cP() == '='){
cur.iterate();
return new OperToken("==", start, cur);
}
return new OperToken("=", start, cur);
default:
if (cur.isLetterOrDigit()) {
String buf = "";
while(cur.isLetterOrDigit()){
buf += new String(Character.toChars(cur.cP()));
if (cur.isDecimalDigit()) {
flag = true;
} else {
flag =false;
}
cur.iterate();
}
if (flag) {
return new IdentToken(buf, start, cur);
}
}
if(cur.cP() == -1)
return new SpecToken(DomainTag.END_OF_PROGRAM, cur, cur );
compiler.AddMessage( true , cur, "unexpected character " + new String(Character.toChars(cur.cP())) );
while (!cur.isWhiteSpace() && cur.cP() != -1)
cur.iterate();
break;
}
}
return new SpecToken(DomainTag.END_OF_PROGRAM, cur, cur );
}
}