2021-03-09 22:57:41 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
2021-05-27 19:56:47 +03:00
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria,
|
|
|
|
contributors: Denis Merigoux <denis.merigoux@inria.fr>, Emile Rolley
|
|
|
|
<emile.rolley@tuta.io>
|
2021-03-09 22:57:41 +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-04-30 10:59:09 +03:00
|
|
|
open Tokens
|
2021-03-09 22:57:41 +03:00
|
|
|
open Sedlexing
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2021-03-09 22:57:41 +03:00
|
|
|
module R = Re.Pcre
|
|
|
|
|
2021-03-10 00:04:36 +03:00
|
|
|
(* Calculates the precedence according a {!val: matched_regex} of the form :
|
|
|
|
'[#]+'.
|
2021-03-09 22:57:41 +03:00
|
|
|
|
2021-05-15 02:16:08 +03:00
|
|
|
@note -2 because [LAW_HEADING] start with at least "#" and the number of '#'
|
|
|
|
remaining corresponds to the precedence. *)
|
|
|
|
let calc_precedence (matched_regex : string) : int =
|
|
|
|
String.length matched_regex - 1
|
2021-03-09 22:57:41 +03:00
|
|
|
|
2021-03-09 23:01:24 +03:00
|
|
|
(* Gets the [LAW_HEADING] token from the current {!val: lexbuf} *)
|
2021-03-09 22:57:41 +03:00
|
|
|
let get_law_heading (lexbuf : lexbuf) : token =
|
2021-05-15 02:16:08 +03:00
|
|
|
let extract_article_title =
|
2022-09-07 18:14:22 +03:00
|
|
|
R.regexp "([#]+)\\s*([^\\|]+)(\\|\\s*([^\\s]+)|)(\\s*(\\[archive\\])|)"
|
2022-03-08 17:03:14 +03:00
|
|
|
in
|
2022-09-07 18:14:22 +03:00
|
|
|
let rex = R.exec ~rex:extract_article_title (Utf8.lexeme lexbuf) in
|
|
|
|
let title = String.trim (R.get_substring rex 2) in
|
2021-05-15 20:50:06 +03:00
|
|
|
let article_id =
|
2022-09-07 18:14:22 +03:00
|
|
|
try Some (String.trim (R.get_substring rex 4)) with Not_found -> None
|
2021-05-15 20:50:06 +03:00
|
|
|
in
|
2022-09-07 18:14:22 +03:00
|
|
|
let is_archive = Option.is_some (Re.Group.get_opt rex 6) in
|
|
|
|
let precedence = calc_precedence (String.trim (R.get_substring rex 1)) in
|
|
|
|
LAW_HEADING (title, article_id, is_archive, precedence)
|
2021-05-26 18:39:39 +03:00
|
|
|
|
2024-09-25 16:20:45 +03:00
|
|
|
type lexing_context = Law | Raw | Code | Directive | Directive_args | Inactive
|
2021-08-17 16:49:48 +03:00
|
|
|
|
2021-05-26 18:39:39 +03:00
|
|
|
(** Boolean reference, used by the lexer as the mutable state to distinguish
|
|
|
|
whether it is lexing code or law. *)
|
2024-09-25 16:20:45 +03:00
|
|
|
let context : lexing_context ref = ref Inactive
|
2021-05-26 18:39:39 +03:00
|
|
|
|
|
|
|
(** Mutable string reference that accumulates the string representation of the
|
|
|
|
body of code being lexed. This string representation is used in the literate
|
|
|
|
programming backends to faithfully capture the spacing pattern of the
|
|
|
|
original program *)
|
2024-09-25 16:20:45 +03:00
|
|
|
let code_buffer : Buffer.t option ref = ref None
|
|
|
|
|
|
|
|
let with_lexing_context filename f =
|
|
|
|
let saved_context = !context in
|
|
|
|
let saved_buffer = !code_buffer in
|
|
|
|
context := Law;
|
|
|
|
code_buffer := Some (Buffer.create 4000);
|
|
|
|
Fun.protect f ~finally:(fun () ->
|
|
|
|
if
|
|
|
|
!context <> Law
|
|
|
|
|| match !code_buffer with Some b -> Buffer.length b > 0 | _ -> false
|
|
|
|
then
|
|
|
|
Message.warning
|
|
|
|
"Unclosed block or missing newline at the end of file %a.@ Did you \
|
|
|
|
forget a @{<yellow>```@} ?"
|
|
|
|
File.format filename;
|
|
|
|
context := saved_context;
|
|
|
|
code_buffer := saved_buffer)
|
2021-05-26 18:39:39 +03:00
|
|
|
|
2021-08-17 16:49:48 +03:00
|
|
|
(** Updates {!val:code_buffer} with the current lexeme *)
|
|
|
|
let update_acc (lexbuf : lexbuf) : unit =
|
2024-09-25 16:20:45 +03:00
|
|
|
match !code_buffer with
|
|
|
|
| None ->
|
|
|
|
Message.error ~internal:true "Lexer update outside of a lexing context"
|
|
|
|
| Some buf -> Buffer.add_string buf (Utf8.lexeme lexbuf)
|
|
|
|
|
|
|
|
let flush_acc () =
|
|
|
|
match !code_buffer with
|
|
|
|
| None ->
|
|
|
|
Message.error ~internal:true "Lexer update outside of a lexing context"
|
|
|
|
| Some buf ->
|
|
|
|
let s = Buffer.contents buf in
|
|
|
|
Buffer.clear buf;
|
|
|
|
s
|
2021-05-26 18:39:39 +03:00
|
|
|
|
2024-07-30 16:20:51 +03:00
|
|
|
exception Lexing_error of (Pos.t * string)
|
|
|
|
|
2021-05-26 18:39:39 +03:00
|
|
|
(** Error-generating helper *)
|
|
|
|
let raise_lexer_error (loc : Pos.t) (token : string) =
|
2024-07-30 16:20:51 +03:00
|
|
|
raise (Lexing_error (loc, token))
|
2021-05-26 18:39:39 +03:00
|
|
|
|
|
|
|
(** Associative list matching each punctuation string part of the Catala syntax
|
|
|
|
with its {!module: Surface.Parser} token. Same for all the input languages
|
|
|
|
(English, French, etc.) *)
|
|
|
|
let token_list_language_agnostic : (string * token) list =
|
|
|
|
[
|
|
|
|
".", DOT;
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
"<=", LESSER_EQUAL KPoly;
|
|
|
|
">=", GREATER_EQUAL KPoly;
|
|
|
|
">", GREATER KPoly;
|
2021-05-26 18:39:39 +03:00
|
|
|
"!=", NOT_EQUAL;
|
|
|
|
"=", EQUAL;
|
|
|
|
"(", LPAREN;
|
|
|
|
")", RPAREN;
|
2022-12-15 13:48:48 +03:00
|
|
|
"{", LBRACE;
|
|
|
|
"}", RBRACE;
|
2021-05-26 18:39:39 +03:00
|
|
|
"{", LBRACKET;
|
|
|
|
"}", RBRACKET;
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
"+", PLUS KPoly;
|
|
|
|
"-", MINUS KPoly;
|
|
|
|
"*", MULT KPoly;
|
|
|
|
"/", DIV KPoly;
|
2021-05-26 18:39:39 +03:00
|
|
|
":", COLON;
|
|
|
|
";", SEMICOLON;
|
|
|
|
"--", ALT;
|
2021-08-19 19:26:06 +03:00
|
|
|
"++", PLUSPLUS;
|
2021-05-26 18:39:39 +03:00
|
|
|
]
|
|
|
|
|
2023-09-11 17:44:35 +03:00
|
|
|
type line_token =
|
|
|
|
| LINE_TEST of string (* ```catala-test { id = xx } *)
|
|
|
|
| LINE_INLINE_TEST (* ```catala-test-inline *)
|
|
|
|
| LINE_BLOCK_END (* ``` *)
|
|
|
|
| LINE_INCLUDE of string (* > Include foo.catala_en *)
|
2023-12-01 17:24:54 +03:00
|
|
|
| LINE_MODULE_DEF of string * bool (* > Module Xxx [external] *)
|
2023-09-11 17:44:35 +03:00
|
|
|
| LINE_MODULE_USE of string (* > Using Xxx [as Yyy] *)
|
|
|
|
| LINE_ANY (* anything else *)
|
|
|
|
|
2021-05-26 18:39:39 +03:00
|
|
|
module type LocalisedLexer = sig
|
|
|
|
val token_list : (string * Tokens.token) list
|
|
|
|
(** Same as {!val: token_list_language_agnostic}, but with tokens specialized
|
|
|
|
to a given language. *)
|
|
|
|
|
2021-08-19 19:26:06 +03:00
|
|
|
val lex_builtin : string -> Ast.builtin_expression option
|
|
|
|
(** Simple lexer for builtins *)
|
2021-05-26 18:39:39 +03:00
|
|
|
|
|
|
|
val lex_code : Sedlexing.lexbuf -> Tokens.token
|
|
|
|
(** Main lexing function used in code blocks *)
|
|
|
|
|
|
|
|
val lex_law : Sedlexing.lexbuf -> Tokens.token
|
|
|
|
(** Main lexing function used outside code blocks *)
|
|
|
|
|
|
|
|
val lexer : Sedlexing.lexbuf -> Tokens.token
|
2022-01-02 16:53:51 +03:00
|
|
|
(** Entry point of the lexer, distributes to {!val: lex_code} or
|
|
|
|
{!val:lex_law} depending of the current
|
|
|
|
{!val:Surface.Lexer_common.context}. *)
|
2023-09-11 17:44:35 +03:00
|
|
|
|
|
|
|
val lex_line : Sedlexing.lexbuf -> (string * line_token) option
|
|
|
|
(** Low-level lexer intended for dependency extraction *)
|
2021-05-26 18:39:39 +03:00
|
|
|
end
|