examples/text-parser-php-test.scm
#lang scheme

; A partial PHP source file parser

(require "../simple-parser.ss"
         "../common.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))

(add-items 
 php-parser 
 ('html
  ["<\\?php" (λ(s)(switch-phase 'php) "PHP-START")]
  )
 ('php
  ["\\?>" (λ(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
  )
 ('php-comment
  ["\\*/" (λ(s)(switch-phase 'php) "COMMENT-END")]
  )
 ('string
  ["\"" (λ(s)(sub-parse-return s))]
  [(txt "\\\"") identity]
  [(txt "\n") "\\n"] ; outputs string on a single line
  )
 )

; Tests :
(define text 
  "<h1>Some title</h1>
echo($variable);
?>
<?php
$x = 5; // This is the beggining of the file
echo($x);
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))
(newline)
(newline)
(display (apply parse-text php-parser 
                (file->lines/latin-1 "cris2.php")))