[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Public WebGL] ESSL 3.5 Comments Compliance



> I think your definition of "compiler" here is perhaps a bit too narrow.
>  IMHO all 9 steps of Sec 3.3 would constitute part of the compilation
> process and thus be "the compiler" as it were (yes including the
> pre-processing and linking).  It seems that you are expecting only steps 6
> and 7 to be the "compiler".

I expect steps 2-9 to be "compilation". Comments do not participate in
preprocessing or subsequent phases after 2. Comments are not required
to be made into preprocessing tokens as this phase specifies their
replacement by whitespace.

Consider:
compile(decomment(decomment(src)) == compile(decomment(src)) == compile(src)

Thus decommenting is both idempotent and identity under compile. Thus
decomment is not part of the compilation.

A decommenting routine that meets Sec 3.3 of the GLESSL spec isn't
hard to write.

Instead of simply arguing, I decided to solve the problem.

Here it is in perl:

sub l() { my($c)=@_; $c=~s/.*?$//mg ; return $c}
s,/\*(.*?)\*/," ".&l($1),sge; # "Those who cannot remember the past are..."
s,//.*?$,,mg;

And in ocaml:

module B = Buffer

let decomment ib =
  let l = B.length ib in
  let ob = B.create l in
  let cat = B.add_char ob in
  let rec next k i = let s = i+1 in if s < l then k s else ob
  and accept i = match B.nth ib i with
    | '/' -> next comment i
    | c -> cat c; next accept i
  and comment i = match B.nth ib i with
    | '/' -> cat ' '; next line i
    | '*' -> cat ' '; next block i
    | c -> cat '/'; cat c; next accept i
  and line i = match B.nth ib i with
    | '\n' -> cat '\n'; next accept i
    | _ -> next line i
  and block i = match B.nth ib i with
    | '\n' -> cat '\n'; next block i
    | '*' -> next endblock i
    | _ -> next block i
  and endblock i = match B.nth ib i with
    | '/' -> next accept i
    | _ -> next block (i-1) (* "... condemned to repeat it." *)
  in if l <> 0 then next accept (-1) else ob

And in javascript:

function decomment(x)
  {
  var i = 0, s = 0, o = "";
  while (i < x.length)
    {
    var c = x[i++], d = x[i];
    if ( c == "\n" )
      o += c;
    else if ( s==2 && c=="*" && d== '/' )
      s = 0, i++;
    else if ( s==1 && d == '\n' )
      s = 0;
    else if ( s==0 )
      {
      if ( c == "/" && (s={"/":1,"*":2}[d]|0))
        c = ' ', i++;
      o += c;
      }
    }
  return o; // @~ George Santayana
  }

And in C++ (UTF-8 and UTF-16!):

#include <assert.h>
#include <string>
#include <iostream>

using namespace std;

template<class T>
void decomment(basic_string<T> x,basic_string<T> &res)
{
  int s = 0;
  typename basic_string<T>::iterator p;
  for (p = x.begin(); p != x.end(); )
    {
      T c = *p++, d = *p;
      if (c == '\n')
        res.push_back(c);
      else if (s==2 && c == '*' && d == '/' )
        s = 0, p++;
      else if (s==1 && d == '\n')
        s = 0 ;
      else if (s==0)
        {
          if (c == '/' && ( s = ( d=='/' ? 1 : d=='*' ? 2 : 0)))
            c = ' ', p++;
          res.push_back(c);
        }
    }
  // $Published: Life of Reason, Reason in Common Sense, Scribner's, 1905$
  assert( p == x.end() );
}

Of course, if a WebGL implementer is already lexing the incoming
shaders (e.g. ANGLE), comment removal is perfectly suited to be done
automatically by their existing lexing mechanism and not by these ad
hoc state machines.

> All that said, this is defined the same as the desktop GLSL specification
> (except that it doesn't list the logical phases of compilation), but I
> suspect that many implementations there may be a bit less strict about these
> things.

You should be liberal in what you accept and conservative in what you emit.

Without at least ASCII comments, here is what you lose:
- automatic documentation annotation/generation tools
- cross compilers with original source in-line
- HTML or XML snippets
- Javascript snippets
- LaTeX documentation of mathematics
- Windows paths
- CVS includes
- Email addresses

With Unicode you also:
- Invite non-latin-alphabet users to write natively in the true spirit
of the World Wide Web

The standards group needs to not be myopically focused on the ES2 standard
specification to the detriment of Web ideals and the WebGL ecosystem.
This single restriction will complicate or kill a significant
portion of tool projects targeting WebGLSL and will stunt any
non-latin-charset WebGL community.

Do the right thing.

David Sheets
Ashima Arts

P.S. The decommenting routines, test shader, and makefile are at
http://download.ashimaarts.com/user/sheets/webgl-decomment.tar.gz
-----------------------------------------------------------
You are currently subscribed to public_webgl@khronos.org.
To unsubscribe, send an email to majordomo@khronos.org with
the following command in the body of your email:
unsubscribe public_webgl
-----------------------------------------------------------