/* Copyright 2021 xensik. All rights reserved. // // Use of this source code is governed by a GNU GPLv3 license // that can be found in the LICENSE file. */ %require "3.7" %skeleton "lalr1.cc" %language "c++" %output "parser.cpp" %defines "parser.hpp" %define api.prefix {S2} %define api.namespace {xsk::gsc::s2} %define api.location.type {xsk::gsc::location} %define api.value.type variant %define api.token.constructor %define api.token.raw %define parse.assert %define parse.trace %define parse.error detailed %define parse.lac full %locations %lex-param { yyscan_t yyscanner } %lex-param { xsk::gsc::location& loc } %parse-param { yyscan_t yyscanner } %parse-param { xsk::gsc::location& loc } %parse-param { xsk::gsc::program_ptr& ast } %code requires { #include "s2.hpp" typedef void *yyscan_t; #define YY_DECL xsk::gsc::s2::parser::symbol_type S2lex(yyscan_t yyscanner, xsk::gsc::location& loc) } %code top { #include "stdafx.hpp" #include "parser.hpp" #include "lexer.hpp" using namespace xsk::gsc; xsk::gsc::s2::parser::symbol_type S2lex(yyscan_t yyscanner, xsk::gsc::location& loc); } %token INCLUDE "#include" %token USINGTREE "#using_animtree" %token ANIMTREE "#animtree" %token ENDON "endon" %token NOTIFY "notify" %token WAIT "wait" %token WAITTILL "waittill" %token WAITTILLMATCH "waittillmatch" %token WAITTILLFRAMEEND "waittillframeend" %token WAITFRAME "waitframe" %token IF "if" %token ELSE "else" %token WHILE "while" %token FOR "for" %token FOREACH "foreach" %token IN "in" %token SWITCH "switch" %token CASE "case" %token DEFAULT "default" %token BREAK "break" %token CONTINUE "continue" %token RETURN "return" %token THREAD "thread" %token CHILDTHREAD "childthread" %token THISTHREAD "thisthread" %token CALL "call" %token TRUE "true" %token FALSE "false" %token UNDEFINED "undefined" %token SIZE "size" %token GAME "game" %token SELF "self" %token ANIM "anim" %token LEVEL "level" %token LPAREN "(" %token RPAREN ")" %token LBRACE "{" %token RBRACE "}" %token LBRACKET "[" %token RBRACKET "]" %token COMMA "," %token DOT "." %token DOUBLECOLON "::" %token COLON ":" %token SEMICOLON ";" %token INCREMENT "++" %token DECREMENT "--" %token LSHIFT "<<" %token RSHIFT ">>" %token OR "||" %token AND "&&" %token EQUALITY "==" %token INEQUALITY "!=" %token LESS_EQUAL "<=" %token GREATER_EQUAL ">=" %token LESS "<" %token GREATER ">" %token NOT "!" %token COMPLEMENT "~" %token ASSIGN "=" %token ASSIGN_ADD "+=" %token ASSIGN_SUB "-=" %token ASSIGN_MULT "*=" %token ASSIGN_DIV "/=" %token ASSIGN_MOD "%=" %token ASSIGN_BITWISE_OR "|=" %token ASSIGN_BITWISE_AND "&=" %token ASSIGN_BITWISE_EXOR "^=" %token ASSIGN_RSHIFT ">>=" %token ASSIGN_LSHIFT "<<=" %token BITWISE_OR "|" %token BITWISE_AND "&" %token BITWISE_EXOR "^" %token ADD "+" %token SUB "-" %token MULT "*" %token DIV "/" %token MOD "%" %token FILE "file path" %token NAME "identifier" %token STRING "string literal" %token ISTRING "localized string" %token FLOAT "float" %token INTEGER "int" %type program %type include %type define %type usingtree %type constant %type thread %type parameters %type stmt %type stmt_block %type stmt_list %type stmt_call %type stmt_assign %type stmt_endon %type stmt_notify %type stmt_wait %type stmt_waittill %type stmt_waittillmatch %type stmt_waittillframeend %type stmt_waitframe %type stmt_if %type stmt_ifelse %type stmt_while %type stmt_for %type stmt_foreach %type stmt_switch %type stmt_case %type stmt_default %type stmt_break %type stmt_continue %type stmt_return %type for_stmt %type for_expr %type expr_assign %type expr %type expr_compare %type expr_binary %type expr_primitive %type expr_call %type expr_call_thread %type expr_call_childthread %type expr_call_function %type expr_call_pointer %type expr_arguments %type expr_arguments_filled %type expr_arguments_empty %type expr_function %type expr_add_array %type expr_array %type expr_field %type expr_size %type object %type thisthread %type empty_array %type undefined %type game %type self %type anim %type level %type animation %type animtree %type name %type file %type istring %type string %type vector %type neg_float %type neg_integer %type float %type integer %type false %type true %nonassoc ADD_ARRAY %nonassoc RBRACKET %nonassoc THEN %nonassoc ELSE %nonassoc INCREMENT DECREMENT %left OR %left AND %left BITWISE_OR %left BITWISE_AND %left BITWISE_EXOR %left EQUALITY INEQUALITY %left LESS GREATER LESS_EQUAL GREATER_EQUAL %left LSHIFT RSHIFT %left ADD SUB %left MULT DIV MOD %right NOT COMPLEMENT %precedence NEG %precedence ANIMREF %precedence PREINC PREDEC %precedence POSTINC POSTDEC %start root %% root : program { ast = std::move($1); } | { ast = std::make_unique(@$); } ; program : program include { $$ = std::move($1); $$->includes.push_back(std::move($2)); } | program define { $$ = std::move($1); $$->definitions.push_back(std::move($2)); } | include { $$ = std::make_unique(@$); $$->includes.push_back(std::move($1)); } | define { $$ = std::make_unique(@$); $$->definitions.push_back(std::move($1)); } ; include : INCLUDE file SEMICOLON { $$ = std::make_unique(@$, std::move($2)); } ; define : usingtree { $$.as_usingtree = std::move($1); } | constant { $$.as_constant = std::move($1); } | thread { $$.as_thread = std::move($1); } ; usingtree : USINGTREE LPAREN string RPAREN SEMICOLON { $$ = std::make_unique(@$, std::move($3)); } ; constant : name ASSIGN expr SEMICOLON { $$ = std::make_unique(@$, std::move($1), std::move($3)); } ; thread : name LPAREN parameters RPAREN stmt_block { $$ = std::make_unique(@$, std::move($1), std::move($3), std::move($5)); } ; parameters : parameters COMMA name { $$ = std::move($1); $$->list.push_back(std::move($3)); } | name { $$ = std::make_unique(@$); $$->list.push_back(std::move($1)); } | { $$ = std::make_unique(@$); } ; stmt : stmt_block { $$.as_list = std::move($1); } | stmt_call { $$.as_call = std::move($1); } | stmt_assign { $$.as_assign = std::move($1); } | stmt_endon { $$.as_endon = std::move($1); } | stmt_notify { $$.as_notify = std::move($1); } | stmt_wait { $$.as_wait = std::move($1); } | stmt_waittill { $$.as_waittill = std::move($1); } | stmt_waittillmatch { $$.as_waittillmatch = std::move($1); } | stmt_waittillframeend { $$.as_waittillframeend = std::move($1); } | stmt_waitframe { $$.as_waitframe = std::move($1); } | stmt_if { $$.as_if = std::move($1); } | stmt_ifelse { $$.as_ifelse = std::move($1); } | stmt_while { $$.as_while = std::move($1); } | stmt_for { $$.as_for = std::move($1); } | stmt_foreach { $$.as_foreach = std::move($1); } | stmt_switch { $$.as_switch = std::move($1); } | stmt_case { $$.as_case = std::move($1); } | stmt_default { $$.as_default = std::move($1); } | stmt_break { $$.as_break = std::move($1); } | stmt_continue { $$.as_continue = std::move($1); } | stmt_return { $$.as_return = std::move($1); } ; stmt_block : LBRACE stmt_list RBRACE { $$ = std::move($2); } | LBRACE RBRACE { $$ = std::make_unique(@$); } ; stmt_list : stmt_list stmt { $$ = std::move($1); $$->stmts.push_back(std::move($2)); } | stmt { $$ = std::make_unique(@$); $$->stmts.push_back(std::move($1)); } ; stmt_call : expr_call SEMICOLON { $$ = std::make_unique(@$, std::move($1)); } | expr_call_thread SEMICOLON { $$ = std::make_unique(@$, std::move($1)); } ; stmt_assign : expr_assign SEMICOLON { $$ = std::make_unique(@$, std::move($1)); } ; stmt_endon : object ENDON LPAREN expr RPAREN SEMICOLON { $$ = std::make_unique(@$, std::move($1), std::move($4)); } ; stmt_notify : object NOTIFY LPAREN expr COMMA expr_arguments RPAREN SEMICOLON { $$ = std::make_unique(@$, std::move($1), std::move($4), std::move($6)); } | object NOTIFY LPAREN expr RPAREN SEMICOLON { $$ = std::make_unique(@$, std::move($1), std::move($4), std::make_unique(@$)); } ; stmt_wait : WAIT expr SEMICOLON { $$ = std::make_unique(@$, std::move($2)); } ; stmt_waittill : object WAITTILL LPAREN expr COMMA expr_arguments RPAREN SEMICOLON { $$ = std::make_unique(@$, std::move($1), std::move($4), std::move($6)); } | object WAITTILL LPAREN expr RPAREN SEMICOLON { $$ = std::make_unique(@$, std::move($1), std::move($4), std::make_unique(@$)); } ; stmt_waittillmatch : object WAITTILLMATCH LPAREN expr COMMA expr_arguments RPAREN SEMICOLON { $$ = std::make_unique(@$, std::move($1), std::move($4), std::move($6)); } | object WAITTILLMATCH LPAREN expr RPAREN SEMICOLON { $$ = std::make_unique(@$, std::move($1), std::move($4), std::make_unique(@$)); } ; stmt_waittillframeend : WAITTILLFRAMEEND SEMICOLON { $$ = std::make_unique(@$); } ; stmt_waitframe : WAITFRAME SEMICOLON { $$ = std::make_unique(@$); } | WAITFRAME LPAREN RPAREN SEMICOLON { $$ = std::make_unique(@$); } ; stmt_if : IF LPAREN expr RPAREN stmt %prec THEN { $$ = std::make_unique(@$, std::move($3), std::move($5)); } ; stmt_ifelse : IF LPAREN expr RPAREN stmt ELSE stmt { $$ = std::make_unique(@$, std::move($3), std::move($5), std::move($7)); } ; stmt_while : WHILE LPAREN expr RPAREN stmt { $$ = std::make_unique(@$, std::move($3), std::move($5)); } ; stmt_for : FOR LPAREN for_stmt SEMICOLON for_expr SEMICOLON for_stmt RPAREN stmt { $$ = std::make_unique(@$, std::move($3), std::move($5), std::move($7), std::move($9)); } ; stmt_foreach : FOREACH LPAREN name IN expr RPAREN stmt { $$ = std::make_unique(@$, expr_ptr(std::move($3)), std::move($5), std::move($7)); } | FOREACH LPAREN name COMMA name IN expr RPAREN stmt { $$ = std::make_unique(@$, expr_ptr(std::move($3)), expr_ptr(std::move($5)), std::move($7), std::move($9)); } ; stmt_switch : SWITCH LPAREN expr RPAREN stmt_block { $$ = std::make_unique(@$, std::move($3), std::move($5)); } ; stmt_case : CASE integer COLON { $$ = std::make_unique(@$, expr_ptr(std::move($2)), std::make_unique(@$)); } | CASE neg_integer COLON { $$ = std::make_unique(@$, expr_ptr(std::move($2)), std::make_unique(@$)); } | CASE string COLON { $$ = std::make_unique(@$, expr_ptr(std::move($2)), std::make_unique(@$)); } ; stmt_default : DEFAULT COLON { $$ = std::make_unique(@$, std::make_unique(@$)); } ; stmt_break : BREAK SEMICOLON { $$ = std::make_unique(@$); } ; stmt_continue : CONTINUE SEMICOLON { $$ = std::make_unique(@$); } ; stmt_return : RETURN expr SEMICOLON { $$ = std::make_unique(@$, std::move($2)); } | RETURN SEMICOLON { $$ = std::make_unique(@$, std::make_unique(@$)); } ; for_stmt : expr_assign { $$.as_list = std::make_unique(@$); $$.as_list->stmts.push_back(stmt_ptr(std::make_unique(@$, std::move($1)))); } | { $$.as_node = std::make_unique(@$); } ; for_expr : expr { $$ = std::move($1); } | { $$.as_node = std::make_unique(@$); } ; expr : expr_compare { $$ = std::move($1); } | expr_binary { $$ = std::move($1); } | expr_primitive { $$ = std::move($1); } ; expr_assign : INCREMENT object %prec PREINC { $$ = std::make_unique(@$, std::move($2)); } | DECREMENT object %prec PREDEC { $$ = std::make_unique(@$, std::move($2)); } | object INCREMENT %prec POSTINC { $$ = std::make_unique(@$, std::move($1)); } | object DECREMENT %prec POSTDEC { $$ = std::make_unique(@$, std::move($1)); } | object ASSIGN expr { $$ = std::make_unique(@$, std::move($1), std::move($3)); } | object ASSIGN_BITWISE_OR expr { $$ = std::make_unique(@$, std::move($1), std::move($3)); } | object ASSIGN_BITWISE_AND expr { $$ = std::make_unique(@$, std::move($1), std::move($3)); } | object ASSIGN_BITWISE_EXOR expr { $$ = std::make_unique(@$, std::move($1), std::move($3)); } | object ASSIGN_LSHIFT expr { $$ = std::make_unique(@$, std::move($1),std::move( $3)); } | object ASSIGN_RSHIFT expr { $$ = std::make_unique(@$, std::move($1), std::move($3)); } | object ASSIGN_ADD expr { $$ = std::make_unique(@$, std::move($1), std::move($3)); } | object ASSIGN_SUB expr { $$ = std::make_unique(@$, std::move($1), std::move($3)); } | object ASSIGN_MULT expr { $$ = std::make_unique(@$, std::move($1), std::move($3)); } | object ASSIGN_DIV expr { $$ = std::make_unique(@$, std::move($1), std::move($3)); } | object ASSIGN_MOD expr { $$ = std::make_unique(@$, std::move($1), std::move($3)); } ; expr_compare : expr OR expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr AND expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr EQUALITY expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr INEQUALITY expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr LESS_EQUAL expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr GREATER_EQUAL expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr LESS expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr GREATER expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } ; expr_binary : expr BITWISE_OR expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr BITWISE_AND expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr BITWISE_EXOR expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr LSHIFT expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr RSHIFT expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr ADD expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr SUB expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr MULT expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr DIV expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } | expr MOD expr { $$.as_node = std::make_unique(@$, std::move($1), std::move($3)); } ; expr_primitive : LPAREN expr RPAREN { $$ = std::move($2); } | COMPLEMENT expr { $$.as_node = std::make_unique(@$, std::move($2)); } | NOT expr { $$.as_node = std::make_unique(@$, std::move($2)); } | expr_call { $$.as_node = std::move($1); } | expr_call_thread { $$.as_node = std::move($1); } | expr_call_childthread { $$.as_node = std::move($1); } | expr_function { $$.as_node = std::move($1); } | expr_add_array { $$.as_node = std::move($1); } | expr_array { $$.as_node = std::move($1); } | expr_field { $$.as_node = std::move($1); } | expr_size { $$.as_node = std::move($1); } | thisthread { $$.as_node = std::move($1); } | empty_array { $$.as_node = std::move($1); } | undefined { $$.as_node = std::move($1); } | game { $$.as_node = std::move($1); } | self { $$.as_node = std::move($1); } | anim { $$.as_node = std::move($1); } | level { $$.as_node = std::move($1); } | animation { $$.as_node = std::move($1); } | animtree { $$.as_node = std::move($1); } | name { $$.as_node = std::move($1); } | istring { $$.as_node = std::move($1); } | string { $$.as_node = std::move($1); } | vector { $$.as_node = std::move($1); } | neg_float { $$.as_node = std::move($1); } | neg_integer { $$.as_node = std::move($1); } | float { $$.as_node = std::move($1); } | integer { $$.as_node = std::move($1); } | false { $$.as_node = std::move($1); } | true { $$.as_node = std::move($1); } ; expr_call : expr_call_function { $$ = std::make_unique(@$, false, false, std::make_unique(@$), std::move($1)); } | expr_call_pointer { $$ = std::make_unique(@$, false, false, std::make_unique(@$), std::move($1)); } | object expr_call_function { $$ = std::make_unique(@$, false, false, std::move($1), std::move($2)); } | object expr_call_pointer { $$ = std::make_unique(@$, false, false, std::move($1), std::move($2)); } ; expr_call_thread : THREAD expr_call_function { $$ = std::make_unique(@$, true, false, std::make_unique(@$), std::move($2)); } | THREAD expr_call_pointer { $$ = std::make_unique(@$, true, false, std::make_unique(@$), std::move($2)); } | object THREAD expr_call_function { $$ = std::make_unique(@$, true, false, std::move($1), std::move($3)); } | object THREAD expr_call_pointer { $$ = std::make_unique(@$, true, false, std::move($1), std::move($3)); } ; expr_call_childthread : CHILDTHREAD expr_call_function { $$ = std::make_unique(@$, false, true, std::make_unique(@$), std::move($2)); } | CHILDTHREAD expr_call_pointer { $$ = std::make_unique(@$, false, true, std::make_unique(@$), std::move($2)); } | object CHILDTHREAD expr_call_function { $$ = std::make_unique(@$, false, true, std::move($1), std::move($3)); } | object CHILDTHREAD expr_call_pointer { $$ = std::make_unique(@$, false, true, std::move($1), std::move($3)); } ; expr_call_function : name LPAREN expr_arguments RPAREN {$$.as_func = std::make_unique(@$, std::make_unique(), std::move($1), std::move($3)); } | file DOUBLECOLON name LPAREN expr_arguments RPAREN { $$.as_func = std::make_unique(@$, std::move($1), std::move($3), std::move($5)); } ; expr_call_pointer : LBRACKET LBRACKET expr RBRACKET RBRACKET LPAREN expr_arguments RPAREN { $$.as_pointer = std::make_unique(@$, false, std::move($3), std::move($7)); } | CALL LBRACKET LBRACKET expr RBRACKET RBRACKET LPAREN expr_arguments RPAREN { $$.as_pointer = std::make_unique(@$, true, std::move($4), std::move($8)); } ; expr_arguments : expr_arguments_filled { $$ = std::move($1); } | expr_arguments_empty { $$ = std::move($1); } ; expr_arguments_filled : expr_arguments COMMA expr { $$ = std::move($1); $$->list.push_back(std::move($3)); } | expr %prec ADD_ARRAY { $$ = std::make_unique(@$); $$->list.push_back(std::move($1)); } ; expr_arguments_empty : { $$ = std::make_unique(@$); } ; expr_function : DOUBLECOLON name { $$ = std::make_unique(@$, std::make_unique(@$), std::move($2)); } | file DOUBLECOLON name { $$ = std::make_unique(@$, std::move($1), std::move($3)); } ; expr_add_array : LBRACKET expr_arguments_filled RBRACKET { $$ = std::make_unique(@$, std::move($2)); } ; expr_array : object LBRACKET expr RBRACKET { $$ = std::make_unique(@$, std::move($1), std::move($3)); } ; expr_field : object DOT name { $$ = std::make_unique(@$, std::move($1), std::move($3)); } ; expr_size : object DOT SIZE { $$ = std::make_unique(@$, std::move($1)); } ; object : expr_call { $$ = std::move($1); } | expr_array { $$ = std::move($1); } | expr_field { $$ = std::move($1); } | game { $$ = std::move($1); } | self { $$ = std::move($1); } | anim { $$ = std::move($1); } | level { $$ = std::move($1); } | name { $$ = std::move($1); } ; thisthread : THISTHREAD { $$ = std::make_unique(@$); }; empty_array : LBRACKET RBRACKET { $$ = std::make_unique(@$); }; undefined : UNDEFINED { $$ = std::make_unique(@$); }; game : GAME { $$ = std::make_unique(@$); }; self : SELF { $$ = std::make_unique(@$); }; anim : ANIM { $$ = std::make_unique(@$); }; level : LEVEL { $$ = std::make_unique(@$); }; animation : MOD NAME %prec ANIMREF { $$ = std::make_unique(@$, $2); }; animtree : ANIMTREE { $$ = std::make_unique(@$); }; name : NAME { $$ = std::make_unique(@$, $1); }; file : FILE { $$ = std::make_unique(@$, $1); }; istring : ISTRING { $$ = std::make_unique(@$, $1); }; string : STRING { $$ = std::make_unique(@$, $1); }; vector : LPAREN expr COMMA expr COMMA expr RPAREN { $$ = std::make_unique(@$, std::move($2), std::move($4), std::move($6)); }; neg_float : SUB FLOAT %prec NEG { $$ = std::make_unique(@$, "-" + $2); }; neg_integer : SUB INTEGER %prec NEG { $$ = std::make_unique(@$, "-" + $2); }; float : FLOAT { $$ = std::make_unique(@$, $1); }; integer : INTEGER { $$ = std::make_unique(@$, $1); }; false : FALSE { $$ = std::make_unique(@$); }; true : TRUE { $$ = std::make_unique(@$); }; %% void xsk::gsc::s2::parser::error(const xsk::gsc::location& loc, const std::string& msg) { throw xsk::gsc::comp_error(loc, msg); }