catala/src/lawspec/parsing/parser.mly

191 lines
3.9 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-03-08 06:28:45 +03:00
%token BEGIN_CODE CHOICE
2020-03-08 07:01:26 +03:00
%token COLON ALT DOT SITUATION SOURCE DATA
%token OF SEMICOLON INTEGER TYPE COLLECTION
%token RULE CONDITION CONSEQUENCE DEFINED AS
2020-03-08 07:12:12 +03:00
%token EXISTS IN SUCH THAT NOW LESSER GREATER
2020-03-08 07:27:46 +03:00
%token BANG AND OR LPAREN RPAREN OPTIONAL EQUAL
%token COMMA CARDINAL LESSER_EQUAL GREATER_EQUAL
2020-03-08 08:06:32 +03:00
%token ASSERTION FIXED BY CONSTANT YEAR
%token PLUS MINUS MULT DIV MATCH WITH VARIES_WITH
%token FORALL WE_HAVE INCREASING DECREASING
%type <Ast.source_file> source_file
%start source_file
%%
2020-03-08 06:28:45 +03:00
choice:
2020-03-08 08:06:32 +03:00
| CONSTRUCTOR situation_type {}
2020-03-08 06:28:45 +03:00
choices:
| ALT choice choices {}
| {}
2020-03-08 07:01:26 +03:00
type_ident:
| IDENT {}
| INTEGER {}
situation_type_alt:
2020-03-08 06:28:45 +03:00
| CHOICE IDENT {}
2020-03-08 07:01:26 +03:00
| TYPE type_ident {}
| SITUATION CONSTRUCTOR {}
situation_type:
| OF situation_type_alt {}
| {}
qident:
| IDENT {}
2020-03-08 07:27:46 +03:00
| CONSTRUCTOR {}
2020-03-08 07:01:26 +03:00
| IDENT BANG IDENT {}
primitive_expression:
| NOW {}
2020-03-08 08:06:32 +03:00
date_qualifier:
| YEAR {}
2020-03-08 07:27:46 +03:00
literal:
| INT_LITERAL {}
2020-03-08 08:06:32 +03:00
| INT_LITERAL date_qualifier {}
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 {}
func:
| CARDINAL {}
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 {}
| func LPAREN separated_nonempty_list(COMMA, expression) RPAREN {}
2020-03-08 07:01:26 +03:00
| qident {}
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-03-08 08:06:32 +03:00
| base_expression mult_op base_expression {}
sum_op:
| PLUS {}
| MINUS {}
sum_expression:
| mult_expression {}
| mult_expression sum_op mult_expression {}
2020-03-08 07:12:12 +03:00
logical_op:
| AND {}
| OR {}
2020-03-08 07:01:26 +03:00
compare_expression:
2020-03-08 07:12:12 +03:00
| sum_expression {}
2020-03-08 07:01:26 +03:00
| sum_expression compare_op sum_expression {}
2020-03-08 07:12:12 +03:00
logical_expression:
| compare_expression {}
| compare_expression logical_op compare_expression {}
2020-03-08 07:01:26 +03:00
2020-03-08 08:06:32 +03:00
optional_binding:
| OF IDENT {}
| {}
match_arm:
| CONSTRUCTOR optional_binding COLON logical_expression {}
match_arms:
| ALT match_arm match_arms {}
| {}
2020-03-08 07:01:26 +03:00
expression:
| EXISTS IDENT IN qident SUCH THAT expression {}
2020-03-08 08:06:32 +03:00
| FORALL IDENT IN qident WE_HAVE expression {}
| MATCH expression WITH match_arms {}
2020-03-08 07:12:12 +03:00
| logical_expression {}
2020-03-08 07:01:26 +03:00
condition:
| CONDITION expression CONSEQUENCE {}
rule_definition:
| AS expression {}
| {}
rule:
2020-03-08 07:27:46 +03:00
| option(condition) qident DEFINED rule_definition {}
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 {}
| qident VARIES_WITH qident option(variation_type) {}
| EXISTS IDENT IN qident SUCH THAT assertion {}
| FORALL IDENT IN qident WE_HAVE assertion {}
constant:
| IDENT situation_type DEFINED AS literal {}
2020-03-08 06:28:45 +03:00
situation:
2020-03-08 07:01:26 +03:00
| DATA IDENT option(COLLECTION) situation_type {}
2020-03-08 07:27:46 +03:00
| RULE option(OPTIONAL) rule {}
2020-03-08 08:06:32 +03:00
| ASSERTION assertion {}
| CONSTANT constant {}
2020-03-08 06:28:45 +03:00
code_item:
2020-03-08 07:01:26 +03:00
| CHOICE IDENT COLON choices { }
| SITUATION CONSTRUCTOR SOURCE IDENT COLON separated_nonempty_list(SEMICOLON, situation) { }
2020-03-08 06:28:45 +03:00
code:
2020-03-08 07:01:26 +03:00
| code_item DOT 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 { [] }