Commit 6c87ce16 authored by POTTIER Francois's avatar POTTIER Francois

Create a new auxiliary function [is_inline_nonterminal].

Use it to simplify [find_inlining_site].
Also, simplify the result of [find_inlining_site].
parent 05014472
...@@ -141,30 +141,39 @@ let rename (used : StringSet.t) producers: Action.subst * producers = ...@@ -141,30 +141,39 @@ let rename (used : StringSet.t) producers: Action.subst * producers =
in in
phi, List.rev producers' phi, List.rev producers'
(* [is_inline_nonterminal grammar symbol] tells whether [symbol] is a nonterminal
symbol (as opposed to a terminal symbol) and is marked %inline. *)
let is_inline_nonterminal grammar symbol : bool =
match StringMap.find symbol grammar.rules with
| rule ->
(* This is a nonterminal symbol. Test its %inline flag. *)
rule.inline_flag
| exception Not_found ->
(* This is a terminal symbol. *)
false
(* [find_inlining_site grammar (prefix, suffix)] traverses a list of producers (* [find_inlining_site grammar (prefix, suffix)] traverses a list of producers
that is already decomposed as [List.rev prefix @ suffix]. It looks for the that is already decomposed as [List.rev prefix @ suffix]. It looks for the
first nonterminal symbol that can be inlined away. If it does not find one, first nonterminal symbol that can be inlined away. If it does not find one,
it raises [NoInlining]. *) it raises [NoInlining]. If it does find one, it returns a decomposition of
the whole list of producers under the form [prefix @ producer :: suffix]. *)
let rec find_inlining_site grammar (prefix, suffix) = let rec find_inlining_site grammar (prefix, suffix) =
match suffix with match suffix with
| [] -> | [] ->
raise NoInlining raise NoInlining
| x :: xs -> | producer :: suffix ->
let nt = producer_symbol x let symbol = producer_symbol producer in
and id = producer_identifier x in if is_inline_nonterminal grammar symbol then begin
match StringMap.find nt grammar.rules with (* We have checked earlier than an %inline symbol does not carry
| r when r.inline_flag -> any attributes. In addition, we now check that the use site of
(* We have checked earlier than an %inline symbol does not carry this symbol does not carry any attributes either. Thus, we need
any attributes. In addition, we now check that the use site of not worry about propagating these attributes through inlining. *)
this symbol does not carry any attributes either. Thus, we need check_no_producer_attributes producer;
not worry about propagating these attributes through inlining. *) List.rev prefix, producer, suffix
check_no_producer_attributes x; end
(* We inline the rule [r] into [b] between [prefix] and [xs]. *) else
List.rev prefix, nt, r, id, xs find_inlining_site grammar (producer :: prefix, suffix)
| _ ->
find_inlining_site grammar (x :: prefix, xs)
| exception Not_found ->
find_inlining_site grammar (x :: prefix, xs)
(* Inline a grammar. The resulting grammar does not contain any definitions (* Inline a grammar. The resulting grammar does not contain any definitions
that can be inlined. *) that can be inlined. *)
...@@ -193,7 +202,10 @@ let inline grammar = ...@@ -193,7 +202,10 @@ let inline grammar =
let rec expand_branch (b : branch) : branch ListMonad.m = let rec expand_branch (b : branch) : branch ListMonad.m =
try try
(* [c] is the identifier under which the callee is known. *) (* [c] is the identifier under which the callee is known. *)
let prefix, nt, p, c, suffix = find_inlining_site grammar ([], b.producers) in let prefix, producer, suffix = find_inlining_site grammar ([], b.producers) in
let nt = producer_symbol producer in
let p = StringMap.find nt grammar.rules in (* cannot fail *)
let c = producer_identifier producer in
let p = expand_rule nt p in let p = expand_rule nt p in
(* These are the names of the producers in the host branch, (* These are the names of the producers in the host branch,
minus the producer that is being inlined away. *) minus the producer that is being inlined away. *)
......
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