blog-static/code/compiler/13/scanner.l

59 lines
2.0 KiB
Plaintext
Raw Normal View History

2020-09-08 18:38:05 -07:00
%option noyywrap
%option reentrant
2020-09-08 18:38:05 -07:00
%{
#include <iostream>
#include "ast.hpp"
#include "definition.hpp"
#include "parse_driver.hpp"
2020-09-08 18:38:05 -07:00
#include "parser.hpp"
#define YY_EXTRA_TYPE parse_driver*
#define YY_USER_ACTION drv.location.step(); drv.location.columns(yyleng);
#define YY_INPUT(buf,result,max_size) \
{ \
int c = yyextra->get(); \
result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \
}
2020-09-08 18:38:05 -07:00
%}
%%
\n { drv.location.lines(); }
2020-09-09 12:21:50 -07:00
[ ]+ {}
\\ { return yy::parser::make_BACKSLASH(drv.location); }
\+ { return yy::parser::make_PLUS(drv.location); }
\* { return yy::parser::make_TIMES(drv.location); }
- { return yy::parser::make_MINUS(drv.location); }
\/ { return yy::parser::make_DIVIDE(drv.location); }
% { return yy::parser::make_MODULO(drv.location); }
== { return yy::parser::make_EQUALS(drv.location); }
\<= { return yy::parser::make_LESS_EQUALS(drv.location); }
2020-09-10 16:04:44 -07:00
` { return yy::parser::make_BACKTICK(drv.location); }
[0-9]+ { return yy::parser::make_INT(atoi(yytext), drv.location); }
defn { return yy::parser::make_DEFN(drv.location); }
data { return yy::parser::make_DATA(drv.location); }
case { return yy::parser::make_CASE(drv.location); }
of { return yy::parser::make_OF(drv.location); }
let { return yy::parser::make_LET(drv.location); }
in { return yy::parser::make_IN(drv.location); }
\{ { return yy::parser::make_OCURLY(drv.location); }
\} { return yy::parser::make_CCURLY(drv.location); }
\( { return yy::parser::make_OPAREN(drv.location); }
\) { return yy::parser::make_CPAREN(drv.location); }
, { return yy::parser::make_COMMA(drv.location); }
-> { return yy::parser::make_ARROW(drv.location); }
= { return yy::parser::make_EQUAL(drv.location); }
[a-z][a-zA-Z]* { return yy::parser::make_LID(std::string(yytext), drv.location); }
[A-Z][a-zA-Z]* { return yy::parser::make_UID(std::string(yytext), drv.location); }
<<EOF>> { return yy::parser::make_YYEOF(drv.location); }
2020-09-08 18:38:05 -07:00
%%
void scanner_init(parse_driver* d, yyscan_t* scanner) {
yylex_init_extra(d, scanner);
}
void scanner_destroy(yyscan_t* scanner) {
yylex_destroy(*scanner);
}