%D \module
%D   [       file=verb-pl,
%D        version=1998.02.7,
%D          title=\CONTEXT\ Verbatim Macros,
%D       subtitle=Pretty \JAVASCRIPT\ Verbatim,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See licen-en.pdf for 
%C details. 

\writestatus{loading}{Context Support Macros / Pretty JavaScript Verbatim}

%D In \CONTEXT\ we support \JAVASCRIPT\ inclusion in \PDF\
%D documents and thereby enter the field of authoring. Of
%D course we also want to pretty print such scripts, that look
%D like: 
%D 
%D \startJS
%D alfa = beta*2 ;       // both alfa and beta are numbers // indeed 
%D if (odd(alfa))
%D   { do_something() }  
%D else
%D   { do_nothing() }    /* As we can see /* in this example */ there
%D how_about(alfa) ;        is no fi needed. Also no semicolons are
%D or_about(beta) ;         needed after a right brace. */
%D \stopJS
%D 
%D Because \JAVASCRIPT\ looks much like \PERL, we will use
%D a slightly adapted \PERL\ visualization. First we load the
%D \PERL\ module:

\ifx\undefined\setupprettyPLtype \input verb-pl \relax \fi

\unprotect

%D The main difference between the \JAVASCRIPT\ and \PERL\
%D interpreters concern comments. Where \TEX, \METAPOST\ and
%D \PERL\ have one comment symbol (\type{%}, \type{%} and
%D \type{#}), \JAVA\ has the one line comment sequence
%D \type{//} and the multi line comment delimiters \type{/*}
%D and \type{*/}.
%D
%D We need a counter to keep track of multi line comment
%D nesting.

\newcount\JVcommentlevel

%D We handle both \type{%} and \type{#} a bit different:

\gdef\JVsetspecials%
  {\PLsetspecials
   \setpretty`\#=32
   \setpretty`\%=41
   \setpretty`\/=43
   \setpretty`\*=44 }

%D We also need a few more handlers: one for \type{//} and
%D \type{/*} and one for \type{*/}.

\gdef\JVsethandlers%
  {\PLsethandlers
   \installprettyhandler 43 \JVtypefourthree
   \installprettyhandler 44 \JVtypefourfour }

%D We can inherit most of the settings:

\gdef\JVsetcontrols%
  {\PLsetcontrols
   \def\obeyedline%
     {\endPLtypesix
      \ifcase\JVcommentlevel \inPLcommentfalse \fi
      \PLverbosefalse
      \PLverboseskipped=0 
      \oldobeyedline}}

\gdef\JVsetvariables
  {\PLsetvariables
   \global\JVcommentlevel=0 }

\gdef\setupprettyJVtype%
  {\def\prettyidentifier{JV}%
   \let\PLidentifiers=\JVidentifiers
   \let\PLvariables=\JVvariables
   \JVsetvariables
   \JVsetcontrols
   \JVsethandlers
   \JVsetspecials
   \PLsetdiagnostics}

%D The main complication is that we have to look upto four
%D characters ahead. Such macros are hard to understand but
%D they do work!

\gdef\JVtypefourthree%
  {\handlenextnextpretty\doJVtypefourthree\PLtypefourtwo}

\gdef\doJVtypefourthree#1#2%
  {\getprettydata{#2}%
   \ifnum\prettytype=43
     \let\next=\dodoJVtypefourthree
   \else\ifnum\prettytype=44
     \global\advance\JVcommentlevel by 1
     \global\inPLcommenttrue
     \PLverbosecorrection
     \let\next=\JVtogglecomment
   \else
     \let\next=\PLtypefourtwo
   \fi\fi
   \next{#1}#2}

\gdef\JVtogglecomment#1#2%
  {\ifnum\JVcommentlevel=1
     \beginofpretty[\!!prettyone]\getpretties{#1}{#2}\endofpretty
   \else
     \getpretties{#1}{#2}%
   \fi}

\gdef\dodoJVtypefourthree% #1%
  {\endPLtypesix
   \handlenextnextpretty\dododoJVtypefourthree\dodododoPLtypefourthree}

% \gdef\dododoJVtypefourthree%
%   {\handlenewpretty\dodododoPLtypefourthree}

\gdef\dododoJVtypefourthree%
  {\ifnewpretty\expandafter\handlenewpretty\fi\dodododoJVtypefourthree}

\gdef\dodododoJVtypefourthree#1#2%
  {\ifinPLcomment
     \getpretties{#1}{#2}%
   \else
     \global\inPLcommenttrue
     \PLverbosecorrection
     \beginofpretty[\!!prettyone]\getpretties{#1}{#2}\endofpretty
   \fi}

\gdef\JVtypefourfour%
  {\handlenextnextpretty\doJVtypefourfour\PLtypefourtwo}

\gdef\doJVtypefourfour#1#2%
  {\getprettydata{#2}%
   \ifnum\prettytype=43
     \JVtogglecomment{#1}#2%
     \global\advance\JVcommentlevel by -1
     \ifcase\JVcommentlevel \global\inPLcommentfalse \fi
   \else
     \beginofpretty[\!!prettyfour]#1\endofpretty\expandafter#2%
   \fi}

%D We need different (less) reserved words. This list replaces
%D the \PERL\ one.

\useprettyidentifiers \JVidentifiers \JVsetspecials
  abstract boolean break byte case catch char class const
  continue default delete do double else extends false final
  finally float for function goto if implements import in
  instanceof int interface long native new null package
  private protected public return short static super switch
  synchronized this throw throws transient true try var void
  while with

\useprettyidentifiers \JVvariables \JVsetspecials
  not-yet-defined

\protect

\endinput  
