-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToken.js
88 lines (85 loc) · 3.52 KB
/
Token.js
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
86
87
88
/**
* Generic container for a parsed token
* Token.name static method can be used to convert individual characters into token names
*
* Written by Nommiin - https://github.com/Nommiin
*/
class Token {
/**
* @param {string} type The type of token
* @param {any} literal The literal value of the token, depends on type
* @param {number} index The position of the token in the file as a whole
* @param {number} col The position of the token on the current line
* @param {number} line The line (1-index'd) of the token
*/
constructor(type, literal, index, col, line) {
this.Type = type;
this.Literal = literal;
this.Index = index;
this.Column = col;
this.Line = line;
this.Children = undefined;
}
static name(val) {
switch (val) {
case "(": return "LeftParentheses";
case ")": return "RightParentheses";
case "{": return "LeftBrace";
case "}": return "RightBrace";
case "[": return "LeftBracket";
case "]": return "RightBracket";
case "?": return "QuestionMark";
case "~": return "BinaryInvert";
case ";": return "Semicolon";
case ",": return "Comma";
case "::": return "ScopeResolver";
case ":": return "Colon";
case "...": return "Arguments";
case ".*": return "PointerToMember";
case ".": return "Period";
case "->*": return "PointerToMember";
case "->": return "PointerAccess";
case "-=": return "AssignSubtract";
case "--": return "Decrement";
case "-": return "Minus";
case "+=": return "AssignAdd";
case "++": return "Increment";
case "+": return "Plus";
case "*=": return "AssignMultiply";
case "*": return "Asterisk";
case "//": return "Comment";
case "/*": return "MultilineComment";
case "/": return "Divide";
case "%": return "AssignModulo";
case "%": return "Modulo";
case "^=": return "AssignBinaryXor";
case "^": return "BinaryXor";
case "&&": return "LogicalAnd";
case "&=": return "AssignBinaryAnd";
case "&": return "BinaryAnd";
case "||": return "LogicalOr";
case "|=": return "AssignBinaryOr";
case "|": return "BinaryOr";
case "!=": return "CompareNot";
case "!": return "LogicalNot";
case "==": return "CompareEqual";
case "=": return "Assign";
case "<<=": return "AssignBinaryShiftLeft";
case "<<": return "BinaryShiftLeft";
case "<=": return "CompareLessThanEqual";
case "<": return "LessThan";
case ">>=": return "AssignBinaryShiftRight";
case ">>": return "BinaryShiftRight";
case ">=": return "CompareGreaterThanEqual";
case ">": return "GreaterThan";
// Helpers
case "()": return "ParenthesesPair";
case "[]": return "BracketPair";
case "{}": return "BracePair";
}
if (val.startsWith("#")) return "PreprocessorDirective";
return undefined;
}
}
module.exports = Token;
if (!global["__program_main"]) if (!global["__program_warn"]) {global["__program_warn"]=1;console.error("Please execute the program by running main.js");}