ocaml-lib / syntax.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
 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
(** Syntax utility functions. *)

(* analyse lexicale générique *)

type t_stream = Token.t Stream.t

type t_list = Token.t list

open Token

(* forbidden idents in syntax of logics *)
let keywords = ["all";"none";"and";"or";"not";"except";"implies";"only"]

let not_PP t = not (Token.is_PP t)
(*
function
  | PP_tilda | PP_space | PP_cut | PP_break _ | PP_newline -> false
  | _ -> true
*)

let rec length = function
  | [] -> 0
  | x::xs -> (if not_PP x then 1 else 0) + length xs

(* deprecated -> moved into logics *)
(*
let rec get_terms : t_list -> string list =
  function
      [] -> []
    | Term name::l -> name::get_terms l
    | _::l -> get_terms l
*)

let from_channel : in_channel -> t_stream =
  fun ch ->
    let lexbuf = Lexing.from_channel ch in
    Stream.from (fun _ ->
      try Some (Lexer.token lexbuf)
      with Eof -> None)

let from_string : string -> t_stream =
  fun s ->
    let lexbuf = Lexing.from_string s in
    Stream.from (fun _ ->
      try Some (Lexer.token lexbuf)
      with Eof -> None)

let rec list_of_stream s =
  try
    let x = Stream.next s in
    x::list_of_stream s
  with _ -> []

let list_of_string s =
  list_of_stream (from_string s)

let rec of_list : t_list -> t_stream =
  fun toks -> Stream.of_list (List.filter not_PP toks)

(* get the string representation of the token *)
type space_of_token = Sep | PonctL | PonctR | Op | Word

open Format

let rec power10 : int -> float =
      function
      | 0 -> 1.
      | p -> if p > 0
             then power10 (p-1) *. 10.
             else power10 (p+1) /. 10.

let string_escaped : string -> string =
  fun s ->
    let l1 = String.length s in
    let buf = Buffer.create (2 * l1) in
    for i=0 to l1-1 do
      if s.[i] = '"' then Buffer.add_string buf "\\\""
      else if s.[i] = '\\' then Buffer.add_string buf "\\\\"
      else Buffer.add_char buf s.[i]
    done;
    Buffer.contents buf

let term_escaped : string -> string =
  fun s ->
    let l1 = String.length s in
    let buf = Buffer.create (2 * l1) in
    for i=0 to l1-1 do
      if s.[i] = '\'' then Buffer.add_string buf "\\\'"
      else if s.[i] = '\\' then Buffer.add_string buf "\\\\"
      else Buffer.add_char buf s.[i]
    done;
    Buffer.contents buf

let pp_print_token : formatter -> Token.t -> Token.t -> unit =
  fun ff pred -> function
    | BackQuote -> pp_print_string ff "`"
    | Tilda -> pp_print_string ff "~"
    | Exclam -> pp_print_string ff "!"
    | At -> pp_print_string ff "@"
    | Sharp -> pp_print_string ff "#"
    | Dollar -> pp_print_string ff "$"
    | Percent -> pp_print_string ff "%"
    | Hat -> pp_print_string ff "^"
    | Et -> pp_print_string ff "&"
    | Star -> pp_print_string ff "*"
    | LeftPar -> pp_print_string ff "("
    | RightPar -> pp_print_string ff ")"
    | Minus -> pp_print_string ff "-"
    | Plus -> pp_print_string ff "+"
    | Equal -> pp_print_string ff "="
    | LeftAcc -> pp_print_string ff "{"
    | RightAcc -> pp_print_string ff "}"
    | LeftBra -> pp_print_string ff "["
    | RightBra -> pp_print_string ff "]"
    | Pipe -> pp_print_string ff "|"
    | BackSlash -> pp_print_string ff "\\"
    | Slash -> pp_print_string ff "/"
    | Interro -> pp_print_string ff "?"
    | LT -> pp_print_string ff "<"
    | GT -> pp_print_string ff ">"
    | Comma -> pp_print_string ff ","
(*
    | DotDot ->
(*
	( match pred with
	| Int _ | Float _ -> pp_print_string ff " "
	| _ -> ());
*)
	pp_print_string ff ".."
*)
    | Dot ->
(*
	( match pred with
	| Int _ | Float _ -> pp_print_string ff " "
	| _ -> ());
*)
	pp_print_string ff "."
    | Colon -> pp_print_string ff ":"
    | SemiColon -> pp_print_string ff ";"
    | DoubleQuote -> pp_print_string ff "\""
    | Quote -> pp_print_string ff "'"
    | Ident s ->
	( match pred with
	| Ident _ | Term _ -> pp_print_string ff " "
	| _ -> ());
	pp_print_string ff s
    | Nat n ->
	(match pred with
	| Nat 0 -> ()
	| Nat _ | Ident _ | Term _ -> pp_print_string ff " "
	| _ -> ());
	pp_print_int ff n
(*
    | Int n ->
	( match pred with
	| Plus | Minus | Int _ | Float _ | Ident _ | Term _ -> pp_print_string ff " "
	| _ -> ());
	pp_print_int ff n
    | Float (f,p) ->
	( match pred with
	| Plus | Minus | Int _ | Float _ | Ident _ | Term _ -> pp_print_string ff " "
	| _ -> ());
        let sm = if f=0. then "" else string_of_int (int_of_float ((abs_float f) *. (power10 (-p)))) in
	let l = String.length sm in
        let e = let x = (p+l) mod 3 in if x >= 0 then x else x+3 in
        let exp e = if e = 0 then "" else "e" ^ string_of_int e in
	let s =
          (if f < 0. then "-" else "") ^
          if e = 1 then
	    if l >= 1 & p+l+2 <> 0 then String.sub sm 0 1 ^ "." ^ String.sub sm 1 (l-1) ^ exp (p+l-1)
	    else "0.00" ^ sm ^ exp (p + l + 2)
          else if e = 2 then
            if l >= 2 & p+l+1 <> 0 then String.sub sm 0 2 ^ "." ^ String.sub sm 2 (l-2) ^ exp (p+l-2)
            else "0.0" ^ sm ^ exp (p + l + 1)
          else
            if l >= 3 & p+l <> 0 then String.sub sm 0 3 ^ "." ^ String.sub sm 3 (l-3) ^ exp (p+l-3)
            else "0." ^ sm ^ exp (p + l) in
	pp_print_string ff s
*)
    | String s ->
	pp_print_string ff ("\"" ^ string_escaped s ^ "\"")
    | Term s ->
	( match pred with
	| Ident _ | Term _ -> pp_print_string ff " "
	| _ -> ());
	let b = ref true in
	(match s.[0] with 'A'..'Z' | '_' -> () | _ -> b:= false);
	if !b then String.iter (function 'a'..'z' | 'A'..'Z' | '_' | '0'..'9' -> () | _ -> b:= false) s;
	if !b
	then pp_print_string ff s
	else pp_print_string ff ("'" ^ term_escaped s ^ "'")
    | Char c -> pp_print_string ff ("`" ^ Char.escaped c ^ "`")
    | PP_tilda -> pp_print_string ff " "
    | PP_space -> pp_print_space ff ()
    | PP_cut -> pp_print_cut ff ()
    | PP_break (spaces,offset) -> pp_print_break ff spaces offset
    | PP_newline -> pp_print_newline ff ()


let rec pp_print_tokens : formatter -> t_list -> unit =
  fun ff toks -> pp_print_tokens2 ff PP_newline toks
and pp_print_tokens2 ff pred =
  function
    | [] -> ()
    | tok::toks ->
	pp_print_token ff pred tok;
	pp_print_tokens2 ff tok toks

let stringizer : t_list -> string =
  fun toks ->
    pp_print_tokens Format.str_formatter (List.map (fun tok -> if not_PP tok then tok else PP_tilda) toks);
    Format.flush_str_formatter ()

let print_tokens : t_list -> unit =
  fun toks ->
    pp_print_tokens Format.std_formatter toks

let print_string : string -> unit =
  fun s ->
    pp_print_string Format.std_formatter s 

let print_int : int -> unit =
  fun n ->
    pp_print_int Format.std_formatter n


(* fonctions generiques pour le printing *)
(* ------------------------------------- *)

(* printing a signed integer to tokens *)
let toks_of_int : int -> t_list =
  fun n ->
    let s = string_of_int n in
    list_of_stream (from_string s)

let toks_of_float : (float * int) -> t_list =
  fun (f,p) ->
    let sm = if f=0. then "" else string_of_int (int_of_float ((abs_float f) *. (power10 (-p)))) in
    let l = String.length sm in
    let e = let x = (p+l) mod 3 in if x >= 0 then x else x+3 in
    let exp e = if e = 0 then "" else "e" ^ string_of_int e in
    let s =
      (if f < 0. then "-" else "") ^
      if e = 1 then
	if l >= 1 & p+l+2 <> 0 then String.sub sm 0 1 ^ "." ^ String.sub sm 1 (l-1) ^ exp (p+l-1)
	else "0.00" ^ sm ^ exp (p + l + 2)
      else if e = 2 then
        if l >= 2 & p+l+1 <> 0 then String.sub sm 0 2 ^ "." ^ String.sub sm 2 (l-2) ^ exp (p+l-2)
        else "0.0" ^ sm ^ exp (p + l + 1)
      else
        if l >= 3 & p+l <> 0 then String.sub sm 0 3 ^ "." ^ String.sub sm 3 (l-3) ^ exp (p+l-3)
        else "0." ^ sm ^ exp (p + l) in
    list_of_stream (from_string s)

(* messages d'erreur syntaxique *)

let error_RightPar = "Syntax error: a closing parenthesis is missing."
let error_RightBra = "Syntax error: a closing bracket is missing."

(* add parenthesis around the given t_list if op (precedence) is above the ctx (precedence) *)

let add_par : int -> int -> t_list -> t_list =
  fun ctx op l -> if op > ctx then LeftPar::(l @ [RightPar]) else l

(* fonctions generiques pour le parsing *)
(* ------------------------------------ *)

(* parsing a signed integer *)
let parse_int : t_stream -> int = parser
  | [<'Nat n>] -> n
  | [<'Plus; 'Nat n>] -> n
  | [<'Minus; 'Nat n>] -> -n

(* parsing a float *)
let rec parse_float : t_stream -> float * int = parser
  | [<s = parse_float0>] -> (float_of_string s, prec_of_sfloat s)
and parse_float0 = parser
  | [<'Dot; m2 = parse_float_decimal; s = parse_float4>] -> "." ^ m2 ^ s
  | [<'Nat m1; s = parse_float2>] -> string_of_int m1 ^ s
  | [<sign = parse_float_sign; s = parse_float1>] -> sign ^ s
and parse_float1 = parser
  | [<'Nat m1; s = parse_float2>] -> string_of_int m1 ^ s
  | [<'Dot; m2 = parse_float_decimal; s = parse_float4>] -> "." ^ m2 ^ s
and parse_float2 = parser
  | [<'Dot; s = parse_float3>] -> "." ^ s
  | [<exp = parse_float4>] -> "." ^ exp
and parse_float3 = parser
  | [<m2 = parse_float_decimal; s = parse_float4>] -> m2 ^ s
  | [<e = parse_float_e; s = parse_float5>] -> e ^ s
  | [<exp = parse_float_exp>] -> exp
  | [<>] -> ""
and parse_float4 = parser
  | [<e = parse_float_e; s = parse_float5>] -> e ^ s
  | [<exp = parse_float_exp>] -> exp
  | [<>] -> ""
and parse_float5 = parser
  | [<p = parse_float_decimal>] -> p
  | [<sign = parse_float_sign; p = parse_float_decimal>] -> sign ^ p
and parse_float_sign = parser
  | [<'Minus>] -> "-"
  | [<'Plus>] -> ""
and parse_float_decimal = parser
  | [<'Nat 0; d = parse_float_decimal2>] -> "0" ^ d
  | [<'Nat d>] -> string_of_int d
and parse_float_decimal2 = parser
  | [<'Nat 0; d = parse_float_decimal2>] -> "0" ^ d
  | [<'Nat d>] -> string_of_int d
  | [<>] -> ""
and parse_float_e = parser
  | [<'Ident "e">] -> "e"
  | [<'Term "E">] -> "e"
and parse_float_exp = parser
  | [<'Ident exp when Str.string_match (Str.regexp "e[0-9]+$") exp 0>] -> exp
  | [<'Term exp when Str.string_match (Str.regexp "E[0-9]+$") exp 0>] -> exp

(* parsing a number *)
type num = Int of int | Float of (float * int)

let rec parse_num : t_stream -> num = parser
  | [<i,s = parse_num0>] ->
      if i
      then Int (int_of_string s)
      else Float (float_of_string s, prec_of_sfloat s)
and parse_num0 = parser
  | [<'Dot; m2 = parse_num_decimal; s = parse_num4>] -> false, "." ^ m2 ^ s
  | [<'Nat m1; i,s = parse_num2>] -> i, string_of_int m1 ^ s
  | [<sign = parse_num_sign; i,s = parse_num1>] -> i, sign ^ s
and parse_num1 = parser
  | [<'Nat m1; i,s = parse_num2>] -> i, string_of_int m1 ^ s
  | [<'Dot; m2 = parse_num_decimal; s = parse_num4>] -> false, "." ^ m2 ^ s
and parse_num2 = parser
  | [<'Dot; s = parse_num3>] -> false, "." ^ s
  | [<exp = parse_num4>] -> if exp = "" then true, "" else false, ("." ^ exp)
  | [<>] -> true, ""
and parse_num3 = parser
  | [<m2 = parse_num_decimal; s = parse_num4>] -> m2 ^ s
  | [<e = parse_num_e; s = parse_num5>] -> e ^ s
  | [<exp = parse_num_exp>] -> exp
  | [<>] -> ""
and parse_num4 = parser
  | [<e = parse_num_e; s = parse_num5>] -> e ^ s
  | [<exp = parse_num_exp>] -> exp
  | [<>] -> ""
and parse_num5 = parser
  | [<p = parse_num_decimal>] -> p
  | [<sign = parse_num_sign; p = parse_num_decimal>] -> sign ^ p
and parse_num_sign = parser
  | [<'Minus>] -> "-"
  | [<'Plus>] -> ""
and parse_num_decimal = parser
  | [<'Nat 0; d = parse_num_decimal2>] -> "0" ^ d
  | [<'Nat d>] -> string_of_int d
and parse_num_decimal2 = parser
  | [<'Nat 0; d = parse_num_decimal2>] -> "0" ^ d
  | [<'Nat d>] -> string_of_int d
  | [<>] -> ""
and parse_num_e = parser
  | [<'Ident "e">] -> "e"
  | [<'Term "E">] -> "e"
and parse_num_exp = parser
  | [<'Ident exp when Str.string_match (Str.regexp "e[0-9]+$") exp 0>] -> exp
  | [<'Term exp when Str.string_match (Str.regexp "E[0-9]+$") exp 0>] -> exp


(* parsing of a list of tokens *)
let rec parse_tokens : t_list -> t_stream -> unit =
fun toks str ->
  match List.filter not_PP toks with
  | [] -> (match str with parser [<>] -> ())
  | tok::toks -> (match str with parser [<'t when t = tok; _ = parse_tokens toks ?? "Syntax error: '"^stringizer toks^"' expected after '"^stringizer [tok]^"'">] -> ())

(* parsing of an optional list of tokens *)
let rec parse_tokens_opt : t_list option -> t_stream -> unit =
fun toks_opt str ->
  match toks_opt with
  | None -> raise Stream.Failure
  | Some toks ->
      match List.filter not_PP toks with
      | [] -> (match str with parser [<>] -> ())
      | tok::toks -> (match str with parser [<'t when t = tok; _ = parse_tokens toks ?? "Syntax error: '"^stringizer toks^"' expected after '"^stringizer [tok]^"'">] -> ())

(* parsing of a list of tokens and something else *)
let rec parse_tokens_and : t_list -> (t_stream -> 'a) -> t_stream -> 'a =
fun toks p str ->
  match List.filter not_PP toks with
  | [] -> (match str with parser [<x = p>] -> x)
  | tok::toks -> (match str with parser [<'t when t = tok; x = parse_tokens_and toks p ?? "Syntax error: '"^stringizer toks^"' expected after '"^stringizer [tok]^"'">] -> x)

(* parsing an indefinite number of pattern given some parser *)
let rec parse_star p = parser
  | [<x = p; xs = parse_star p>] -> x::xs
  | [<>] -> []

let parse_plus p = parser
  | [<x = p; xs = parse_star p>] -> x::xs

(* optional parsing *)
let parse_opt : (t_stream -> 'a) -> 'a -> (t_stream -> 'a) =
  fun p e -> parser
    | [<x = p>] -> x
    | [<>] -> e


(* parsing options *)

let wrong_option s n = "Syntax error: option -"^n^" expected instead of -"^s
let wrong_option_name n = "Syntax error in some option: '"^n^"' expected after '-'"

let parse_option_bool n = parser
| [<'Minus; 'Ident s ?? wrong_option_name n>] -> s=n or raise (Stream.Error (wrong_option s n))
| [<>] -> false

let parse_option_int ?(default=0) n = parser
| [<'Minus; 'Ident s when s=n ?? wrong_option_name n; i = parse_int ?? "Syntax error: integer expected after option -"^n>] -> i
| [<>] -> default

let parse_option_float ? (default=0.) n = parser
| [<'Minus; 'Ident s when s=n ?? wrong_option_name n; (x,_) = parse_float ?? "Syntax error: float expected after option -"^n>] -> x
| [<>] -> default

let parse_option_string ?(default="") n = parser
| [<'Minus; 'Ident s when s=n ?? wrong_option_name n; 'String s2 ?? "Syntax error: string expected after opion -"^n>] -> s2
| [<>] -> default


(* parsing of proposition-like language, where operations and atoms are parameterized *)

type 'a spec_prop = { 
    all : string; none : string; a : string; an : string; o : string; n :  string;
    taut : 'a; cont : 'a; neg : 'a -> 'a; conj : 'a -> 'a -> 'a; disj : 'a -> 'a -> 'a;
      atom : t_stream -> 'a
  } 

let wrong_prop spec s = "Syntax error: '(', '" ^ spec.n ^ "', '" ^ spec.all ^ "', '" ^ spec.none ^ "' or atom expected after '" ^ s ^ "'"
let wrong_term spec s = "Syntax error: '(', '" ^ spec.n ^ "' or atom expected after '" ^ s ^ "'"
let wrong_fact spec s = "Syntax error: '(', '" ^ spec.n ^ "' or atom expected after '" ^ s ^ "'"

let rec parse_prop spec = parser
  | [<'Token.Ident s when s = spec.all >] -> spec.taut
  | [<'Token.Ident s when s = spec.none>] -> spec.cont
  | [<q = parse_term spec; f = parse_suite spec>] -> f q
and parse_suite spec = parser
    [<'Token.Ident s when s = spec.a; q = parse_prop spec ?? wrong_prop spec s>] -> (fun q' -> spec.conj q' q)
  | [<'Token.Ident s when s = spec.an; q = parse_fact spec ?? wrong_fact spec s>] -> (fun q' -> spec.conj q' (spec.neg q))
  | [<>] -> (fun q' -> q')
and parse_term spec = parser
  | [<q = parse_fact spec; f = parse_term_suite spec>] -> f q
and parse_term_suite spec = parser
  | [<'Token.Ident s when s = spec.o; q = parse_term spec ?? wrong_term spec s>] -> (fun q' -> spec.disj q' q)
  | [<>] -> (fun q' -> q')
and parse_fact spec = parser
  | [<'Token.LeftPar; q = parse_prop spec ?? wrong_prop spec "("; 'Token.RightPar ?? "Syntax error: missing ')' after proposition">] -> q
  | [<'Token.Ident s when s = spec.n; q = parse_fact spec ?? wrong_fact spec s>] -> spec.neg q
  | [<a = spec.atom>] -> a

(* generic functions about strings *)
(* ------------------------------- *)

let rec split (normalize, separator, stopword) (s : string) = 
      split2 (normalize, separator, stopword) s 0 "" []
and split2 (normalize, separator, stopword) s i w ws =
      if i>=String.length s then addword (normalize, separator, stopword) w ws
      else if separator s.[i] then split2 (normalize, separator, stopword) s (i+1) "" (addword (normalize, separator, stopword) w ws)
      else split2 (normalize, separator, stopword) s (i+1) (w ^ String.make 1 s.[i]) ws
and addword (normalize, separator, stopword) w ws =
      if w = "" or stopword (normalize w) then ws else w::ws
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.