2018-06-13 00:39:33 +00:00
|
|
|
package io.github.applecommander.bastools.api.model;
|
2018-05-29 03:33:41 +00:00
|
|
|
|
2018-06-13 00:39:33 +00:00
|
|
|
import io.github.applecommander.bastools.api.Visitor;
|
2018-05-06 19:31:36 +00:00
|
|
|
|
2018-05-09 01:25:09 +00:00
|
|
|
/**
|
|
|
|
* A Token in the classic compiler sense, in that this represents a component of the application.
|
|
|
|
*
|
|
|
|
* @author rob
|
|
|
|
*/
|
2018-05-14 03:27:56 +00:00
|
|
|
public class Token {
|
2018-05-06 19:31:36 +00:00
|
|
|
public final int line;
|
|
|
|
public final Type type;
|
|
|
|
public final ApplesoftKeyword keyword;
|
|
|
|
public final Double number;
|
|
|
|
public final String text;
|
|
|
|
|
|
|
|
private Token(int line, Type type, ApplesoftKeyword keyword, Double number, String text) {
|
|
|
|
this.line = line;
|
|
|
|
this.type = type;
|
|
|
|
this.keyword = keyword;
|
|
|
|
this.number = number;
|
|
|
|
this.text = text;
|
|
|
|
}
|
2018-05-14 03:27:56 +00:00
|
|
|
public Token accept(Visitor t) {
|
|
|
|
return t.visit(this);
|
2018-05-14 02:15:14 +00:00
|
|
|
}
|
|
|
|
@Override
|
2018-05-06 19:31:36 +00:00
|
|
|
public String toString() {
|
|
|
|
switch (type) {
|
|
|
|
case EOL:
|
|
|
|
return type.toString();
|
|
|
|
case KEYWORD:
|
|
|
|
return keyword.toString();
|
|
|
|
case NUMBER:
|
|
|
|
return String.format("%s(%f)", type, number);
|
|
|
|
default:
|
|
|
|
return String.format("%s(%s)", type, text);
|
|
|
|
}
|
|
|
|
}
|
2018-07-13 04:37:19 +00:00
|
|
|
public String asString() {
|
|
|
|
switch (type) {
|
|
|
|
case EOL:
|
|
|
|
return "\n";
|
|
|
|
case KEYWORD:
|
|
|
|
return keyword.toString();
|
|
|
|
case NUMBER:
|
|
|
|
return number.toString();
|
|
|
|
default:
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
2018-05-06 19:31:36 +00:00
|
|
|
|
|
|
|
public static Token eol(int line) {
|
|
|
|
return new Token(line, Type.EOL, null, null, null);
|
|
|
|
}
|
|
|
|
public static Token number(int line, Double number) {
|
|
|
|
return new Token(line, Type.NUMBER, null, number, null);
|
|
|
|
}
|
|
|
|
public static Token ident(int line, String text) {
|
2018-05-16 01:43:30 +00:00
|
|
|
return new Token(line, Type.IDENT, null, null, text.toUpperCase());
|
2018-05-06 19:31:36 +00:00
|
|
|
}
|
|
|
|
public static Token comment(int line, String text) {
|
|
|
|
return new Token(line, Type.COMMENT, null, null, text);
|
|
|
|
}
|
|
|
|
public static Token string(int line, String text) {
|
|
|
|
return new Token(line, Type.STRING, null, null, text);
|
|
|
|
}
|
|
|
|
public static Token keyword(int line, ApplesoftKeyword keyword) {
|
|
|
|
// Note that the text component is useful to have for parsing, so we replicate it...
|
|
|
|
return new Token(line, Type.KEYWORD, keyword, null, keyword.text);
|
|
|
|
}
|
|
|
|
public static Token syntax(int line, int ch) {
|
|
|
|
return new Token(line, Type.SYNTAX, null, null, String.format("%c", ch));
|
|
|
|
}
|
2018-05-21 00:54:11 +00:00
|
|
|
public static Token directive(int line, String text) {
|
|
|
|
return new Token(line, Type.DIRECTIVE, null, null, text);
|
|
|
|
}
|
2018-05-06 19:31:36 +00:00
|
|
|
|
|
|
|
public static enum Type {
|
2018-05-21 00:54:11 +00:00
|
|
|
EOL, NUMBER, IDENT, COMMENT, STRING, KEYWORD, SYNTAX, DIRECTIVE
|
2018-05-06 19:31:36 +00:00
|
|
|
}
|
|
|
|
}
|