Commit f5268155 authored by Nicaudie Charlotte's avatar Nicaudie Charlotte

Added file to print tokens

parent 9e304cc5
open Parser
open Printf
(** UTF8 sequences for different symbols. *)
let lquote = "\xe2\x80\x98"
let rquote = "\xe2\x80\x98"
let rtriangle = "\xe2\x96\xb8"
let ltriangle = "\xe2\x97\x82"
(** [print_token show_loc out token] prints [token] on the out channel [out].
If [show_loc] is true, prints position informations for the identifiers. *)
let print_token show_loc out = function
| INIT ->
fprintf out "INIT"
| ITER ->
fprintf out "ITER"
| TRANSLATION ->
fprintf out "TRANSLATION"
| ROTATION ->
fprintf out "ROTATION"
| OR ->
fprintf out "OR"
| INTEGER ->
fprintf out "INTEGER"
| FLOAT ->
fprintf out "FLOAT"
| IDENT id ->
if show_loc then
let pos = Location.startpos id in
let l = pos.pos_lnum in
let c = pos.pos_cnum - pos.pos_bol + 1 in
fprintf out "IDENT %s%s%s %s line %d, char %d %s" lquote (Location.content id) rquote rtriangle l c ltriangle
else
fprintf out "IDENT %s%s%s" lquote (Location.content id) rquote
| COMMA ->
fprintf out "COMMA"
| SEMICOLON ->
fprintf out "SEMICOLON"
| LPAREN ->
fprintf out "LPAREN"
| RPAREN ->
fprintf out "RPAREN"
| BEGIN ->
fprintf out "BEGIN"
| END ->
fprintf out "END"
| EOF ->
fprintf out "EOF"
let print out lexbuf show_loc =
let token = ref (Lexer.get_token lexbuf) in
fprintf out "%a\n" (print_token show_loc) !token;
while !token <> EOF do
token := Lexer.get_token lexbuf;
fprintf out "%a\n" (print_token show_loc) !token
done
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment