File: mlsyntax.ml

package info (click to toggle)
camlp5 8.04.00-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,968 kB
  • sloc: ml: 137,918; makefile: 2,055; perl: 1,729; sh: 1,653; python: 38
file content (174 lines) | stat: -rw-r--r-- 4,863 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
(* camlp5r *)
(* mlsyntax.ml *)
(* Copyright (c) INRIA 2007-2017 *)

let symbolchar_or f st ?lim s =
  let list =
    ['!'; '$'; '%'; '&'; '*'; '+'; '-'; '.'; '/'; ':'; '<'; '='; '>'; '?';
     '@'; '^'; '|'; '~']
  in
  let lim =
    match lim with
      None -> String.length s
    | Some j -> j
  in
  let rec loop i =
    if i == lim then true
    else if List.mem s.[i] list || f s.[i] then loop (i + 1)
    else false
  in
  loop st
;;

let symbolchar = symbolchar_or (fun x -> false);;

let dotsymbolchar st ?lim s =
  let list =
    ['!'; '$'; '%'; '&'; '*'; '+'; '-'; '/'; ':'; '='; '>'; '?'; '@'; '^';
     '|']
  in
  let lim =
    match lim with
      None -> String.length s
    | Some j -> j
  in
  let rec loop i =
    if i == lim then true
    else if List.mem s.[i] list then loop (i + 1)
    else false
  in
  loop st
;;

let kwdopchar =
  let list = ['$'; '&'; '*'; '+'; '-'; '/'; '<'; '='; '>'; '@'; '^'; '|'] in
  fun s i -> if i == String.length s then true else List.mem s.[i] list
;;

module Original =
  struct
    let is_prefixop =
      let list = ['!'; '?'; '~'] in
      let excl = ["!="; "??"; "?!"] in
      fun x ->
        not (List.mem x excl) && String.length x >= 2 &&
        List.mem x.[0] list && symbolchar_or (fun x -> '#' = x) 1 x
    ;;
    let is_infixop0_0 =
      let list = ['|'] in
      let excl = ["||"] in
      fun x ->
        not (List.mem x excl) && String.length x >= 2 &&
        List.mem x.[0] list && symbolchar 1 x
    ;;
    let is_infixop0_1 =
      let list = ['&'] in
      let excl = ["&&"] in
      fun x ->
        not (List.mem x excl) && String.length x >= 2 &&
        List.mem x.[0] list && symbolchar 1 x
    ;;
    let is_infixop0_2 =
      let list = ['='; '<'; '>'; '$'] in
      let excl = ["<-"] in
      fun x ->
        not (List.mem x excl) && (x = "$" || String.length x >= 2) &&
        List.mem x.[0] list && symbolchar 1 x
    ;;
    let is_infixop0 s =
      is_infixop0_0 s || is_infixop0_1 s || is_infixop0_2 s
    ;;
    let is_infixop1 =
      let list = ['@'; '^'] in
      fun x -> String.length x >= 2 && List.mem x.[0] list && symbolchar 1 x
    ;;
    let is_infixop2 =
      let list = ['+'; '-'] in
      fun x ->
        x <> "->" && String.length x >= 2 && List.mem x.[0] list &&
        symbolchar 1 x
    ;;
    let is_infixop3 =
      let list = ['*'; '/'; '%'] in
      let excl = ["**"] in
      fun x ->
        not (List.mem x excl) && String.length x >= 2 &&
        List.mem x.[0] list && symbolchar 1 x
    ;;
    let is_infixop4 x =
      String.length x >= 3 && x.[0] == '*' && x.[1] == '*' && symbolchar 2 x
    ;;
    let is_hashop =
      let list = ['#'] in
      let excl = ["#"] in
      fun x ->
        not (List.mem x excl) && String.length x >= 2 &&
        List.mem x.[0] list && symbolchar_or (fun x -> '#' = x) 1 x
    ;;
    let is_operator0 =
      let ht = Hashtbl.create 73 in
      let ct = Hashtbl.create 73 in
      List.iter (fun x -> Hashtbl.add ht x true)
        ["asr"; "land"; "lor"; "lsl"; "lsr"; "lxor"; "mod"; "or"];
      List.iter (fun x -> Hashtbl.add ct x true)
        ['!'; '&'; '*'; '+'; '-'; '/'; ':'; '<'; '='; '>'; '@'; '^'; '|'; '~';
         '?'; '%'; '.'; '$'];
      fun x ->
        try Hashtbl.find ht x with
          Not_found -> try Hashtbl.find ct x.[0] with _ -> false
    ;;
    let is_andop s =
      String.length s > 3 && String.sub s 0 3 = "and" && kwdopchar s 3 &&
      dotsymbolchar 4 s
    ;;
    let is_letop s =
      String.length s > 3 && String.sub s 0 3 = "let" && kwdopchar s 3 &&
      dotsymbolchar 4 s
    ;;
    let is_operator s = is_operator0 s || is_hashop s;;
    let is_infix_operator op =
      is_operator op &&
      (match op.[0] with
         '!' | '?' | '~' -> false
       | _ -> true)
    ;;
    let is_dotop s =
      String.length s >= 2 && String.get s 0 = '.' &&
      dotsymbolchar 1 ~lim:2 s && symbolchar 2 s
    ;;
    let is_special_op s =
      is_operator s || is_letop s || is_andop s || is_dotop s
    ;;
  end
;;

module Revised =
  struct
    include Original;;
    let is_infixop0_2 =
      let list = ['='; '<'; '>'; '$'] in
      let excl = ["<-"] in
      fun x ->
        not (List.mem x excl) && String.length x >= 2 &&
        List.mem x.[0] list && symbolchar 1 x
    ;;
    let is_infixop0 s =
      is_infixop0_0 s || is_infixop0_1 s || is_infixop0_2 s
    ;;
    let is_operator0 s = s <> "$" && is_operator s;;
    let is_operator s = is_operator0 s || is_hashop s;;
    let is_infix_operator op =
      is_operator op &&
      (match op.[0] with
         '!' | '?' | '~' -> false
       | _ -> true)
    ;;
    let is_dotop s =
      String.length s >= 2 && String.get s 0 = '.' &&
      dotsymbolchar 1 ~lim:2 s && symbolchar 2 s
    ;;
    let is_special_op s =
      is_operator s || is_letop s || is_andop s || is_dotop s
    ;;
  end
;;