From c2f87ff1e6e5676968804cd50b86fc2f0f9ad672 Mon Sep 17 00:00:00 2001 From: Chimrod <> Date: Sat, 7 Oct 2023 12:24:37 +0200 Subject: Made explicit the use of the report in the parser --- lib/qparser/parser.mly | 58 +++++++++++++++++++++++++++++++++++------ lib/qparser/qsp_expression.mly | 4 +-- lib/qparser/qsp_instruction.mly | 22 +++++++++++----- 3 files changed, 67 insertions(+), 17 deletions(-) (limited to 'lib/qparser') diff --git a/lib/qparser/parser.mly b/lib/qparser/parser.mly index 84c1af8..8547e17 100644 --- a/lib/qparser/parser.mly +++ b/lib/qparser/parser.mly @@ -1,6 +1,23 @@ %{ module T = Qsp_syntax.T + open StdLabels + + type action_block = + { loc : Qsp_syntax.S.pos + ; expression : + Qsp_syntax.Report.t list + -> Analyzer.Expression.t' * Qsp_syntax.Report.t list + ; body : Analyzer.Instruction.t Qsp_syntax.S.repr list + ; pos : Qsp_syntax.S.pos + ; else_ : ( + ( Analyzer.Instruction.clause list + * Analyzer.Instruction.t Qsp_syntax.S.repr list + ) option ) + } + + module Helper = Qsp_syntax.S.Helper(Analyzer.Expression) + module HelperI = Qsp_syntax.S.Helper(Analyzer.Instruction) %} %parameter @@ -13,10 +30,11 @@ main: | before_location* LOCATION_START EOL+ - expressions = line_statement* + instructions = line_statement* LOCATION_END { - Analyzer.Location.location $loc expressions + let instructions = List.map instructions ~f:(HelperI.v) in + Analyzer.Location.location $loc instructions } before_location: @@ -31,20 +49,20 @@ line_statement: | s = terminated(inline_action, line_sep) { s } | a = action_bloc(IF, elif_else_body) - { let loc, expression, statements, loc_s, body = a in - let elifs, else_ = match body with + { let {loc; expression; body; pos; else_ } = a in + let elifs, else_ = match else_ with | None -> [], [] | Some (elifs, else_) -> (elifs, else_) in Analyzer.Instruction.if_ loc - (loc_s, expression, statements) + (pos, expression, body) ~elifs ~else_ } | a = action_bloc(ACT, empty_body) - { let loc, label, statements, _, _ = a in - Analyzer.Instruction.act loc ~label statements + { let {loc; expression; body; _} = a in + Analyzer.Instruction.act loc ~label:expression body } (** Represent an instruction which can either be on a single line, @@ -58,7 +76,31 @@ line_statement: b = BODY END TOKEN? line_sep - { $loc, e, s, $loc(s), b } + { + let expression = Helper.v e in + let else_ = match b with + | None -> None + | Some (elifs, else_) -> + let elifs = begin match elifs with + | [] -> [] + | _ -> + List.map elifs + ~f:(fun ((pos:Qsp_syntax.S.pos), e, instructions) -> + let e = Helper.v e in + (pos, e, instructions) + ) + + end in + Some (elifs, else_) + in + + { loc = $loc + ; expression + ; body = s + ; else_ = else_ + ; pos = $loc(s) + } + } empty_body: | { None } diff --git a/lib/qparser/qsp_expression.mly b/lib/qparser/qsp_expression.mly index 06cfadd..799be31 100644 --- a/lib/qparser/qsp_expression.mly +++ b/lib/qparser/qsp_expression.mly @@ -20,7 +20,7 @@ *) %inline argument(X): - | a = delimited(L_PAREN, arguments(X), R_PAREN) { a } + | a = delimited(L_PAREN, arguments(X), R_PAREN) { a } | a = X { [ a ] } (** Declare an expression *) @@ -82,5 +82,5 @@ unary_operator: (* No declaration, consider index at 0 *) None | Some other -> other in - Analyzer.Expression.{ pos = $loc ; name ; index } + Qsp_syntax.S.{ pos = $loc ; name ; index } } diff --git a/lib/qparser/qsp_instruction.mly b/lib/qparser/qsp_instruction.mly index bc1ca37..fe8a51a 100644 --- a/lib/qparser/qsp_instruction.mly +++ b/lib/qparser/qsp_instruction.mly @@ -4,6 +4,8 @@ optionnal_delimited(opening, X, closing): | v = delimited(opening, X, closing) { v } | v = X { v } +(* Redefine the arguments from expression here because we accept + * values without parens. *) argument(X): | a = optionnal_delimited(L_PAREN, arguments(X), R_PAREN) { a } | a = X { [ a ] } @@ -16,44 +18,47 @@ argument(X): %public inline_action: | a = onliner(ACT) { let loc, label, statements, _, _ = a in + let label = Helper.v label in Analyzer.Instruction.act loc ~label statements } | a = onliner(IF) else_opt = preceded(ELSE, instruction)? - { let loc, expression, statements, loc_s, _body = a in + { let loc, expr, statements, loc_s, _body = a in let elifs = [] and else_ = Option.to_list else_opt in Analyzer.Instruction.if_ loc - (loc_s, expression, statements) + (loc_s, Helper.v expr, statements) ~elifs ~else_ } | a = onliner(IF) else_= preceded(ELSE, inline_action) - { let loc, expression, statements, loc_s, _body = a in + { let loc, expr, statements, loc_s, _body = a in let elifs = [] and else_ = [ else_ ] in + Analyzer.Instruction.if_ loc - (loc_s, expression, statements) + (loc_s, Helper.v expr, statements) ~elifs ~else_ } single_instruction: | expr = expression { + let expr = Helper.v expr in Analyzer.Instruction.expression expr } | e = let_assignation { e } | k = keyword - arg = argument(expression) + args = argument(expression) { - Analyzer.Instruction.call $loc k arg + let args = List.map args ~f:(Helper.v) in + Analyzer.Instruction.call $loc k args } keyword: - (*| STAR k = KEYWORD { "*" ^ k }*) | k = KEYWORD { k } let_assignation: @@ -62,6 +67,9 @@ let_assignation: op = assignation_operator value = expression { + let variable = Helper.variable variable + and value = Helper.v value in + Analyzer.Instruction.assign $loc variable op value } -- cgit v1.2.3