13bool isIdentifierStart(
char c) {
14 return std::isalpha(
static_cast<unsigned char>(
c)) != 0 ||
c ==
'_';
17bool isIdentifierBody(
char c) {
18 return std::isalnum(
static_cast<unsigned char>(
c)) != 0 ||
c ==
'_' ||
c ==
'.';
21bool isDigit(
char c) {
return c >=
'0' &&
c <=
'9'; }
27 {{
"line", std::to_string(
line)}, {
"column", std::to_string(
column)}},
"dnut.lexer"));
31std::size_t twoCharacterPunctuatorLength(std::string_view
source, std::size_t i) {
32 if (i + 1 >=
source.size())
return 0;
33 const std::string_view pair =
source.substr(i, 2);
34 for (
const std::string_view candidate : {
"==",
"!=",
">=",
"<=",
"&&",
"||",
"->"}) {
35 if (pair == candidate)
return 2;
40bool isSingleCharacterPunctuator(
char c) {
58 case '|':
return true;
59 default:
return false;
64bool appendEscape(
char escape, std::string&
out) {
66 case 'n':
out +=
'\n';
return true;
67 case 't':
out +=
'\t';
return true;
68 case 'r':
out +=
'\r';
return true;
69 case '"':
out +=
'"';
return true;
70 case '\'':
out +=
'\'';
return true;
71 case '\\':
out +=
'\\';
return true;
72 case '{':
out +=
'{';
return true;
73 case '}':
out +=
'}';
return true;
97 std::vector<DnutToken>
tokens;
103 const auto advanceOne = [&]() {
116 if (
c ==
'\n' ||
c ==
' ' ||
c ==
'\t' ||
c ==
'\r') {
120 if (
c ==
'/' && i + 1 <
size &&
source[i + 1] ==
'/') {
124 if (
c ==
'/' && i + 1 <
size &&
source[i + 1] ==
'*') {
125 const int startLine =
line;
126 const int startColumn =
column;
140 return lexFailure(
path, startLine, startColumn,
"unterminated block comment");
143 if (
c ==
'"' ||
c ==
'\'') {
145 const int startLine =
line;
146 const int startColumn =
column;
149 bool terminated =
false;
158 if (i >=
size)
break;
161 std::string(
"unknown escape sequence '\\") +
source[i] +
"'");
168 if (!terminated)
return lexFailure(
path, startLine, startColumn,
"unterminated string literal");
172 if (isDigit(
c) || (
c ==
'.' && i + 1 <
size && isDigit(
source[i + 1]))) {
173 const int startLine =
line;
174 const int startColumn =
column;
175 const std::size_t
start = i;
178 if (isDigit(
d) ||
d ==
'.') {
180 }
else if ((
d ==
'e' ||
d ==
'E') && i + 1 <
size &&
192 if (isIdentifierStart(
c)) {
193 const int startLine =
line;
194 const int startColumn =
column;
195 const std::size_t
start = i;
203 while (i <
size && continuesIdentifier(i)) advanceOne();
208 if (
const std::size_t pairLength = twoCharacterPunctuatorLength(
source, i); pairLength != 0) {
209 const int startLine =
line;
210 const int startColumn =
column;
211 const std::string
text(
source.substr(i, pairLength));
217 if (isSingleCharacterPunctuator(
c)) {
218 const int startLine =
line;
219 const int startColumn =
column;
220 const std::string
text(1,
c);
225 return lexFailure(
path,
line,
column, std::string(
"unexpected character '") +
c +
"'");
232bool hasErrors(
const std::vector<DnutDiagnostic>& diagnostics) {
233 for (
const auto& diagnostic : diagnostics) {
Shared lexer for every `.dnut` dialect.
std::array< float, 3 > position
std::vector< Token > tokens
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
EVENGINE_API_PLATFORM bool hasErrors(const std::vector< DnutDiagnostic > &diagnostics)
Return whether any diagnostic is an error.
eve::Result< std::vector< DnutToken > > lexDnut(std::string_view source, const std::string &path)
Lex one .dnut source buffer into owned tokens.
const char * dnutTokenKindName(DnutTokenKind kind) noexcept
Return the stable lowercase spelling of a token kind.
DnutTokenKind
Token classes produced by the shared .dnut lexer.
@ String
Quoted string with escapes already resolved.
@ Identifier
Bare word: keyword, field name, identifier, or bare enum value.
@ Punctuator
One- or two-character punctuator such as {, =, ==, ->.
@ EndOfFile
Synthetic terminal token; always the last element.
@ Number
Unsigned numeric literal; sign handling belongs to the parser.