3
#include "kscript_parsenode.h"
4
#include "kscript_parser.h"
11
extern const char* idl_lexFile;
12
extern int idl_line_no;
14
void kscriptInitFlex( const char *_code, int extension );
16
void yyerror( const char *s )
18
theParser->parse_error( idl_lexFile, s, idl_line_no );
31
KScript::Double _float;
34
%token <_str> T_FILE_OP
35
%token <_str> T_MATCH_LINE
41
%token <_char> T_CHARACTER_LITERAL
50
%token <_float> T_FLOATING_PT_LITERAL
51
%token T_GREATER_THAN_SIGN
52
%token <ident> T_IDENTIFIER
55
%token <_int> T_INTEGER_LITERAL
56
%token T_LEFT_CURLY_BRACKET
57
%token T_LEFT_PARANTHESIS
58
%token T_LEFT_SQUARE_BRACKET
59
%token T_LESS_THAN_SIGN
64
%token T_RIGHT_CURLY_BRACKET
65
%token T_RIGHT_PARANTHESIS
66
%token T_RIGHT_SQUARE_BRACKET
72
%token <_str> T_STRING_LITERAL
73
%token <_str> T_PRAGMA
78
%token T_VERTICAL_LINE
80
%token T_LESS_OR_EQUAL
81
%token T_GREATER_OR_EQUAL
95
%token <ident> T_SUBST
107
%token <_str> T_RANGE
111
%token T_MINUS_ASSIGN
116
%type <node> definitions
117
%type <node> definition
118
%type <node> const_dcl
120
%type <node> xor_expr
121
%type <node> and_expr
122
%type <node> shift_expr
123
%type <node> add_expr
124
%type <node> mult_expr
125
%type <node> unary_expr
126
%type <node> primary_expr
128
%type <node> scoped_name
129
%type <node> inheritance_spec
130
%type <node> class_dcl
131
%type <node> class_header
132
%type <node> class_body
133
%type <node> class_exports
134
%type <node> class_export
135
%type <node> func_dcl
136
%type <node> func_body
137
%type <node> func_lines
138
%type <node> func_line
139
%type <node> func_param
140
%type <node> func_params
141
%type <node> assign_expr
142
%type <node> equal_expr
143
%type <node> index_expr
144
%type <node> member_expr
145
%type <node> array_elements
146
%type <node> dict_elements
147
%type <node> func_call_params
149
%type <node> incr_expr
153
%type <node> match_expr
154
%type <node> signal_param
155
%type <node> signal_params
156
%type <node> struct_dcl
157
%type <node> struct_export
158
%type <node> struct_exports
159
%type <node> struct_members
160
%type <node> qualified_names
161
%type <node> qualified_name
163
%type <node> single_catch
164
%type <node> loop_body
165
%type <_str> import_list
167
%type <node> bool_and
175
theParser->setRootNode( NULL );
177
| T_ASSIGN equal_expr
179
theParser->setRootNode( $2 );
183
theParser->setRootNode( new KSParseNode( definitions, $1 ) );
190
$$ = new KSParseNode( definitions, $1 );
192
| definition definitions
194
$$ = new KSParseNode( definitions, $1 );
195
$$->setBranch( 2, $2 );
201
: const_dcl T_SEMICOLON
205
| struct_dcl T_SEMICOLON
209
| class_dcl T_SEMICOLON
213
| func_dcl T_RIGHT_CURLY_BRACKET
217
| T_IMPORT T_IDENTIFIER T_SEMICOLON
219
$$ = new KSParseNode( import );
222
| T_FROM T_IDENTIFIER T_IMPORT T_ASTERISK T_SEMICOLON
224
$$ = new KSParseNode( from );
226
$$->setStringLiteral( QString( "" ) );
228
| T_FROM T_IDENTIFIER T_IMPORT import_list T_SEMICOLON
230
$$ = new KSParseNode( from );
232
$$->setStringLiteral( $4 );
236
$$ = new KSParseNode( t_pragma );
250
| T_IDENTIFIER T_COMMA import_list
259
: T_MAIN T_LEFT_CURLY_BRACKET func_body T_RIGHT_CURLY_BRACKET
261
$$ = new KSParseNode( func_dcl );
262
$$->setBranch( 2, $3 );
263
$$->setIdent( "main" );
265
| T_MAIN T_LEFT_PARANTHESIS func_params T_RIGHT_PARANTHESIS T_LEFT_CURLY_BRACKET func_body T_RIGHT_CURLY_BRACKET
267
$$ = new KSParseNode( func_dcl, $3, $6 );
268
$$->setIdent( "main" );
273
: T_COLON qualified_names
279
/* A list of qualified names like "KOM.Base, OpenParts.MainWindow, Qt.QWidget" */
283
$$ = new KSParseNode( t_qualified_names, $1 );
285
| qualified_name T_COMMA qualified_names
287
$$ = new KSParseNode( t_qualified_names, $1 );
288
$$->setBranch( 2, $3 );
292
/* A name like "QWidget" or "Qt.QWidget" or "Qt.Extension.PNG" */
298
| qualified_name T_MEMBER T_IDENTIFIER
300
$$ = new KSParseNode( member_expr, $1 );
304
/* A usual identifier that may start with "::". For example "MyClass" or "::MyModule" */
308
$$ = new KSParseNode( scoped_name );
311
| T_SCOPE T_IDENTIFIER
313
$$ = new KSParseNode( scoped_name );
317
$$->setIdent( name );
321
/* This rule fits for assignments like "a = 100" */
323
: bool_or T_ASSIGN assign_expr
325
$$ = new KSParseNode( assign_expr, $1, $3 );
327
| bool_or T_PLUS_ASSIGN assign_expr
329
$$ = new KSParseNode( plus_assign, $1, $3 );
331
| bool_or T_MINUS_ASSIGN assign_expr
333
$$ = new KSParseNode( minus_assign, $1, $3 );
342
: bool_and T_OR bool_or
344
$$ = new KSParseNode( bool_or, $1, $3 );
353
: equal_expr T_AND bool_and
355
$$ = new KSParseNode( bool_and, $1, $3 );
364
: or_expr T_EQUAL equal_expr
366
$$ = new KSParseNode( t_equal, $1, $3 );
368
| or_expr T_NOTEQUAL equal_expr
370
$$ = new KSParseNode( t_notequal, $1, $3 );
372
| or_expr T_LESS_OR_EQUAL equal_expr
374
$$ = new KSParseNode( t_less_or_equal, $1, $3 );
376
| or_expr T_GREATER_OR_EQUAL equal_expr
378
$$ = new KSParseNode( t_greater_or_equal, $1, $3 );
380
| or_expr T_LESS_THAN_SIGN equal_expr
382
$$ = new KSParseNode( t_less, $1, $3 );
384
| or_expr T_GREATER_THAN_SIGN equal_expr
386
$$ = new KSParseNode( t_greater, $1, $3 );
400
| or_expr T_VERTICAL_LINE xor_expr
402
$$ = new KSParseNode( t_vertical_line, $1, $3 );
412
| xor_expr T_CIRCUMFLEX and_expr
414
$$ = new KSParseNode( t_circumflex, $1, $3 );
424
| and_expr T_AMPERSAND shift_expr
426
$$ = new KSParseNode( t_ampersand, $1, $3 );
436
| shift_expr T_SHIFTRIGHT add_expr
438
$$ = new KSParseNode( t_shiftright, $1, $3 );
440
| shift_expr T_SHIFTLEFT add_expr
442
$$ = new KSParseNode( t_shiftleft, $1, $3 );
452
| add_expr T_PLUS_SIGN mult_expr
454
$$ = new KSParseNode( t_plus_sign, $1, $3 );
456
| add_expr T_MINUS_SIGN mult_expr
458
$$ = new KSParseNode( t_minus_sign, $1, $3 );
468
| mult_expr T_ASTERISK unary_expr
470
$$ = new KSParseNode( t_asterik, $1, $3 );
472
| mult_expr T_SOLIDUS unary_expr
474
$$ = new KSParseNode( t_solidus, $1, $3 );
476
| mult_expr T_PERCENT_SIGN unary_expr
478
$$ = new KSParseNode( t_percent_sign, $1, $3 );
484
: T_MINUS_SIGN index_expr
486
$$ = new KSParseNode( t_minus_sign, $2 );
488
| T_PLUS_SIGN index_expr
490
$$ = new KSParseNode( t_plus_sign, $2 );
494
$$ = new KSParseNode( t_tilde, $2 );
498
$$ = new KSParseNode( t_not, $2 );
507
: index_expr T_LEFT_SQUARE_BRACKET assign_expr T_RIGHT_SQUARE_BRACKET
509
$$ = new KSParseNode( t_array, $1, $3 );
511
| index_expr T_LEFT_CURLY_BRACKET assign_expr T_RIGHT_CURLY_BRACKET
513
$$ = new KSParseNode( t_dict, $1, $3 );
515
| index_expr T_LEFT_PARANTHESIS T_RIGHT_PARANTHESIS
517
$$ = new KSParseNode( t_func_call, $1 );
519
| index_expr T_LEFT_PARANTHESIS func_call_params T_RIGHT_PARANTHESIS
521
$$ = new KSParseNode( t_func_call, $1, $3 );
530
: assign_expr T_COMMA func_call_params
532
$$ = new KSParseNode( func_call_params, $1, $3 );
537
$$ = new KSParseNode( func_call_params, $1 );
542
: index_expr T_MEMBER T_IDENTIFIER
544
$$ = new KSParseNode( member_expr, $1 );
556
$$ = new KSParseNode( t_incr, $2 );
560
$$ = new KSParseNode( t_decr, $2 );
564
/* Setting $1 twice indicates that this is a postfix operator */
565
$$ = new KSParseNode( t_incr, $1, $1 );
569
/* Setting $1 twice indicates that this is a postfix operator */
570
$$ = new KSParseNode( t_decr, $1, $1 );
579
: primary_expr T_MATCH
581
$$ = new KSParseNode( t_match, $1 );
584
| primary_expr T_SUBST
586
$$ = new KSParseNode( t_subst, $1 );
607
$$ = new KSParseNode( t_input );
611
$$ = new KSParseNode( t_match_line );
614
| T_FILE_OP assign_expr T_RIGHT_PARANTHESIS
616
$$ = new KSParseNode( t_file_op, $2 );
619
| T_LESS_THAN_SIGN or_expr T_GREATER_THAN_SIGN { }
620
| T_LEFT_PARANTHESIS assign_expr T_RIGHT_PARANTHESIS
631
$$ = new KSParseNode( t_integer_literal );
632
$$->setIntegerLiteral( $1 );
636
$$ = new KSParseNode( t_cell );
637
$$->setStringLiteral( $1 );
641
$$ = new KSParseNode( t_range );
642
$$->setStringLiteral( $1 );
646
$$ = new KSParseNode( t_string_literal );
647
$$->setStringLiteral( $1 );
649
| T_CHARACTER_LITERAL
651
$$ = new KSParseNode( t_character_literal );
652
$$->setCharacterLiteral( $1 );
654
| T_FLOATING_PT_LITERAL
656
$$ = new KSParseNode( t_floating_pt_literal );
657
$$->setFloatingPtLiteral( $1 );
659
| T_TRUE /*boolean_literal*/
661
$$ = new KSParseNode( t_boolean_literal );
662
$$->setBooleanLiteral( true );
664
| T_FALSE /*boolean_literal*/
666
$$ = new KSParseNode( t_boolean_literal );
667
$$->setBooleanLiteral( false );
669
| T_LEFT_SQUARE_BRACKET T_RIGHT_SQUARE_BRACKET
671
$$ = new KSParseNode( t_array_const );
673
| T_LEFT_SQUARE_BRACKET array_elements T_RIGHT_SQUARE_BRACKET
675
$$ = new KSParseNode( t_array_const, $2 );
677
| T_LEFT_CURLY_BRACKET T_RIGHT_CURLY_BRACKET
679
$$ = new KSParseNode( t_dict_const );
681
| T_LEFT_CURLY_BRACKET dict_elements T_RIGHT_CURLY_BRACKET
683
$$ = new KSParseNode( t_dict_const, $2 );
687
$$ = new KSParseNode( t_line );
689
| T_DOLLAR T_INTEGER_LITERAL
691
$$ = new KSParseNode( t_regexp_group );
692
$$->setIntegerLiteral( $2 );
697
: assign_expr T_COMMA array_elements
699
$$ = new KSParseNode( t_array_element, $1, $3 );
703
$$ = new KSParseNode( t_array_element, $1 );
708
: T_LEFT_PARANTHESIS assign_expr T_COMMA equal_expr T_RIGHT_PARANTHESIS T_COMMA dict_elements
710
$$ = new KSParseNode( t_dict_element, $2, $4, $7 );
712
| T_LEFT_PARANTHESIS assign_expr T_COMMA equal_expr T_RIGHT_PARANTHESIS
714
$$ = new KSParseNode( t_dict_element, $2, $4 );
722
: T_STRUCT T_IDENTIFIER T_LEFT_CURLY_BRACKET struct_exports T_RIGHT_CURLY_BRACKET
724
$$ = new KSParseNode( t_struct, $4 );
732
$$ = new KSParseNode( exports, $1 );
734
| struct_export struct_exports
736
$$ = new KSParseNode( exports, $1 );
737
$$->setBranch( 2, $2 );
742
: func_dcl T_RIGHT_CURLY_BRACKET
746
| T_VAR struct_members T_SEMICOLON
750
| const_dcl T_SEMICOLON
759
$$ = new KSParseNode( t_struct_members );
762
| T_IDENTIFIER T_COMMA struct_members
764
$$ = new KSParseNode( t_struct_members, $3 );
771
: class_header T_LEFT_CURLY_BRACKET class_body T_RIGHT_CURLY_BRACKET
773
$$ = new KSParseNode( class_dcl, $1, $3 );
779
: T_CLASS T_IDENTIFIER
781
$$ = new KSParseNode( class_header );
784
| T_CLASS T_IDENTIFIER inheritance_spec
786
$$ = new KSParseNode( class_header );
788
$$->setBranch( 1, $3 );
807
$$ = new KSParseNode( exports, $1 );
809
| class_export class_exports
811
$$ = new KSParseNode( exports, $1 );
812
$$->setBranch( 2, $2 );
817
: func_dcl T_RIGHT_CURLY_BRACKET
821
| struct_dcl T_SEMICOLON
825
| T_SIGNAL T_IDENTIFIER T_LEFT_PARANTHESIS signal_params T_RIGHT_PARANTHESIS T_SEMICOLON
827
$$ = new KSParseNode( signal_dcl, $4 );
830
| T_SIGNAL T_IDENTIFIER T_LEFT_PARANTHESIS T_RIGHT_PARANTHESIS T_SEMICOLON
832
$$ = new KSParseNode( signal_dcl, 0 );
835
| T_DELETE T_LEFT_PARANTHESIS T_IN T_IDENTIFIER T_RIGHT_PARANTHESIS T_LEFT_CURLY_BRACKET func_body T_RIGHT_CURLY_BRACKET
837
$$ = new KSParseNode( destructor_dcl );
838
KSParseNode* n = new KSParseNode( func_param_in );
840
$$->setBranch( 1, n );
841
$$->setBranch( 2, $7 );
842
$$->setIdent( "delete" );
844
| const_dcl T_SEMICOLON
851
: T_IDENTIFIER T_LEFT_PARANTHESIS T_RIGHT_PARANTHESIS T_LEFT_CURLY_BRACKET func_body
853
$$ = new KSParseNode( func_dcl );
854
$$->setBranch( 2, $5 );
857
| T_IDENTIFIER T_LEFT_PARANTHESIS func_params T_RIGHT_PARANTHESIS T_LEFT_CURLY_BRACKET func_body
859
$$ = new KSParseNode( func_dcl, $3, $6 );
867
$$ = new KSParseNode( func_params, $1 );
869
| func_param T_COMMA func_params
871
$$ = new KSParseNode( func_params, $1, $3 );
878
$$ = new KSParseNode( func_param_in );
881
| T_IN T_IDENTIFIER T_ASSIGN assign_expr
883
$$ = new KSParseNode( func_param_in, $4 );
888
$$ = new KSParseNode( func_param_out );
891
| T_INOUT T_IDENTIFIER
893
$$ = new KSParseNode( func_param_inout );
901
$$ = new KSParseNode( signal_params, $1 );
903
| signal_param T_COMMA func_params
905
$$ = new KSParseNode( signal_params, $1, $3 );
912
$$ = new KSParseNode( signal_param );
915
| T_IN T_IDENTIFIER T_ASSIGN assign_expr
917
$$ = new KSParseNode( signal_param, $4 );
936
$$ = new KSParseNode( func_lines, $1 );
938
| func_line func_lines
940
$$ = new KSParseNode( func_lines, $1 );
941
$$->setBranch( 2, $2 );
946
: assign_expr T_SEMICOLON
950
| T_RAISE assign_expr T_COMMA assign_expr T_SEMICOLON
952
$$ = new KSParseNode( t_raise, $2, $4 );
954
| T_EMIT assign_expr T_SEMICOLON
956
$$ = new KSParseNode( t_emit, $2 );
958
| T_RETURN T_SEMICOLON
960
$$ = new KSParseNode( t_return );
962
| T_RETURN assign_expr T_SEMICOLON
964
$$ = new KSParseNode( t_return, $2 );
970
| T_TRY T_LEFT_CURLY_BRACKET func_lines T_RIGHT_CURLY_BRACKET catches
972
$$ = new KSParseNode( t_try, $3, $5 );
974
| T_LEFT_CURLY_BRACKET func_lines T_RIGHT_CURLY_BRACKET
976
$$ = new KSParseNode( t_scope, $2 );
985
| single_catch catches
988
$$->setBranch( 4, $2 );
993
: T_CATCH T_LEFT_PARANTHESIS assign_expr T_COMMA T_IDENTIFIER T_RIGHT_PARANTHESIS
994
T_LEFT_CURLY_BRACKET func_lines T_RIGHT_CURLY_BRACKET
996
$$ = new KSParseNode( t_catch, $3, $8 );
999
| T_DEFAULT T_LEFT_PARANTHESIS T_IDENTIFIER T_COMMA T_IDENTIFIER T_RIGHT_PARANTHESIS
1000
T_LEFT_CURLY_BRACKET func_lines T_RIGHT_CURLY_BRACKET
1002
KSParseNode* x = new KSParseNode( scoped_name );
1004
$$ = new KSParseNode( t_catch_default, x, $8 );
1010
: T_CONST T_IDENTIFIER T_ASSIGN equal_expr
1012
$$ = new KSParseNode( const_dcl, $4 );
1020
$$ = new KSParseNode( t_while, $1, $2 );
1022
| T_FOR T_LEFT_PARANTHESIS assign_expr T_SEMICOLON assign_expr T_SEMICOLON assign_expr T_RIGHT_PARANTHESIS loop_body
1024
$$ = new KSParseNode( t_for, $3, $5, $7, $9 );
1026
| T_DO loop_body while T_SEMICOLON
1028
$$ = new KSParseNode( t_do, $2, $3 );
1030
| T_IF T_LEFT_PARANTHESIS assign_expr T_RIGHT_PARANTHESIS loop_body else
1032
$$ = new KSParseNode( t_if, $3, $5, $6 );
1034
| T_FOREACH T_LEFT_PARANTHESIS T_IDENTIFIER T_COMMA assign_expr T_RIGHT_PARANTHESIS loop_body
1036
$$ = new KSParseNode( t_foreach, $5, $7 );
1039
| T_FOREACH T_LEFT_PARANTHESIS T_IDENTIFIER T_COMMA T_IDENTIFIER T_COMMA assign_expr T_RIGHT_PARANTHESIS loop_body
1041
/* We set $9 twice to indicate thet this is the foreach for maps */
1042
$$ = new KSParseNode( t_foreach, $7, $9, $9 );
1044
$$->setStringLiteral( $5 );
1053
| T_ELSE T_IF T_LEFT_PARANTHESIS assign_expr T_RIGHT_PARANTHESIS loop_body else
1055
$$ = new KSParseNode( t_if, $4, $6, $7 );
1064
: T_WHILE T_LEFT_PARANTHESIS assign_expr T_RIGHT_PARANTHESIS
1071
: T_LEFT_CURLY_BRACKET func_body T_RIGHT_CURLY_BRACKET
1073
$$ = new KSParseNode( t_scope, $2 );
1078
void kscriptParse( const char *_code, int extension )
1080
kscriptInitFlex( _code, extension );