Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,365 changes: 699 additions & 666 deletions src/frontend/parser.messages

Large diffs are not rendered by default.

31 changes: 16 additions & 15 deletions src/frontend/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,28 @@ function_block:

data_block:
| DATABLOCK LBRACE tvd=list(top_var_decl_no_assign) RBRACE
{ grammar_logger "data_block" ; tvd }
{ grammar_logger "data_block" ; List.concat tvd }

transformed_data_block:
| TRANSFORMEDDATABLOCK LBRACE tvds=list(top_vardecl_or_statement) RBRACE
{ grammar_logger "transformed_data_block" ; tvds }
{ grammar_logger "transformed_data_block" ; List.concat tvds }
(* NOTE: this allows mixing of statements and top_var_decls *)

parameters_block:
| PARAMETERSBLOCK LBRACE tvd=list(top_var_decl_no_assign) RBRACE
{ grammar_logger "parameters_block" ; tvd }
{ grammar_logger "parameters_block" ; List.concat tvd }

transformed_parameters_block:
| TRANSFORMEDPARAMETERSBLOCK LBRACE tvds=list(top_vardecl_or_statement) RBRACE
{ grammar_logger "transformed_parameters_block" ; tvds }
{ grammar_logger "transformed_parameters_block" ; List.concat tvds }

model_block:
| MODELBLOCK LBRACE vds=list(vardecl_or_statement) RBRACE
{ grammar_logger "model_block" ; vds }
{ grammar_logger "model_block" ; List.concat vds }

generated_quantities_block:
| GENERATEDQUANTITIESBLOCK LBRACE tvds=list(top_vardecl_or_statement) RBRACE
{ grammar_logger "generated_quantities_block" ; tvds }
{ grammar_logger "generated_quantities_block" ; List.concat tvds }

(* function definitions *)
identifier:
Expand Down Expand Up @@ -265,7 +265,7 @@ decl(type_rule, rhs):
| ty=type_rule id=decl_identifier dims=dims rhs_opt=optional_assignment(rhs)
SEMICOLON
{ (fun ~is_global ->
{ stmt=
[{ stmt=
VarDecl {
decl_type= Sized (reducearray (fst ty, dims))
; transformation= snd ty
Expand All @@ -276,8 +276,9 @@ decl(type_rule, rhs):
; smeta= {
loc= Location_span.of_positions_exn $loc
}
})
}])
}

(* This rule matches non-array declarations and also the new array syntax, e.g:
array[1,2] int x = ..;
*)
Expand All @@ -289,7 +290,7 @@ decl(type_rule, rhs):
would occur if "array[x,y,z]" were its own rule without reserving the
keyword. *)
| dims_opt=ioption(lhs) ty=type_rule
id_rhs=id_and_optional_assignment(rhs) SEMICOLON
vs=separated_nonempty_list(COMMA, id_and_optional_assignment(rhs)) SEMICOLON
{ (fun ~is_global ->
let int_ix ix = match ix with
| Single e -> Some e
Expand All @@ -316,7 +317,7 @@ decl(type_rule, rhs):
| None -> []
| _ -> error "Found a declaration following an expression."
in
let (id, rhs_opt) = id_rhs in
List.map vs ~f:(fun (id, rhs_opt) ->
{ stmt=
VarDecl {
decl_type= Sized (reducearray (fst ty, dims))
Expand Down Expand Up @@ -348,7 +349,7 @@ decl(type_rule, rhs):
in
Location_span.of_positions_exn (startpos, $endpos)
}
}
})
)}

var_decl:
Expand Down Expand Up @@ -749,19 +750,19 @@ nested_statement:
| FOR LPAREN id=identifier IN e=expression RPAREN s=statement
{ grammar_logger "foreach_statement" ; ForEach (id, e, s) }
| PROFILE LPAREN st=string_literal RPAREN LBRACE l=list(vardecl_or_statement) RBRACE
{ grammar_logger "profile_statement" ; Profile (st, l) }
{ grammar_logger "profile_statement" ; Profile (st, List.concat l) }
| LBRACE l=list(vardecl_or_statement) RBRACE
{ grammar_logger "block_statement" ; Block l } (* NOTE: I am choosing to allow mixing of statements and var_decls *)
{ grammar_logger "block_statement" ; Block (List.concat l) } (* NOTE: I am choosing to allow mixing of statements and var_decls *)

(* statement or var decls *)
vardecl_or_statement:
| s=statement
{ grammar_logger "vardecl_or_statement_statement" ; s }
{ grammar_logger "vardecl_or_statement_statement" ; [s] }
| v=var_decl
{ grammar_logger "vardecl_or_statement_vardecl" ; v }

top_vardecl_or_statement:
| s=statement
{ grammar_logger "top_vardecl_or_statement_statement" ; s }
{ grammar_logger "top_vardecl_or_statement_statement" ; [s] }
| v=top_var_decl
{ grammar_logger "top_vardecl_or_statement_top_vardecl" ; v }
3 changes: 3 additions & 0 deletions test/integration/bad/array-multi-decl-bad.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
model {
real x[5], y;
}
3 changes: 3 additions & 0 deletions test/integration/bad/array-multi-decl-bad2.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
model {
real x[3] = {1,2,3}, y;
}
2 changes: 1 addition & 1 deletion test/integration/bad/lang/stanc.expected
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Syntax error in 'bad_bounds1.stan', line 2, column 15 to column 17, parsing erro
4: parameters {
-------------------------------------------------

">" or ", upper = expression (not containing binary logical operators) ">" expected after specifying lower bound for type.
">" or ", upper = expression>" (with expression not containing binary logical operators) expected after specifying lower bound for type.
$ ../../../../../install/default/bin/stanc bad_cov_exp_quad_arr_rvec_data.stan
Semantic error in 'bad_cov_exp_quad_arr_rvec_data.stan', line 13, column 41 to column 48:
-------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions test/integration/bad/new/stanc.expected
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ Warning in 'compound-assign-decl-bad1.stan', line 1, column 18: assignment opera
^
-------------------------------------------------

Expected ";" or "=" expression ";".
Expected ";" or assignment.
$ ../../../../../install/default/bin/stanc compound-assign-decl-bad2.stan
Warning in 'compound-assign-decl-bad2.stan', line 1, column 29: assignment operator <- is deprecated in the Stan language; use = instead.Syntax error in 'compound-assign-decl-bad2.stan', line 1, column 29 to column 31, parsing error:
-------------------------------------------------
1: transformed data { real T[1] <- {1.1};}
^
-------------------------------------------------

Expected ";" or "=" expression ";".
Expected ";" or assignment.
$ ../../../../../install/default/bin/stanc fun-app-bad1.stan
Syntax error in 'fun-app-bad1.stan', line 1, column 30 to column 31, parsing error:
-------------------------------------------------
Expand Down
27 changes: 24 additions & 3 deletions test/integration/bad/stanc.expected
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ Syntax error in 'array-expr-decl-bad3.stan', line 2, column 2 to column 6, parsi
-------------------------------------------------

Found a declaration following an expression.
$ ../../../../install/default/bin/stanc array-multi-decl-bad.stan
Syntax error in 'array-multi-decl-bad.stan', line 2, column 11 to column 12, parsing error:
-------------------------------------------------
1: model {
2: real x[5], y;
^
3: }
-------------------------------------------------

Expected ";" or assignment.
$ ../../../../install/default/bin/stanc array-multi-decl-bad2.stan
Syntax error in 'array-multi-decl-bad2.stan', line 2, column 21 to column 22, parsing error:
-------------------------------------------------
1: model {
2: real x[3] = {1,2,3}, y;
^
3: }
-------------------------------------------------

Multiple declarations are not allowed when array dimensions are given in TYPE IDENTIFIER[DIMENSIONS] form.
When all declarations share array dimensions, use 'array[DIMENSIONS] TYPE IDENTIFIER' form, otherwise use separate declarations.
$ ../../../../install/default/bin/stanc array_expr_bad1.stan
Semantic error in 'array_expr_bad1.stan', line 2, column 2 to column 32:
-------------------------------------------------
Expand Down Expand Up @@ -1342,16 +1363,16 @@ Semantic error in 'matrix_expr_bad2.stan', line 2, column 34 to column 35:

Matrix expression must have all row_vector entries. Found type int.
$ ../../../../install/default/bin/stanc matrix_expr_bad3.stan
Syntax error in 'matrix_expr_bad3.stan', line 2, column 38 to column 39, parsing error:
Syntax error in 'matrix_expr_bad3.stan', line 2, column 40 to column 41, parsing error:
-------------------------------------------------
1: transformed data {
2: matrix[3,2] td_mat32 = [ [ [ 1 ] ] ], [ [ 2 ] ] ];
^
^
3: }
4: model {
-------------------------------------------------

Ill-formed expression. Expression followed by ";" expected after "=".
Expected identifier after comma in multiple declaration.
$ ../../../../install/default/bin/stanc matrix_expr_bad4.stan
Syntax error in 'matrix_expr_bad4.stan', line 2, column 44 to column 45, parsing error:
-------------------------------------------------
Expand Down
149 changes: 149 additions & 0 deletions test/integration/good/declare-define-multi.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
data {
int a0, a1, a2;
array[5] real b0, b1, b3;
array[3,2] vector[5] c0, c1, c2, c4;
array[3,2] matrix[5,4] d0, d1;
}
transformed data {
int td_a0=0, td_a1 = 2;
int td_a2, td_a3;
int td_a4 = 4, td_a5, td_a6 = 6;

real td_b0=0.5, td_b1 = 2.5;
real td_b2, td_b3;
real td_b4 = 4.5, td_b5, td_b6 = 6.5;

vector[2] td_c0 = [-1.5, -2.5]', td_c1 = [1.5, 2.5]';
vector[2] td_c2, td_c3;
vector[2] td_c4 = [-1.5, -2.5]', td_c5, td_c6 = [1.5, 2.5]';

array[2] real td_d0 = {-1.5, -2.5}, td_d1 = {1.5, 2.5};
array[2] real td_d2, td_d3;
array[2] real td_d4 = {-1.5, -2.5}, td_d5, td_d6 = {1.5, 2.5};

{
int loc_a0=0, loc_a1 = 2;
int loc_a2, loc_a3;
int loc_a4 = 4, loc_a5, loc_a6 = 6;

real loc_b0=0.5, loc_b1 = 2.5;
real loc_b2, loc_b3;
real loc_b4 = 4.5, loc_b5, loc_b6 = 6.5;

vector[2] loc_c0 = [-1.5, -2.5]', loc_c1 = [1.5, 2.5]';
vector[2] loc_c2, loc_c3;
vector[2] loc_c4 = [-1.5, -2.5]', loc_c5, loc_c6 = [1.5, 2.5]';

array[2] real loc_d0 = {-1.5, -2.5}, loc_d1 = {1.5, 2.5};
array[2] real loc_d2, loc_d3;
array[2] real loc_d4 = {-1.5, -2.5}, loc_d5, loc_d6 = {1.5, 2.5};
}
}
parameters {
array[5] real par_b0, par_b1, par_b3;
array[3,2] vector[5] par_c0, par_c1, par_c2, par_c4;
array[3,2] matrix[5,4] par_d0, par_d1;
}
transformed parameters {
real tpar_b0=0.5, tpar_b1 = 2.5;
real tpar_b2, tpar_b3;
real tpar_b4 = 4.5, tpar_b5, tpar_b6 = 6.5;

vector[2] tpar_c0 = [-1.5, -2.5]', tpar_c1 = [1.5, 2.5]';
vector[2] tpar_c2, tpar_c3;
vector[2] tpar_c4 = [-1.5, -2.5]', tpar_c5, tpar_c6 = [1.5, 2.5]';

array[2] real tpar_d0 = {-1.5, -2.5}, tpar_d1 = {1.5, 2.5};
array[2] real tpar_d2, tpar_d3;
array[2] real tpar_d4 = {-1.5, -2.5}, tpar_d5, tpar_d6 = {1.5, 2.5};

{
int loc_tpar_a0=0, loc_tpar_a1 = 2;
int loc_tpar_a2, loc_tpar_a3;
int loc_tpar_a4 = 4, loc_tpar_a5, loc_tpar_a6 = 6;

real loc_tpar_b0=0.5, loc_tpar_b1 = 2.5;
real loc_tpar_b2, loc_tpar_b3;
real loc_tpar_b4 = 4.5, loc_tpar_b5, loc_tpar_b6 = 6.5;

vector[2] loc_tpar_c0 = [-1.5, -2.5]', loc_tpar_c1 = [1.5, 2.5]';
vector[2] loc_tpar_c2, loc_tpar_c3;
vector[2] loc_tpar_c4 = [-1.5, -2.5]', loc_tpar_c5, loc_tpar_c6 = [1.5, 2.5]';

array[2] real loc_tpar_d0 = {-1.5, -2.5}, loc_tpar_d1 = {1.5, 2.5};
array[2] real loc_tpar_d2, loc_tpar_d3;
array[2] real loc_tpar_d4 = {-1.5, -2.5}, loc_tpar_d5, loc_tpar_d6 = {1.5, 2.5};
}
}

model {
int model_a0=0, model_a1 = 2;
int model_a2, model_a3;
int model_a4 = 4, model_a5, model_a6 = 6;

real model_b0=0.5, model_b1 = 2.5;
real model_b2, model_b3;
real model_b4 = 4.5, model_b5, model_b6 = 6.5;

vector[2] model_c0 = [-1.5, -2.5]', model_c1 = [1.5, 2.5]';
vector[2] model_c2, model_c3;
vector[2] model_c4 = [-1.5, -2.5]', model_c5, model_c6 = [1.5, 2.5]';

array[2] real model_d0 = {-1.5, -2.5}, model_d1 = {1.5, 2.5};
array[2] real model_d2, model_d3;
array[2] real model_d4 = {-1.5, -2.5}, model_d5, model_d6 = {1.5, 2.5};

{
int loc_model_a0=0, loc_model_a1 = 2;
int loc_model_a2, loc_model_a3;
int loc_model_a4 = 4, loc_model_a5, loc_model_a6 = 6;

real loc_model_b0=0.5, loc_model_b1 = 2.5;
real loc_model_b2, loc_model_b3;
real loc_model_b4 = 4.5, loc_model_b5, loc_model_b6 = 6.5;

vector[2] loc_model_c0 = [-1.5, -2.5]', loc_model_c1 = [1.5, 2.5]';
vector[2] loc_model_c2, loc_model_c3;
vector[2] loc_model_c4 = [-1.5, -2.5]', loc_model_c5, loc_model_c6 = [1.5, 2.5]';

array[2] real loc_model_d0 = {-1.5, -2.5}, loc_model_d1 = {1.5, 2.5};
array[2] real loc_model_d2, loc_model_d3;
array[2] real loc_model_d4 = {-1.5, -2.5}, loc_model_d5, loc_model_d6 = {1.5, 2.5};
}

}
generated quantities {
int gq_a0=0, gq_a1 = 2;
int gq_a2, gq_a3;
int gq_a4 = 4, gq_a5, gq_a6 = 6;

real gq_b0=0.5, gq_b1 = 2.5;
real gq_b2, gq_b3;
real gq_b4 = 4.5, gq_b5, gq_b6 = 6.5;

vector[2] gq_c0 = [-1.5, -2.5]', gq_c1 = [1.5, 2.5]';
vector[2] gq_c2, gq_c3;
vector[2] gq_c4 = [-1.5, -2.5]', gq_c5, gq_c6 = [1.5, 2.5]';

array[2] real gq_d0 = {-1.5, -2.5}, gq_d1 = {1.5, 2.5};
array[2] real gq_d2, gq_d3;
array[2] real gq_d4 = {-1.5, -2.5}, gq_d5, gq_d6 = {1.5, 2.5};

{
int loc_gq_a0=0, loc_gq_a1 = 2;
int loc_gq_a2, loc_gq_a3;
int loc_gq_a4 = 4, loc_gq_a5, loc_gq_a6 = 6;

real loc_gq_b0=0.5, loc_gq_b1 = 2.5;
real loc_gq_b2, loc_gq_b3;
real loc_gq_b4 = 4.5, loc_gq_b5, loc_gq_b6 = 6.5;

vector[2] loc_gq_c0 = [-1.5, -2.5]', loc_gq_c1 = [1.5, 2.5]';
vector[2] loc_gq_c2, loc_gq_c3;
vector[2] loc_gq_c4 = [-1.5, -2.5]', loc_gq_c5, loc_gq_c6 = [1.5, 2.5]';

array[2] real loc_gq_d0 = {-1.5, -2.5}, loc_gq_d1 = {1.5, 2.5};
array[2] real loc_gq_d2, loc_gq_d3;
array[2] real loc_gq_d4 = {-1.5, -2.5}, loc_gq_d5, loc_gq_d6 = {1.5, 2.5};
}
}
Loading