elna/source/lexer.ll

167 lines
6.5 KiB
LLVM
Raw Normal View History

2024-12-27 10:51:46 +01:00
/*
* This Source Code Form is subject to the terms of the Mozilla Public License
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*/
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
2024-12-23 13:54:11 +01:00
#define YY_DECL yy::parser::symbol_type elna::source::lexer::lex(elna::source::driver& driver)
2024-12-21 00:08:48 +01:00
#define yyterminate() return yy::parser::make_YYEOF(this->location)
%}
%option c++ noyywrap never-interactive
2024-12-21 14:05:27 +01:00
%option yyclass="elna::source::lexer"
2024-12-21 00:08:48 +01:00
%%
%{
this->location.step();
%}
\-\-.* {
/* Skip the comment */
}
[\ \t\r] ; /* Skip the whitespaces */
\n+ {
this->location.lines(yyleng);
this->location.step();
}
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);
}
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);
}
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);
}
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);
}
[A-Za-z_][A-Za-z0-9_]* {
return yy::parser::make_IDENTIFIER(yytext, this->location);
}
[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-01 23:02:19 +01:00
'[^']' {
return yy::parser::make_CHARACTER(
std::string(yytext, 1, strlen(yytext) - 2), this->location);
}
2025-01-03 22:18:35 +01:00
\"[^\"]*\" {
return yy::parser::make_STRING(
std::string(yytext, 1, strlen(yytext) - 2), this->location);
}
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);
}
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);
}
\/= {
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);
}
\- {
return yy::parser::make_MINUS(this->location);
}
\* {
return yy::parser::make_MULTIPLICATION(this->location);
}
\/ {
return yy::parser::make_DIVISION(this->location);
}
:= {
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
}
%%