1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.mal_lang.lib;
18
19 import java.util.List;
20
21 public class Token extends Position {
22 public final TokenType type;
23 public final String stringValue;
24 public final double doubleValue;
25 public final int intValue;
26 public final List<Token> preComments;
27 public final List<Token> postComments;
28
29 public Token(TokenType type, String filename, int line, int col) {
30 super(filename, line, col);
31 this.type = type;
32 this.stringValue = "";
33 this.doubleValue = 0.0;
34 this.intValue = 0;
35 preComments = List.of();
36 postComments = List.of();
37 }
38
39 public Token(TokenType type, String filename, int line, int col, String stringValue) {
40 super(filename, line, col);
41 this.type = type;
42 this.stringValue = stringValue;
43 this.doubleValue = 0.0;
44 this.intValue = 0;
45 preComments = List.of();
46 postComments = List.of();
47 }
48
49 public Token(Token tok, List<Token> preComments, List<Token> postComments) {
50 super(tok.filename, tok.line, tok.col);
51 type = tok.type;
52 stringValue = tok.stringValue;
53 doubleValue = tok.doubleValue;
54 intValue = tok.intValue;
55 this.preComments = preComments;
56 this.postComments = postComments;
57 }
58
59 public Token(TokenType type, String filename, int line, int col, double doubleValue) {
60 super(filename, line, col);
61 this.type = type;
62 this.stringValue = "";
63 this.doubleValue = doubleValue;
64 this.intValue = 0;
65 preComments = List.of();
66 postComments = List.of();
67 }
68
69 public Token(TokenType type, String filename, int line, int col, int intValue) {
70 super(filename, line, col);
71 this.type = type;
72 this.stringValue = "";
73 this.doubleValue = 0.0;
74 this.intValue = intValue;
75 preComments = List.of();
76 postComments = List.of();
77 }
78
79 @Override
80 public String toString() {
81 StringBuilder sb = new StringBuilder();
82 sb.append(type);
83 sb.append(", ");
84 sb.append(posString());
85 switch (type) {
86 case FLOAT:
87 sb.append(", ");
88 sb.append(doubleValue);
89 break;
90 case INT:
91 sb.append(", ");
92 sb.append(intValue);
93 break;
94 case ID:
95 case STRING:
96 sb.append(", ");
97 sb.append(stringValue);
98 break;
99 default:
100 break;
101 }
102 return sb.toString();
103 }
104 }