1 |
/* |
2 |
* Copyright (c) 2015-2019 Christian Schoenebeck |
3 |
* |
4 |
* http://www.linuxsampler.org |
5 |
* |
6 |
* This file is part of LinuxSampler and released under the same terms. |
7 |
* See README file for details. |
8 |
*/ |
9 |
|
10 |
#ifndef CRUDEBYTE_CODE_TOKEN_H |
11 |
#define CRUDEBYTE_CODE_TOKEN_H |
12 |
|
13 |
#include "../../common/global.h" |
14 |
|
15 |
namespace LinuxSampler { |
16 |
|
17 |
class SourceToken { |
18 |
public: |
19 |
enum BaseType_t { |
20 |
// most fundamental tokens |
21 |
END_OF_FILE = 0, |
22 |
NEW_LINE, |
23 |
// base token types |
24 |
KEYWORD, |
25 |
VARIABLE_NAME, |
26 |
IDENTIFIER, // i.e. function name |
27 |
NUMBER_LITERAL, |
28 |
STRING_LITERAL, |
29 |
COMMENT, |
30 |
PREPROCESSOR, |
31 |
METRIC_PREFIX, |
32 |
STANDARD_UNIT, |
33 |
OTHER, // anything else not classified here |
34 |
}; |
35 |
|
36 |
enum ExtType_t { |
37 |
NO_EXT, ///< no extended type available for this token |
38 |
INTEGER_VARIABLE, |
39 |
REAL_VARIABLE, |
40 |
STRING_VARIABLE, |
41 |
INTEGER_ARRAY_VARIABLE, |
42 |
REAL_ARRAY_VARIABLE, |
43 |
EVENT_HANDLER_NAME // only NKSP language |
44 |
}; |
45 |
|
46 |
SourceToken() : baseType(END_OF_FILE), extType(NO_EXT), line(0), column(0) { |
47 |
} |
48 |
|
49 |
SourceToken(BaseType_t t, String s = "") : baseType(t), extType(NO_EXT), txt(s), line(0), column(0) { |
50 |
} |
51 |
|
52 |
SourceToken(ExtType_t t, String s = "") : baseType(OTHER), extType(t), txt(s), line(0), column(0) { |
53 |
switch (t) { |
54 |
case NO_EXT: baseType = OTHER; break; |
55 |
case INTEGER_VARIABLE: baseType = VARIABLE_NAME; break; |
56 |
case REAL_VARIABLE: baseType = VARIABLE_NAME; break; |
57 |
case STRING_VARIABLE: baseType = VARIABLE_NAME; break; |
58 |
case INTEGER_ARRAY_VARIABLE: baseType = VARIABLE_NAME; break; |
59 |
case REAL_ARRAY_VARIABLE: baseType = VARIABLE_NAME; break; |
60 |
case EVENT_HANDLER_NAME: baseType = IDENTIFIER; break; |
61 |
} |
62 |
} |
63 |
|
64 |
BaseType_t baseType; |
65 |
ExtType_t extType; |
66 |
String txt; |
67 |
int line; |
68 |
int column; |
69 |
|
70 |
String text() const { return txt; } |
71 |
int firstLine() const { return line; } |
72 |
int firstColumn() const { return column; } |
73 |
|
74 |
// base types |
75 |
bool isEOF() const { return baseType == END_OF_FILE; } |
76 |
bool isNewLine() const { return baseType == NEW_LINE; } |
77 |
bool isKeyword() const { return baseType == KEYWORD; } |
78 |
bool isVariableName() const { return baseType == VARIABLE_NAME; } |
79 |
bool isIdentifier() const { return baseType == IDENTIFIER; } |
80 |
bool isNumberLiteral() const { return baseType == NUMBER_LITERAL; } |
81 |
bool isStringLiteral() const { return baseType == STRING_LITERAL; } |
82 |
bool isComment() const { return baseType == COMMENT; } |
83 |
bool isPreprocessor() const { return baseType == PREPROCESSOR; } |
84 |
bool isMetricPrefix() const { return baseType == METRIC_PREFIX; } |
85 |
bool isStdUnit() const { return baseType == STANDARD_UNIT; } |
86 |
bool isOther() const { return baseType == OTHER; } |
87 |
|
88 |
// extended types |
89 |
bool isIntegerVariable() const { return extType == INTEGER_VARIABLE; } |
90 |
bool isRealVariable() const { return extType == REAL_VARIABLE; } |
91 |
bool isStringVariable() const { return extType == STRING_VARIABLE; } |
92 |
bool isIntegerArrayVariable() const { return extType == INTEGER_ARRAY_VARIABLE; } |
93 |
bool isRealArrayVariable() const { return extType == REAL_ARRAY_VARIABLE; } |
94 |
bool isEventHandlerName() const { return extType == EVENT_HANDLER_NAME; } |
95 |
|
96 |
operator bool() const { return baseType != END_OF_FILE; } |
97 |
|
98 |
bool equalsType(const SourceToken& other) const { |
99 |
return baseType == other.baseType && extType == other.extType; |
100 |
} |
101 |
}; |
102 |
|
103 |
} // namespace LinuxSampler |
104 |
|
105 |
#endif // CRUDEBYTE_CODE_TOKEN_H |