Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Compiler_Token.H
Go to the documentation of this file.
1/*
2 This file is part of Aleph-w system
3
4 Copyright (c) 2002-2026 Leandro Rabindranath Leon
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23*/
24
36#ifndef COMPILER_TOKEN_H
37#define COMPILER_TOKEN_H
38
39#include <sstream>
40#include <string>
41#include <string_view>
42#include <ah-source.H>
43
44namespace Aleph {
45
54{
55 None,
56 Left,
57 Right
58};
59
69{
70 Invalid,
72
73 // Identifiers and Literals
78
79 // Trivia
82
83 // Keywords
84 Kw_If,
85 Kw_Else,
87 Kw_For,
91 Kw_Func,
92 Kw_Fn = Kw_Func,
93 Kw_Var,
94 Kw_Let,
96 Kw_Type,
98 Kw_Enum,
100 Kw_Import,
101 Kw_Export,
103 Kw_Try,
104 Kw_Catch,
105 Kw_Throw,
106 Kw_True,
107 Kw_False,
108
109 // Punctuation
110 LParen,
111 RParen,
112 LBrace,
113 RBrace,
114 LBracket,
115 RBracket,
116 Comma,
117 Semicolon,
118 Colon,
119 Dot,
120 Arrow,
122 Question,
123
124 // Operators
125 Assign,
126 Plus,
127 Minus,
128 Star,
129 Slash,
130 Percent,
131 EqEq,
132 NotEq,
133 Less,
134 LessEq,
135 Greater,
136 GreaterEq,
137 AndAnd,
138 OrOr,
139 Bang,
140 Amp,
141 Pipe,
142 Caret,
143 Tilde,
144 PlusEq,
145 MinusEq,
146 StarEq,
147 SlashEq,
148 PercentEq,
149 PlusPlus,
151};
152
163
173Compiler_Token_Kind classify_compiler_keyword(std::string_view lexeme) noexcept;
174
183
192
201
210
222
231
242{
244 std::string lexeme;
246
256
266
274 [[nodiscard]] bool is(const Compiler_Token_Kind expected) const noexcept
275 {
276 return kind == expected;
277 }
278
287 [[nodiscard]] std::string to_string() const;
288};
289
290namespace Compiler_Token_Detail {
291inline std::string escape_token_lexeme(std::string_view text)
292{
293 std::string escaped;
294 escaped.reserve(text.size() + 8);
295
296 for (const char ch : text)
297 switch (ch)
298 {
299 case '\\':
300 escaped += "\\\\";
301 break;
302 case '"':
303 escaped += "\\\"";
304 break;
305 case '\n':
306 escaped += "\\n";
307 break;
308 case '\r':
309 escaped += "\\r";
310 break;
311 case '\t':
312 escaped += "\\t";
313 break;
314 default:
315 escaped.push_back(ch);
316 }
317
318 return escaped;
319}
320} // namespace Compiler_Token_Detail
321
323inline const char *compiler_token_kind_name(const Compiler_Token_Kind kind)
324{
325 switch (kind)
326 {
328 return "Invalid";
330 return "End_Of_File";
332 return "Identifier";
334 return "Integer_Literal";
336 return "String_Literal";
338 return "Char_Literal";
340 return "Line_Comment";
342 return "Block_Comment";
344 return "Kw_If";
346 return "Kw_Else";
348 return "Kw_While";
350 return "Kw_For";
352 return "Kw_Return";
354 return "Kw_Break";
356 return "Kw_Continue";
358 return "Kw_Fn";
360 return "Kw_Var";
362 return "Kw_Let";
364 return "Kw_Const";
366 return "Kw_Type";
368 return "Kw_Struct";
370 return "Kw_Enum";
372 return "Kw_Class";
374 return "Kw_Import";
376 return "Kw_Export";
378 return "Kw_Namespace";
380 return "Kw_Try";
382 return "Kw_Catch";
384 return "Kw_Throw";
386 return "Kw_True";
388 return "Kw_False";
390 return "LParen";
392 return "RParen";
394 return "LBrace";
396 return "RBrace";
398 return "LBracket";
400 return "RBracket";
402 return "Comma";
404 return "Semicolon";
406 return "Colon";
408 return "Dot";
410 return "Arrow";
412 return "Double_Colon";
414 return "Question";
416 return "Assign";
418 return "Plus";
420 return "Minus";
422 return "Star";
424 return "Slash";
426 return "Percent";
428 return "EqEq";
430 return "NotEq";
432 return "Less";
434 return "LessEq";
436 return "Greater";
438 return "GreaterEq";
440 return "AndAnd";
442 return "OrOr";
444 return "Bang";
446 return "Amp";
448 return "Pipe";
450 return "Caret";
452 return "Tilde";
454 return "PlusEq";
456 return "MinusEq";
458 return "StarEq";
460 return "SlashEq";
462 return "PercentEq";
464 return "PlusPlus";
466 return "MinusMinus";
467 }
468
469 return "Unknown";
470}
471
473inline Compiler_Token_Kind classify_compiler_keyword(std::string_view lexeme) noexcept
474{
475 if (lexeme == "if")
477 if (lexeme == "else")
479 if (lexeme == "while")
481 if (lexeme == "for")
483 if (lexeme == "return")
485 if (lexeme == "break")
487 if (lexeme == "continue")
489 if (lexeme == "fn")
491 if (lexeme == "var")
493 if (lexeme == "let")
495 if (lexeme == "const")
497 if (lexeme == "type")
499 if (lexeme == "struct")
501 if (lexeme == "enum")
503 if (lexeme == "class")
505 if (lexeme == "import")
507 if (lexeme == "export")
509 if (lexeme == "namespace")
511 if (lexeme == "try")
513 if (lexeme == "catch")
515 if (lexeme == "throw")
517 if (lexeme == "true")
519 if (lexeme == "false")
521
523}
524
558
560inline bool compiler_token_is_literal(const Compiler_Token_Kind kind) noexcept
561{
562 switch (kind)
563 {
569 return true;
570 default:
571 return false;
572 }
573}
574
580
618
620inline int compiler_binary_precedence(const Compiler_Token_Kind kind) noexcept
621{
622 switch (kind)
623 {
630 return 1;
632 return 2;
634 return 3;
637 return 4;
642 return 5;
645 return 6;
649 return 7;
650 default:
651 return 0;
652 }
653}
654
687
689inline std::string Compiler_Token::to_string() const
690{
691 std::ostringstream out;
694 if (span.is_valid())
695 out << span.to_string();
696 else
697 out << "<invalid-span>";
698 return out.str();
699}
700
701} // namespace Aleph
702
703#endif // COMPILER_TOKEN_H
Source file and span management utilities for compiler-style tooling.
size_t size_t int32_t * out
Definition ca-c-api.h:120
Minimal std::expected-style result type for C++20.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
std::string escape_token_lexeme(std::string_view text)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Compiler_Token_Kind classify_compiler_keyword(std::string_view lexeme) noexcept
Classifies a lexeme as a keyword or Identifier.
bool compiler_token_is_keyword(Compiler_Token_Kind kind) noexcept
Predicate: Is the kind a reserved keyword?
@ Identifier
Variable or function name reference.
@ Integer_Literal
Numeric integer constant.
@ Char_Literal
Character constant.
@ String_Literal
String constant.
const char * compiler_token_kind_name(Compiler_Token_Kind kind)
Returns a human-readable name for a token kind.
@ Invalid
Uninitialized register/slot or explicit invalid placeholder.
int compiler_binary_precedence(Compiler_Token_Kind kind) noexcept
Returns binary precedence for an operator.
bool compiler_token_is_comment(Compiler_Token_Kind kind) noexcept
Predicate: Is the kind a comment?
Compiler_Associativity
Associativity metadata for binary operators.
@ None
The operator is not associative (e.g., chained comparators prohibited).
@ Right
Right-associative: a = (b = c).
@ Left
Left-associative: (a - b) - c.
bool compiler_token_is_literal(Compiler_Token_Kind kind) noexcept
Predicate: Is the kind a literal (number, string, bool, etc.)?
bool compiler_token_is_operator(Compiler_Token_Kind kind) noexcept
Predicate: Is the kind an operator (arithmetic, logical, etc.)?
Compiler_Token_Kind
Token kinds supported by the compiler MVP.
@ Line_Comment
Single-line comment (// ...).
@ Identifier
Named identifier (e.g., variable or function name).
@ Integer_Literal
Integer constant.
@ End_Of_File
End of the source stream.
@ Invalid
Sentinel for lexer errors or uninitialized tokens.
@ Char_Literal
Single character constant.
@ Block_Comment
Multi-line comment (/* ... *&zwj;/).
@ String_Literal
String constant (with escape sequences).
Compiler_Associativity compiler_binary_associativity(Compiler_Token_Kind kind) noexcept
Returns binary associativity for an operator.
Lexical token representation with its value and location.
Source_Span span
Precise location in the source code.
Compiler_Token_Kind kind
Logical category (e.g., Kw_If).
bool is(const Compiler_Token_Kind expected) const noexcept
Fast kind check.
std::string lexeme
Exact text found in the source.
std::string to_string() const
Formats the token as a descriptive string for debugging.
bool is_invalid() const noexcept
Returns whether the token represents a lexical error.
bool is_eof() const noexcept
Returns whether the token marks the end of the file.
Represents a missing value.
Half-open byte range inside a source file.
Definition ah-source.H:100
bool is_valid() const noexcept
Returns whether the span belongs to a registered file.
Definition ah-source.H:106
std::string to_string() const
Formats the span as #file:[begin,end).
Definition ah-source.H:130