X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=tiger%2Fsrc%2Flib%2Ftiger%2Ftiger_lexer.mll;h=9e08685c5c788a718b9165f294f13c279a34e837;hb=b828a6e7c28426ba6630d254cb3d9cbb8563765e;hp=86bf0f8438f81299458d6a4dbf99d0a29786df14;hpb=78c9eca51ebc5150d79f84e255a57bb9df9f82fc;p=tiger.ml.git diff --git a/tiger/src/lib/tiger/tiger_lexer.mll b/tiger/src/lib/tiger/tiger_lexer.mll index 86bf0f8..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,34 +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 - } - - (* Eat unimplemented. FIXME: stop indiscriminate eating *) - | _ { - token lexbuf + | "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 *) @@ -100,7 +95,7 @@ and string_literal = parse | '"' { let string = Buffer.contents string_buf in Buffer.reset string_buf; - STRING string + Some (STRING string) } @@ -111,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 *)