2021-05-09 23:55:50 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax and social benefits
|
2021-05-27 19:56:47 +03:00
|
|
|
computation rules. Copyright (C) 2020 Inria, contributors: Denis Merigoux
|
|
|
|
<denis.merigoux@inria.fr>, Emile Rolley <emile.rolley@tuta.io>
|
2021-05-09 23:55:50 +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. *)
|
|
|
|
|
|
|
|
open Tokens
|
|
|
|
open Sedlexing
|
|
|
|
open Utils
|
2021-05-26 18:39:39 +03:00
|
|
|
module L = Lexer_common
|
2021-05-09 23:55:50 +03:00
|
|
|
module R = Re.Pcre
|
|
|
|
|
2021-08-19 19:26:06 +03:00
|
|
|
(* The localised strings and regexps for the tokens and specific parsing rules
|
|
|
|
are defined as CPPO macros in the `lexer_XX.cppo.ml` files.
|
|
|
|
|
|
|
|
- `MS_*` macros define token strings
|
|
|
|
- `MR_*` are sedlex regexps matching the token (inferred from the strings if absent,
|
|
|
|
but should be present for any token containing spacing, and for any non-latin1
|
|
|
|
character)
|
|
|
|
- `MX_*` are full matching rules of the form `sedlex regexp -> ocaml expression`
|
|
|
|
*)
|
|
|
|
|
|
|
|
(* Avoid the need for defining the regexps when they are simple strings *)
|
|
|
|
#ifndef MR_SCOPE
|
|
|
|
#define MR_SCOPE MS_SCOPE
|
|
|
|
#endif
|
|
|
|
#ifndef MR_CONSEQUENCE
|
|
|
|
#define MR_CONSEQUENCE MS_CONSEQUENCE
|
|
|
|
#endif
|
|
|
|
#ifndef MR_DATA
|
|
|
|
#define MR_DATA MS_DATA
|
|
|
|
#endif
|
|
|
|
#ifndef MR_DEPENDS
|
|
|
|
#define MR_DEPENDS MS_DEPENDS
|
|
|
|
#endif
|
|
|
|
#ifndef MR_DECLARATION
|
|
|
|
#define MR_DECLARATION MS_DECLARATION
|
|
|
|
#endif
|
|
|
|
#ifndef MR_CONTEXT
|
|
|
|
#define MR_CONTEXT MS_CONTEXT
|
|
|
|
#endif
|
|
|
|
#ifndef MR_DECREASING
|
|
|
|
#define MR_DECREASING MS_DECREASING
|
|
|
|
#endif
|
|
|
|
#ifndef MR_INCREASING
|
|
|
|
#define MR_INCREASING MS_INCREASING
|
|
|
|
#endif
|
|
|
|
#ifndef MR_OF
|
|
|
|
#define MR_OF MS_OF
|
|
|
|
#endif
|
|
|
|
#ifndef MR_COLLECTION
|
|
|
|
#define MR_COLLECTION MS_COLLECTION
|
|
|
|
#endif
|
|
|
|
#ifndef MR_ENUM
|
|
|
|
#define MR_ENUM MS_ENUM
|
|
|
|
#endif
|
|
|
|
#ifndef MR_INTEGER
|
|
|
|
#define MR_INTEGER MS_INTEGER
|
|
|
|
#endif
|
|
|
|
#ifndef MR_MONEY
|
|
|
|
#define MR_MONEY MS_MONEY
|
|
|
|
#endif
|
|
|
|
#ifndef MR_TEXT
|
|
|
|
#define MR_TEXT MS_TEXT
|
|
|
|
#endif
|
|
|
|
#ifndef MR_DECIMAL
|
|
|
|
#define MR_DECIMAL MS_DECIMAL
|
|
|
|
#endif
|
|
|
|
#ifndef MR_DATE
|
|
|
|
#define MR_DATE MS_DATE
|
|
|
|
#endif
|
|
|
|
#ifndef MR_DURATION
|
|
|
|
#define MR_DURATION MS_DURATION
|
|
|
|
#endif
|
|
|
|
#ifndef MR_BOOLEAN
|
|
|
|
#define MR_BOOLEAN MS_BOOLEAN
|
|
|
|
#endif
|
|
|
|
#ifndef MR_SUM
|
|
|
|
#define MR_SUM MS_SUM
|
|
|
|
#endif
|
|
|
|
#ifndef MR_FILLED
|
|
|
|
#define MR_FILLED MS_FILLED
|
|
|
|
#endif
|
|
|
|
#ifndef MR_DEFINITION
|
|
|
|
#define MR_DEFINITION MS_DEFINITION
|
|
|
|
#endif
|
2022-02-28 16:33:07 +03:00
|
|
|
#ifndef MR_STATE
|
|
|
|
#define MR_STATE MS_STATE
|
|
|
|
#endif
|
2021-08-19 19:26:06 +03:00
|
|
|
#ifndef MR_LABEL
|
|
|
|
#define MR_LABEL MS_LABEL
|
|
|
|
#endif
|
|
|
|
#ifndef MR_EXCEPTION
|
|
|
|
#define MR_EXCEPTION MS_EXCEPTION
|
|
|
|
#endif
|
|
|
|
#ifndef MR_DEFINED_AS
|
|
|
|
#define MR_DEFINED_AS MS_DEFINED_AS
|
|
|
|
#endif
|
|
|
|
#ifndef MR_MATCH
|
|
|
|
#define MR_MATCH MS_MATCH
|
|
|
|
#endif
|
|
|
|
#ifndef MR_WILDCARD
|
|
|
|
#define MR_WILDCARD MS_WILDCARD
|
|
|
|
#endif
|
|
|
|
#ifndef MR_WITH
|
|
|
|
#define MR_WITH MS_WITH
|
|
|
|
#endif
|
|
|
|
#ifndef MR_UNDER_CONDITION
|
|
|
|
#define MR_UNDER_CONDITION MS_UNDER_CONDITION
|
|
|
|
#endif
|
|
|
|
#ifndef MR_IF
|
|
|
|
#define MR_IF MS_IF
|
|
|
|
#endif
|
|
|
|
#ifndef MR_THEN
|
|
|
|
#define MR_THEN MS_THEN
|
|
|
|
#endif
|
|
|
|
#ifndef MR_ELSE
|
|
|
|
#define MR_ELSE MS_ELSE
|
|
|
|
#endif
|
|
|
|
#ifndef MR_CONDITION
|
|
|
|
#define MR_CONDITION MS_CONDITION
|
|
|
|
#endif
|
|
|
|
#ifndef MR_CONTENT
|
|
|
|
#define MR_CONTENT MS_CONTENT
|
|
|
|
#endif
|
|
|
|
#ifndef MR_STRUCT
|
|
|
|
#define MR_STRUCT MS_STRUCT
|
|
|
|
#endif
|
|
|
|
#ifndef MR_ASSERTION
|
|
|
|
#define MR_ASSERTION MS_ASSERTION
|
|
|
|
#endif
|
|
|
|
#ifndef MR_VARIES
|
|
|
|
#define MR_VARIES MS_VARIES
|
|
|
|
#endif
|
|
|
|
#ifndef MR_WITH_V
|
|
|
|
#define MR_WITH_V MS_WITH_V
|
|
|
|
#endif
|
|
|
|
#ifndef MR_FOR
|
|
|
|
#define MR_FOR MS_FOR
|
|
|
|
#endif
|
|
|
|
#ifndef MR_ALL
|
|
|
|
#define MR_ALL MS_ALL
|
|
|
|
#endif
|
|
|
|
#ifndef MR_WE_HAVE
|
|
|
|
#define MR_WE_HAVE MS_WE_HAVE
|
|
|
|
#endif
|
|
|
|
#ifndef MR_FIXED
|
|
|
|
#define MR_FIXED MS_FIXED
|
|
|
|
#endif
|
|
|
|
#ifndef MR_BY
|
|
|
|
#define MR_BY MS_BY
|
|
|
|
#endif
|
|
|
|
#ifndef MR_RULE
|
|
|
|
#define MR_RULE MS_RULE
|
|
|
|
#endif
|
|
|
|
#ifndef MR_EXISTS
|
|
|
|
#define MR_EXISTS MS_EXISTS
|
|
|
|
#endif
|
|
|
|
#ifndef MR_IN
|
|
|
|
#define MR_IN MS_IN
|
|
|
|
#endif
|
|
|
|
#ifndef MR_SUCH
|
|
|
|
#define MR_SUCH MS_SUCH
|
|
|
|
#endif
|
|
|
|
#ifndef MR_THAT
|
|
|
|
#define MR_THAT MS_THAT
|
|
|
|
#endif
|
|
|
|
#ifndef MR_AND
|
|
|
|
#define MR_AND MS_AND
|
|
|
|
#endif
|
|
|
|
#ifndef MR_OR
|
|
|
|
#define MR_OR MS_OR
|
|
|
|
#endif
|
|
|
|
#ifndef MR_XOR
|
|
|
|
#define MR_XOR MS_XOR
|
|
|
|
#endif
|
|
|
|
#ifndef MR_NOT
|
|
|
|
#define MR_NOT MS_NOT
|
|
|
|
#endif
|
|
|
|
#ifndef MR_MAXIMUM
|
|
|
|
#define MR_MAXIMUM MS_MAXIMUM
|
|
|
|
#endif
|
|
|
|
#ifndef MR_MINIMUM
|
|
|
|
#define MR_MINIMUM MS_MINIMUM
|
|
|
|
#endif
|
|
|
|
#ifndef MR_FILTER
|
|
|
|
#define MR_FILTER MS_FILTER
|
|
|
|
#endif
|
|
|
|
#ifndef MR_MAP
|
|
|
|
#define MR_MAP MS_MAP
|
|
|
|
#endif
|
|
|
|
#ifndef MR_INIT
|
|
|
|
#define MR_INIT MS_INIT
|
|
|
|
#endif
|
|
|
|
#ifndef MR_CARDINAL
|
|
|
|
#define MR_CARDINAL MS_CARDINAL
|
|
|
|
#endif
|
|
|
|
#ifndef MR_YEAR
|
|
|
|
#define MR_YEAR MS_YEAR
|
|
|
|
#endif
|
|
|
|
#ifndef MR_MONTH
|
|
|
|
#define MR_MONTH MS_MONTH
|
|
|
|
#endif
|
|
|
|
#ifndef MR_DAY
|
|
|
|
#define MR_DAY MS_DAY
|
|
|
|
#endif
|
|
|
|
#ifndef MR_TRUE
|
|
|
|
#define MR_TRUE MS_TRUE
|
|
|
|
#endif
|
|
|
|
#ifndef MR_FALSE
|
|
|
|
#define MR_FALSE MS_FALSE
|
|
|
|
#endif
|
|
|
|
#ifndef MR_IntToDec
|
|
|
|
#define MR_IntToDec MS_IntToDec
|
|
|
|
#endif
|
2022-07-19 12:48:27 +03:00
|
|
|
#ifndef MR_MoneyToDec
|
|
|
|
#define MR_MoneyToDec MS_MoneyToDec
|
|
|
|
#endif
|
|
|
|
#ifndef MR_DecToMoney
|
|
|
|
#define MR_DecToMoney MS_DecToMoney
|
|
|
|
#endif
|
2022-03-17 14:30:14 +03:00
|
|
|
#ifndef MR_RoundMoney
|
|
|
|
#define MR_RoundMoney MS_RoundMoney
|
|
|
|
#endif
|
2022-04-29 22:18:15 +03:00
|
|
|
#ifndef MR_RoundDecimal
|
|
|
|
#define MR_RoundDecimal MS_RoundDecimal
|
|
|
|
#endif
|
2021-08-19 19:26:06 +03:00
|
|
|
#ifndef MR_GetDay
|
|
|
|
#define MR_GetDay MS_GetDay
|
|
|
|
#endif
|
|
|
|
#ifndef MR_GetMonth
|
|
|
|
#define MR_GetMonth MS_GetMonth
|
|
|
|
#endif
|
|
|
|
#ifndef MR_GetYear
|
|
|
|
#define MR_GetYear MS_GetYear
|
|
|
|
#endif
|
2022-07-21 15:11:56 +03:00
|
|
|
#ifndef MR_FirstDayOfMonth
|
|
|
|
#define MR_FirstDayOfMonth MS_FirstDayOfMonth
|
|
|
|
#endif
|
|
|
|
#ifndef MR_LastDayOfMonth
|
|
|
|
#define MR_LastDayOfMonth MS_LastDayOfMonth
|
|
|
|
#endif
|
2022-01-27 20:03:47 +03:00
|
|
|
#ifndef MR_INPUT
|
|
|
|
#define MR_INPUT MS_INPUT
|
|
|
|
#endif
|
|
|
|
#ifndef MR_OUTPUT
|
|
|
|
#define MR_OUTPUT MS_OUTPUT
|
|
|
|
#endif
|
|
|
|
#ifndef MR_INTERNAL
|
|
|
|
#define MR_INTERNAL MS_INTERNAL
|
|
|
|
#endif
|
2021-08-19 19:26:06 +03:00
|
|
|
|
2021-05-09 23:55:50 +03:00
|
|
|
let token_list : (string * token) list =
|
|
|
|
[
|
2021-08-19 19:26:06 +03:00
|
|
|
(MS_SCOPE, SCOPE);
|
|
|
|
(MS_CONSEQUENCE, CONSEQUENCE);
|
|
|
|
(MS_DATA, DATA);
|
|
|
|
(MS_DEPENDS, DEPENDS);
|
|
|
|
(MS_DECLARATION, DECLARATION);
|
|
|
|
(MS_CONTEXT, CONTEXT);
|
|
|
|
(MS_DECREASING, DECREASING);
|
|
|
|
(MS_INCREASING, INCREASING);
|
|
|
|
(MS_OF, OF);
|
|
|
|
(MS_COLLECTION, COLLECTION);
|
|
|
|
(MS_ENUM, ENUM);
|
|
|
|
(MS_INTEGER, INTEGER);
|
|
|
|
(MS_MONEY, MONEY);
|
|
|
|
(MS_TEXT, TEXT);
|
|
|
|
(MS_DECIMAL, DECIMAL);
|
|
|
|
(MS_DATE, DATE);
|
|
|
|
(MS_DURATION, DURATION);
|
|
|
|
(MS_BOOLEAN, BOOLEAN);
|
|
|
|
(MS_SUM, SUM);
|
|
|
|
(MS_FILLED, FILLED);
|
|
|
|
(MS_DEFINITION, DEFINITION);
|
2022-02-28 16:33:07 +03:00
|
|
|
(MS_STATE, STATE);
|
2021-08-19 19:26:06 +03:00
|
|
|
(MS_LABEL, LABEL);
|
|
|
|
(MS_EXCEPTION, EXCEPTION);
|
|
|
|
(MS_DEFINED_AS, DEFINED_AS);
|
|
|
|
(MS_MATCH, MATCH);
|
|
|
|
(MS_WILDCARD, WILDCARD);
|
|
|
|
(MS_WITH, WITH);
|
|
|
|
(MS_UNDER_CONDITION, UNDER_CONDITION);
|
|
|
|
(MS_IF, IF);
|
|
|
|
(MS_THEN, THEN);
|
|
|
|
(MS_ELSE, ELSE);
|
|
|
|
(MS_CONDITION, CONDITION);
|
|
|
|
(MS_CONTENT, CONTENT);
|
|
|
|
(MS_STRUCT, STRUCT);
|
|
|
|
(MS_ASSERTION, ASSERTION);
|
|
|
|
(MS_VARIES, VARIES);
|
|
|
|
(MS_WITH_V, WITH_V);
|
|
|
|
(MS_FOR, FOR);
|
|
|
|
(MS_ALL, ALL);
|
|
|
|
(MS_WE_HAVE, WE_HAVE);
|
|
|
|
(MS_FIXED, FIXED);
|
|
|
|
(MS_BY, BY);
|
|
|
|
(MS_RULE, RULE);
|
|
|
|
(MS_EXISTS, EXISTS);
|
|
|
|
(MS_IN, IN);
|
|
|
|
(MS_SUCH, SUCH);
|
|
|
|
(MS_THAT, THAT);
|
|
|
|
(MS_AND, AND);
|
|
|
|
(MS_OR, OR);
|
|
|
|
(MS_XOR, XOR);
|
|
|
|
(MS_NOT, NOT);
|
|
|
|
(MS_MAXIMUM, MAXIMUM);
|
|
|
|
(MS_MINIMUM, MINIMUM);
|
|
|
|
(MS_FILTER, FILTER);
|
|
|
|
(MS_MAP, MAP);
|
|
|
|
(MS_INIT, INIT);
|
|
|
|
(MS_CARDINAL, CARDINAL);
|
|
|
|
(MS_YEAR, YEAR);
|
|
|
|
(MS_MONTH, MONTH);
|
|
|
|
(MS_DAY, DAY);
|
|
|
|
(MS_TRUE, TRUE);
|
|
|
|
(MS_FALSE, FALSE);
|
2022-01-27 20:03:47 +03:00
|
|
|
(MS_INPUT, INPUT);
|
|
|
|
(MS_OUTPUT, OUTPUT);
|
|
|
|
(MS_INTERNAL, INTERNAL)
|
2021-05-09 23:55:50 +03:00
|
|
|
]
|
|
|
|
@ L.token_list_language_agnostic
|
|
|
|
|
2021-08-19 19:26:06 +03:00
|
|
|
(** Localised builtin functions *)
|
|
|
|
let lex_builtin (s : string) : Ast.builtin_expression option =
|
|
|
|
let lexbuf = Utf8.from_string s in
|
|
|
|
match%sedlex lexbuf with
|
2021-08-19 21:41:34 +03:00
|
|
|
| MR_IntToDec, eof -> Some IntToDec
|
2022-07-19 12:48:27 +03:00
|
|
|
| MR_DecToMoney, eof -> Some DecToMoney
|
|
|
|
| MR_MoneyToDec, eof -> Some MoneyToDec
|
2021-08-19 21:41:34 +03:00
|
|
|
| MR_GetDay, eof -> Some GetDay
|
|
|
|
| MR_GetMonth, eof -> Some GetMonth
|
|
|
|
| MR_GetYear, eof -> Some GetYear
|
2022-07-21 15:11:56 +03:00
|
|
|
| MR_FirstDayOfMonth -> Some FirstDayOfMonth
|
|
|
|
| MR_LastDayOfMonth -> Some LastDayOfMonth
|
2022-03-17 14:30:14 +03:00
|
|
|
| MR_RoundMoney, eof -> Some RoundMoney
|
2022-04-29 22:18:15 +03:00
|
|
|
| MR_RoundDecimal, eof -> Some RoundDecimal
|
2021-08-19 19:26:06 +03:00
|
|
|
| _ -> None
|
2021-05-09 23:55:50 +03:00
|
|
|
|
2021-05-24 17:23:45 +03:00
|
|
|
(** Regexp matching any digit character.
|
|
|
|
|
|
|
|
@note can not be used outside the current module (@see <
|
|
|
|
https://github.com/ocaml-community/sedlex#lexer-specifications >). *)
|
|
|
|
let digit = [%sedlex.regexp? '0' .. '9']
|
|
|
|
|
2021-06-03 16:47:48 +03:00
|
|
|
(** Regexp matching at least one space. *)
|
|
|
|
let space_plus = [%sedlex.regexp? Plus white_space]
|
|
|
|
|
2021-08-17 16:49:48 +03:00
|
|
|
(** Regexp matching white space but not newlines *)
|
|
|
|
let hspace = [%sedlex.regexp? Sub (white_space, Chars "\n\r")]
|
|
|
|
|
2021-05-09 23:55:50 +03:00
|
|
|
(** Main lexing function used in code blocks *)
|
|
|
|
let rec lex_code (lexbuf : lexbuf) : token =
|
|
|
|
let prev_lexeme = Utf8.lexeme lexbuf in
|
|
|
|
let prev_pos = lexing_positions lexbuf in
|
|
|
|
match%sedlex lexbuf with
|
|
|
|
| white_space ->
|
|
|
|
(* Whitespaces *)
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
lex_code lexbuf
|
|
|
|
| '#', Star (Compl '\n'), '\n' ->
|
|
|
|
(* Comments *)
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
lex_code lexbuf
|
|
|
|
| "```" ->
|
|
|
|
(* End of code section *)
|
2021-08-17 16:49:48 +03:00
|
|
|
L.context := Law;
|
|
|
|
END_CODE (Buffer.contents L.code_buffer)
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_SCOPE ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
SCOPE
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_DATA ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
DATA
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_DEPENDS ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
DEPENDS
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_DECLARATION ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
DECLARATION
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_CONTEXT ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
CONTEXT
|
2022-02-04 17:10:47 +03:00
|
|
|
| MR_INPUT ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
INPUT
|
|
|
|
| MR_OUTPUT ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
OUTPUT
|
|
|
|
| MR_INTERNAL ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
INTERNAL
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_DECREASING ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
DECREASING
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_INCREASING ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
INCREASING
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_OF ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
OF
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_COLLECTION ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
COLLECTION
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_ENUM ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
ENUM
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_INTEGER ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
INTEGER
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_MONEY ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
MONEY
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_TEXT ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
TEXT
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_DECIMAL ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
DECIMAL
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_DATE ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
DATE
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_DURATION ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
DURATION
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_BOOLEAN ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
BOOLEAN
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_SUM ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
SUM
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_FILLED ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
FILLED
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_DEFINITION ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
DEFINITION
|
2022-02-28 16:33:07 +03:00
|
|
|
| MR_STATE ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
STATE
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_LABEL ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
LABEL
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_EXCEPTION ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
EXCEPTION
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_DEFINED_AS ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
DEFINED_AS
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_MATCH ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
MATCH
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_WITH ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
WITH
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_WILDCARD ->
|
2021-06-03 16:33:40 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
WILDCARD
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_UNDER_CONDITION ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
UNDER_CONDITION
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_IF ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
IF
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_CONSEQUENCE ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
CONSEQUENCE
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_THEN ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
THEN
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_ELSE ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
ELSE
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_CONDITION ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
CONDITION
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_CONTENT ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
CONTENT
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_STRUCT ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
STRUCT
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_ASSERTION ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
ASSERTION
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_VARIES ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
VARIES
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_WITH_V ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
WITH_V
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_FOR ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
FOR
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_ALL ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
ALL
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_WE_HAVE ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
WE_HAVE
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_FIXED ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
FIXED
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_BY ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
BY
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_RULE ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
RULE
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_EXISTS ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
EXISTS
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_IN ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
IN
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_SUCH ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
SUCH
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_THAT ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
THAT
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_AND ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
AND
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_OR ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
OR
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_XOR ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
XOR
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_NOT ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
NOT
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_MAXIMUM ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
MAXIMUM
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_MINIMUM ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
MINIMUM
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_FILTER ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
FILTER
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_MAP ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
MAP
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_INIT ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
INIT
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_CARDINAL ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
CARDINAL
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_TRUE ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
TRUE
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_FALSE ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
FALSE
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_YEAR ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
YEAR
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_MONTH ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
MONTH
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_DAY ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
DAY
|
2021-08-20 15:23:10 +03:00
|
|
|
| MR_MONEY_PREFIX, digit, Opt (Star (digit | MR_MONEY_DELIM), digit), Opt (MC_DECIMAL_SEPARATOR, Rep (digit, 0 .. 2)), MR_MONEY_SUFFIX ->
|
|
|
|
let s = Utf8.lexeme lexbuf in
|
|
|
|
let units = Buffer.create (String.length s) in
|
|
|
|
let cents = Buffer.create 2 in
|
|
|
|
let buf = ref units in
|
|
|
|
for i = 0 to String.length s - 1 do
|
|
|
|
match s.[i] with
|
|
|
|
| '0'..'9' as c -> Buffer.add_char !buf c
|
|
|
|
| MC_DECIMAL_SEPARATOR -> buf := cents
|
|
|
|
| _ -> ()
|
|
|
|
done;
|
2022-05-06 11:29:39 +03:00
|
|
|
(* If the user has written $0.3 it means 30 cents so we have to pad
|
|
|
|
with a 0 *)
|
|
|
|
Buffer.add_string cents (String.make (2 - Buffer.length cents) '0');
|
2021-08-20 15:23:10 +03:00
|
|
|
L.update_acc lexbuf;
|
2022-04-22 15:26:28 +03:00
|
|
|
MONEY_AMOUNT (Buffer.contents units, Buffer.contents cents)
|
2021-08-20 15:23:10 +03:00
|
|
|
| Plus digit, MC_DECIMAL_SEPARATOR, Star digit ->
|
2021-08-19 21:32:23 +03:00
|
|
|
let rex =
|
|
|
|
Re.(compile @@ whole_string @@ seq [
|
|
|
|
group (rep1 digit);
|
2021-08-20 15:23:10 +03:00
|
|
|
char MC_DECIMAL_SEPARATOR;
|
2021-08-19 21:32:23 +03:00
|
|
|
group (rep digit)
|
|
|
|
]) in
|
|
|
|
let dec_parts = R.get_substring (R.exec ~rex (Utf8.lexeme lexbuf)) in
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
DECIMAL_LITERAL
|
2022-04-22 15:26:28 +03:00
|
|
|
(dec_parts 1, dec_parts 2)
|
2021-05-09 23:55:50 +03:00
|
|
|
| "<=@" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
LESSER_EQUAL_DATE
|
|
|
|
| "<@" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
LESSER_DATE
|
|
|
|
| ">=@" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
GREATER_EQUAL_DATE
|
|
|
|
| ">@" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
GREATER_DATE
|
|
|
|
| "-@" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
MINUSDATE
|
|
|
|
| "+@" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
PLUSDATE
|
|
|
|
| "<=^" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
LESSER_EQUAL_DURATION
|
|
|
|
| "<^" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
LESSER_DURATION
|
|
|
|
| ">=^" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
GREATER_EQUAL_DURATION
|
|
|
|
| ">^" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
GREATER_DURATION
|
|
|
|
| "+^" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
PLUSDURATION
|
|
|
|
| "-^" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
MINUSDURATION
|
2021-05-30 14:21:28 +03:00
|
|
|
| "/^" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
DIVDURATION
|
2022-05-31 21:00:52 +03:00
|
|
|
| "*^" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
MULDURATION
|
2021-08-19 19:26:06 +03:00
|
|
|
| "<=", MR_MONEY_OP_SUFFIX ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
LESSER_EQUAL_MONEY
|
2021-08-19 19:26:06 +03:00
|
|
|
| '<', MR_MONEY_OP_SUFFIX ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
LESSER_MONEY
|
2021-08-19 19:26:06 +03:00
|
|
|
| ">=", MR_MONEY_OP_SUFFIX ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
GREATER_EQUAL_MONEY
|
2021-08-19 19:26:06 +03:00
|
|
|
| '>', MR_MONEY_OP_SUFFIX ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
GREATER_MONEY
|
2021-08-19 19:26:06 +03:00
|
|
|
| '+', MR_MONEY_OP_SUFFIX ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
PLUSMONEY
|
2021-08-19 19:26:06 +03:00
|
|
|
| '-', MR_MONEY_OP_SUFFIX ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
MINUSMONEY
|
2021-08-19 19:26:06 +03:00
|
|
|
| '*', MR_MONEY_OP_SUFFIX ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
MULTMONEY
|
2021-08-19 19:26:06 +03:00
|
|
|
| '/', MR_MONEY_OP_SUFFIX ->
|
2021-05-09 23:55:50 +03:00
|
|
|
L.update_acc lexbuf;
|
|
|
|
DIVMONEY
|
|
|
|
| "<=." ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
LESSER_EQUAL_DEC
|
|
|
|
| "<." ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
LESSER_DEC
|
|
|
|
| ">=." ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
GREATER_EQUAL_DEC
|
|
|
|
| ">." ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
GREATER_DEC
|
|
|
|
| "+." ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
PLUSDEC
|
|
|
|
| "-." ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
MINUSDEC
|
|
|
|
| "*." ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
MULTDEC
|
|
|
|
| "/." ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
DIVDEC
|
|
|
|
| "<=" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
LESSER_EQUAL
|
|
|
|
| '<' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
LESSER
|
|
|
|
| ">=" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
GREATER_EQUAL
|
|
|
|
| '>' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
GREATER
|
|
|
|
| '+' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
PLUS
|
|
|
|
| '-' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
MINUS
|
|
|
|
| '*' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
MULT
|
|
|
|
| '/' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
DIV
|
|
|
|
| "!=" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
NOT_EQUAL
|
|
|
|
| '=' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
EQUAL
|
|
|
|
| '%' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
PERCENT
|
|
|
|
| '(' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
LPAREN
|
|
|
|
| ')' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
RPAREN
|
|
|
|
| '{' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
LBRACKET
|
|
|
|
| '}' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
RBRACKET
|
|
|
|
| '[' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
LSQUARE
|
|
|
|
| ']' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
RSQUARE
|
|
|
|
| '|' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
VERTICAL
|
|
|
|
| ':' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
COLON
|
|
|
|
| ';' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
SEMICOLON
|
|
|
|
| "--" ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
ALT
|
2021-07-08 17:27:46 +03:00
|
|
|
| "++" ->
|
|
|
|
L.update_acc lexbuf;
|
2021-08-19 19:26:06 +03:00
|
|
|
PLUSPLUS
|
2021-05-09 23:55:50 +03:00
|
|
|
| '.' ->
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
DOT
|
2021-05-24 17:23:45 +03:00
|
|
|
| uppercase, Star (uppercase | lowercase | digit | '_' | '\'') ->
|
2021-05-09 23:55:50 +03:00
|
|
|
(* Name of constructor *)
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
CONSTRUCTOR (Utf8.lexeme lexbuf)
|
2021-05-24 17:23:45 +03:00
|
|
|
| lowercase, Star (lowercase | uppercase | digit | '_' | '\'') ->
|
2021-05-09 23:55:50 +03:00
|
|
|
(* Name of variable *)
|
|
|
|
L.update_acc lexbuf;
|
|
|
|
IDENT (Utf8.lexeme lexbuf)
|
2021-05-24 17:23:45 +03:00
|
|
|
| Plus digit ->
|
2021-05-09 23:55:50 +03:00
|
|
|
(* Integer literal*)
|
|
|
|
L.update_acc lexbuf;
|
2022-04-22 15:26:28 +03:00
|
|
|
INT_LITERAL (Utf8.lexeme lexbuf)
|
2021-05-09 23:55:50 +03:00
|
|
|
| _ -> L.raise_lexer_error (Pos.from_lpos prev_pos) prev_lexeme
|
|
|
|
|
2021-08-17 16:49:48 +03:00
|
|
|
let rec lex_directive_args (lexbuf : lexbuf) : token =
|
2021-05-09 23:55:50 +03:00
|
|
|
let prev_lexeme = Utf8.lexeme lexbuf in
|
|
|
|
let prev_pos = lexing_positions lexbuf in
|
|
|
|
match%sedlex lexbuf with
|
2021-08-17 16:49:48 +03:00
|
|
|
| '@', Star hspace, "p.", Star hspace, Plus digit ->
|
|
|
|
let s = Utf8.lexeme lexbuf in
|
|
|
|
let i = String.index s '.' in
|
|
|
|
AT_PAGE (int_of_string (String.trim (String.sub s i (String.length s - i))))
|
2021-08-18 19:20:56 +03:00
|
|
|
| Plus (Compl white_space) -> DIRECTIVE_ARG (Utf8.lexeme lexbuf)
|
2021-08-17 16:49:48 +03:00
|
|
|
| Plus hspace -> lex_directive_args lexbuf
|
|
|
|
| '\n' | eof ->
|
|
|
|
L.context := Law;
|
|
|
|
END_DIRECTIVE
|
|
|
|
| _ -> L.raise_lexer_error (Pos.from_lpos prev_pos) prev_lexeme
|
2021-05-09 23:55:50 +03:00
|
|
|
|
2021-08-17 16:49:48 +03:00
|
|
|
let rec lex_directive (lexbuf : lexbuf) : token =
|
|
|
|
let prev_lexeme = Utf8.lexeme lexbuf in
|
|
|
|
let prev_pos = lexing_positions lexbuf in
|
|
|
|
match%sedlex lexbuf with
|
|
|
|
| Plus hspace -> lex_directive lexbuf
|
2021-08-19 19:26:06 +03:00
|
|
|
| MR_LAW_INCLUDE -> LAW_INCLUDE
|
2021-08-17 16:49:48 +03:00
|
|
|
| ":" ->
|
|
|
|
L.context := Directive_args;
|
|
|
|
COLON
|
|
|
|
| '\n' | eof ->
|
|
|
|
L.context := Law;
|
|
|
|
END_DIRECTIVE
|
2021-05-09 23:55:50 +03:00
|
|
|
| _ -> L.raise_lexer_error (Pos.from_lpos prev_pos) prev_lexeme
|
|
|
|
|
2021-08-17 16:49:48 +03:00
|
|
|
(** Main lexing function used outside code blocks *)
|
2021-08-18 19:20:56 +03:00
|
|
|
let lex_law (lexbuf : lexbuf) : token =
|
2021-08-17 16:49:48 +03:00
|
|
|
let prev_lexeme = Utf8.lexeme lexbuf in
|
|
|
|
let ((_, start_pos) as prev_pos) = lexing_positions lexbuf in
|
|
|
|
let at_bol = Lexing.(start_pos.pos_bol = start_pos.pos_cnum) in
|
|
|
|
if at_bol then
|
|
|
|
match%sedlex lexbuf with
|
|
|
|
| eof -> EOF
|
2021-10-01 11:01:25 +03:00
|
|
|
| "```catala", Star white_space, ('\n' | eof) ->
|
2021-08-17 16:49:48 +03:00
|
|
|
L.context := Code;
|
|
|
|
Buffer.clear L.code_buffer;
|
|
|
|
BEGIN_CODE
|
2021-10-01 11:01:25 +03:00
|
|
|
| "```catala-metadata", Star white_space, ('\n' | eof) ->
|
|
|
|
L.context := Code;
|
|
|
|
Buffer.clear L.code_buffer;
|
|
|
|
BEGIN_METADATA
|
2021-08-18 19:20:56 +03:00
|
|
|
| '>' ->
|
2021-08-17 16:49:48 +03:00
|
|
|
L.context := Directive;
|
|
|
|
BEGIN_DIRECTIVE
|
|
|
|
| Plus '#', Star hspace, Plus (Compl '\n'), Star hspace, ('\n' | eof) ->
|
|
|
|
L.get_law_heading lexbuf
|
|
|
|
| _ -> (
|
2021-08-18 19:20:56 +03:00
|
|
|
(* Nested match for lower priority; `_` matches length 0 so we effectively retry the
|
|
|
|
sub-match at the same point *)
|
2021-08-17 16:49:48 +03:00
|
|
|
let lexbuf = lexbuf in
|
|
|
|
(* workaround sedlex bug, see https://github.com/ocaml-community/sedlex/issues/12 *)
|
|
|
|
match%sedlex lexbuf with
|
|
|
|
| Star (Compl '\n'), ('\n' | eof) -> LAW_TEXT (Utf8.lexeme lexbuf)
|
|
|
|
| _ -> L.raise_lexer_error (Pos.from_lpos prev_pos) prev_lexeme)
|
|
|
|
else
|
|
|
|
match%sedlex lexbuf with
|
|
|
|
| eof -> EOF
|
|
|
|
| Star (Compl '\n'), ('\n' | eof) -> LAW_TEXT (Utf8.lexeme lexbuf)
|
|
|
|
| _ -> L.raise_lexer_error (Pos.from_lpos prev_pos) prev_lexeme
|
|
|
|
|
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}. *)
|
2021-08-17 16:49:48 +03:00
|
|
|
let lexer (lexbuf : lexbuf) : token =
|
|
|
|
match !L.context with
|
|
|
|
| Law -> lex_law lexbuf
|
|
|
|
| Code -> lex_code lexbuf
|
|
|
|
| Directive -> lex_directive lexbuf
|
|
|
|
| Directive_args -> lex_directive_args lexbuf
|