weltling / parle

Parser and lexer for PHP
Other
87 stars 10 forks source link

[future request] Parser: Add import method. (load result of dump method). #29

Closed mastir closed 1 year ago

mastir commented 3 years ago

Hey, i`m working to implement some templating with php expression support. As for now i have to rewrite parts of zend_language_parser.y to php method calls. This can be match easer to load it from bison/yakk format (without C calls for sure) Like one i got as result of dump() method call:

%token T_LNUMBER T_DNUMBER T_STRING T_ARRAY T_OBJECT_OPERATOR T_WHITESPACE T_VARIABLE STR_D STR_S '?' ':' '|' '^' '&' '<' '>' '+' '-' '*' '/' '%' '(' ')' '[' ']' '$' '=' '.' ',' '~'
%left T_LOGICAL_OR
%left T_LOGICAL_AND
%precedence T_DOUBLE_ARROW
%left T_BOOLEAN_OR
%left T_BOOLEAN_AND
%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL T_SPACESHIP
%nonassoc T_IS_SMALLER_OR_EQUAL T_IS_GREATER_OR_EQUAL
%left T_SL T_SR
%precedence '!'
%right T_POW
%left T_LOGICAL_XOR
%%

start: PARAMS;

PARAMS: PARAMS T_WHITESPACE PARAM
      | PARAM;

PARAM: T_STRING '=' expr
     | expr;

expr: NUMBER
    | STRING
    | VAR
    | ARRAY
    | expr T_BOOLEAN_OR expr
    | expr T_BOOLEAN_AND expr
    | expr T_LOGICAL_OR expr
    | expr T_LOGICAL_AND expr
    | expr T_LOGICAL_XOR expr
...
BenHanson commented 1 year ago

There is is already a read_bison() method for C++. I can expose that to PHP if you like.

mastir commented 1 year ago

yes, this would be great, thanks!

BenHanson commented 1 year ago

I have created a pull request. The method is readBison("%%\nstart: 'a';\n%%\n");

weltling commented 1 year ago

@BenHanson a follow up question - do you think something similar would be possible for Lexer? I guess this would first need some support in lexertl or most likely this might need something PHP specific (if referring to something like flex syntax).

Thansk

BenHanson commented 1 year ago

Definitely possible, see build_config_parser() in https://github.com/BenHanson/gram_grep/blob/master/main.cpp

I will look into doing a dump() function for rules first.