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