/[ascend]/branches/hugo/ocaml/testTokens.ml
ViewVC logotype

Contents of /branches/hugo/ocaml/testTokens.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3167 - (show annotations) (download)
Tue Aug 16 10:54:02 2016 UTC (22 months, 1 week ago) by hugo
File size: 1246 byte(s)
Alternate parser, written in Ocaml

This commit contains an experimental parser for ASCEND, written in Ocaml

1 (* This test program reads an a4c ASCEND file from stdin
2 * and outputs a stream of tokens, one per line.
3 *
4 * To output directly from the lexer:
5 *
6 * ./testTokens.byte lexer
7 *
8 * To parse the program to an AST and then convert it back to tokens:
9 *
10 * ./testTokens.byte parser
11 *)
12
13
14 let exit_with_error s =
15 Printf.fprintf stderr "%s\n" s;
16 exit 1
17
18 let token_sequence_from_lexer lexbuf =
19 Sequence.from_fun (fun () ->
20 match Lexer.initial lexbuf with
21 | Parser.EOF -> None
22 | tok -> Some(tok)
23 )
24
25 let token_sequence_from_parser lexbuf =
26 TokenizeTree.tokenize_ast (Parser.start Lexer.initial lexbuf)
27
28 let () =
29 try
30 let lexbuf = Lexing.from_channel stdin in
31 let tokens = match Sys.argv with
32 | [| _; "lexer" |] -> token_sequence_from_lexer lexbuf
33 | [| _; "parser" |] -> token_sequence_from_parser lexbuf
34 | _ -> exit_with_error "Bad command line"
35 in
36 tokens |> Sequence.iter (fun tok ->
37 Printf.printf "%s\n" (Lexer.string_of_token tok))
38 with
39 | Types.LexerError s -> exit_with_error ("Lexer error: " ^ s)
40 | Types.SyntaxError s -> exit_with_error ("Parser error: " ^ s)
41 | Types.NotImpl n -> exit_with_error ("Not implemented yet: " ^ string_of_int n)

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22