Skip to content

Commit

Permalink
Adding closure-arrow support at parsing level, better location of met…
Browse files Browse the repository at this point in the history
…hod/class related errors in parser
  • Loading branch information
andresgutierrez committed Oct 26, 2014
1 parent 32195a6 commit 45200db
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Library/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
class Compiler
{
const VERSION = '0.5.6a';
const VERSION = '0.5.7a';

/**
* @var CompilerFile[]
Expand Down
3 changes: 3 additions & 0 deletions Library/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,9 @@ public function compile(CompilationContext $compilationContext)
$closure->setExpectReturn($this->_expecting, $this->_expectingVariable);
return $closure->compile($expression, $compilationContext);

case 'closure-arrow':
return new LiteralCompiledExpression('null', null, $expression);

default:
throw new CompilerException("Unknown expression: " . $type, $expression);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/php_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define PHP_TEST_VERSION "1.0.0"
#define PHP_TEST_EXTNAME "test"
#define PHP_TEST_AUTHOR "Zephir Team and contributors"
#define PHP_TEST_ZEPVERSION "0.5.6a"
#define PHP_TEST_ZEPVERSION "0.5.7a"
#define PHP_TEST_DESCRIPTION "Description test for<br/>Test Extension"

typedef struct _zephir_struct_test {
Expand Down
14 changes: 14 additions & 0 deletions ext/test/closures.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ext/test/closures.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions parser/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ int xx_parse_program(char *program, unsigned int program_length, char *file_path
state->active_file = file_path;
state->active_line = 1;
state->active_char = 1;
state->class_line = 0;
state->class_char = 0;
state->method_line = 0;
state->method_char = 0;

state->end = state->start;

Expand Down Expand Up @@ -359,6 +363,9 @@ int xx_parse_program(char *program, unsigned int program_length, char *file_path
case XX_T_ARROW:
xx_(xx_parser, XX_ARROW, NULL, parser_status);
break;
case XX_T_DOUBLEARROW:
xx_(xx_parser, XX_DOUBLEARROW, NULL, parser_status);
break;
case XX_T_DOUBLECOLON:
xx_(xx_parser, XX_DOUBLECOLON, NULL, parser_status);
break;
Expand Down
225 changes: 113 additions & 112 deletions parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,115 +5,116 @@
#define XX_SCOPED 5
#define XX_COMMA 6
#define XX_REQUIRE 7
#define XX_QUESTION 8
#define XX_LIKELY 9
#define XX_UNLIKELY 10
#define XX_OR 11
#define XX_AND 12
#define XX_INSTANCEOF 13
#define XX_BITWISE_OR 14
#define XX_BITWISE_AND 15
#define XX_BITWISE_XOR 16
#define XX_BITWISE_SHIFTLEFT 17
#define XX_BITWISE_SHIFTRIGHT 18
#define XX_EQUALS 19
#define XX_IDENTICAL 20
#define XX_LESS 21
#define XX_GREATER 22
#define XX_LESSEQUAL 23
#define XX_GREATEREQUAL 24
#define XX_NOTIDENTICAL 25
#define XX_NOTEQUALS 26
#define XX_ADD 27
#define XX_SUB 28
#define XX_CONCAT 29
#define XX_MUL 30
#define XX_DIV 31
#define XX_MOD 32
#define XX_ISSET 33
#define XX_FETCH 34
#define XX_EMPTY 35
#define XX_TYPEOF 36
#define XX_CLONE 37
#define XX_NEW 38
#define XX_NOT 39
#define XX_PARENTHESES_CLOSE 40
#define XX_SBRACKET_OPEN 41
#define XX_ARROW 42
#define XX_NAMESPACE 43
#define XX_IDENTIFIER 44
#define XX_DOTCOMMA 45
#define XX_USE 46
#define XX_AS 47
#define XX_INTERFACE 48
#define XX_EXTENDS 49
#define XX_CLASS 50
#define XX_IMPLEMENTS 51
#define XX_ABSTRACT 52
#define XX_FINAL 53
#define XX_BRACKET_OPEN 54
#define XX_BRACKET_CLOSE 55
#define XX_COMMENT 56
#define XX_ASSIGN 57
#define XX_CONST 58
#define XX_CONSTANT 59
#define XX_FUNCTION 60
#define XX_PARENTHESES_OPEN 61
#define XX_INLINE 62
#define XX_DEPRECATED 63
#define XX_VOID 64
#define XX_NULL 65
#define XX_THIS 66
#define XX_SBRACKET_CLOSE 67
#define XX_TYPE_INTEGER 68
#define XX_TYPE_UINTEGER 69
#define XX_TYPE_LONG 70
#define XX_TYPE_ULONG 71
#define XX_TYPE_CHAR 72
#define XX_TYPE_UCHAR 73
#define XX_TYPE_DOUBLE 74
#define XX_TYPE_BOOL 75
#define XX_TYPE_STRING 76
#define XX_TYPE_ARRAY 77
#define XX_TYPE_VAR 78
#define XX_TYPE_CALLABLE 79
#define XX_TYPE_RESOURCE 80
#define XX_TYPE_OBJECT 81
#define XX_BREAK 82
#define XX_CONTINUE 83
#define XX_IF 84
#define XX_ELSE 85
#define XX_ELSEIF 86
#define XX_SWITCH 87
#define XX_CASE 88
#define XX_COLON 89
#define XX_DEFAULT 90
#define XX_LOOP 91
#define XX_WHILE 92
#define XX_DO 93
#define XX_TRY 94
#define XX_CATCH 95
#define XX_FOR 96
#define XX_IN 97
#define XX_REVERSE 98
#define XX_LET 99
#define XX_ADDASSIGN 100
#define XX_SUBASSIGN 101
#define XX_MULASSIGN 102
#define XX_DIVASSIGN 103
#define XX_CONCATASSIGN 104
#define XX_MODASSIGN 105
#define XX_STRING 106
#define XX_DOUBLECOLON 107
#define XX_INCR 108
#define XX_DECR 109
#define XX_ECHO 110
#define XX_RETURN 111
#define XX_UNSET 112
#define XX_THROW 113
#define XX_INTEGER 114
#define XX_CHAR 115
#define XX_DOUBLE 116
#define XX_TRUE 117
#define XX_FALSE 118
#define XX_CBLOCK 119
#define XX_DOUBLEARROW 8
#define XX_QUESTION 9
#define XX_LIKELY 10
#define XX_UNLIKELY 11
#define XX_OR 12
#define XX_AND 13
#define XX_INSTANCEOF 14
#define XX_BITWISE_OR 15
#define XX_BITWISE_AND 16
#define XX_BITWISE_XOR 17
#define XX_BITWISE_SHIFTLEFT 18
#define XX_BITWISE_SHIFTRIGHT 19
#define XX_EQUALS 20
#define XX_IDENTICAL 21
#define XX_LESS 22
#define XX_GREATER 23
#define XX_LESSEQUAL 24
#define XX_GREATEREQUAL 25
#define XX_NOTIDENTICAL 26
#define XX_NOTEQUALS 27
#define XX_ADD 28
#define XX_SUB 29
#define XX_CONCAT 30
#define XX_MUL 31
#define XX_DIV 32
#define XX_MOD 33
#define XX_ISSET 34
#define XX_FETCH 35
#define XX_EMPTY 36
#define XX_TYPEOF 37
#define XX_CLONE 38
#define XX_NEW 39
#define XX_NOT 40
#define XX_PARENTHESES_CLOSE 41
#define XX_SBRACKET_OPEN 42
#define XX_ARROW 43
#define XX_NAMESPACE 44
#define XX_IDENTIFIER 45
#define XX_DOTCOMMA 46
#define XX_USE 47
#define XX_AS 48
#define XX_INTERFACE 49
#define XX_EXTENDS 50
#define XX_CLASS 51
#define XX_IMPLEMENTS 52
#define XX_ABSTRACT 53
#define XX_FINAL 54
#define XX_BRACKET_OPEN 55
#define XX_BRACKET_CLOSE 56
#define XX_COMMENT 57
#define XX_ASSIGN 58
#define XX_CONST 59
#define XX_CONSTANT 60
#define XX_FUNCTION 61
#define XX_PARENTHESES_OPEN 62
#define XX_INLINE 63
#define XX_DEPRECATED 64
#define XX_VOID 65
#define XX_NULL 66
#define XX_THIS 67
#define XX_SBRACKET_CLOSE 68
#define XX_TYPE_INTEGER 69
#define XX_TYPE_UINTEGER 70
#define XX_TYPE_LONG 71
#define XX_TYPE_ULONG 72
#define XX_TYPE_CHAR 73
#define XX_TYPE_UCHAR 74
#define XX_TYPE_DOUBLE 75
#define XX_TYPE_BOOL 76
#define XX_TYPE_STRING 77
#define XX_TYPE_ARRAY 78
#define XX_TYPE_VAR 79
#define XX_TYPE_CALLABLE 80
#define XX_TYPE_RESOURCE 81
#define XX_TYPE_OBJECT 82
#define XX_BREAK 83
#define XX_CONTINUE 84
#define XX_IF 85
#define XX_ELSE 86
#define XX_ELSEIF 87
#define XX_SWITCH 88
#define XX_CASE 89
#define XX_COLON 90
#define XX_DEFAULT 91
#define XX_LOOP 92
#define XX_WHILE 93
#define XX_DO 94
#define XX_TRY 95
#define XX_CATCH 96
#define XX_FOR 97
#define XX_IN 98
#define XX_REVERSE 99
#define XX_LET 100
#define XX_ADDASSIGN 101
#define XX_SUBASSIGN 102
#define XX_MULASSIGN 103
#define XX_DIVASSIGN 104
#define XX_CONCATASSIGN 105
#define XX_MODASSIGN 106
#define XX_STRING 107
#define XX_DOUBLECOLON 108
#define XX_INCR 109
#define XX_DECR 110
#define XX_ECHO 111
#define XX_RETURN 112
#define XX_UNSET 113
#define XX_THROW 114
#define XX_INTEGER 115
#define XX_CHAR 116
#define XX_DOUBLE 117
#define XX_TRUE 118
#define XX_FALSE 119
#define XX_CBLOCK 120
14 changes: 10 additions & 4 deletions parser/parser.lemon
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

%left COMMA .
%right REQUIRE .
%right DOUBLEARROW .
%right QUESTION .
%right LIKELY UNLIKELY .
%left OR .
Expand Down Expand Up @@ -269,8 +270,8 @@ static json_object *xx_ret_class_definition(json_object *properties, json_object
}

json_object_object_add(ret, "file", json_object_new_string(state->active_file));
json_object_object_add(ret, "line", json_object_new_int(state->active_line));
json_object_object_add(ret, "char", json_object_new_int(state->active_char));
json_object_object_add(ret, "line", json_object_new_int(state->class_line));
json_object_object_add(ret, "char", json_object_new_int(state->class_char));

return ret;
}
Expand Down Expand Up @@ -383,8 +384,8 @@ static json_object *xx_ret_class_method(json_object *visibility, xx_parser_token
}

json_object_object_add(ret, "file", json_object_new_string(state->active_file));
json_object_object_add(ret, "line", json_object_new_int(state->active_line));
json_object_object_add(ret, "char", json_object_new_int(state->active_char));
json_object_object_add(ret, "line", json_object_new_int(state->method_line));
json_object_object_add(ret, "char", json_object_new_int(state->method_char));

return ret;
}
Expand Down Expand Up @@ -2972,6 +2973,11 @@ xx_common_expr(R) ::= FUNCTION PARENTHESES_OPEN xx_parameter_list(L) PARENTHESES
R = xx_ret_expr("closure", L, S, NULL, status->scanner_state);
}

/** x => x + 1 */
xx_common_expr(R) ::= IDENTIFIER(I) DOUBLEARROW xx_common_expr(E) . {
R = xx_ret_expr("closure-arrow", xx_ret_literal(XX_T_IDENTIFIER, I, status->scanner_state), E, NULL, status->scanner_state);
}

xx_array_list(R) ::= xx_array_list(L) COMMA xx_array_item(I) . {
R = xx_ret_list(L, I);
}
Expand Down
5 changes: 5 additions & 0 deletions parser/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
#define XX_T_MODASSIGN 418
#define XX_T_BITWISE_SHIFTLEFT 419
#define XX_T_BITWISE_SHIFTRIGHT 420
#define XX_T_DOUBLEARROW 440

#define XX_T_CBLOCK 451

Expand All @@ -167,6 +168,10 @@ typedef struct _xx_scanner_state {
int mode;
unsigned int active_line;
unsigned int active_char;
unsigned int class_line;
unsigned int class_char;
unsigned int method_line;
unsigned int method_char;
char *active_file;
} xx_scanner_state;

Expand Down
Loading

0 comments on commit 45200db

Please sign in to comment.