package morbig

  1. Overview
  2. Docs

Source file pretoken.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
79
80
81
82
(**************************************************************************)
(*  Copyright (C) 2017-2023 Yann Régis-Gianas, Nicolas Jeannerod,         *)
(*  Ralf Treinen.                                                         *)
(*                                                                        *)
(*  This is free software: you can redistribute it and/or modify it       *)
(*  under the terms of the GNU General Public License, version 3.         *)
(*                                                                        *)
(*  Additional terms apply, due to the reproduction of portions of        *)
(*  the POSIX standard. Please refer to the file COPYING for details.     *)
(**************************************************************************)

open Parser

type t =
  | PreWord of string * CST.word_cst
  | IoNumber of string
  | Operator of Parser.token
  | EOF
  | NEWLINE

let string_of_pretoken = function
  | PreWord (s, _) -> Printf.sprintf "PREWORD(%s)" s
  | IoNumber s -> Printf.sprintf "IONUM(%s)" s
  | Operator t -> Printf.sprintf "OPERATOR(%s)" (Token.string_of_token t)
  | EOF -> "EOF"
  | NEWLINE -> "NEWLINE"

let operators = Hashtbl.(
    let t = create 17 in
    List.iter (fun (sym, tok) -> add t sym tok) [
        "&&",  AND_IF;
        "||", OR_IF;
        ";;", DSEMI;
        "<&", LESSAND;
        ">&", GREATAND;
        "<>", LESSGREAT;
        ">>", DGREAT;
        ">|", CLOBBER;
        "|",  Pipe;
        "(",  Lparen;
        ")",  Rparen;
        "<",  LESS;
        ">",  GREAT;
        ";",  Semicolon;
        "&",  Uppersand
      ];
    t
 )

let optoken_of_string s =
  try
    Operator (Hashtbl.find operators s)
  with Not_found ->
    Printf.eprintf
      "Internal error: `%s' is not a valid operator token.\n"
       s;
    assert false

let preword_of_operator = function
    | AND_IF -> "&&"
    | OR_IF -> "||"
    | DSEMI -> ";;"
    | LESSAND -> "<&"
    | GREATAND -> ">&"
    | LESSGREAT -> "<>"
    | DGREAT -> ">>"
    | CLOBBER -> ">|"
    | Pipe -> "|"
    | Lparen -> "("
    | Rparen -> ")"
    | LESS -> "<"
    | DLESS _ -> "<<"
    | GREAT -> ">"
    | Semicolon -> ";"
    | Uppersand -> "&"
    | _ -> assert false (* By definition of operators. *)

let preword_of_pretoken = function
  | IoNumber s -> PreWord (s, [WordLiteral s])
  | Operator o -> let s = preword_of_operator o in PreWord (s, [WordLiteral s])
  | NEWLINE -> PreWord ("\n", [WordLiteral "\n"])
  | p -> p