Merge commit 'ocaml3102'
[ocaml.git] / test / Lex / main.ml
blob6382401e2b17464f58431ca7d369a71188708601
1 (***********************************************************************)
2 (* *)
3 (* Objective Caml *)
4 (* *)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6 (* *)
7 (* Copyright 1996 Institut National de Recherche en Informatique et *)
8 (* en Automatique. All rights reserved. This file is distributed *)
9 (* under the terms of the Q Public License version 1.0. *)
10 (* *)
11 (***********************************************************************)
13 (* $Id$ *)
15 (* The lexer generator. Command-line parsing. *)
17 open Syntax
18 open Scanner
19 open Grammar
20 open Lexgen
21 open Output
23 let main () =
24 if Array.length Sys.argv <> 2 then begin
25 prerr_string "Usage: camllex <input file>\n";
26 exit 2
27 end;
28 let source_name = Sys.argv.(1) in
29 let dest_name =
30 if Filename.check_suffix source_name ".mll" then
31 Filename.chop_suffix source_name ".mll" ^ ".ml"
32 else
33 source_name ^ ".ml" in
34 ic := open_in source_name;
35 oc := open_out dest_name;
36 let lexbuf = Lexing.from_channel !ic in
37 let (Lexdef(header,_) as def) =
38 try
39 Grammar.lexer_definition Scanner.main lexbuf
40 with
41 Parsing.Parse_error ->
42 prerr_string "Syntax error around char ";
43 prerr_int (Lexing.lexeme_start lexbuf);
44 prerr_endline ".";
45 exit 2
46 | Scan_aux.Lexical_error s ->
47 prerr_string "Lexical error around char ";
48 prerr_int (Lexing.lexeme_start lexbuf);
49 prerr_string ": ";
50 prerr_string s;
51 prerr_endline ".";
52 exit 2 in
53 let ((init, states, acts) as dfa) = make_dfa def in
54 output_lexdef header dfa;
55 close_in !ic;
56 close_out !oc
58 let _ = main(); exit 0
61 (*****
62 let main () =
63 ic := stdin;
64 oc := stdout;
65 let lexbuf = lexing.from_channel ic in
66 let (Lexdef(header,_) as def) =
67 try
68 grammar.lexer_definition scanner.main lexbuf
69 with
70 parsing.Parse_error x ->
71 prerr_string "Syntax error around char ";
72 prerr_int (lexing.lexeme_start lexbuf);
73 prerr_endline ".";
74 sys.exit 2
75 | scan_aux.Lexical_error s ->
76 prerr_string "Lexical error around char ";
77 prerr_int (lexing.lexeme_start lexbuf);
78 prerr_string ": ";
79 prerr_string s;
80 prerr_endline ".";
81 sys.exit 2 in
82 let ((init, states, acts) as dfa) = make_dfa def in
83 output_lexdef header dfa
85 ****)
87 (****
88 let debug_scanner lexbuf =
89 let tok = scanner.main lexbuf in
90 begin match tok with
91 Tident s -> prerr_string "Tident "; prerr_string s
92 | Tchar c -> prerr_string "Tchar "; prerr_char c
93 | Tstring s -> prerr_string "Tstring "; prerr_string s
94 | Taction(Location(i1,i2)) ->
95 prerr_string "Taction "; prerr_int i1; prerr_string "-";
96 prerr_int i2
97 | Trule -> prerr_string "Trule"
98 | Tparse -> prerr_string "Tparse"
99 | Tand -> prerr_string "Tand"
100 | Tequal -> prerr_string "Tequal"
101 | Tend -> prerr_string "Tend"
102 | Tor -> prerr_string "Tor"
103 | Tunderscore -> prerr_string "Tunderscore"
104 | Teof -> prerr_string "Teof"
105 | Tlbracket -> prerr_string "Tlbracket"
106 | Trbracket -> prerr_string "Trbracket"
107 | Tstar -> prerr_string "Tstar"
108 | Tmaybe -> prerr_string "Tmaybe"
109 | Tplus -> prerr_string "Tplus"
110 | Tlparen -> prerr_string "Tlparen"
111 | Trparen -> prerr_string "Trparen"
112 | Tcaret -> prerr_string "Tcaret"
113 | Tdash -> prerr_string "Tdash"
114 end;
115 prerr_newline();
118 ****)