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

57 lines
1.7 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"
2020-09-09 12:21:50 -07:00
yy::parser::location_type location;
#define YY_EXTRA_TYPE parse_driver*
2020-09-09 12:21:50 -07:00
#define YY_USER_ACTION location.step(); 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
%}
%%
2020-09-09 12:21:50 -07:00
\n { location.lines(); }
[ ]+ {}
\\ { return yy::parser::make_BACKSLASH(location); }
\+ { return yy::parser::make_PLUS(location); }
\* { return yy::parser::make_TIMES(location); }
- { return yy::parser::make_MINUS(location); }
\/ { return yy::parser::make_DIVIDE(location); }
[0-9]+ { return yy::parser::make_INT(atoi(yytext), location); }
defn { return yy::parser::make_DEFN(location); }
data { return yy::parser::make_DATA(location); }
case { return yy::parser::make_CASE(location); }
of { return yy::parser::make_OF(location); }
let { return yy::parser::make_LET(location); }
in { return yy::parser::make_IN(location); }
\{ { return yy::parser::make_OCURLY(location); }
\} { return yy::parser::make_CCURLY(location); }
\( { return yy::parser::make_OPAREN(location); }
\) { return yy::parser::make_CPAREN(location); }
, { return yy::parser::make_COMMA(location); }
-> { return yy::parser::make_ARROW(location); }
= { return yy::parser::make_EQUAL(location); }
[a-z][a-zA-Z]* { return yy::parser::make_LID(std::string(yytext), location); }
[A-Z][a-zA-Z]* { return yy::parser::make_UID(std::string(yytext), location); }
<<EOF>> { return yy::parser::make_YYEOF(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);
}