#lang scheme

; A partial PHP source file parser

(require "../simple-parser.ss"

(define php-keywords
  '(echo function array for while if else elseif as
         foreach return isset unset empty
         true false))

(define php-parser (new-parser #:phase 'html))

  ["<\\?php" (λ(s)(switch-phase 'php) "PHP-START")]
  ["\\?>" (λ(s)(switch-phase 'html) "PHP-END")]
  ["\\$(\\w*)" (λ(s v)(string-append "VAR(" v ")"))]
       (λ(s)(string-append "STRING1(" s ")"))]
  ["\"" (λ(t)(sub-parse 'string (λ(s)(string-append "STRING2[" s "]")))t)]
  ["//\\s*(.*)$" (λ(s comm)(string-append "COMMENT(" comm ")"))]
  ["/\\*" (λ(s)(switch-phase 'php-comment) "COMMENT-START")]
  [php-keywords string-upcase]
  ; symbols are treated as words with boundaries
  ["\\*/" (λ(s)(switch-phase 'php) "COMMENT-END")]
  ["\"" (λ(s)(sub-parse-return s))]
  [(txt "\\\"") identity]
  [(txt "\n") "\\n"] ; outputs string on a single line

; Tests :
(define text 
  "<h1>Some title</h1>
$x = 5; // This is the beggining of the file
return 12; /* echo($arz);
return 23;
*/ function plop(){
    echo('ha c\\'est sympa et c\\'est chouette !'); echo ('c\\'est sur !');
    return 4;
    echo(\"ha c\\\"est sympa et c\\\"est chouette !\"); echo (\"c\\\"est sur !\");
    echo(\" et 
confortable en 
plus !\");
    return 5;
function plip(){
return echo $DF;

(display (parse-text php-parser text))
(display (apply parse-text php-parser 
                (file->lines/latin-1 "cris2.php")))