From 8e1d9182f7573112cb36c7d0ff0c8612f34ffd57 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sun, 22 Dec 2019 16:59:59 +0000 Subject: [PATCH] Expose parsing of expressions from command line. --- src/main.ml | 9 +++------ src/parse.ml | 32 ++++++++++++++++++++++++-------- src/parse.mli | 4 ++-- src/parser.mly | 4 ++-- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/main.ml b/src/main.ml index 643e1b5..be1baff 100644 --- a/src/main.ml +++ b/src/main.ml @@ -50,7 +50,7 @@ let main () = let filename = !filename in (* Parse the input file. *) - let file = Parse.parse_from_file filename in + let file = Parse.parse_goalfile filename in Ast.print_file stdout file; @@ -65,7 +65,7 @@ let main () = function | Ast.Goal (name, [], _, _, _) -> if !initial_targets = [] then - initial_targets := name :: !initial_targets + initial_targets := Ast.ECall (name, []) :: !initial_targets | Ast.Goal (name, _, _, _, _) -> if !initial_targets = [] then failwithf "%s: first target ‘%s’ has parameters and so cannot be used as the default target" @@ -74,9 +74,6 @@ let main () = ) file; let initial_targets = List.rev !initial_targets in - - eprintf "initial targets:"; - List.iter (eprintf " %s") initial_targets; - eprintf "\n" + ignore initial_targets let () = main () diff --git a/src/parse.ml b/src/parse.ml index 6b7c03d..9363143 100644 --- a/src/parse.ml +++ b/src/parse.ml @@ -22,13 +22,12 @@ 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 +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) +let parse_file lexbuf = try Parser.file Lexer.read lexbuf with | SyntaxError msg -> @@ -38,10 +37,27 @@ let parse lexbuf = eprintf "%a: parse error\n" print_position lexbuf; exit 1 -let parse_from_file filename = +let parse_expr lexbuf = + try Parser.expr Lexer.read lexbuf + with + | SyntaxError msg -> + eprintf "%a: %s\n" print_position lexbuf msg; + exit 1 + | Parser.Error -> + eprintf "%a: parse error\n" print_position lexbuf; + exit 1 + +(* This is used to parse the Goalfile. *) +let parse_goalfile 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 file : Ast.file = parse_file lexbuf in close_in fp; file + +(* This is used to parse dependency expressions on the command line. *) +let parse_cli_expr str = + let lexbuf = Lexing.from_string str in + lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = "" }; + parse_expr lexbuf diff --git a/src/parse.mli b/src/parse.mli index 692524c..7535de2 100644 --- a/src/parse.mli +++ b/src/parse.mli @@ -17,5 +17,5 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) -val parse : Lexing.lexbuf -> Ast.file -val parse_from_file : string -> Ast.file +val parse_goalfile : string -> Ast.file +val parse_cli_expr : string -> Ast.expr diff --git a/src/parser.mly b/src/parser.mly index 4cb18af..add72bb 100644 --- a/src/parser.mly +++ b/src/parser.mly @@ -36,11 +36,11 @@ open Printf %token RIGHT_PAREN %token STRING -%type expr %type stmt -(* Start non-terminal. *) +(* Start nonterminals. *) %start file +%start expr %% file: -- 1.8.3.1