catala/src/lawspec/parsing/parser.mly

231 lines
5.0 KiB
OCaml
Raw Normal View History

(*
This file is part of the Lawspec compiler, a specification language for tax and social benefits
computation rules.
Copyright (C) 2019 Inria, contributor: Denis Merigoux <denis.merigoux@inria.fr>
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.
*)
%{
2020-03-08 03:52:31 +03:00
open Ast
%}
%token EOF
2020-03-08 03:52:31 +03:00
%token<string> LAW_ARTICLE
%token<string> LAW_CODE
%token<string> LAW_TEXT
2020-03-08 06:28:45 +03:00
%token<string> CONSTRUCTOR IDENT
%token<string> END_CODE
2020-03-08 07:27:46 +03:00
%token<int> INT_LITERAL
2020-04-03 23:58:34 +03:00
%token BEGIN_CODE
%token COLON ALT DATA
%token OF INTEGER COLLECTION
%token RULE CONDITION DEFINED_AS
2020-03-08 07:12:12 +03:00
%token EXISTS IN SUCH THAT NOW LESSER GREATER
2020-04-03 23:34:11 +03:00
%token DOT AND OR LPAREN RPAREN OPTIONAL EQUAL
2020-03-08 07:27:46 +03:00
%token COMMA CARDINAL LESSER_EQUAL GREATER_EQUAL
2020-04-03 23:58:34 +03:00
%token ASSERTION FIXED BY YEAR
2020-03-08 08:06:32 +03:00
%token PLUS MINUS MULT DIV MATCH WITH VARIES_WITH
%token FORALL WE_HAVE INCREASING DECREASING
2020-04-03 23:58:34 +03:00
%token NOT BOOLEAN
2020-04-03 23:34:11 +03:00
%token FIELD FILLED IFF EURO NOT_EQUAL DEFINITION
2020-04-03 23:58:34 +03:00
%token STRUCT CONTENT IF THEN DEPENDS DECLARATION
%token CONTEXT INCLUDES
2020-04-03 23:34:11 +03:00
%type <Ast.source_file> source_file
%start source_file
%%
2020-04-03 23:58:34 +03:00
typ_base:
2020-03-08 07:01:26 +03:00
| IDENT {}
| INTEGER {}
| BOOLEAN {}
2020-04-03 23:58:34 +03:00
| CONSTRUCTOR {}
2020-03-08 07:01:26 +03:00
2020-04-03 23:58:34 +03:00
typ:
| typ_base option(OPTIONAL) {}
2020-03-08 07:01:26 +03:00
qident:
| IDENT {}
2020-04-07 14:59:52 +03:00
| IDENT DOT qident {}
| CONSTRUCTOR DOT qident {}
2020-03-08 07:01:26 +03:00
primitive_expression:
| NOW {}
2020-04-03 23:34:11 +03:00
| qident {}
2020-03-08 07:01:26 +03:00
2020-03-08 08:06:32 +03:00
date_qualifier:
| YEAR {}
constructor_payload:
| OF base_expression {}
2020-03-08 07:27:46 +03:00
literal:
| INT_LITERAL {}
2020-04-03 23:34:11 +03:00
| INT_LITERAL EURO {}
2020-03-08 08:06:32 +03:00
| INT_LITERAL date_qualifier {}
| CONSTRUCTOR option(constructor_payload) {}
2020-03-08 07:27:46 +03:00
2020-03-08 07:01:26 +03:00
compare_op:
| LESSER {}
2020-03-08 07:27:46 +03:00
| LESSER_EQUAL {}
2020-03-08 07:12:12 +03:00
| GREATER {}
2020-03-08 07:27:46 +03:00
| GREATER_EQUAL {}
| EQUAL {}
2020-04-03 23:34:11 +03:00
| NOT_EQUAL {}
2020-03-08 07:27:46 +03:00
func:
| CARDINAL {}
2020-04-03 23:34:11 +03:00
| qident {}
2020-03-08 07:01:26 +03:00
2020-03-08 07:12:12 +03:00
base_expression:
2020-03-08 07:01:26 +03:00
| primitive_expression {}
2020-03-08 07:27:46 +03:00
| literal {}
2020-04-03 23:34:11 +03:00
| func OF separated_nonempty_list(COMMA, primitive_expression) {}
2020-03-08 07:12:12 +03:00
| LPAREN expression RPAREN {}
2020-03-08 08:06:32 +03:00
mult_op:
| MULT {}
| DIV {}
mult_expression:
2020-03-08 07:12:12 +03:00
| base_expression {}
2020-04-03 23:34:11 +03:00
| base_expression mult_op mult_expression {}
2020-03-08 08:06:32 +03:00
sum_op:
| PLUS {}
| MINUS {}
sum_expression:
| mult_expression {}
2020-04-03 23:34:11 +03:00
| mult_expression sum_op sum_expression {}
2020-03-08 07:12:12 +03:00
logical_op:
| AND {}
| OR {}
2020-04-03 23:34:11 +03:00
| IFF {}
2020-03-08 07:01:26 +03:00
2020-03-08 08:49:29 +03:00
logical_unop:
| NOT {}
2020-03-08 07:01:26 +03:00
compare_expression:
2020-03-08 07:12:12 +03:00
| sum_expression {}
2020-04-03 23:34:11 +03:00
| sum_expression compare_op compare_expression {}
2020-03-08 07:01:26 +03:00
2020-03-08 07:12:12 +03:00
logical_expression:
| compare_expression {}
2020-03-08 08:49:29 +03:00
| logical_unop compare_expression {}
2020-04-03 23:34:11 +03:00
| compare_expression logical_op logical_expression {}
2020-03-08 07:01:26 +03:00
2020-03-08 08:06:32 +03:00
optional_binding:
| {}
2020-03-08 08:49:29 +03:00
| OF IDENT {}
| OF LPAREN constructor_binding RPAREN {}
constructor_binding:
| CONSTRUCTOR optional_binding {}
2020-03-08 08:06:32 +03:00
match_arm:
2020-03-08 08:49:29 +03:00
| constructor_binding COLON logical_expression {}
2020-03-08 08:06:32 +03:00
match_arms:
| ALT match_arm match_arms {}
| {}
forall_prefix:
| FORALL separated_nonempty_list(COMMA,IDENT) IN separated_nonempty_list(COMMA,qident) WE_HAVE {}
exists_prefix:
| EXISTS IDENT IN qident SUCH THAT {}
2020-03-08 07:01:26 +03:00
expression:
| exists_prefix expression {}
| forall_prefix expression {}
2020-03-08 08:06:32 +03:00
| MATCH expression WITH match_arms {}
2020-03-08 07:12:12 +03:00
| logical_expression {}
2020-03-08 07:01:26 +03:00
condition:
2020-04-07 14:59:52 +03:00
| IF expression {}
2020-03-08 07:01:26 +03:00
2020-04-07 14:59:52 +03:00
condition_consequence:
| condition THEN {}
2020-04-03 23:34:11 +03:00
rule:
2020-04-07 14:59:52 +03:00
| option(condition_consequence) option(forall_prefix) qident FILLED {}
2020-03-08 08:49:29 +03:00
2020-04-03 23:34:11 +03:00
definition_parameters:
| OF separated_nonempty_list(COMMA, IDENT) {}
2020-04-03 23:34:11 +03:00
definition:
2020-04-07 14:59:52 +03:00
| option(forall_prefix) qident option(definition_parameters) option(condition_consequence) DEFINED_AS expression {}
2020-03-08 06:28:45 +03:00
2020-03-08 08:06:32 +03:00
variation_type:
| INCREASING {}
| DECREASING {}
assertion:
| logical_expression {}
| qident FIXED BY IDENT {}
2020-04-03 23:34:11 +03:00
| qident VARIES_WITH base_expression option(variation_type) {}
| exists_prefix assertion {}
| forall_prefix assertion {}
2020-03-08 08:06:32 +03:00
2020-04-03 23:34:11 +03:00
application_field_item:
2020-03-08 07:27:46 +03:00
| RULE option(OPTIONAL) rule {}
2020-04-03 23:34:11 +03:00
| DEFINITION option(OPTIONAL) definition {}
2020-04-07 14:59:52 +03:00
| ASSERTION option(condition_consequence) assertion {}
| field_decl_includes {}
2020-03-08 06:28:45 +03:00
2020-04-03 23:58:34 +03:00
struct_field_base:
| DATA IDENT option(COLLECTION) CONTENT typ {}
| CONDITION IDENT {}
struct_field_func:
| DEPENDS OF typ {}
struct_field:
| struct_field_base option(struct_field_func) {}
field_decl_item:
| CONTEXT IDENT STRUCT CONSTRUCTOR {}
field_decl_include:
2020-04-07 14:59:52 +03:00
| CONSTRUCTOR DOT IDENT EQUAL CONSTRUCTOR DOT IDENT option(condition) {}
field_decl_includes_context:
| CONTEXT nonempty_list(field_decl_include) {}
2020-04-03 23:58:34 +03:00
field_decl_includes:
2020-04-07 14:59:52 +03:00
| INCLUDES FIELD CONSTRUCTOR option(field_decl_includes_context) {}
2020-04-03 23:58:34 +03:00
2020-03-08 06:28:45 +03:00
code_item:
2020-04-03 23:34:11 +03:00
| FIELD CONSTRUCTOR COLON nonempty_list(application_field_item) { }
2020-04-04 21:22:16 +03:00
| DECLARATION STRUCT CONSTRUCTOR COLON nonempty_list(struct_field) {}
2020-04-07 14:59:52 +03:00
| DECLARATION FIELD CONSTRUCTOR COLON nonempty_list(field_decl_item) list(field_decl_includes) {}
2020-03-08 06:28:45 +03:00
code:
2020-04-03 23:34:11 +03:00
| code_item code {}
2020-03-08 06:28:45 +03:00
| {}
source_file_item:
2020-03-08 03:52:31 +03:00
| title = LAW_ARTICLE { LawArticle title }
| code = LAW_CODE { LawCode code }
| text = LAW_TEXT { LawText text }
2020-03-08 06:28:45 +03:00
| BEGIN_CODE code text = END_CODE { CodeBlock text }
source_file:
2020-03-08 03:52:31 +03:00
| i = source_file_item f = source_file { i::f }
| EOF { [] }