# Last edited on 2000-05-23 16:50:52 by stolfi

function get_components(w)
{
  # 
  # Extracts from the simple word "w" the core/mantle/crust
  # components, assuming they are delimited by nested braces "{}".
  #
  # The components are returned in the global variables
  #
  #   core
  #   premantle mantle sufmantle 
  #   precrust crust sufcrust
  #
  # Also sets the global variable "maxlevel" to the 
  # depth of the nesting: 
  # 
  #   maxlevel = 0  -- empty word
  #   maxlevel = 1  -- nonempty crust, no core/mantle
  #   maxlevel = 2  -- nonempty mantle, no core
  #   maxlevel = 3  -- nonempty core
  #
  # The variable "mantle" is set only if there is no core
  # ("maxlevel" <= 2), otherwise "premantle" and "postmantle"
  # are set instead.  Similarly, "crust" is set only if 
  # there is no core-mantle ("maxlevel" <= 1), otherwise
  # "precrust" and "sufcrust" are set intead.
  
  core = "";
  premantle = ""; mantle = ""; sufmantle = "";
  precrust = ""; crust = ""; sufcrust = "";
  maxlevel = 0;

  # Pick crust components:
  if (w != "")
    { if (match(w, /^[^()<>]*$/))
        { crust = w; w = ""; }
      else 
        { if (! match(w, /^[^()<>]*[(<]/)) { error(("bad split \"" w "\"")); }
          precrust = substr(w,RSTART,RLENGTH-1); w = substr(w,RLENGTH);
          if (! match(w, /[>)][^()<>]*$/)) { error(("bad split \"" w "\"")); }
          sufcrust = substr(w,RSTART+1,RLENGTH-1); w = substr(w,1,RSTART);
        }
      maxlevel++;     
    }
  
  # Pick mantle components:
  if (w != "")
    { if (match(w, /^[(][^<>]*[)]$/))
        { mantle = substr(w,RSTART+1,RLENGTH-2); w = "";
          gsub(/[()<>]/, "", mantle);
        }
      else 
        { if (! match(w, /^[(][^<>]*[<]/)) { error(("bad split \"" w "\"")); }
          premantle = substr(w,RSTART+1,RLENGTH-2); w = substr(w,RLENGTH);
          gsub(/[()<>]/, "", premantle);
          if (! match(w, /[>][^<>]*[)]$/)) { error(("bad split \"" w "\"")); }
          sufmantle = substr(w,RSTART+1,RLENGTH-2); w = substr(w,1,RSTART);
          gsub(/[()<>]/, "", sufmantle);
        }
      maxlevel++;     
    }
  
  # Pick core component(s):
  if (w != "")
    { if (match(w, /^[<].*[>]$/))
        { core = substr(w,RSTART+1,RLENGTH-2); w = "";
          gsub(/[()<>]/, "", core);
        }
      else 
        { error(("bad split \"" w "\"")); }
      maxlevel++;     
    }
}