Enable locations.

This commit is contained in:
Danila Fedorin 2020-09-09 12:21:50 -07:00
parent 5dbf75b5e4
commit 0e40c9e216
3 changed files with 41 additions and 34 deletions

View File

@ -17,7 +17,7 @@
#include "llvm/Target/TargetOptions.h" #include "llvm/Target/TargetOptions.h"
#include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetMachine.h"
void yy::parser::error(const std::string& msg) { void yy::parser::error(const yy::location& loc, const std::string& msg) {
std::cout << "An error occured: " << msg << std::endl; std::cout << "An error occured: " << msg << std::endl;
} }

View File

@ -39,6 +39,8 @@ extern yy::parser::symbol_type yylex();
%define api.value.type variant %define api.value.type variant
%define api.token.constructor %define api.token.constructor
%locations
%type <std::vector<std::string>> lowercaseParams %type <std::vector<std::string>> lowercaseParams
%type <std::vector<branch_ptr>> branches %type <std::vector<branch_ptr>> branches
%type <std::vector<constructor_ptr>> constructors %type <std::vector<constructor_ptr>> constructors
@ -78,26 +80,26 @@ lowercaseParams
; ;
aAdd aAdd
: aAdd PLUS aMul { $$ = ast_ptr(new ast_binop(PLUS, std::move($1), std::move($3))); } : aAdd PLUS aMul { $$ = ast_ptr(new ast_binop(PLUS, std::move($1), std::move($3), @$)); }
| aAdd MINUS aMul { $$ = ast_ptr(new ast_binop(MINUS, std::move($1), std::move($3))); } | aAdd MINUS aMul { $$ = ast_ptr(new ast_binop(MINUS, std::move($1), std::move($3), @$)); }
| aMul { $$ = std::move($1); } | aMul { $$ = std::move($1); }
; ;
aMul aMul
: aMul TIMES app { $$ = ast_ptr(new ast_binop(TIMES, std::move($1), std::move($3))); } : aMul TIMES app { $$ = ast_ptr(new ast_binop(TIMES, std::move($1), std::move($3), @$)); }
| aMul DIVIDE app { $$ = ast_ptr(new ast_binop(DIVIDE, std::move($1), std::move($3))); } | aMul DIVIDE app { $$ = ast_ptr(new ast_binop(DIVIDE, std::move($1), std::move($3), @$)); }
| app { $$ = std::move($1); } | app { $$ = std::move($1); }
; ;
app app
: app appBase { $$ = ast_ptr(new ast_app(std::move($1), std::move($2))); } : app appBase { $$ = ast_ptr(new ast_app(std::move($1), std::move($2), @$)); }
| appBase { $$ = std::move($1); } | appBase { $$ = std::move($1); }
; ;
appBase appBase
: INT { $$ = ast_ptr(new ast_int($1)); } : INT { $$ = ast_ptr(new ast_int($1, @$)); }
| LID { $$ = ast_ptr(new ast_lid(std::move($1))); } | LID { $$ = ast_ptr(new ast_lid(std::move($1), @$)); }
| UID { $$ = ast_ptr(new ast_uid(std::move($1))); } | UID { $$ = ast_ptr(new ast_uid(std::move($1), @$)); }
| OPAREN aAdd CPAREN { $$ = std::move($2); } | OPAREN aAdd CPAREN { $$ = std::move($2); }
| case { $$ = std::move($1); } | case { $$ = std::move($1); }
| let { $$ = std::move($1); } | let { $$ = std::move($1); }
@ -106,17 +108,17 @@ appBase
let let
: LET OCURLY definitions CCURLY IN OCURLY aAdd CCURLY : LET OCURLY definitions CCURLY IN OCURLY aAdd CCURLY
{ $$ = ast_ptr(new ast_let(std::move($3), std::move($7))); } { $$ = ast_ptr(new ast_let(std::move($3), std::move($7), @$)); }
; ;
lambda lambda
: BACKSLASH lowercaseParams ARROW OCURLY aAdd CCURLY : BACKSLASH lowercaseParams ARROW OCURLY aAdd CCURLY
{ $$ = ast_ptr(new ast_lambda(std::move($2), std::move($5))); } { $$ = ast_ptr(new ast_lambda(std::move($2), std::move($5), @$)); }
; ;
case case
: CASE aAdd OF OCURLY branches CCURLY : CASE aAdd OF OCURLY branches CCURLY
{ $$ = ast_ptr(new ast_case(std::move($2), std::move($5))); } { $$ = ast_ptr(new ast_case(std::move($2), std::move($5), @$)); }
; ;
branches branches

View File

@ -6,33 +6,38 @@
#include "definition.hpp" #include "definition.hpp"
#include "parser.hpp" #include "parser.hpp"
yy::parser::location_type location;
#define YY_DECL yy::parser::symbol_type yylex() #define YY_DECL yy::parser::symbol_type yylex()
#define YY_USER_ACTION location.step(); location.columns(yyleng);
%} %}
%% %%
[ \n]+ {} \n { location.lines(); }
\\ { return yy::parser::make_BACKSLASH(); } [ ]+ {}
\+ { return yy::parser::make_PLUS(); } \\ { return yy::parser::make_BACKSLASH(location); }
\* { return yy::parser::make_TIMES(); } \+ { return yy::parser::make_PLUS(location); }
- { return yy::parser::make_MINUS(); } \* { return yy::parser::make_TIMES(location); }
\/ { return yy::parser::make_DIVIDE(); } - { return yy::parser::make_MINUS(location); }
[0-9]+ { return yy::parser::make_INT(atoi(yytext)); } \/ { return yy::parser::make_DIVIDE(location); }
defn { return yy::parser::make_DEFN(); } [0-9]+ { return yy::parser::make_INT(atoi(yytext), location); }
data { return yy::parser::make_DATA(); } defn { return yy::parser::make_DEFN(location); }
case { return yy::parser::make_CASE(); } data { return yy::parser::make_DATA(location); }
of { return yy::parser::make_OF(); } case { return yy::parser::make_CASE(location); }
let { return yy::parser::make_LET(); } of { return yy::parser::make_OF(location); }
in { return yy::parser::make_IN(); } let { return yy::parser::make_LET(location); }
\{ { return yy::parser::make_OCURLY(); } in { return yy::parser::make_IN(location); }
\} { return yy::parser::make_CCURLY(); } \{ { return yy::parser::make_OCURLY(location); }
\( { return yy::parser::make_OPAREN(); } \} { return yy::parser::make_CCURLY(location); }
\) { return yy::parser::make_CPAREN(); } \( { return yy::parser::make_OPAREN(location); }
, { return yy::parser::make_COMMA(); } \) { return yy::parser::make_CPAREN(location); }
-> { return yy::parser::make_ARROW(); } , { return yy::parser::make_COMMA(location); }
= { return yy::parser::make_EQUAL(); } -> { return yy::parser::make_ARROW(location); }
[a-z][a-zA-Z]* { return yy::parser::make_LID(std::string(yytext)); } = { return yy::parser::make_EQUAL(location); }
[A-Z][a-zA-Z]* { return yy::parser::make_UID(std::string(yytext)); } [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); }
%% %%