X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=src%2Fparse.ml;h=da42b87a378315624991a6ca882c858b4ed7d46c;hb=54c8ad92025a9c77c2b10644499b3944e1299187;hp=6b7c03d72872362731862f6a9cd0c24034747f7b;hpb=5a6a8b2b8e515941f9a2b3cc051da646ae696251;p=goals.git diff --git a/src/parse.ml b/src/parse.ml index 6b7c03d..da42b87 100644 --- a/src/parse.ml +++ b/src/parse.ml @@ -17,31 +17,51 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) -open Lexer open Lexing - open Printf -let parse lexbuf = - let print_position fp lexbuf = - let pos = lexbuf.lex_curr_p in - fprintf fp "%s:%d:%d" - pos.pos_fname pos.pos_lnum (pos.pos_cnum - pos.pos_bol) - in +open Utils +open Lexer + +let string_position () lexbuf = + let pos = lexbuf.lex_curr_p in + sprintf "%s:%d:%d" pos.pos_fname pos.pos_lnum (pos.pos_cnum - pos.pos_bol) + +let parse_file env lexbuf = + try + let env' = Parser.file Lexer.read lexbuf in + Ast.Env.merge env env' + with + | SyntaxError msg -> + failwithf "%a: %s" string_position lexbuf msg + | Parser.Error -> + failwithf "%a: parse error" string_position lexbuf - try Parser.file Lexer.read lexbuf +let parse_expr lexbuf = + try Parser.expr_only Lexer.read lexbuf with | SyntaxError msg -> - eprintf "%a: %s\n" print_position lexbuf msg; - exit 1 + failwithf "%a: %s" string_position lexbuf msg | Parser.Error -> - eprintf "%a: parse error\n" print_position lexbuf; - exit 1 + failwithf "%a: parse error" string_position lexbuf -let parse_from_file filename = +(* This is used to parse the Goalfile. *) +let parse_goalfile env filename = + Cmdline.debug "parse file: %s" filename; let fp = open_in filename in - let lexbuf = Lexing.from_channel fp in - lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = filename }; - let file : Ast.file = parse lexbuf in + let lexbuf = Lexing.from_channel ~with_positions:true fp in + let pos = lexbuf.lex_curr_p in + lexbuf.lex_curr_p <- { pos with pos_fname = filename }; + let env' = parse_file env lexbuf in close_in fp; - file + env' + +(* This is used to parse expressions on the command line and + * the output from functions. + *) +let parse_expr source str = + Cmdline.debug "parse expression: %S" str; + let lexbuf = Lexing.from_string ~with_positions:true str in + let pos = lexbuf.lex_curr_p in + lexbuf.lex_curr_p <- { pos with pos_fname = source }; + parse_expr lexbuf