elna/boot/lexer.ll

302 lines
12 KiB
LLVM
Raw Normal View History

/* Lexical analyzer.
Copyright (C) 2025 Free Software Foundation, Inc.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
2024-12-21 00:08:48 +01:00
%{
#define YY_NO_UNISTD_H
#define YY_USER_ACTION this->location.columns(yyleng);
#include <sstream>
2024-12-23 13:54:11 +01:00
#include "parser.hh"
2024-12-21 00:08:48 +01:00
#undef YY_DECL
2025-01-31 09:46:17 +01:00
#define YY_DECL yy::parser::symbol_type elna::boot::lexer::lex(elna::boot::driver& driver)
2024-12-21 00:08:48 +01:00
#define yyterminate() return yy::parser::make_YYEOF(this->location)
%}
%option c++ noyywrap never-interactive
2025-01-31 09:46:17 +01:00
%option yyclass="elna::boot::lexer"
2024-12-21 00:08:48 +01:00
2025-01-30 01:03:16 +01:00
%x IN_COMMENT
2024-12-21 00:08:48 +01:00
%%
%{
this->location.step();
%}
2025-01-30 01:03:16 +01:00
<IN_COMMENT>{
\*\) BEGIN(INITIAL);
[^*\n]+ ; /* Eat comment in chunks. */
\* ; /* Eat the lone star. */
\n+ {
this->location.lines(yyleng);
this->location.step();
2024-12-21 00:08:48 +01:00
}
2025-01-30 01:03:16 +01:00
}
\(\* BEGIN(IN_COMMENT);
[ \t\r] {
this->location.step();
}
2024-12-21 00:08:48 +01:00
\n+ {
this->location.lines(yyleng);
}
if {
return yy::parser::make_IF(this->location);
}
then {
return yy::parser::make_THEN(this->location);
}
2024-12-30 23:12:47 +01:00
else {
return yy::parser::make_ELSE(this->location);
}
2025-01-25 19:50:36 +01:00
elsif {
return yy::parser::make_ELSIF(this->location);
}
2024-12-21 00:08:48 +01:00
while {
return yy::parser::make_WHILE(this->location);
}
do {
return yy::parser::make_DO(this->location);
}
proc {
return yy::parser::make_PROCEDURE(this->location);
}
begin {
return yy::parser::make_BEGIN_BLOCK(this->location);
}
end {
return yy::parser::make_END_BLOCK(this->location);
}
2025-01-16 15:09:58 +01:00
extern {
return yy::parser::make_EXTERN(this->location);
}
2024-12-21 00:08:48 +01:00
const {
return yy::parser::make_CONST(this->location);
}
var {
return yy::parser::make_VAR(this->location);
}
2025-01-06 15:08:23 +01:00
array {
return yy::parser::make_ARRAY(this->location);
}
of {
return yy::parser::make_OF(this->location);
}
2025-01-07 14:37:30 +01:00
type {
return yy::parser::make_TYPE(this->location);
}
2025-01-08 23:23:27 +01:00
record {
return yy::parser::make_RECORD(this->location);
}
union {
return yy::parser::make_UNION(this->location);
}
pointer {
return yy::parser::make_POINTER(this->location);
}
to {
return yy::parser::make_TO(this->location);
}
2024-12-21 14:05:27 +01:00
true {
2024-12-21 00:08:48 +01:00
return yy::parser::make_BOOLEAN(true, this->location);
}
2024-12-21 14:05:27 +01:00
false {
2024-12-21 00:08:48 +01:00
return yy::parser::make_BOOLEAN(false, this->location);
}
2025-01-30 01:03:16 +01:00
nil {
return yy::parser::make_NIL(this->location);
}
2025-01-13 11:55:19 +01:00
and {
return yy::parser::make_AND(this->location);
}
2025-02-14 08:05:03 +01:00
xor {
return yy::parser::make_XOR(this->location);
}
2025-01-13 11:55:19 +01:00
or {
return yy::parser::make_OR(this->location);
}
not {
return yy::parser::make_NOT(this->location);
}
2025-01-18 21:30:11 +01:00
return {
return yy::parser::make_RETURN(this->location);
}
2025-01-28 11:21:02 +01:00
cast {
return yy::parser::make_CAST(this->location);
}
2025-02-07 22:12:59 +01:00
defer {
return yy::parser::make_DEFER(this->location);
}
2024-12-21 00:08:48 +01:00
[A-Za-z_][A-Za-z0-9_]* {
return yy::parser::make_IDENTIFIER(yytext, this->location);
}
2025-01-24 11:41:14 +01:00
[0-9]+u {
return yy::parser::make_WORD(strtoul(yytext, NULL, 10), this->location);
}
2024-12-21 00:08:48 +01:00
[0-9]+ {
2025-01-01 23:02:19 +01:00
return yy::parser::make_INTEGER(strtol(yytext, NULL, 10), this->location);
2024-12-21 00:08:48 +01:00
}
2024-12-31 18:10:34 +01:00
[0-9]+\.[0-9] {
return yy::parser::make_FLOAT(strtof(yytext, NULL), this->location);
}
2025-01-22 20:19:26 +01:00
'[[:print:]]' {
2025-01-24 11:41:14 +01:00
if (yytext[1] == '\\' || yytext[1] == '\'')
2025-01-22 20:19:26 +01:00
{
REJECT;
}
else
{
return yy::parser::make_CHARACTER(std::string(yytext, 1, 1), this->location);
}
}
2025-01-30 23:09:51 +01:00
'\\x[0-9a-fA-F]{1,2}' {
char character = static_cast<char>(std::stoi(yytext + 3, nullptr, 16));
return yy::parser::make_CHARACTER(std::string(&character, 1), this->location);
}
2025-01-22 20:19:26 +01:00
'\\[0nabtfrv\\'"?]' {
2025-02-07 22:12:59 +01:00
char escape = elna::boot::escape_char(yytext[2]);
if (escape == escape_invalid_char)
2025-01-22 20:19:26 +01:00
{
REJECT;
}
2025-02-07 22:12:59 +01:00
return yy::parser::make_CHARACTER(std::string(&escape, 1), this->location);
2025-01-01 23:02:19 +01:00
}
2025-01-24 11:41:14 +01:00
\"[[:print:]]*\" {
std::string result;
const char *current_position = yytext + 1;
while (*current_position != '\0')
{
if (*current_position == '\\' && *(current_position + 1) == 'x')
{
current_position += 2;
std::size_t processed;
char character = static_cast<char>(std::stoi(current_position, &processed, 16));
if (processed == 0)
2025-01-30 23:09:51 +01:00
{
REJECT;
}
else
{
2025-01-24 11:41:14 +01:00
current_position += processed - 1;
result.push_back(character);
2025-01-30 23:09:51 +01:00
}
2025-01-24 11:41:14 +01:00
}
else if (*current_position == '\\')
{
++current_position;
2025-02-07 22:12:59 +01:00
char escape = elna::boot::escape_char(*current_position);
if (escape == elna::boot::escape_invalid_char)
2025-01-30 23:09:51 +01:00
{
REJECT;
}
2025-02-07 22:12:59 +01:00
result.push_back(escape);
2025-01-24 11:41:14 +01:00
}
else
{
result.push_back(*current_position);
}
++current_position;
}
result.pop_back();
return yy::parser::make_STRING(result, this->location);
2025-01-03 22:18:35 +01:00
}
2024-12-21 00:08:48 +01:00
\( {
return yy::parser::make_LEFT_PAREN(this->location);
}
\) {
return yy::parser::make_RIGHT_PAREN(this->location);
}
2025-01-05 00:06:51 +01:00
\[ {
return yy::parser::make_LEFT_SQUARE(this->location);
}
\] {
return yy::parser::make_RIGHT_SQUARE(this->location);
}
2025-02-15 00:38:46 +01:00
\<\< {
return yy::parser::make_SHIFT_LEFT(this->location);
}
\>\> {
return yy::parser::make_SHIFT_RIGHT(this->location);
}
2024-12-21 00:08:48 +01:00
\>= {
return yy::parser::make_GREATER_EQUAL(this->location);
}
\<= {
return yy::parser::make_LESS_EQUAL(this->location);
}
\> {
return yy::parser::make_GREATER_THAN(this->location);
}
\< {
return yy::parser::make_LESS_THAN(this->location);
}
2025-01-30 01:03:16 +01:00
\<\> {
2024-12-21 00:08:48 +01:00
return yy::parser::make_NOT_EQUAL(this->location);
}
= {
return yy::parser::make_EQUALS(this->location);
}
; {
return yy::parser::make_SEMICOLON(this->location);
}
\. {
return yy::parser::make_DOT(this->location);
}
, {
return yy::parser::make_COMMA(this->location);
}
\+ {
return yy::parser::make_PLUS(this->location);
}
2025-02-12 13:32:59 +01:00
\-> {
return yy::parser::make_ARROW(this->location);
}
2024-12-21 00:08:48 +01:00
\- {
return yy::parser::make_MINUS(this->location);
}
\* {
return yy::parser::make_MULTIPLICATION(this->location);
}
\/ {
return yy::parser::make_DIVISION(this->location);
}
2025-01-25 19:50:36 +01:00
% {
return yy::parser::make_REMAINDER(this->location);
}
2024-12-21 00:08:48 +01:00
:= {
return yy::parser::make_ASSIGNMENT(this->location);
}
: {
return yy::parser::make_COLON(this->location);
}
\^ {
return yy::parser::make_HAT(this->location);
}
@ {
return yy::parser::make_AT(this->location);
}
. {
std::stringstream ss;
2024-12-21 14:05:27 +01:00
ss << "Illegal character 0x" << std::hex << static_cast<unsigned int>(yytext[0]);
2024-12-23 13:54:11 +01:00
driver.error(this->location, ss.str());
2024-12-21 00:08:48 +01:00
}
%%