Commit f0e52e86 authored by POTTIER Francois's avatar POTTIER Francois

More refactoring. Moved the interesting functions [rename_sw_outer]

and [rename_sw_inner] to [Nonterminaldefinitioninlining], where they
belong.
parent be7b4b48
......@@ -80,31 +80,6 @@ let extend x y (phi : subst ref) =
type sw =
Keyword.subject * Keyword.where
type keyword_renaming =
string * sw * sw
let rename_sw_outer
((psym, first_prod, last_prod) : keyword_renaming)
(subject, where) : sw option =
match subject with
| RightNamed s ->
if s = psym then
match where with
| WhereStart -> Some first_prod
| WhereEnd -> Some last_prod
else
None
| Left ->
None
let rename_sw_inner
((_, first_prod, last_prod) : keyword_renaming)
(subject, where) : sw option =
match subject, where with
| Left, WhereStart -> Some first_prod
| Left, WhereEnd -> Some last_prod
| RightNamed _, _ -> None
(* [rename_keyword f phi keyword] applies the function [f] to possibly change
the keyword [keyword]. If [f] decides to change this keyword (by returning
[Some _]) then this decision is obeyed. Otherwise, the keyword is renamed
......@@ -129,10 +104,11 @@ let rename_keyword (f : sw -> sw option) (phi : subst ref) keyword : keyword =
phi;
Position (subject', where', flavor)
(* [rename f phi a] applies to the semantic action [a] the renaming [phi]
as well as the renaming decisions made by the function [f]. [f] is
applied to (not-yet-renamed) keywords and may decide to change them
(by returning [Some _]). *)
(* [rename f phi a] applies to the semantic action [a] the renaming [phi] as
well as the transformations decided by the function [f]. The function [f] is
applied to each (not-yet-renamed) keyword and may decide to transform it, by
returning [Some _], or to not transform it, by returning [None]. (In the
latter case, [phi] still applies to the keyword.) *)
let rename f phi a =
......@@ -154,12 +130,6 @@ let rename f phi a =
keywords = keywords;
}
let rename_outer renaming =
rename (rename_sw_outer renaming)
let rename_inner renaming =
rename (rename_sw_inner renaming)
let to_il_expr action =
action.expr
......
......@@ -7,26 +7,22 @@ val compose : string -> t -> t -> t
(* TEMPORARY document this: *)
type subst =
(string * string) list
type sw =
Keyword.subject * Keyword.where
type keyword_renaming =
string * sw * sw
(** [rename_inner keyword_renaming phi a] builds the action
[let x1 = x1' and ... xn = xn' in a] if [phi] is [(x1, x1') ... (xn, xn')].
Moreover, [renaming_env] is used to correctly replace $startpos/$endpos
present in the semantic action. *)
val rename_inner:
keyword_renaming
-> (string * string) list -> t -> t
(** [rename_outer keyword_renaming phi a] updates the occurrences of the
inlined non terminal in the action [a].
*)
val rename_outer:
keyword_renaming
-> (string * string) list -> t -> t
(** [rename f phi a] applies to the semantic action [a] the renaming [phi] as
well as the transformations decided by the function [f]. The function [f] is
applied to each (not-yet-renamed) keyword and may decide to transform it, by
returning [Some _], or to not transform it, by returning [None]. (In the
latter case, [phi] still applies to the keyword.) *)
val rename:
(sw -> sw option) ->
subst ->
t ->
t
(** Semantic actions are translated into [IL] code using the
[IL.ETextual] and [IL.ELet] constructors. *)
......
open Keyword
open UnparameterizedSyntax
open ListMonad
......@@ -9,6 +10,34 @@ type 'a color =
| BeingExpanded
| Expanded of 'a
(* [rename_sw_outer] transforms the keywords in the outer production (the caller)
during inlining. *)
let rename_sw_outer
(psym, first_prod, last_prod)
(subject, where) =
match subject with
| RightNamed s ->
if s = psym then
match where with
| WhereStart -> Some first_prod
| WhereEnd -> Some last_prod
else
None
| Left ->
None
(* [rename_sw_inner] transforms the keywords in the inner production (the callee)
during inlining. *)
let rename_sw_inner
(_, first_prod, last_prod)
(subject, where) =
match subject, where with
| Left, WhereStart -> Some first_prod
| Left, WhereEnd -> Some last_prod
| RightNamed _, _ -> None
(* Inline a grammar. The resulting grammar does not contain any definitions
that can be inlined. *)
let inline grammar =
......@@ -129,7 +158,7 @@ let inline grammar =
| (_, x) :: _ -> (Keyword.RightNamed x, Keyword.WhereStart)
in
let renaming_env =
let renaming =
(psym, start_position, end_position)
in
(* Rename the host semantic action.
......@@ -138,10 +167,10 @@ let inline grammar =
the first producer of the inlined branch if it is not empty or
the preceding producer found in the prefix. *)
let outer_action =
Action.rename_outer renaming_env [] b.action
Action.rename (rename_sw_outer renaming) [] b.action
in
let action' =
Action.rename_inner renaming_env phi pb.action
Action.rename (rename_sw_inner renaming) phi pb.action
in
{ b with
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment