This repository has been archived on 2022-12-27. You can view files and clone it, but cannot push or open issues or pull requests.
compilateurMIPS/lexer.mll

52 lines
1.6 KiB
OCaml
Raw Normal View History

2022-12-06 20:39:15 +01:00
{
open Errors
2022-12-06 20:39:15 +01:00
open Lexing
open Parser
}
2022-12-08 19:55:22 +01:00
let alpha = ['a'-'z' 'A'-'Z']
let num = ['0'-'9']
2022-12-08 21:30:39 +01:00
let bool = "true" | "false"
2022-12-08 19:55:22 +01:00
let ident = alpha (alpha | num | '-' | '_')*
2022-12-06 20:39:15 +01:00
rule token = parse
2022-12-11 03:35:52 +01:00
| eof { Lend }
| [ ' ' '\t' ] { token lexbuf }
| '\n' { Lexing.new_line lexbuf; token lexbuf }
| num+ as n { Lint (int_of_string n) }
| bool as b { Lbool (bool_of_string b) }
| "return" { Lreturn }
| "int" { Ltype (Int_t) }
| "bool" { Ltype (Bool_t) }
| "void" { Ltype (Void_t) }
| "str" { Ltype (Str_t) }
| '{' { Lbracedeb }
| '}' { Lbracefin }
| '(' { Lpardeb }
| ')' { Lparfin }
| ',' { Lcomma }
| '=' { Lassign }
| ';' { Lsc }
| '+' { Ladd }
| '-' { Lsub }
| '*' { Lmul }
| '/' { Ldiv }
| '"' { read_string (Buffer.create 16) lexbuf }
| ident as i { Lvar i }
| '#' { comment lexbuf }
| _ as c { raise (LexerError c) }
2022-12-08 21:29:42 +01:00
and comment = parse
2022-12-11 03:35:52 +01:00
| eof { Lend }
| '\n' { Lexing.new_line lexbuf; token lexbuf }
| _ { comment lexbuf }
and read_string buffer = parse
| '"' { Lstr (Buffer.contents buffer) }
2022-12-13 14:36:38 +01:00
| '\\' 'n' { Buffer.add_string buffer "\\n"; read_string buffer lexbuf }
2022-12-11 03:35:52 +01:00
| [^ '"' '\\']+ { Buffer.add_string buffer (Lexing.lexeme lexbuf)
; read_string buffer lexbuf
}
| _ as c { raise (LexerError c) }
| eof { raise (SyntaxError "String is not terminated") }