alecAbstraction Layer for Escape Codes |
git clone git://git.dimitrijedobrota.com/alec.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
parser.y (2243B)
0 %require "3.8.2" 1 %language "c++" 2 3 %code requires { 4 #include <string> 5 #include <cstdint> 6 #include "location.hpp" 7 #include "generator.h" 8 9 namespace alec { 10 class driver; 11 } // namespace alec 12 } 13 14 %define api.namespace { alec } 15 %define api.parser.class { parser } 16 %define api.value.type variant 17 %define api.location.type { location_t } 18 19 %locations 20 %define parse.error detailed 21 22 %header 23 %verbose 24 25 %parse-param {driver &drv} 26 %parse-param {const bool debug} 27 28 %initial-action 29 { 30 #if YYDEBUG != 0 31 set_debug_level(debug); 32 #endif 33 }; 34 35 %code { 36 #include "driver.hpp" 37 38 namespace alec { 39 template<typename RHS> 40 void calcLocation(location_t ¤t, const RHS &rhs, const std::size_t n); 41 42 std::vector<record> records; 43 std::vector<std::string> epilogue; 44 std::vector<std::string> prologue; 45 } // namespace alec 46 47 #define YYLLOC_DEFAULT(Cur, Rhs, N) calcLocation(Cur, Rhs, N) 48 #define yylex drv.yylex 49 } 50 51 %left <char *> LITERAL COMMENT PROLOGUE EPILOGUE 52 %token EOL COMMA SWITCH EMPTY 53 54 %type <record> record 55 %type <std::vector<std::string>> list items 56 %type <std::string> name 57 58 %start document 59 60 %% 61 62 document: prologue grammar epilogue 63 64 prologue: %empty 65 | prologue PROLOGUE { prologue.emplace_back($2); } 66 ; 67 68 epilogue: SWITCH 69 | epilogue EPILOGUE { epilogue.emplace_back($2); } 70 ; 71 72 grammar: SWITCH 73 | grammar EOL 74 | grammar record { records.emplace_back($2); } 75 ; 76 77 record: name list list list { $$ = record($1, $2, $3, $4); } 78 | COMMENT { $$ = record($1, {}, {}, {}); } 79 ; 80 81 name: LITERAL EOL { $$ = $1; } 82 ; 83 84 list: EMPTY { $$ = {}; } 85 | items EOL { $$ = $1; } 86 ; 87 88 items: LITERAL { $$ = { $1 }; } 89 | items COMMA LITERAL { $1.emplace_back($3); $$ = $1; } 90 ; 91 92 %% 93 94 namespace alec { 95 template<typename RHS> 96 inline void calcLocation(location_t ¤t, const RHS &rhs, const std::size_t n) 97 { 98 current = location_t(YYRHSLOC(rhs, 0).first, YYRHSLOC(rhs, n).second); 99 } 100 101 void parser::error(const location_t &location, const std::string &message) 102 { 103 std::cerr << "Error at lines " << location << ": " << message << std::endl; 104 } 105 } // namespace alec