diff --git a/lib/interface.ml b/lib/interface.ml index 8479d2677dfe75c625617a0a1e37cc5c67dfa964..24d1a8d5a594b8d727ab6a0b8e572581625fa587 100644 --- a/lib/interface.ml +++ b/lib/interface.ml @@ -16,7 +16,13 @@ let parse_file f filename = result with Parser.Error -> close_in in_channel; - raise (Failure ("Invalid Syntax line " ^ string_of_int !Lexer.numero_ligne ^". Problem close to character " ^string_of_int !Lexer.numero_carac^" '" ^ Lexing.lexeme lexbuf ^"'")) + close_in in_channel; + let pos = Lexing.lexeme_start_p lexbuf in + Printf.eprintf "Error at line %d, column %d: Problem close to %s \n" + pos.Lexing.pos_lnum + (pos.Lexing.pos_cnum - pos.Lexing.pos_bol) + (Lexing.lexeme lexbuf); + exit 1 let parse_description filename = parse_file Parser.description filename diff --git a/lib/lexer.mll b/lib/lexer.mll index dad71b358ccc297ccc7e8622a33ec24e093268a6..f96a35cc951416a2c5d7affe62528de5a0b28a51 100644 --- a/lib/lexer.mll +++ b/lib/lexer.mll @@ -64,53 +64,59 @@ rule token = parse | "Fullcompass" {FULLCOMPASS} | "Chirality" {CHIRALITY} | "1-Axe Orientation" {ONEAXEORIEN} - | "[" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; BRACKETOPEN} - | "]" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; BRACKETCLOSE} - | "." {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; POINT} - | ":" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; COLON} - | ";" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; SEMICOLON} - | "(" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; PARENOPEN} - | ")" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; PARENCLOSE} - | "if" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; IF} - | "then" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; THEN} - | "else" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; ELSE} - | "true" | "false" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; BOOLEAN (bool_of_string (Lexing.lexeme lexbuf))} - | "match" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; MATCH} - | "with" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; WITH} - | "|" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; PIPE} - | "end" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; END} - | "->" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; ARROW} - | "=>" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; IMPLICATION} - | "<=>" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; EQUIVALENCE} - | "let" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; LET} - | "=" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; AFFECT} - | "in" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; IN} - | ":=" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; IS} - | '+' {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; PLUS} - | '-' {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; MINUS} - | '*' {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; MULT} - | '/' {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; DIV} - | "&&" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; AND} - | "||" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; OR} - | '>' {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; SUP} - | '<' {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; INF} - | "==" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; EQUAL} - | "!=" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; DIFF} - | "!" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; NOT} - | "fun" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; FUN} - | "/*" {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; comment lexbuf} - | integer {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; INT (int_of_string (Lexing.lexeme lexbuf))} - | float {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; FLOAT (float_of_string(Lexing.lexeme lexbuf) )} - | "," {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; COMMA} - | keyword {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; KEYWORD (Lexing.lexeme lexbuf)} - | variable {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; VARIABLE (Lexing.lexeme lexbuf)} - | space {numero_carac := !numero_carac + (String.length (Lexing.lexeme lexbuf)) ; token lexbuf} - | "\n" {numero_ligne := !numero_ligne + 1; numero_carac := 1 ; token lexbuf} + | "[" { BRACKETOPEN} + | "]" { BRACKETCLOSE} + | "." { POINT} + | ":" { COLON} + | ";" { SEMICOLON} + | "(" { PARENOPEN} + | ")" { PARENCLOSE} + | "if" { IF} + | "then" { THEN} + | "else" { ELSE} + | "true" | "false" { BOOLEAN (bool_of_string (Lexing.lexeme lexbuf))} + | "match" { MATCH} + | "with" { WITH} + | "|" { PIPE} + | "end" { END} + | "->" { ARROW} + | "=>" { IMPLICATION} + | "<=>" { EQUIVALENCE} + | "let" { LET} + | "=" { AFFECT} + | "in" { IN} + | ":=" { IS} + | '+' { PLUS} + | '-' { MINUS} + | '*' { MULT} + | '/' { DIV} + | "&&" { AND} + | "||" { OR} + | '>' { SUP} + | '<' { INF} + | "==" { EQUAL} + | "!=" { DIFF} + | "!" { NOT} + | "fun" { FUN} + | "/*" { comment lexbuf} + | integer { INT (int_of_string (Lexing.lexeme lexbuf))} + | float { FLOAT (float_of_string(Lexing.lexeme lexbuf) )} + | "," { COMMA} + | keyword { KEYWORD (Lexing.lexeme lexbuf)} + | variable { VARIABLE (Lexing.lexeme lexbuf)} + | space { let pos = lexbuf.Lexing.lex_curr_p in + lexbuf.Lexing.lex_curr_p <- { pos with Lexing.pos_cnum = pos.Lexing.pos_cnum + 1; }; + token lexbuf} + | "\n" { let pos = lexbuf.Lexing.lex_curr_p in + lexbuf.Lexing.lex_curr_p <- { pos with Lexing.pos_lnum = pos.Lexing.pos_lnum + 1; Lexing.pos_bol = pos.Lexing.pos_cnum; }; + token lexbuf + } | eof {EOF} - | _ {raise (Failure ("Character not allowed in source text line "^ string_of_int !numero_ligne^" character "^string_of_int !numero_carac^": '" ^ - Lexing.lexeme lexbuf ^ "'"))} + and comment = parse - | "*/" {numero_carac := !numero_carac + 1 ;token lexbuf} - | "\n" {numero_ligne := !numero_ligne + 1 ;numero_carac := 1; comment lexbuf} - | _ {numero_carac := !numero_carac + 1 ;comment lexbuf} + | "*/" {token lexbuf} + | "\n" {let pos = lexbuf.Lexing.lex_curr_p in + lexbuf.Lexing.lex_curr_p <- { pos with Lexing.pos_lnum = pos.Lexing.pos_lnum + 1; Lexing.pos_bol = pos.Lexing.pos_cnum; }; + comment lexbuf} + | _ {comment lexbuf}