package lrgrep

  1. Overview
  2. Docs
Analyse the stack of a Menhir-generated LR parser using regular expressions

Install

dune-project
 Dependency

Authors

Maintainers

Sources

lrgrep-0.3.tbz
sha256=84a1874d0c063da371e19c84243aac7c40bfcb9aaf204251e0eb0d1f077f2cde
sha512=5a16ff42a196fd741bc64a1bdd45b4dca0098633e73aa665829a44625ec15382891c3643fa210dbe3704336eab095d4024e093e37ae5313810f6754de6119d55

doc/src/utils/code_printer.ml.html

Source file code_printer.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(* MIT License
   Copyright (c) 2025 Frédéric Bour

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all
   copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE.*)

(** This module provides a simple framework for outputting text with line
  tracking and location printing, useful for compilers and interpreters that
  need to generate code or output messages with precise location information. *)

type t = {
  filename: string;         (* The name of the file being processed *)
  mutable line: int;        (* Current line number in the output *)
  output: string -> unit;   (* Function to output a string *)
  mutable reloc: bool;      (* Flag indicating if relocation info should be printed next *)
  mutable last_is_nl: bool; (* Flag indicating if the last output character was a newline *)
}

(* Output a string, updating line count and newline status *)
let output t = function
  | "" -> ()
  | str ->
    let nl = ref 0 in
    let last = String.length str - 1 in
    for i = 0 to last do
      if str.[i] = '\n' then incr nl;
    done;
    t.line <- t.line + !nl;
    t.last_is_nl <- str.[last] = '\n';
    t.output str

(* Create a new code printer *)
let create ~filename ?(line=1) output =
  {filename; line; output; reloc = false; last_is_nl = true}

(* Print relocation information for a given file and line *)
let print_loc_dir t filename line =
  output t (Printf.sprintf "# %d %S\n" (line + 1) filename)

(* Print relocation information for a given location *)
let print_loc t (pos : Lexing.position) =
  print_loc_dir t pos.pos_fname (pos.pos_lnum - 1);
  output t (String.make (pos.pos_cnum - pos.pos_bol) ' ')

(* Print a message, optionally with relocation information *)
let print ?loc t msg =
  match loc with
  | None ->
    if t.reloc then (
      if not t.last_is_nl then output t "\n";
      print_loc_dir t t.filename t.line;
      t.reloc <- false;
    );
    output t msg
  | Some loc ->
    if not t.last_is_nl then output t "\n";
    print_loc t loc;
    t.reloc <- true;
    output t msg

(* Print a formatted message, optionally with relocation information *)
let fmt ?loc t fmt =
  Printf.ksprintf (print ?loc t) fmt