IDE navigatable token classes
[m6w6/ext-psi] / src / parser_proc.y
index 8ada20fa4a688e0b430ff3e0279e0e709d3de9ef..00e4c68d3c5ec211b086782b68900a08e862e03f 100644 (file)
@@ -6,27 +6,56 @@
 
 #include "plist.h"
 #include "parser.h"
+
+/* rename lemon symbols, works better than DEF(%name) */
+#define ParseAlloc psi_parser_proc_init_ex
+#define Parse psi_parser_proc_parse
+#define ParseTrace psi_parser_proc_trace
+#define ParseFree psi_parser_proc_free_ex
+
+/* fwd decls */
+void *ParseAlloc(void *(*mallocProc)(size_t));
+void ParseFree(void *p, void (*freeProc)(void*));
+
+/* wrappers */
+void *psi_parser_proc_init(void)
+{
+       return ParseAlloc(malloc);
+}
+
+void psi_parser_proc_free(void **parser_proc)
+{
+       if (*parser_proc) {
+               ParseFree(*parser_proc, free);
+               *parser_proc = NULL;
+       }
+}
+
 }
-%name psi_parser_proc_
 %token_prefix PSI_T_
 %token_type {struct psi_token *}
 %token_destructor {free($$);}
 %default_destructor {(void)P;}
 %extra_argument {struct psi_parser *P}
 %syntax_error { ++P->errors; if (TOKEN && TOKEN->type != PSI_T_EOF) { psi_error(PSI_WARNING, TOKEN->file, TOKEN->line, "PSI syntax error: Unexpected token '%s' at pos %u", TOKEN->text, TOKEN->col); } else { psi_error(PSI_WARNING, P->file.fn, P->line, "PSI syntax error: Unexpected end of input"); } }
+%token_class const_type_token BOOL INT FLOAT STRING .
+%token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME .
+%token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING .
+%token_class number_token NUMBER NSNAME .
+%token_class num_exp_binary_op_token PIPE CARET AMPERSAND LSHIFT RSHIFT PLUS MINUS ASTERISK SLASH MODULO .
+%token_class num_exp_unary_op_token TILDE NOT PLUS MINUS .
+%token_class let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL COUNT .
+%token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID .
+%token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE .
 %nonassoc NAME.
+%right NOT TILDE.
+%left PIPE.
+%left CARET.
+%left AMPERSAND.
+%left LSHIFT RSHIFT.
 %left PLUS MINUS.
-%left ASTERISK SLASH.
-%nonassoc AMPERSAND.
+%left ASTERISK SLASH MODULO.
 %fallback NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING COUNT.
-%token_class const_type_token BOOL INT FLOAT STRING.
-%token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
-%token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
-%token_class num_exp_token NUMBER NSNAME.
-%token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
-%token_class let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL COUNT.
-%token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID.
-%token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE.
 %type decl_enum {struct psi_decl_enum *}
 %destructor decl_enum {psi_decl_enum_free(&$$);}
 %type decl_enum_items {struct psi_plist*}
 %destructor impl_stmts {psi_plist_free($$);}
 %type impl_stmt {struct psi_token**}
 %destructor impl_stmt {psi_impl_stmt_free(&$$);}
+%type number {struct psi_number*}
+%destructor number {psi_number_free(&$$);}
 %type num_exp {struct psi_num_exp*}
 %destructor num_exp {psi_num_exp_free(&$$);}
 %type let_stmt {struct psi_let_stmt*}
 %destructor free_exp {psi_free_exp_free(&$$);}
 %type impl_type {struct psi_impl_type*}
 %destructor impl_type {psi_impl_type_free(&$$);}
-%type reference {char}
+%type reference {bool}
 %type indirection {unsigned}
 %type pointers {unsigned}
 file ::= blocks.
@@ -469,8 +500,12 @@ decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
  arg_->var->token = N;
  arg_->token = N;
 }
-decl_args ::= .
-decl_args ::= VOID.
+decl_args(args) ::= . {
+ args = NULL;
+}
+decl_args(args) ::= VOID. {
+ args = NULL;
+}
 decl_args(args) ::= decl_arg(arg). {
  args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free),
    &arg);
@@ -696,19 +731,29 @@ impl_stmt(i) ::= set_stmt(s). {
 impl_stmt(i) ::= free_stmt(f). {
  i = (struct psi_token**) f;
 }
-num_exp(exp) ::= num_exp_token(tok). {
- exp = psi_num_exp_init(tok->type, tok->text);
+number(exp) ::= number_token(tok). {
+ exp = psi_number_init(tok->type, tok->text);
  exp->token = tok;
 }
-num_exp(exp) ::= decl_var(var). {
- exp = psi_num_exp_init(PSI_T_NAME, var);
+number(exp) ::= decl_var(var). {
+ exp = psi_number_init(PSI_T_NAME, var);
  exp->token = psi_token_copy(var->token);
 }
-num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
- exp_->op = operator_->type;
- exp_->operand = operand_;
- exp = exp_;
- free(operator_);
+num_exp(exp) ::= number(num). {
+ exp = psi_num_exp_init_num(num);
+ exp->token = psi_token_copy(num->token);
+}
+num_exp(exp) ::= LPAREN(L) num_exp(exp_) RPAREN. {
+ exp = psi_num_exp_init_unary(PSI_T_LPAREN, exp_);
+ exp->token = L;
+}
+num_exp(exp) ::= num_exp(lhs_) num_exp_binary_op_token(OP) num_exp(rhs_). {
+ exp = psi_num_exp_init_binary(OP->type, lhs_, rhs_);
+ exp->token = OP;
+}
+num_exp(exp) ::= num_exp_unary_op_token(OP) num_exp(exp_). {
+ exp = psi_num_exp_init_unary(OP->type, exp_);
+ exp->token = OP;
 }
 let_exp(val) ::= NULL. {
  val = psi_let_exp_init(PSI_LET_NULL, NULL);
@@ -779,7 +824,9 @@ let_exps(exps) ::= let_exp(exp). {
 let_exps(exps) ::= let_exps(exps_) COMMA let_exp(exp). {
  exps = psi_plist_add(exps_, &exp);
 }
-callback_arg_list ::= .
+callback_arg_list(args) ::= . {
+ args = NULL;
+}
 callback_arg_list(args) ::= callback_args(args_). {
  args = args_;
 }
@@ -835,8 +882,9 @@ return_stmt(ret) ::= RETURN(T) set_func(func) EOS. {
  ret = psi_return_stmt_init(psi_set_exp_init(PSI_SET_FUNC, func));
  ret->token = T;
 }
-free_stmt(free) ::= FREE free_exps(calls) EOS. {
+free_stmt(free) ::= FREE(T) free_exps(calls) EOS. {
  free = psi_free_stmt_init(calls);
+ free->token = T;
 }
 free_exps(calls) ::= free_exp(call). {
  calls = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_free_exp_free),
@@ -850,10 +898,10 @@ free_exp(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
  call->token = F;
 }
 reference(r) ::= . {
- r = 0;
+ r = false;
 }
 reference(r) ::= AMPERSAND. {
- r = 1;
+ r = true;
 }
 indirection(i) ::= .{
  i = 0;