| 1 | (************************************************************************) |
| 2 | (* * The Rocq Prover / The Rocq Development Team *) |
| 3 | (* v * Copyright INRIA, CNRS and contributors *) |
| 4 | (* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 | (* \VV/ **************************************************************) |
| 6 | (* // * This file is distributed under the terms of the *) |
| 7 | (* * GNU Lesser General Public License Version 2.1 *) |
| 8 | (* * (see LICENSE file for the text of the license) *) |
| 9 | (************************************************************************) |
| 10 | |
| 11 | (** When one registers a keyword she can declare it starts a quotation. |
| 12 | In particular using QUOTATION("name:") in a grammar rule |
| 13 | declares "name:" as a keyword and the token QUOTATION is |
| 14 | matched whenever the keyword is followed by an identifier or a |
| 15 | parenthesized text. Eg |
| 16 | |
| 17 | constr:x |
| 18 | string:[....] |
| 19 | ltac:(....) |
| 20 | ltac:{....} |
| 21 | |
| 22 | The delimiter is made of 1 or more occurrences of the same parenthesis, |
| 23 | eg ((.....)) or [[[[....]]]]. The idea being that if the text happens to |
| 24 | contain the closing delimiter, one can make the delimiter longer and avoid |
| 25 | confusion (no escaping). Eg |
| 26 | |
| 27 | string:[[ .. ']' .. ]] |
| 28 | |
| 29 | |
| 30 | Nesting the delimiter is allowed, eg ((..((...))..)) is OK. |
| 31 | |
| 32 | Keywords don't need to end in ':' *) |
| 33 | type starts_quotation = NoQuotation | Quotation |
| 34 | |
| 35 | type keyword_state |
| 36 | |
| 37 | val empty_keyword_state : keyword_state |
| 38 | |
| 39 | val add_keyword : ?quotation:starts_quotation -> keyword_state -> string -> keyword_state |
| 40 | |
| 41 | val is_keyword : keyword_state -> string -> bool |
| 42 | val keywords : keyword_state -> CString.Set.t |
| 43 | |
| 44 | val check_ident : string -> unit |
| 45 | val is_ident : string -> bool |
| 46 | val check_keyword : string -> unit |
| 47 | |
| 48 | val add_keyword_tok : keyword_state -> 'c Tok.p -> keyword_state |
| 49 | |
| 50 | (** When string is not an ident, returns a keyword. |
| 51 | (returns [PIDENT (Some _)] for idents which have been declared as keywords, |
| 52 | as the keyword state is not checked) |
| 53 | *) |
| 54 | val terminal : string -> string Tok.p |
| 55 | |
| 56 | (** Precondition: the input is a number (c.f. [NumTok.t]) *) |
| 57 | val terminal_number : string -> NumTok.Unsigned.t Tok.p |
| 58 | |
| 59 | (** [after loc] Will advance a lexing location as the lexer does; this |
| 60 | can be used to implement parsing resumption from a given position: |
| 61 | {[ |
| 62 | let loc = Procq.Parsable.loc pa |> after in |
| 63 | let str = Gramlib.Stream.of_string text in |
| 64 | (* Stream.count being correct is critical for Rocq's lexer *) |
| 65 | Gramlib.Stream.njunk loc.ep str; |
| 66 | let pa = Procq.Parsable.make ~loc str in |
| 67 | (* ready to resume parsing *) |
| 68 | ]} |
| 69 | *) |
| 70 | val after : Loc.t -> Loc.t |
| 71 | |
| 72 | (** The lexer of Rocq: *) |
| 73 | |
| 74 | module Lexer : |
| 75 | Gramlib.Plexing.S |
| 76 | with type keyword_state = keyword_state |
| 77 | and type te = Tok.t |
| 78 | and type 'c pattern = 'c Tok.p |
| 79 | |
| 80 | |
| 81 | module Error : sig |
| 82 | type t |
| 83 | exception E of t |
| 84 | val to_string : t -> string |
| 85 | end |
| 86 | |
| 87 | (** LexerDiff ensures that, ignoring white space, the concatenated |
| 88 | tokens equal the input string. Specifically: |
| 89 | - for strings, return the enclosing quotes as tokens and treat the |
| 90 | quoted value as if it was unquoted, possibly becoming multiple |
| 91 | tokens. |
| 92 | - for comments, return the "(\*" (\ to be kind to syntax |
| 93 | highlighters) as a token and treat the contents of the comment as |
| 94 | if it was not in a comment, possibly becoming multiple tokens. |
| 95 | - return any unrecognized Ascii or UTF-8 character as a string. |
| 96 | *) |
| 97 | |
| 98 | module LexerDiff : |
| 99 | Gramlib.Plexing.S |
| 100 | with type keyword_state = keyword_state |
| 101 | and type te = Tok.t |
| 102 | and type 'c pattern = 'c Tok.p |
| 103 | |
| 104 | val record_comments : bool ref |