From b828a6e7c28426ba6630d254cb3d9cbb8563765e Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Wed, 23 May 2018 21:10:16 -0400 Subject: [PATCH] Fix warnings: 4, 6, 29 4 : fragile pattern-matching: using option to mean EOF, not yet sure if will be compatible with ocamlyacc - may need to switch to raising exception 6 : label f was omitted 29 : unescaped end-of-line --- tiger/src/exe/tiger_tests.ml | 8 +-- tiger/src/exe/tigerc.ml | 11 ++-- tiger/src/lib/tiger/tiger_lexer.mli | 2 +- tiger/src/lib/tiger/tiger_lexer.mll | 90 ++++++++++++++--------------- 4 files changed, 56 insertions(+), 55 deletions(-) diff --git a/tiger/src/exe/tiger_tests.ml b/tiger/src/exe/tiger_tests.ml index af707b0..3e97eec 100644 --- a/tiger/src/exe/tiger_tests.ml +++ b/tiger/src/exe/tiger_tests.ml @@ -33,7 +33,7 @@ let test_02 = let \ type myint = int \ type arrtype = array of myint \ - var arr1:arrtype := + var arr1:arrtype := \ arrtype [10] of 0 \ in \ arr1 \ @@ -98,8 +98,8 @@ let tokens_of_code code = let lexbuf = Lexing.from_string code in let rec tokens () = match Tiger.Lexer.token lexbuf with - | Tiger.Parser.Token.EOF -> [] - | token -> token :: tokens () + | None -> [] + | Some token -> token :: tokens () in tokens () @@ -121,7 +121,7 @@ let () = print_endline "OK"; with Assert_failure _ -> let tokens_to_string tokens = - String.concat "; " (List.map Tiger.Parser.Token.to_string tokens) + String.concat "; " (List.map ~f:Tiger.Parser.Token.to_string tokens) in printf "ERROR\n Expected: %s\n Emitted : %s\n\n" diff --git a/tiger/src/exe/tigerc.ml b/tiger/src/exe/tigerc.ml index 77eafb4..9c901a7 100644 --- a/tiger/src/exe/tigerc.ml +++ b/tiger/src/exe/tigerc.ml @@ -5,11 +5,12 @@ let () = let ic = open_in path_to_program_file in let lexbuf = Lexing.from_channel ic in let rec parse_and_print () = - let token = Tiger.Lexer.token lexbuf in - printf "%s\n" (Tiger.Parser.Token.to_string token); - match token with - | Tiger.Parser.Token.EOF -> () - | _ -> parse_and_print () + match Tiger.Lexer.token lexbuf with + | None -> + () + | Some token -> + printf "%s\n" (Tiger.Parser.Token.to_string token); + parse_and_print () in parse_and_print (); close_in ic; diff --git a/tiger/src/lib/tiger/tiger_lexer.mli b/tiger/src/lib/tiger/tiger_lexer.mli index fd30dce..dceeee5 100644 --- a/tiger/src/lib/tiger/tiger_lexer.mli +++ b/tiger/src/lib/tiger/tiger_lexer.mli @@ -1 +1 @@ -val token : Lexing.lexbuf -> Tiger_parser.Token.t +val token : Lexing.lexbuf -> Tiger_parser.Token.t option diff --git a/tiger/src/lib/tiger/tiger_lexer.mll b/tiger/src/lib/tiger/tiger_lexer.mll index c19eabf..9e08685 100644 --- a/tiger/src/lib/tiger/tiger_lexer.mll +++ b/tiger/src/lib/tiger/tiger_lexer.mll @@ -11,7 +11,7 @@ let newline = '\n' | '\r' | "\n\r" rule token = parse | eof { - EOF + None } (* Track line number *) @@ -26,29 +26,29 @@ rule token = parse comment lexbuf } - | ":=" {ASSIGN} - | "<=" {LE} - | ">=" {GE} - | "<>" {NEQ} - | '&' {AND} - | '(' {LPAREN} - | ')' {RPAREN} - | '*' {TIMES} - | '+' {PLUS} - | '-' {MINUS} - | '/' {DIVIDE} - | ',' {COMMA} - | '.' {DOT} - | ':' {COLON} - | ';' {SEMICOLON} - | '>' {GT} - | '<' {LT} - | '=' {EQ} - | '[' {LBRACK} - | ']' {RBRACK} - | '{' {LBRACE} - | '}' {RBRACE} - | '|' {OR} + | ":=" {Some ASSIGN} + | "<=" {Some LE} + | ">=" {Some GE} + | "<>" {Some NEQ} + | '&' {Some AND} + | '(' {Some LPAREN} + | ')' {Some RPAREN} + | '*' {Some TIMES} + | '+' {Some PLUS} + | '-' {Some MINUS} + | '/' {Some DIVIDE} + | ',' {Some COMMA} + | '.' {Some DOT} + | ':' {Some COLON} + | ';' {Some SEMICOLON} + | '>' {Some GT} + | '<' {Some LT} + | '=' {Some EQ} + | '[' {Some LBRACK} + | ']' {Some RBRACK} + | '{' {Some LBRACE} + | '}' {Some RBRACE} + | '|' {Some OR} (* String literal *) | '"' { @@ -61,29 +61,29 @@ rule token = parse } | (num+ as int) { - INT (int_of_string int) + Some (INT (int_of_string int)) } | (alpha (alpha | num | '_')* as id) { match id with - | "array" -> ARRAY - | "break" -> BREAK - | "do" -> DO - | "else" -> ELSE - | "end" -> END - | "for" -> FOR - | "function" -> FUNCTION - | "if" -> IF - | "in" -> IN - | "let" -> LET - | "nil" -> NIL - | "of" -> OF - | "then" -> THEN - | "to" -> TO - | "type" -> TYPE - | "var" -> VAR - | "while" -> WHILE - | _ -> ID id + | "array" -> Some ARRAY + | "break" -> Some BREAK + | "do" -> Some DO + | "else" -> Some ELSE + | "end" -> Some END + | "for" -> Some FOR + | "function" -> Some FUNCTION + | "if" -> Some IF + | "in" -> Some IN + | "let" -> Some LET + | "nil" -> Some NIL + | "of" -> Some OF + | "then" -> Some THEN + | "to" -> Some TO + | "type" -> Some TYPE + | "var" -> Some VAR + | "while" -> Some WHILE + | _ -> Some (ID id) } and string_literal = parse (* Keep escaped quote marks as part of the string literal *) @@ -95,7 +95,7 @@ and string_literal = parse | '"' { let string = Buffer.contents string_buf in Buffer.reset string_buf; - STRING string + Some (STRING string) } @@ -106,7 +106,7 @@ and string_literal = parse and comment = parse | eof { (* TODO: Error: unterminated comment? or we don't care? *) - EOF + None } (* Track line number *) -- 2.20.1