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 |
STRING_VARIABLE, |
40 |
ARRAY_VARIABLE, |
41 |
EVENT_HANDLER_NAME // only NKSP language |
42 |
}; |
43 |
|
44 |
SourceToken() : baseType(END_OF_FILE), extType(NO_EXT), line(0), column(0) { |
45 |
} |
46 |
|
47 |
SourceToken(BaseType_t t, String s = "") : baseType(t), extType(NO_EXT), txt(s), line(0), column(0) { |
48 |
} |
49 |
|
50 |
SourceToken(ExtType_t t, String s = "") : baseType(OTHER), extType(t), txt(s), line(0), column(0) { |
51 |
switch (t) { |
52 |
case NO_EXT: baseType = OTHER; break; |
53 |
case INTEGER_VARIABLE: baseType = VARIABLE_NAME; break; |
54 |
case STRING_VARIABLE: baseType = VARIABLE_NAME; break; |
55 |
case ARRAY_VARIABLE: baseType = VARIABLE_NAME; break; |
56 |
case EVENT_HANDLER_NAME: baseType = IDENTIFIER; break; |
57 |
} |
58 |
} |
59 |
|
60 |
BaseType_t baseType; |
61 |
ExtType_t extType; |
62 |
String txt; |
63 |
int line; |
64 |
int column; |
65 |
|
66 |
String text() const { return txt; } |
67 |
int firstLine() const { return line; } |
68 |
int firstColumn() const { return column; } |
69 |
|
70 |
// base types |
71 |
bool isEOF() const { return baseType == END_OF_FILE; } |
72 |
bool isNewLine() const { return baseType == NEW_LINE; } |
73 |
bool isKeyword() const { return baseType == KEYWORD; } |
74 |
bool isVariableName() const { return baseType == VARIABLE_NAME; } |
75 |
bool isIdentifier() const { return baseType == IDENTIFIER; } |
76 |
bool isNumberLiteral() const { return baseType == NUMBER_LITERAL; } |
77 |
bool isStringLiteral() const { return baseType == STRING_LITERAL; } |
78 |
bool isComment() const { return baseType == COMMENT; } |
79 |
bool isPreprocessor() const { return baseType == PREPROCESSOR; } |
80 |
bool isMetricPrefix() const { return baseType == METRIC_PREFIX; } |
81 |
bool isStdUnit() const { return baseType == STANDARD_UNIT; } |
82 |
bool isOther() const { return baseType == OTHER; } |
83 |
|
84 |
// extended types |
85 |
bool isIntegerVariable() const { return extType == INTEGER_VARIABLE; } |
86 |
bool isStringVariable() const { return extType == STRING_VARIABLE; } |
87 |
bool isArrayVariable() const { return extType == ARRAY_VARIABLE; } |
88 |
bool isEventHandlerName() const { return extType == EVENT_HANDLER_NAME; } |
89 |
|
90 |
operator bool() const { return baseType != END_OF_FILE; } |
91 |
|
92 |
bool equalsType(const SourceToken& other) const { |
93 |
return baseType == other.baseType && extType == other.extType; |
94 |
} |
95 |
}; |
96 |
|
97 |
} // namespace LinuxSampler |
98 |
|
99 |
#endif // CRUDEBYTE_CODE_TOKEN_H |