/**
 * Main module for parsing PHP sources for version 4 and 5.
 *
 * @author Eric Bouwers
 */
module php/parse/main
imports
  php/parse/options
  php/parse/tables
  php/parse/postprocess

/**
 * Actual parse strategies. There are strtegies for:
 *  - files
 *  - streams
 *  - strings
 */
strategies

  /**
   * @type FILE -> Document or TemplateDocument
   */
  parse-php =
    parse-php(|<get-start-symbol>)

  parse-php(|symbol) =
     where(?FILE(name) ; <set-current-php-file> name) // current file being processed
   ; parse-xtc-file-pt-report-errors(|<get-parse-table(|symbol); php-open-parse-table>, symbol)
   ; process-parse-php-options

  /**
   * @type FILE -> Document or TemplateDocument
   */
  parse-php4 =
    using-php4(parse-php)

  /**
   * @type FILE -> Document or TemplateDocument
   */
  parse-php5 =
    using-php5(parse-php)


  /**
   * @type String -> Document or TemplateDocument
   */
  parse-php4-string =
    using-php4(parse-php-string)
  
  /**
   * @type String -> Document or TemplateDocument
   * @param Symbol (String or None())
   */
  parse-php4-string(|symbol) =
    using-php4(parse-php-string(|symbol))
  
  /**
   * @type String -> Document or TemplateDocument
   */
  parse-php5-string =
    using-php5(parse-php-string)
  
  /**
   * @type String -> Document or TemplateDocument
   * @param Symbol (String or None())
   */
  parse-php5-string(|symbol) =
    using-php5(parse-php-string(|symbol))
  
  /**
   * Parses a PHP string using the default start symbol.
   *
   * @type String -> Document or TemplateDocument
   */
  parse-php-string =
    parse-php-string(|<get-start-symbol>)

  /**
   * Parses a PHP string using the given start symbol.
   *
   * @param Symbol (String or None())
   * @type String -> Start symbol
   */
  parse-php-string(|symbol) =
    parse-string(|<get-parse-table(|symbol) ; php-open-parse-table>, symbol)
  ; post-process-structures

strategies
  /**
   * Parses a stream with PHP-source to a Document
   *
   * @type Stream -> Document or TemplateDocument
   */
  parse-php-stream =
    parse-php-stream(|<get-input-path>)

  /**
   * Parses a stream with PHP-source to a Document
   *
   * @param path The path to the stream
   * @type   Stream -> Document or TemplateDocument
   */
  parse-php-stream(|path) =
    parse-php-stream(|<get-start-symbol>,path)

  /**
   * Parses a stream with PHP-source to the given
   * start symbol.
   *
   * @param symbol The start symbol to use
   * @param path The path to the stream
   * @type   Stream -> Document or Start symbol
   */
  parse-php-stream(|symbol,path) =
    log(|Debug(), "Parsing file: ", path)
   ;parse-stream-pt(strsglr-report-parse-error
                   | <get-parse-table(|symbol) ; php-open-parse-table>
                   , symbol
                   , path)
   ;process-parse-php-options

  /**
   * @type STREAM -> Document or TemplateDocument
   */
  parse-php4-stream =
    using-php4(parse-php-stream)

  /**
   * @type STREAM -> Document or TemplateDocument
   */
  parse-php5-stream =
    using-php5(parse-php-stream)

  /**
   * Processes a php stream. Matches the input- and output-streams,
   * applies the given strategy and prints the result in the right 
   * format.
   *
   * @type (Stream, Stream) -> (Stream, Stream)
   */
  process-php-stream(s) =
     ?(<id>, fout)
    ; s
    ; if <get-config> "-b" then
        <write-in-baf-to-stream> (fout, <id>)
      else 
        <fprint> (fout, [<id>])
      end