2020-03-08 02:21:55 +03:00
|
|
|
(*
|
2020-04-16 18:47:35 +03:00
|
|
|
This file is part of the Catala compiler, a specification language for tax and social benefits
|
2020-03-08 02:21:55 +03:00
|
|
|
computation rules.
|
2021-05-27 19:56:47 +03:00
|
|
|
Copyright (C) 2020 Inria, contributors: Denis Merigoux <denis.merigoux@inria.fr>,
|
|
|
|
Emile Rolley <emile.rolley@tuta.io>
|
2020-03-08 02:21:55 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2021-03-09 20:48:58 +03:00
|
|
|
*)
|
2020-03-08 02:21:55 +03:00
|
|
|
|
|
|
|
%{
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2021-05-15 02:16:08 +03:00
|
|
|
%}
|
2020-03-08 02:21:55 +03:00
|
|
|
|
2021-04-30 10:59:09 +03:00
|
|
|
%parameter<Localisation: sig
|
2021-08-19 19:26:06 +03:00
|
|
|
val lex_builtin: string -> Ast.builtin_expression option
|
2021-04-30 10:59:09 +03:00
|
|
|
end>
|
2020-04-03 23:34:11 +03:00
|
|
|
|
2021-08-19 12:19:45 +03:00
|
|
|
(* The token is returned for every line of law text, make them right-associative
|
|
|
|
so that we concat them efficiently as much as possible. *)
|
|
|
|
%right LAW_TEXT
|
|
|
|
|
2022-12-14 17:45:13 +03:00
|
|
|
(* Precedence of expression constructions *)
|
|
|
|
%right top_expr
|
|
|
|
%right ALT
|
2024-02-13 20:23:13 +03:00
|
|
|
%right let_expr
|
2023-01-04 18:12:36 +03:00
|
|
|
%right AND OR XOR (* Desugaring enforces proper parens later on *)
|
2023-01-04 12:54:12 +03:00
|
|
|
%nonassoc GREATER GREATER_EQUAL LESSER LESSER_EQUAL EQUAL NOT_EQUAL
|
|
|
|
%left PLUS MINUS PLUSPLUS
|
|
|
|
%left MULT DIV
|
2024-04-11 19:30:51 +03:00
|
|
|
%right apply OF CONTAINS FOR SUCH WITH BUT_REPLACE
|
2023-02-09 17:44:27 +03:00
|
|
|
%right COMMA
|
2022-12-14 17:45:13 +03:00
|
|
|
%right unop_expr
|
|
|
|
%right CONTENT
|
2023-01-04 18:12:36 +03:00
|
|
|
%nonassoc UIDENT
|
2022-12-14 17:45:13 +03:00
|
|
|
%left DOT
|
2023-01-04 18:12:36 +03:00
|
|
|
|
|
|
|
(* Types of all rules, in order. Without this, Menhir type errors are nearly
|
|
|
|
impossible to debug because of inlining *)
|
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
%type<Ast.uident Mark.pos> addpos(UIDENT)
|
2023-01-04 18:12:36 +03:00
|
|
|
%type<Pos.t> pos(CONDITION)
|
2023-02-24 12:02:12 +03:00
|
|
|
%type<Ast.primitive_typ> primitive_typ
|
|
|
|
%type<Ast.base_typ_data> typ_data
|
|
|
|
%type<Ast.base_typ> typ
|
2023-05-17 16:44:57 +03:00
|
|
|
%type<Ast.uident Mark.pos> uident
|
|
|
|
%type<Ast.lident Mark.pos> lident
|
2023-01-04 18:12:36 +03:00
|
|
|
%type<Ast.scope_var> scope_var
|
2023-05-17 16:44:57 +03:00
|
|
|
%type<Ast.path * Ast.uident Mark.pos> quident
|
|
|
|
%type<Ast.path * Ast.lident Mark.pos> qlident
|
2023-01-04 18:12:36 +03:00
|
|
|
%type<Ast.expression> expression
|
|
|
|
%type<Ast.naked_expression> naked_expression
|
2023-05-17 16:44:57 +03:00
|
|
|
%type<Ast.lident Mark.pos * expression> struct_content_field
|
2023-01-04 18:12:36 +03:00
|
|
|
%type<Ast.naked_expression> struct_or_enum_inject
|
|
|
|
%type<Ast.literal_number> num_literal
|
|
|
|
%type<Ast.literal_unit> unit_literal
|
|
|
|
%type<Ast.literal> literal
|
2023-05-17 16:44:57 +03:00
|
|
|
%type<(Ast.lident Mark.pos * expression) list> scope_call_args
|
2023-01-04 18:12:36 +03:00
|
|
|
%type<bool> minmax
|
|
|
|
%type<Ast.unop> unop
|
|
|
|
%type<Ast.binop> binop
|
|
|
|
%type<Ast.match_case_pattern> constructor_binding
|
|
|
|
%type<Ast.match_case> match_arm
|
|
|
|
%type<Ast.expression> condition_consequence
|
2023-05-17 16:44:57 +03:00
|
|
|
%type<Ast.scope_var Mark.pos * Ast.lident Mark.pos list Mark.pos option> rule_expr
|
2023-01-04 18:12:36 +03:00
|
|
|
%type<bool> rule_consequence
|
|
|
|
%type<Ast.rule> rule
|
2023-05-17 16:44:57 +03:00
|
|
|
%type<Ast.lident Mark.pos list> definition_parameters
|
|
|
|
%type<Ast.lident Mark.pos> label
|
|
|
|
%type<Ast.lident Mark.pos> state
|
2023-01-04 18:12:36 +03:00
|
|
|
%type<Ast.exception_to> exception_to
|
|
|
|
%type<Ast.definition> definition
|
|
|
|
%type<Ast.variation_typ> variation_type
|
|
|
|
%type<Ast.scope_use_item> assertion
|
2023-05-17 16:44:57 +03:00
|
|
|
%type<Ast.scope_use_item Mark.pos> scope_item
|
|
|
|
%type<Ast.lident Mark.pos * Ast.base_typ Mark.pos> struct_scope_base
|
2023-01-04 18:12:36 +03:00
|
|
|
%type<Ast.struct_decl_field> struct_scope
|
2024-03-20 19:51:01 +03:00
|
|
|
%type<Ast.io_input option> scope_decl_item_attribute_input
|
2023-01-04 18:12:36 +03:00
|
|
|
%type<bool> scope_decl_item_attribute_output
|
2024-03-20 19:51:01 +03:00
|
|
|
%type<Ast.io_input option Mark.pos * bool Mark.pos * Ast.lident Mark.pos> scope_decl_item_attribute
|
|
|
|
%type<Ast.scope_decl_context_io * Ast.lident Mark.pos> scope_decl_item_attribute_mandatory
|
2023-01-04 18:12:36 +03:00
|
|
|
%type<Ast.scope_decl_context_item> scope_decl_item
|
|
|
|
%type<Ast.enum_decl_case> enum_decl_line
|
|
|
|
%type<Ast.code_item> code_item
|
|
|
|
%type<Ast.code_block> code
|
2023-05-17 16:44:57 +03:00
|
|
|
%type<Ast.code_block * string Mark.pos> metadata_block
|
2023-01-04 18:12:36 +03:00
|
|
|
%type<Ast.law_heading> law_heading
|
|
|
|
%type<string> law_text
|
|
|
|
%type<Ast.law_structure> source_file_item
|
|
|
|
%type<Ast.law_structure list> source_file
|
|
|
|
|
|
|
|
%start source_file
|
|
|
|
|
2020-03-08 02:21:55 +03:00
|
|
|
%%
|
|
|
|
|
2022-12-14 17:45:13 +03:00
|
|
|
let pos(x) ==
|
2023-01-04 18:12:36 +03:00
|
|
|
| x ; { Pos.from_lpos $loc }
|
2022-12-14 17:45:13 +03:00
|
|
|
|
|
|
|
let addpos(x) ==
|
|
|
|
| ~=x ; { x, Pos.from_lpos $loc(x) }
|
|
|
|
|
2023-02-24 12:02:12 +03:00
|
|
|
let primitive_typ :=
|
2022-12-14 17:45:13 +03:00
|
|
|
| INTEGER ; { Integer }
|
|
|
|
| BOOLEAN ; { Boolean }
|
|
|
|
| MONEY ; { Money }
|
|
|
|
| DURATION ; { Duration }
|
|
|
|
| TEXT ; { Text }
|
|
|
|
| DECIMAL ; { Decimal }
|
|
|
|
| DATE ; { Date }
|
2023-01-04 18:12:36 +03:00
|
|
|
| c = quident ; { let path, uid = c in Named (path, uid) }
|
2020-04-14 12:46:48 +03:00
|
|
|
|
2023-02-24 12:02:12 +03:00
|
|
|
let typ_data :=
|
|
|
|
| t = primitive_typ ; <Primitive>
|
2023-12-05 17:06:28 +03:00
|
|
|
| LIST ; t = addpos(typ_data) ; <Collection>
|
2023-12-08 16:53:27 +03:00
|
|
|
| LPAREN ; tl = separated_nonempty_list(COMMA,addpos(typ_data)) ; RPAREN ; {
|
|
|
|
match tl with
|
|
|
|
| [t, _] -> t
|
|
|
|
| ts -> TTuple ts
|
|
|
|
}
|
2023-02-24 12:02:12 +03:00
|
|
|
|
|
|
|
let typ == t = typ_data ; <Data>
|
2020-03-08 07:01:26 +03:00
|
|
|
|
2023-01-04 18:12:36 +03:00
|
|
|
let uident ==
|
|
|
|
| ~ = addpos(UIDENT) ; <>
|
|
|
|
|
|
|
|
let lident :=
|
|
|
|
| i = LIDENT ; {
|
|
|
|
match Localisation.lex_builtin i with
|
|
|
|
| Some _ ->
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error ~pos:
|
2023-01-04 18:12:36 +03:00
|
|
|
(Pos.from_lpos $sloc)
|
|
|
|
"Reserved builtin name"
|
|
|
|
| None ->
|
|
|
|
(i, Pos.from_lpos $sloc)
|
|
|
|
}
|
|
|
|
|
|
|
|
let scope_var ==
|
|
|
|
| b = separated_nonempty_list(DOT, addpos(LIDENT)) ; <>
|
|
|
|
|
|
|
|
let quident :=
|
|
|
|
| uid = uident ; DOT ; quid = quident ; {
|
|
|
|
let path, quid = quid in uid :: path, quid
|
|
|
|
}
|
|
|
|
| id = uident ; { [], id }
|
2022-12-14 17:45:13 +03:00
|
|
|
|
2023-01-04 18:12:36 +03:00
|
|
|
let qlident :=
|
|
|
|
| uid = uident ; DOT ; qlid = qlident ; {
|
|
|
|
let path, lid = qlid in uid :: path, lid
|
|
|
|
}
|
|
|
|
| id = lident ; { [], id }
|
2022-12-14 17:45:13 +03:00
|
|
|
|
2024-01-25 19:37:00 +03:00
|
|
|
let mbinder ==
|
|
|
|
| id = lident ; { [id] }
|
|
|
|
| LPAREN ; ids = separated_nonempty_list(COMMA,lident) ; RPAREN ; <>
|
|
|
|
|
2023-01-04 18:12:36 +03:00
|
|
|
let expression :=
|
|
|
|
| e = addpos(naked_expression) ; <>
|
2020-04-14 18:29:50 +03:00
|
|
|
|
2024-02-12 20:15:48 +03:00
|
|
|
let state_qualifier ==
|
|
|
|
| STATE ; state = addpos(LIDENT); <>
|
|
|
|
|
2023-01-04 18:12:36 +03:00
|
|
|
let naked_expression ==
|
2024-02-12 20:15:48 +03:00
|
|
|
| id = addpos(LIDENT) ; state = option(state_qualifier) ; {
|
|
|
|
match Localisation.lex_builtin (Mark.remove id), state with
|
|
|
|
| Some b, None -> Builtin b
|
|
|
|
| Some _, Some _ ->
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error ~pos:
|
2024-02-12 20:15:48 +03:00
|
|
|
(Pos.from_lpos $loc(id))
|
|
|
|
"Invalid use of built-in @{<bold>%s@}" (Mark.remove id)
|
|
|
|
| None, state -> Ident ([], id, state)
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
|
|
|
| uid = uident ; DOT ; qlid = qlident ; {
|
2024-02-12 20:15:48 +03:00
|
|
|
let path, lid = qlid in Ident (uid :: path, lid, None)
|
2022-12-14 17:45:13 +03:00
|
|
|
}
|
|
|
|
| l = literal ; {
|
|
|
|
Literal l
|
|
|
|
}
|
2023-12-08 16:53:27 +03:00
|
|
|
| LPAREN ; el = separated_nonempty_list(COMMA, expression) ; RPAREN ; {
|
|
|
|
match el with
|
|
|
|
| [e] -> Paren e
|
|
|
|
| es -> Tuple es
|
|
|
|
}
|
2022-12-14 17:45:13 +03:00
|
|
|
| e = expression ;
|
2023-01-04 18:12:36 +03:00
|
|
|
DOT ; i = addpos(qlident) ; <Dotted>
|
2024-04-12 12:36:43 +03:00
|
|
|
| e = expression ; DOT ; arg = addpos(INT_LITERAL) ; {
|
|
|
|
let n_str, pos_n = arg in
|
|
|
|
let n = int_of_string n_str in
|
|
|
|
if n <= 0 then
|
|
|
|
Message.error ~pos:pos_n "Tuple indices must be >= 1";
|
|
|
|
TupleAccess (e, (n, pos_n))
|
|
|
|
}
|
2022-12-13 19:55:16 +03:00
|
|
|
| CARDINAL ; {
|
2022-12-14 17:45:13 +03:00
|
|
|
Builtin Cardinal
|
|
|
|
}
|
|
|
|
| DECIMAL ; {
|
|
|
|
Builtin ToDecimal
|
|
|
|
}
|
|
|
|
| MONEY ; {
|
|
|
|
Builtin ToMoney
|
2022-10-21 16:47:17 +03:00
|
|
|
}
|
2023-01-04 18:12:36 +03:00
|
|
|
| LBRACKET ; l = separated_list(SEMICOLON, expression) ; RBRACKET ;
|
|
|
|
<ArrayLit>
|
2022-12-14 17:45:13 +03:00
|
|
|
| e = struct_or_enum_inject ; <>
|
|
|
|
| e1 = expression ;
|
|
|
|
OF ;
|
2023-02-09 17:44:27 +03:00
|
|
|
args = funcall_args ; {
|
|
|
|
FunCall (e1, args)
|
|
|
|
}
|
2022-12-14 17:45:13 +03:00
|
|
|
| OUTPUT ; OF ;
|
2023-01-04 18:12:36 +03:00
|
|
|
c = addpos(quident) ;
|
2022-12-14 17:45:13 +03:00
|
|
|
fields = option(scope_call_args) ; {
|
|
|
|
let fields = Option.value ~default:[] fields in
|
|
|
|
ScopeCall (c, fields)
|
2020-04-14 20:13:20 +03:00
|
|
|
}
|
2022-12-14 17:45:13 +03:00
|
|
|
| e = expression ;
|
|
|
|
WITH ; c = constructor_binding ; {
|
|
|
|
TestMatchCase (e, (c, Pos.from_lpos $sloc))
|
|
|
|
}
|
2024-04-11 19:30:51 +03:00
|
|
|
| e = expression ;
|
|
|
|
BUT_REPLACE ;
|
|
|
|
LBRACE ;
|
|
|
|
fields = nonempty_list(preceded (ALT, struct_content_field)) ;
|
|
|
|
RBRACE ; {
|
|
|
|
StructReplace (e, fields)
|
|
|
|
}
|
2024-04-30 17:35:08 +03:00
|
|
|
| coll = expression ;
|
|
|
|
pos = pos(CONTAINS) ;
|
|
|
|
element = expression ; {
|
|
|
|
CollectionOp ((Member { element }, pos), coll)
|
2022-12-14 17:45:13 +03:00
|
|
|
} %prec apply
|
2024-04-30 17:35:08 +03:00
|
|
|
| pos = pos(SUM) ; typ = addpos(primitive_typ) ;
|
2022-12-14 17:45:13 +03:00
|
|
|
OF ; coll = expression ; {
|
2024-04-30 17:35:08 +03:00
|
|
|
CollectionOp ((AggregateSum { typ = Mark.remove typ }, pos), coll)
|
2022-12-14 17:45:13 +03:00
|
|
|
} %prec apply
|
|
|
|
| f = expression ;
|
2024-04-30 17:35:08 +03:00
|
|
|
pos = pos(FOR) ; i = mbinder ;
|
2022-12-14 17:45:13 +03:00
|
|
|
AMONG ; coll = expression ; {
|
2024-04-30 17:35:08 +03:00
|
|
|
CollectionOp ((Map {f = i, f}, pos), coll)
|
2022-12-14 17:45:13 +03:00
|
|
|
} %prec apply
|
2024-04-30 17:35:08 +03:00
|
|
|
| maxp = addpos(minmax) ;
|
2022-12-14 17:45:13 +03:00
|
|
|
OF ; coll = expression ;
|
2023-12-05 17:06:28 +03:00
|
|
|
OR ; IF ; LIST_EMPTY ; THEN ;
|
2022-12-14 17:45:13 +03:00
|
|
|
default = expression ; {
|
2024-04-30 17:35:08 +03:00
|
|
|
let max, pos = maxp in
|
|
|
|
CollectionOp ((AggregateExtremum { max; default }, pos), coll)
|
2022-12-14 17:45:13 +03:00
|
|
|
} %prec apply
|
2023-01-04 18:12:36 +03:00
|
|
|
| op = addpos(unop) ; e = expression ; {
|
2022-12-14 17:45:13 +03:00
|
|
|
Unop (op, e)
|
|
|
|
} %prec unop_expr
|
|
|
|
| e1 = expression ;
|
2023-01-04 12:54:12 +03:00
|
|
|
binop = addpos(binop) ;
|
2022-12-14 17:45:13 +03:00
|
|
|
e2 = expression ; {
|
|
|
|
Binop (binop, e1, e2)
|
2023-01-04 12:54:12 +03:00
|
|
|
}
|
2024-04-30 17:35:08 +03:00
|
|
|
| pos = pos(EXISTS) ; i = mbinder ;
|
2022-12-14 17:45:13 +03:00
|
|
|
AMONG ; coll = expression ;
|
|
|
|
SUCH ; THAT ; predicate = expression ; {
|
2024-04-30 17:35:08 +03:00
|
|
|
CollectionOp ((Exists {predicate = i, predicate}, pos), coll)
|
2022-12-14 17:45:13 +03:00
|
|
|
} %prec let_expr
|
2024-04-30 17:35:08 +03:00
|
|
|
| pos = pos(FOR) ; ALL ; i = mbinder ;
|
2022-12-14 17:45:13 +03:00
|
|
|
AMONG ; coll = expression ;
|
|
|
|
WE_HAVE ; predicate = expression ; {
|
2024-04-30 17:35:08 +03:00
|
|
|
CollectionOp ((Forall {predicate = i, predicate}, pos), coll)
|
2022-12-14 17:45:13 +03:00
|
|
|
} %prec let_expr
|
|
|
|
| MATCH ; e = expression ;
|
|
|
|
WITH ;
|
|
|
|
arms = addpos(nonempty_list(addpos(preceded(ALT, match_arm)))) ; {
|
|
|
|
MatchWith (e, arms)
|
|
|
|
}
|
|
|
|
| IF ; e1 = expression ;
|
|
|
|
THEN ; e2 = expression ;
|
|
|
|
ELSE ; e3 = expression ; {
|
|
|
|
IfThenElse (e1, e2, e3)
|
|
|
|
} %prec let_expr
|
2024-01-25 19:37:00 +03:00
|
|
|
| LET ; ids = mbinder ;
|
2022-12-14 17:45:13 +03:00
|
|
|
DEFINED_AS ; e1 = expression ;
|
|
|
|
IN ; e2 = expression ; {
|
2023-12-08 17:07:32 +03:00
|
|
|
LetIn (ids, e1, e2)
|
2022-12-14 17:45:13 +03:00
|
|
|
} %prec let_expr
|
2024-04-30 17:35:08 +03:00
|
|
|
| pos = pos(LIST); ids = mbinder ;
|
2022-12-14 17:45:13 +03:00
|
|
|
AMONG ; coll = expression ;
|
|
|
|
SUCH ; THAT ; f = expression ; {
|
2024-04-30 17:35:08 +03:00
|
|
|
CollectionOp ((Filter {f = ids, f}, pos), coll)
|
2022-12-14 17:45:13 +03:00
|
|
|
} %prec top_expr
|
2022-12-19 12:51:35 +03:00
|
|
|
| fmap = expression ;
|
2024-04-30 17:35:08 +03:00
|
|
|
pfor = pos(FOR) ; i = mbinder ;
|
2022-12-19 12:51:35 +03:00
|
|
|
AMONG ; coll = expression ;
|
2024-04-30 17:35:08 +03:00
|
|
|
psuch = pos(SUCH) ; THAT ; ffilt = expression ; {
|
|
|
|
CollectionOp ((Map {f = i, fmap}, pfor), (CollectionOp ((Filter {f = i, ffilt}, psuch), coll), Pos.from_lpos $loc))
|
2022-12-19 12:51:35 +03:00
|
|
|
} %prec top_expr
|
2024-04-30 17:35:08 +03:00
|
|
|
| pos = pos(CONTENT); OF; ids = mbinder ;
|
2022-12-14 17:45:13 +03:00
|
|
|
AMONG ; coll = expression ;
|
|
|
|
SUCH ; THAT ; f = expression ;
|
|
|
|
IS ; max = minmax ;
|
2023-12-05 17:06:28 +03:00
|
|
|
OR ; IF ; LIST_EMPTY ; THEN ; default = expression ; {
|
2024-04-30 17:35:08 +03:00
|
|
|
CollectionOp ((AggregateArgExtremum { max; default; f = ids, f }, pos), coll)
|
2022-12-14 17:45:13 +03:00
|
|
|
} %prec top_expr
|
2020-07-01 00:15:14 +03:00
|
|
|
|
2022-12-14 17:45:13 +03:00
|
|
|
|
|
|
|
let struct_content_field :=
|
2023-01-04 18:12:36 +03:00
|
|
|
| field = lident ; COLON ; e = expression ; <>
|
2022-12-14 17:45:13 +03:00
|
|
|
|
|
|
|
let struct_or_enum_inject ==
|
2023-01-04 18:12:36 +03:00
|
|
|
| uid = addpos(quident) ;
|
|
|
|
data = option(preceded(CONTENT,expression)) ; {
|
|
|
|
EnumInject(uid, data)
|
2022-12-14 17:45:13 +03:00
|
|
|
}
|
2023-01-04 18:12:36 +03:00
|
|
|
| c = addpos(quident) ;
|
2022-12-15 13:48:48 +03:00
|
|
|
LBRACE ;
|
2022-12-14 17:45:13 +03:00
|
|
|
fields = nonempty_list(preceded(ALT, struct_content_field)) ;
|
2022-12-15 13:48:48 +03:00
|
|
|
RBRACE ; {
|
2022-12-14 17:45:13 +03:00
|
|
|
StructLit(c, fields)
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2021-03-09 20:48:58 +03:00
|
|
|
|
2022-12-14 17:45:13 +03:00
|
|
|
let num_literal ==
|
|
|
|
| d = INT_LITERAL ; <Int>
|
2022-12-13 19:55:16 +03:00
|
|
|
| d = DECIMAL_LITERAL ; {
|
2022-12-14 17:45:13 +03:00
|
|
|
let (d1, d2) = d in Dec (d1, d2)
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2020-04-14 20:13:20 +03:00
|
|
|
|
2022-12-14 17:45:13 +03:00
|
|
|
let unit_literal ==
|
|
|
|
| PERCENT ; { Percent }
|
|
|
|
| YEAR ; { Year}
|
|
|
|
| MONTH ; { Month }
|
|
|
|
| DAY ; { Day }
|
2020-04-11 19:16:15 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let literal :=
|
2022-12-14 17:45:13 +03:00
|
|
|
| l = addpos(num_literal); u = option(addpos(unit_literal)) ; <LNumber>
|
2022-12-13 19:55:16 +03:00
|
|
|
| money = MONEY_AMOUNT ; {
|
2020-04-25 16:17:44 +03:00
|
|
|
let (units, cents) = money in
|
2022-12-14 17:45:13 +03:00
|
|
|
LMoneyAmount {
|
2020-04-25 16:17:44 +03:00
|
|
|
money_amount_units = units;
|
|
|
|
money_amount_cents = cents;
|
2022-12-14 17:45:13 +03:00
|
|
|
}
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2023-01-13 16:49:05 +03:00
|
|
|
| d = DATE_LITERAL ; {
|
2022-07-21 15:14:22 +03:00
|
|
|
let (y,m,d) = d in
|
2022-12-14 17:45:13 +03:00
|
|
|
LDate {
|
2021-05-15 02:16:08 +03:00
|
|
|
literal_date_year = y;
|
|
|
|
literal_date_month = m;
|
|
|
|
literal_date_day = d;
|
2022-12-14 17:45:13 +03:00
|
|
|
}
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2022-12-14 17:45:13 +03:00
|
|
|
| TRUE ; { LBool true }
|
|
|
|
| FALSE ; { LBool false }
|
2022-12-13 19:55:16 +03:00
|
|
|
|
2022-12-14 17:45:13 +03:00
|
|
|
let scope_call_args ==
|
2022-12-13 19:55:16 +03:00
|
|
|
| WITH_V ;
|
2022-12-15 13:48:48 +03:00
|
|
|
LBRACE ;
|
2022-12-13 19:55:16 +03:00
|
|
|
fields = list(preceded (ALT, struct_content_field)) ;
|
2022-12-15 13:48:48 +03:00
|
|
|
RBRACE ; {
|
2022-12-07 20:31:08 +03:00
|
|
|
fields
|
|
|
|
}
|
|
|
|
|
2023-02-09 17:44:27 +03:00
|
|
|
let funcall_args :=
|
|
|
|
| e = expression; { [e] } %prec apply
|
|
|
|
| e = expression; COMMA; el = funcall_args ; { e :: el }
|
|
|
|
|
2022-12-14 17:45:13 +03:00
|
|
|
let minmax ==
|
2022-12-13 19:55:16 +03:00
|
|
|
| MAXIMUM ; { true }
|
|
|
|
| MINIMUM ; { false }
|
2022-12-12 18:02:07 +03:00
|
|
|
|
2022-12-14 17:45:13 +03:00
|
|
|
let unop ==
|
2023-01-04 18:12:36 +03:00
|
|
|
| NOT ; { Not }
|
|
|
|
| k = MINUS ; <Minus>
|
2022-05-11 19:27:14 +03:00
|
|
|
|
2023-01-04 12:54:12 +03:00
|
|
|
let binop ==
|
|
|
|
| k = MULT ; <Mult>
|
|
|
|
| k = DIV ; <Div>
|
|
|
|
| k = PLUS ; <Add>
|
|
|
|
| k = MINUS ; <Sub>
|
|
|
|
| PLUSPLUS ; { Concat }
|
|
|
|
| k = LESSER ; <Lt>
|
|
|
|
| k = LESSER_EQUAL ; <Lte>
|
|
|
|
| k = GREATER ; <Gt>
|
|
|
|
| k = GREATER_EQUAL ; <Gte>
|
|
|
|
| EQUAL ; { Eq }
|
|
|
|
| NOT_EQUAL ; { Neq }
|
2022-12-20 18:17:22 +03:00
|
|
|
| AND ; { And }
|
|
|
|
| OR ; { Or }
|
|
|
|
| XOR ; { Xor }
|
2021-03-09 20:48:58 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let constructor_binding :=
|
2023-01-04 18:12:36 +03:00
|
|
|
| uid = addpos(quident) ; OF ; lid = lident ; {
|
|
|
|
([uid], Some lid)
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2023-01-04 18:12:36 +03:00
|
|
|
| uid = addpos(quident) ; {
|
|
|
|
([uid], None)
|
2022-12-14 17:45:13 +03:00
|
|
|
} %prec apply
|
2020-03-08 08:06:32 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let match_arm :=
|
2022-12-14 17:45:13 +03:00
|
|
|
| WILDCARD ; COLON ; ~ = expression ; <WildCard>
|
|
|
|
%prec ALT
|
|
|
|
| pat = addpos(constructor_binding) ;
|
|
|
|
COLON ; e = expression ; {
|
|
|
|
MatchCase {
|
|
|
|
match_case_pattern = pat;
|
2021-06-01 19:56:03 +03:00
|
|
|
match_case_expr = e;
|
2022-12-14 17:45:13 +03:00
|
|
|
}
|
|
|
|
} %prec ALT
|
2021-05-15 02:16:08 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let condition_consequence :=
|
2023-01-04 18:12:36 +03:00
|
|
|
| UNDER_CONDITION ; c = expression ; CONSEQUENCE ; <>
|
2020-03-08 08:30:05 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let rule_expr :=
|
2023-02-28 16:40:05 +03:00
|
|
|
| i = addpos(scope_var) ; p = option(addpos(definition_parameters)) ; <>
|
2020-04-14 20:16:40 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let rule_consequence :=
|
|
|
|
| flag = option(NOT); FILLED ; {
|
2022-12-14 17:45:13 +03:00
|
|
|
None = flag
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2020-08-07 11:57:57 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let rule :=
|
|
|
|
| label = option(label) ;
|
2023-03-06 16:42:24 +03:00
|
|
|
except = option(addpos(exception_to)) ;
|
2023-02-28 16:40:05 +03:00
|
|
|
pos_rule = pos(RULE) ;
|
2022-12-13 19:55:16 +03:00
|
|
|
name_and_param = rule_expr ;
|
|
|
|
state = option(state) ;
|
2023-03-06 13:08:50 +03:00
|
|
|
cond = option(condition_consequence) ;
|
2022-12-14 17:45:13 +03:00
|
|
|
consequence = addpos(rule_consequence) ; {
|
2023-02-28 16:40:05 +03:00
|
|
|
let (name, params_applied) = name_and_param in
|
2023-05-17 16:44:57 +03:00
|
|
|
let cons : bool Mark.pos = consequence in
|
2022-12-13 19:55:16 +03:00
|
|
|
let rule_exception = match except with
|
|
|
|
| None -> NotAnException
|
2023-05-17 16:44:57 +03:00
|
|
|
| Some x -> Mark.remove x
|
2022-12-13 19:55:16 +03:00
|
|
|
in
|
2023-02-28 16:40:05 +03:00
|
|
|
let pos_start =
|
2023-05-17 16:44:57 +03:00
|
|
|
match label with Some l -> Mark.get l
|
|
|
|
| None -> match except with Some e -> Mark.get e
|
2023-02-28 16:40:05 +03:00
|
|
|
| None -> pos_rule
|
|
|
|
in
|
2022-12-13 19:55:16 +03:00
|
|
|
{
|
|
|
|
rule_label = label;
|
|
|
|
rule_exception_to = rule_exception;
|
2023-02-28 16:40:05 +03:00
|
|
|
rule_parameter = params_applied;
|
2022-12-13 19:55:16 +03:00
|
|
|
rule_condition = cond;
|
|
|
|
rule_name = name;
|
|
|
|
rule_id = Shared_ast.RuleName.fresh
|
2023-05-17 16:44:57 +03:00
|
|
|
(String.concat "." (List.map (fun i -> Mark.remove i) (Mark.remove name)),
|
|
|
|
Pos.join pos_start (Mark.get name));
|
2022-12-13 19:55:16 +03:00
|
|
|
rule_consequence = cons;
|
|
|
|
rule_state = state;
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
2022-12-13 19:55:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let definition_parameters :=
|
2023-02-28 16:40:05 +03:00
|
|
|
| OF ; args = separated_nonempty_list(COMMA,lident) ; <>
|
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
|
|
|
|
let label :=
|
2023-01-04 18:12:36 +03:00
|
|
|
| LABEL ; i = lident ; <>
|
2022-12-13 19:55:16 +03:00
|
|
|
|
|
|
|
let state :=
|
2023-01-04 18:12:36 +03:00
|
|
|
| STATE ; s = lident ; <>
|
2022-12-13 19:55:16 +03:00
|
|
|
|
|
|
|
let exception_to :=
|
2023-01-04 18:12:36 +03:00
|
|
|
| EXCEPTION ; i = option(lident) ; {
|
2022-12-13 19:55:16 +03:00
|
|
|
match i with
|
|
|
|
| None -> UnlabeledException
|
|
|
|
| Some x -> ExceptionToLabel x
|
|
|
|
}
|
|
|
|
|
|
|
|
let definition :=
|
2022-12-20 15:10:41 +03:00
|
|
|
| label = option(label);
|
2022-12-13 19:55:16 +03:00
|
|
|
except = option(exception_to) ;
|
2023-02-28 16:40:05 +03:00
|
|
|
pos_def = pos(DEFINITION) ;
|
2023-01-04 18:12:36 +03:00
|
|
|
name = addpos(scope_var) ;
|
2023-02-28 16:40:05 +03:00
|
|
|
params = option(addpos(definition_parameters)) ;
|
2022-12-13 19:55:16 +03:00
|
|
|
state = option(state) ;
|
|
|
|
cond = option(condition_consequence) ;
|
|
|
|
DEFINED_AS ;
|
|
|
|
e = expression ; {
|
|
|
|
let def_exception = match except with
|
|
|
|
| None -> NotAnException
|
|
|
|
| Some x -> x
|
|
|
|
in
|
2023-02-28 16:40:05 +03:00
|
|
|
let pos_start =
|
|
|
|
match label with Some _ -> Pos.from_lpos $loc(label)
|
|
|
|
| None -> match except with Some _ -> Pos.from_lpos $loc(except)
|
|
|
|
| None -> pos_def
|
|
|
|
in
|
2022-12-13 19:55:16 +03:00
|
|
|
{
|
|
|
|
definition_label = label;
|
|
|
|
definition_exception_to = def_exception;
|
|
|
|
definition_name = name;
|
2023-02-28 16:40:05 +03:00
|
|
|
definition_parameter = params;
|
2022-12-13 19:55:16 +03:00
|
|
|
definition_condition = cond;
|
|
|
|
definition_id =
|
|
|
|
Shared_ast.RuleName.fresh
|
2023-05-17 16:44:57 +03:00
|
|
|
(String.concat "." (List.map (fun i -> Mark.remove i) (Mark.remove name)),
|
|
|
|
Pos.join pos_start (Mark.get name));
|
2022-12-13 19:55:16 +03:00
|
|
|
definition_expr = e;
|
|
|
|
definition_state = state;
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
2022-12-13 19:55:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let variation_type :=
|
2023-01-04 18:12:36 +03:00
|
|
|
| INCREASING ; { Increasing }
|
|
|
|
| DECREASING ; { Decreasing }
|
2022-12-13 19:55:16 +03:00
|
|
|
|
|
|
|
let assertion :=
|
|
|
|
| cond = option(condition_consequence) ;
|
2023-01-04 18:12:36 +03:00
|
|
|
base = expression ; {
|
2021-05-15 02:16:08 +03:00
|
|
|
(Assertion {
|
|
|
|
assertion_condition = cond;
|
2020-04-25 15:21:26 +03:00
|
|
|
assertion_content = base;
|
2021-03-09 20:48:58 +03:00
|
|
|
})
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2023-01-04 18:12:36 +03:00
|
|
|
| FIXED ; q = addpos(scope_var) ; BY ; i = lident ; {
|
2022-12-13 19:55:16 +03:00
|
|
|
MetaAssertion (FixedBy (q, i))
|
|
|
|
}
|
2023-01-04 18:12:36 +03:00
|
|
|
| VARIES ; q = addpos(scope_var) ;
|
2022-12-14 17:45:13 +03:00
|
|
|
WITH_V ; e = expression ;
|
2023-01-04 18:12:36 +03:00
|
|
|
t = option(addpos(variation_type)) ; {
|
2021-05-15 02:16:08 +03:00
|
|
|
MetaAssertion (VariesWith (q, e, t))
|
|
|
|
}
|
2020-03-08 08:06:32 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let scope_item :=
|
2023-03-06 16:42:24 +03:00
|
|
|
| r = rule ; {
|
2023-05-17 16:44:57 +03:00
|
|
|
Rule r, Mark.get (Shared_ast.RuleName.get_info r.rule_id)
|
2023-03-06 16:42:24 +03:00
|
|
|
}
|
|
|
|
| d = definition ; {
|
2023-05-17 16:44:57 +03:00
|
|
|
Definition d, Mark.get (Shared_ast.RuleName.get_info d.definition_id)
|
2023-03-06 16:42:24 +03:00
|
|
|
}
|
|
|
|
| ASSERTION ; contents = addpos(assertion) ; <>
|
2023-01-20 20:18:53 +03:00
|
|
|
| DATE ; i = LIDENT ; v = addpos(variation_type) ;
|
|
|
|
{
|
|
|
|
(* Round is a builtin, we need to check which one it is *)
|
|
|
|
match Localisation.lex_builtin i with
|
|
|
|
| Some Round ->
|
2023-05-17 16:44:57 +03:00
|
|
|
DateRounding(v), Mark.get v
|
2023-05-30 16:41:49 +03:00
|
|
|
| _ ->
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error ~pos:
|
2023-03-10 11:30:20 +03:00
|
|
|
(Pos.from_lpos $loc(i))
|
|
|
|
"Expected the form 'date round increasing' or 'date round decreasing'"
|
2023-01-20 20:18:53 +03:00
|
|
|
}
|
2020-04-14 12:46:48 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let struct_scope_base :=
|
2023-01-04 18:12:36 +03:00
|
|
|
| DATA ; i = lident ;
|
2023-02-24 12:02:12 +03:00
|
|
|
CONTENT ; t = addpos(typ) ; <>
|
2023-01-04 18:12:36 +03:00
|
|
|
| pos = pos(CONDITION) ; i = lident ; {
|
2021-05-15 02:16:08 +03:00
|
|
|
(i, (Condition, pos))
|
|
|
|
}
|
2020-04-03 23:58:34 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let struct_scope :=
|
|
|
|
| name_and_typ = struct_scope_base ;
|
2023-02-24 12:02:12 +03:00
|
|
|
args = depends_stance; {
|
2021-05-15 02:16:08 +03:00
|
|
|
let (name, typ) = name_and_typ in
|
2023-02-24 12:02:12 +03:00
|
|
|
(* let (typ, typ_pos) = typ in *)
|
2022-12-13 19:55:16 +03:00
|
|
|
{
|
2021-05-15 02:16:08 +03:00
|
|
|
struct_decl_field_name = name;
|
2023-02-24 12:02:12 +03:00
|
|
|
struct_decl_field_typ = Ast.type_from_args args typ;
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2020-04-03 23:58:34 +03:00
|
|
|
|
2024-03-20 19:51:01 +03:00
|
|
|
let scope_decl_item_attribute_input ==
|
|
|
|
| CONTEXT ; { Some Context }
|
|
|
|
| INPUT ; { Some Input }
|
|
|
|
| INTERNAL ; { Some Internal }
|
|
|
|
| { None }
|
2022-01-27 20:03:47 +03:00
|
|
|
|
2024-03-20 19:51:01 +03:00
|
|
|
let scope_decl_item_attribute_output ==
|
2023-01-04 18:12:36 +03:00
|
|
|
| OUTPUT ; { true }
|
|
|
|
| { false }
|
2022-02-07 12:30:36 +03:00
|
|
|
|
2024-03-20 19:51:01 +03:00
|
|
|
let scope_decl_item_attribute ==
|
2023-01-04 18:12:36 +03:00
|
|
|
| input = addpos(scope_decl_item_attribute_input) ;
|
2024-03-20 19:51:01 +03:00
|
|
|
output = addpos(scope_decl_item_attribute_output) ;
|
|
|
|
i = lident ; {
|
|
|
|
match input, output with
|
|
|
|
| (Some Internal, _), (true, pos) ->
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error ~pos
|
2024-03-20 19:51:01 +03:00
|
|
|
"A variable cannot be declared both 'internal' and 'output'."
|
|
|
|
| input, output -> input, output, i
|
|
|
|
}
|
|
|
|
|
|
|
|
let scope_decl_item_attribute_mandatory ==
|
|
|
|
| attr = scope_decl_item_attribute ; {
|
|
|
|
let in_attr_opt, out_attr, i = attr in
|
|
|
|
let in_attr = match in_attr_opt, out_attr with
|
|
|
|
| (None, _), (false, _) ->
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error ~pos:(Pos.from_lpos $loc(attr))
|
2024-03-20 19:51:01 +03:00
|
|
|
"Variable declaration requires input qualification ('internal', \
|
|
|
|
'input' or 'context')"
|
|
|
|
| (None, pos), (true, _) -> Internal, pos
|
|
|
|
| (Some i, pos), _ -> i, pos
|
|
|
|
in
|
|
|
|
{
|
|
|
|
scope_decl_context_io_input = in_attr;
|
|
|
|
scope_decl_context_io_output = out_attr;
|
|
|
|
}, i
|
|
|
|
}
|
2022-02-07 12:30:36 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let scope_decl_item :=
|
2024-03-20 19:51:01 +03:00
|
|
|
| attr_i = scope_decl_item_attribute_mandatory ;
|
2022-12-14 17:45:13 +03:00
|
|
|
CONTENT ; t = addpos(typ) ;
|
2023-02-24 12:02:12 +03:00
|
|
|
args_typ = depends_stance ;
|
2022-12-13 19:55:16 +03:00
|
|
|
states = list(state) ; {
|
2024-03-20 19:51:01 +03:00
|
|
|
let attr, i = attr_i in
|
2023-01-04 18:12:36 +03:00
|
|
|
ContextData {
|
2021-05-15 02:16:08 +03:00
|
|
|
scope_decl_context_item_name = i;
|
2022-01-27 20:03:47 +03:00
|
|
|
scope_decl_context_item_attribute = attr;
|
2023-02-27 11:50:42 +03:00
|
|
|
scope_decl_context_item_parameters =
|
2023-02-28 16:40:05 +03:00
|
|
|
Option.map
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.map
|
2023-02-28 16:40:05 +03:00
|
|
|
(List.map (fun (lbl, (base_t, m)) -> lbl, (Base base_t, m))))
|
|
|
|
args_typ;
|
2023-02-24 12:02:12 +03:00
|
|
|
scope_decl_context_item_typ = type_from_args args_typ t;
|
2022-02-28 16:33:07 +03:00
|
|
|
scope_decl_context_item_states = states;
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2024-03-20 19:51:01 +03:00
|
|
|
| attr = scope_decl_item_attribute ;
|
|
|
|
SCOPE ; c = addpos(quident) ; {
|
|
|
|
let in_attr_opt, out_attr, i = attr in
|
|
|
|
let attr = match in_attr_opt, out_attr with
|
|
|
|
| (None, pos), out -> {
|
|
|
|
scope_decl_context_io_input = (Internal, pos);
|
|
|
|
scope_decl_context_io_output = out;
|
|
|
|
};
|
|
|
|
| (Some _, pos), _ ->
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error ~pos
|
2024-03-20 19:51:01 +03:00
|
|
|
"Scope declaration does not support input qualifiers ('internal', \
|
|
|
|
'input' or 'context')"
|
|
|
|
in
|
2023-01-04 18:12:36 +03:00
|
|
|
ContextScope{
|
2021-05-15 02:16:08 +03:00
|
|
|
scope_decl_context_scope_name = i;
|
|
|
|
scope_decl_context_scope_sub_scope = c;
|
2024-03-20 19:51:01 +03:00
|
|
|
scope_decl_context_scope_attribute = attr;
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2024-03-20 19:51:01 +03:00
|
|
|
| attr_i = scope_decl_item_attribute_mandatory ;
|
2023-01-04 18:12:36 +03:00
|
|
|
pos_condition = pos(CONDITION) ;
|
2023-02-24 12:02:12 +03:00
|
|
|
args = depends_stance ;
|
2022-12-13 19:55:16 +03:00
|
|
|
states = list(state) ; {
|
2024-03-20 19:51:01 +03:00
|
|
|
let attr, i = attr_i in
|
2022-12-13 19:55:16 +03:00
|
|
|
ContextData {
|
2021-05-15 02:16:08 +03:00
|
|
|
scope_decl_context_item_name = i;
|
2022-01-27 20:03:47 +03:00
|
|
|
scope_decl_context_item_attribute = attr;
|
2023-02-27 11:50:42 +03:00
|
|
|
scope_decl_context_item_parameters =
|
2023-02-28 16:40:05 +03:00
|
|
|
Option.map
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.map
|
2023-02-28 16:40:05 +03:00
|
|
|
(List.map (fun (lbl, (base_t, m)) -> lbl, (Base base_t, m))))
|
|
|
|
args;
|
2021-05-15 02:16:08 +03:00
|
|
|
scope_decl_context_item_typ =
|
2023-02-24 12:02:12 +03:00
|
|
|
Ast.type_from_args args (Condition, pos_condition);
|
2022-02-28 16:33:07 +03:00
|
|
|
scope_decl_context_item_states = states;
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2020-04-03 23:58:34 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let enum_decl_line :=
|
2023-01-04 18:12:36 +03:00
|
|
|
| ALT ; c = uident ;
|
2022-12-19 12:51:35 +03:00
|
|
|
t = option(preceded(CONTENT,addpos(typ))) ; {
|
|
|
|
{
|
2021-05-15 02:16:08 +03:00
|
|
|
enum_decl_case_name = c;
|
2022-12-19 12:51:35 +03:00
|
|
|
enum_decl_case_typ =
|
2023-02-24 12:02:12 +03:00
|
|
|
Option.map (fun (t, t_pos) -> Base t, t_pos) t;
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2020-04-14 12:46:48 +03:00
|
|
|
|
2023-01-12 13:12:48 +03:00
|
|
|
let var_content ==
|
|
|
|
| ~ = lident ; CONTENT ; ty = addpos(typ) ; <>
|
|
|
|
let depends_stance ==
|
2023-02-28 16:40:05 +03:00
|
|
|
| DEPENDS ; args = separated_nonempty_list(COMMA,var_content) ; {
|
|
|
|
Some (args, Pos.from_lpos $sloc)
|
|
|
|
}
|
|
|
|
| DEPENDS ; LPAREN ; args = separated_nonempty_list(COMMA,var_content) ; RPAREN ; {
|
|
|
|
Some (args, Pos.from_lpos $sloc)
|
|
|
|
}
|
|
|
|
| { None }
|
2023-01-12 13:12:48 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let code_item :=
|
2023-01-04 18:12:36 +03:00
|
|
|
| SCOPE ; c = uident ;
|
|
|
|
e = option(preceded(UNDER_CONDITION,expression)) ;
|
2023-03-06 16:42:24 +03:00
|
|
|
COLON ; items = nonempty_list(scope_item) ; {
|
2023-01-04 18:12:36 +03:00
|
|
|
ScopeUse {
|
2021-05-15 02:16:08 +03:00
|
|
|
scope_use_name = c;
|
2020-05-18 12:29:22 +03:00
|
|
|
scope_use_condition = e;
|
2020-05-14 22:19:46 +03:00
|
|
|
scope_use_items = items;
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2023-01-04 18:12:36 +03:00
|
|
|
| DECLARATION ; STRUCT ; c = uident ;
|
|
|
|
COLON ; scopes = list(addpos(struct_scope)) ; {
|
|
|
|
StructDecl {
|
2021-05-15 02:16:08 +03:00
|
|
|
struct_decl_name = c;
|
2020-05-14 22:19:46 +03:00
|
|
|
struct_decl_fields = scopes;
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2023-01-04 18:12:36 +03:00
|
|
|
| DECLARATION ; SCOPE ; c = uident ;
|
|
|
|
COLON ; context = nonempty_list(addpos(scope_decl_item)) ; {
|
|
|
|
ScopeDecl {
|
2021-05-15 02:16:08 +03:00
|
|
|
scope_decl_name = c;
|
|
|
|
scope_decl_context = context;
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2023-01-04 18:12:36 +03:00
|
|
|
| DECLARATION ; ENUM ; c = uident ;
|
|
|
|
COLON ; cases = list(addpos(enum_decl_line)) ; {
|
|
|
|
EnumDecl {
|
2021-05-15 02:16:08 +03:00
|
|
|
enum_decl_name = c;
|
2020-04-14 13:34:09 +03:00
|
|
|
enum_decl_cases = cases;
|
2023-01-04 18:12:36 +03:00
|
|
|
}
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2023-01-12 13:12:48 +03:00
|
|
|
| DECLARATION ; name = lident ;
|
|
|
|
CONTENT ; ty = addpos(typ) ;
|
|
|
|
args = depends_stance ;
|
2023-05-11 18:39:38 +03:00
|
|
|
topdef_expr = option(opt_def) ; {
|
2023-01-23 14:19:36 +03:00
|
|
|
Topdef {
|
2023-01-12 13:12:48 +03:00
|
|
|
topdef_name = name;
|
|
|
|
topdef_args = args;
|
2023-02-24 12:02:12 +03:00
|
|
|
topdef_type = type_from_args args ty;
|
2023-05-11 18:39:38 +03:00
|
|
|
topdef_expr;
|
2023-01-12 13:12:48 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-09 20:48:58 +03:00
|
|
|
|
2023-05-11 18:39:38 +03:00
|
|
|
let opt_def ==
|
|
|
|
| DEFINED_AS; e = expression; <>
|
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let code :=
|
2023-01-04 18:12:36 +03:00
|
|
|
| code = list(addpos(code_item)) ; <>
|
2021-03-09 20:48:58 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let metadata_block :=
|
|
|
|
| BEGIN_METADATA ; option(law_text) ;
|
2023-01-04 18:12:36 +03:00
|
|
|
~ = code ;
|
2022-12-13 19:55:16 +03:00
|
|
|
text = END_CODE ; {
|
2023-01-04 18:12:36 +03:00
|
|
|
(code, (text, Pos.from_lpos $sloc))
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let law_heading :=
|
|
|
|
| title = LAW_HEADING ; {
|
2022-09-07 18:14:22 +03:00
|
|
|
let (title, id, is_archive, precedence) = title in {
|
2021-05-15 02:16:08 +03:00
|
|
|
law_heading_name = (title, Pos.from_lpos $sloc);
|
|
|
|
law_heading_id = id;
|
2022-09-07 18:14:22 +03:00
|
|
|
law_heading_is_archive = is_archive;
|
2020-10-04 02:25:37 +03:00
|
|
|
law_heading_precedence = precedence;
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let law_text :=
|
|
|
|
| lines = nonempty_list(LAW_TEXT) ; { String.trim (String.concat "" lines) }
|
2021-05-15 02:16:08 +03:00
|
|
|
|
2023-09-05 16:00:55 +03:00
|
|
|
let directive :=
|
|
|
|
| LAW_INCLUDE ; COLON ;
|
|
|
|
args = nonempty_list(DIRECTIVE_ARG) ;
|
|
|
|
page = option(AT_PAGE) ; {
|
|
|
|
let filename = String.trim (String.concat "" args) in
|
|
|
|
let pos = Pos.from_lpos $sloc in
|
|
|
|
let jorftext = Re.Pcre.regexp "(JORFARTI\\d{12}|LEGIARTI\\d{12}|CETATEXT\\d{12})" in
|
|
|
|
if Re.Pcre.pmatch ~rex:jorftext filename && page = None then
|
|
|
|
LawInclude (Ast.LegislativeText (filename, pos))
|
|
|
|
else if Filename.extension filename = ".pdf" || page <> None then
|
|
|
|
LawInclude (Ast.PdfFile ((filename, pos), page))
|
|
|
|
else
|
|
|
|
LawInclude (Ast.CatalaFile (filename, pos))
|
|
|
|
}
|
2023-12-01 17:24:54 +03:00
|
|
|
| MODULE_DEF ; m = addpos(DIRECTIVE_ARG) ;
|
|
|
|
ext = option (MODULE_EXTERNAL) ; {
|
|
|
|
ModuleDef (m, ext <> None)
|
|
|
|
}
|
2023-09-05 16:00:55 +03:00
|
|
|
| MODULE_USE ; m = addpos(DIRECTIVE_ARG) ;
|
|
|
|
alias = option (preceded(MODULE_ALIAS,addpos(DIRECTIVE_ARG))) ; {
|
|
|
|
ModuleUse (m, alias)
|
|
|
|
}
|
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let source_file_item :=
|
|
|
|
| text = law_text ; { LawText text }
|
|
|
|
| BEGIN_CODE ;
|
2023-01-04 18:12:36 +03:00
|
|
|
~ = code ;
|
2022-12-13 19:55:16 +03:00
|
|
|
text = END_CODE ; {
|
2022-08-10 11:45:49 +03:00
|
|
|
CodeBlock (code, (text, Pos.from_lpos $sloc), false)
|
2021-05-15 02:16:08 +03:00
|
|
|
}
|
2022-12-13 19:55:16 +03:00
|
|
|
| heading = law_heading ; {
|
2021-05-15 02:16:08 +03:00
|
|
|
LawHeading (heading, [])
|
|
|
|
}
|
2022-12-13 19:55:16 +03:00
|
|
|
| code = metadata_block ; {
|
2021-05-15 02:16:08 +03:00
|
|
|
let (code, source_repr) = code in
|
|
|
|
CodeBlock (code, source_repr, true)
|
|
|
|
}
|
2023-09-05 16:00:55 +03:00
|
|
|
| BEGIN_DIRECTIVE ; ~ = directive ; END_DIRECTIVE ; { directive }
|
2021-05-15 02:16:08 +03:00
|
|
|
|
2022-12-13 19:55:16 +03:00
|
|
|
let source_file :=
|
|
|
|
| hd = source_file_item ; tl = source_file ; { hd::tl }
|
|
|
|
| EOF ; { [] }
|