/**
 * Strategies to handle the location of terms.
 * These strategies can only be used on terms that are annotated with their
 * position-info, i.e. are parsed with the option --preserve-positions 
 */
module php/strategy/location

strategies

  /**
   * Checks for position info annotations
   */
  has-position-info =
   where(get-raw-area-in-file)
     
  /**
   * Several getter-methods
   */
  get-start-line    = get-specific-child(|0)

  get-end-line      = get-specific-child(|2)

  get-start-column  = get-specific-child(|1)

  get-end-column    = get-specific-child(|3)

  get-length        = get-specific-child(|5)

  get-offset        = get-specific-child(|4)

  get-filename      = 
    get-raw-posinfo ; ?(_,x) ; !x
 
  /**
   * For internal purposes only
   */
  get-raw-area-in-file = 
    get-raw-posinfo ; ?(x,_) ; !x
  
  get-raw-posinfo =                                             log(|Debug(), "Getting raw pos-info.");
     ?t{annos*}                                               ; log(|Vomit(), "On term: "  , <id>)
   ; <fetch-elem(?[pos-info,area-in-file(name,area)])> annos* ; log(|Vomit(), "Raw-area: " , <id>)
   ; !(area,name)                                             ; log(|Vomit(), "Result: "   , <id>)

  get-specific-child(|pos) = 
     get-raw-area-in-file
   ; ?area#(chlds)
   ; <at-index(?res)> (pos,chlds)
   ; !res

strategies
  /**
   * Formats a given string with information about the current term.
   * Within the string the following 'holes' are filled with the 
   * appropiate information from the location of the term:
   *   - [STARTLINE]     - [STARTCOLUMN]
   *   - [ENDLINE]       - [ENDCOLUMN]
   *   - [OFFSET]        - [LENGTH]
   *   - [FILENAME]
   * Note that the holes should be wrapped in '[' and ']'.
   *
   * @param msg String to be formatted
   * @type ATerm -> String
   */
  format-location-string(|message) = ?term         ; log(|Debug(), "Making message of ", term)
    ; !message                                     ; log(|Debug(), "Using message: ", message)
    ; string-replace(|"[STARTLINE]"  ,<get-start-line  ; int-to-string> term) ; log(|Vomit(), "Startline.")
    ; string-replace(|"[ENDLINE]"    ,<get-end-line    ; int-to-string> term) ; log(|Vomit(), "Endline.")
    ; string-replace(|"[OFFSET]"     ,<get-offset      ; int-to-string> term) ; log(|Vomit(), "Offset.")
    ; string-replace(|"[STARTCOLUMN]",<get-start-column; int-to-string> term) ; log(|Vomit(), "Start column.")
    ; string-replace(|"[ENDCOLUMN]"  ,<get-end-column  ; int-to-string> term) ; log(|Vomit(), "End-column.")
    ; string-replace(|"[LENGTH]"     ,<get-length      ; int-to-string> term) ; log(|Vomit(), "Get-length.")
    ; string-replace(|"[FILENAME]"   ,<get-filename                   > term) ; log(|Vomit(), "Filename, done.")