package FormLang;

use 5.006;
use strict;
use warnings;

require Exporter;
use AutoLoader qw(AUTOLOAD);

our @ISA = qw(Exporter);

# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration	use FormLang ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw() ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw();
our $VERSION = '0.01';

use grammar;
use Data::Dumper;

our %all_entries;

sub dumpScript {
  %all_entries = ();
  my $struct = shift;
  return "" unless ref($struct) eq "ARRAY";
  my @pages = @$struct;
  my $i = 0;
  my $return = "#!/usr/bin/perl";
  $return .= "\nuse CGI qw/:cgi-lib :standard/;";
  $return .= "\n\%vars = Vars();";
  my $page;
  for $page (@pages) {
    if ($i) {
      $return .= "\n} elsif (\$vars{__page__} == ".($i-1).") {";
    } else {
      $return .= "\nif (!keys \%vars) {";
    }
    $return .= dumpPage($page,$i);
    $i++;
  }
  $return .= "\n}";
}

sub dumpPage {
  my $page = shift;
  my $pageNumber = shift;
  my @ent = ();
  my @entries = @$page;
  my $return = "\n\tprint header();";
  $return .= "\n\tprint start_form();";
  $return .= "\n\tprint \"<input type=\\\"hidden\\\" name=\\\"__page__\\\" value=\\\"$pageNumber\\\">\";";
  for (keys %all_entries) {
    $return .= "\n\tprint \"<input type=\\\"hidden\\\" name=\\\"$_\\\" value=\\\"\$vars{$_}\\\">\";";
  }
  for (@entries) {
    $return .= "\n\tprint \"<li>\";";
    if ($_->{type} eq "entry") {
      if (exists($_->{times})) {
	my $time = 1;
	while($time <= $_->{times}) {
	  $return .= "\n\tprint '$_->{title}: <input name=\\\"$_->{id}$time\\\">';";
	  push @ent, "$_->{id}$time";
	  $time++;
	}
      } else {
	$return .= "\n\tprint '$_->{title}: <input name=\\\"$_->{id}\\\">';";
	push @ent, "$_->{id}";
      }
    } else {
      ## TODO -- blocks
    }
    $return .= "\n\tprint \"</li>\";";
  }
  @all_entries{@ent} ++;
  $return.= "\n\tprint \"<input type=\\\"submit\\\">\";";
  $return.= "\n\tprint end_form();";
  return $return;
}

sub parse {
  my $p = new grammar();
  return $p->YYParse(yylex=>\&grammar::lex,
		     yyerror => sub{
		       print "There's a ",$_[0]->YYCurtok,"(",
			 $_[0]->YYCurval,") where I expect a ",$_[0]->YYExpect,"\n";
		     }),"\n";
}

sub parseFile {
  my $p = new grammar();
  my $file = shift;
  open F, $file or die ("");
  *STDIN = *F;
  return $p->YYParse(yylex=>\&grammar::lex,
		     yyerror => sub{
		       if ($_[0]->YYCurtok) {
			 print "There's a ",$_[0]->YYCurtok,"(",
			   $_[0]->YYCurval,") where I expect one of ",join(", ",$_[0]->YYExpect),"\n";
		       } else {
			 print "Expecting one of ",join(", ",$_[0]->YYExpect),"\n";
		       }
		     }),"\n";
}


# Preloaded methods go here.

# Autoload methods go after =cut, and are processed by the autosplit program.

1;
__END__
# Below is stub documentation for your module. You better edit it!

=head1 NAME

FormLang - Perl extension for blah blah blah

=head1 SYNOPSIS

  use FormLang;
  blah blah blah

=head1 DESCRIPTION

Stub documentation for FormLang, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.

Blah blah blah.

=head2 EXPORT

None by default.


=head1 AUTHOR

A. U. Thor, E<lt>a.u.thor@a.galaxy.far.far.awayE<gt>

=head1 SEE ALSO

L<perl>.

=cut
