Commit b812de8d authored by Bruno Guillaume's avatar Bruno Guillaume

Check for duplicate use of id in packages

parent 9f3e91e3
......@@ -482,15 +482,14 @@ module New_ast = struct
| Seq of strat list (* a sequence of strategies to apply one after the other *)
| Iter of strat (* a strategy to apply iteratively *)
| If of strat * strat * strat (* choose a stragegy with a test *)
(* syntactic sugar *)
| Try of strat (* ≜ If (S, S, Empty): pick one normal form a the given strategy; return input if nf *)
type decl =
| Features of Ast.feature_spec list
| Labels of (string * string list) list
| Package of (Ast.simple_ident * decl list)
| Package of (Loc.t * Ast.simple_ident * decl list)
| Rule of Ast.rule
| Strategy of (Ast.simple_ident * strat)
| Strategy of (Loc.t * Ast.simple_ident * strat)
| Import of string
| Include of string
......
......@@ -253,15 +253,14 @@ module New_ast : sig
| Seq of strat list (* a sequence of strategies to apply one after the other *)
| Iter of strat (* a strategy to apply iteratively *)
| If of strat * strat * strat (* choose a stragegy with a test *)
(* syntactic sugar *)
| Try of strat (* ≜ If (S, S, Empty): pick one normal form a the given strategy; return input if nf *)
type decl =
| Features of Ast.feature_spec list
| Labels of (string * string list) list
| Package of (Ast.simple_ident * decl list)
| Package of (Loc.t * Ast.simple_ident * decl list)
| Rule of Ast.rule
| Strategy of (Ast.simple_ident * strat)
| Strategy of (Loc.t * Ast.simple_ident * strat)
| Import of string
| Include of string
......
......@@ -643,9 +643,9 @@ module New_grs = struct
let rec build_decl ?domain = function
| New_ast.Package (name, decl_list) -> Package (name, List.map build_decl decl_list)
| New_ast.Package (loc, name, decl_list) -> Package (name, List.map build_decl decl_list)
| New_ast.Rule ast_rule -> Rule (Rule.build ?domain "TODO" ast_rule)
| New_ast.Strategy (name, ast_strat) -> Strategy (name, ast_strat)
| New_ast.Strategy (loc, name, ast_strat) -> Strategy (name, ast_strat)
| _ -> Error.bug "[build_decl] Inconsistent ast for new_grs"
let domain t = t.domain
......
......@@ -95,6 +95,34 @@ module Loader = struct
Ast.strategies = grs_wi.Ast.strategies_wi;
}
let rec check_duplicate_id id = function
| [] -> None
| New_ast.Rule r :: _ when r.Ast.rule_id = id -> Some r.Ast.rule_loc
| New_ast.Package (loc, name, _) :: _ when name = id -> Some loc
| New_ast.Strategy (loc, name, _) :: _ when name = id -> Some loc
| _ -> None
let rec check_grs = function
| [] -> ()
| New_ast.Rule r :: tail ->
begin
match check_duplicate_id r.Ast.rule_id tail with
| None -> ()
| Some loc -> Error.build "Identifier \"%s\" is used twice in the same package (%s and %s)"
r.Ast.rule_id (Loc.to_string r.Ast.rule_loc) (Loc.to_string loc)
end;
check_grs tail
| New_ast.Strategy (loc, name, _) :: tail
| New_ast.Package (loc, name, _) :: tail ->
begin
match check_duplicate_id name tail with
| None -> ()
| Some loc2 -> Error.build "Identifier \"%s\" is used twice in the same package (%s and %s)"
name (Loc.to_string loc) (Loc.to_string loc2)
end;
check_grs tail
| _ :: tail -> check_grs tail
let rec loc_new_grs file =
try
Global.new_file file;
......@@ -113,17 +141,20 @@ module Loader = struct
| None -> Error.build "Imported file must have the \".grs\" file extension" in
let sub = loc_new_grs filename in
let unfolded_sub = unfold_new_grs false sub in
New_ast.Package (pack_name, unfolded_sub) :: acc
New_ast.Package (Loc.file filename, pack_name, unfolded_sub) :: acc
| New_ast.Include filename ->
let sub = loc_new_grs filename in
let unfolded_sub = unfold_new_grs top sub in
unfolded_sub @ acc
| New_ast.Features _ when not top -> Error.bug "Non top features declaration"
| New_ast.Labels _ when not top -> Error.bug "Non top labels declaration"
| New_ast.Features _ when not top -> Error.build "Non top features declaration"
| New_ast.Labels _ when not top -> Error.build "Non top labels declaration"
| x -> x :: acc
) [] new_ast_grs
let new_grs file = unfold_new_grs true (loc_new_grs file)
let new_grs file =
let final_grs = unfold_new_grs true (loc_new_grs file) in
check_grs final_grs;
final_grs
(* ------------------------------------------------------------------------------------------*)
let gr file =
......
......@@ -754,13 +754,13 @@ new_grs:
| decls = list(decl) EOF { decls }
decl:
| f=features_group { New_ast.Features f }
| l=labels { New_ast.Labels l }
| r=rule { New_ast.Rule r }
| IMPORT f=STRING { New_ast.Import f }
| INCL f=STRING { New_ast.Include f }
| PACKAGE id_loc=simple_id_with_loc LACC l=list(decl) RACC { New_ast.Package (fst id_loc, l) }
| STRAT id_loc=simple_id_with_loc LACC d = strat_desc RACC { New_ast.Strategy (fst id_loc, d) }
| f=features_group { New_ast.Features f }
| l=labels { New_ast.Labels l }
| r=rule { New_ast.Rule r }
| IMPORT f=STRING { New_ast.Import f }
| INCL f=STRING { New_ast.Include f }
| PACKAGE id_loc=simple_id_with_loc LACC l=list(decl) RACC { New_ast.Package (snd id_loc, fst id_loc, l) }
| STRAT id_loc=simple_id_with_loc LACC d = strat_desc RACC { New_ast.Strategy (snd id_loc, fst id_loc, d) }
strat_desc:
| id = node_id { New_ast.Ref id }
......
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