www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Question about the syntax of Mixin statement

reply "Sammy" <not there.com> writes:
I am working on the Grammar of D ( yes, another ).

I first began to work with the html version of the grammar
published on the DM site.

Then i had a look into parse.c to see how it was programmed.

As i saw some differences i decided to work only with
the parse.c source code.

I am almost done with it. But i have some questions.

Here is the syntax of mixin from the D spec:

TemplateMixin:
  mixin TemplateIdentifier ;
  mixin TemplateIdentifier MixinIdentifier ;
  mixin TemplateIdentifier !( TemplateArgumentList ) ;
  mixin TemplateIdentifier !( TemplateArgumentList ) MixinIdentifier ;

 MixinIdentifier:
  Identifier

This syntax, is much simpler than what is expressed in parse.c

Here is an EBNF expression that closely match the parseMixin() function:

Mixin : "mixin" ( "." | ["typeof" "(" Expr ")" "." ] Ident ) [ "!" "(" Args
")" ]
                ( "." Ident [ "!" "(" Args ")" ] )... [Ident] ";"

From that, you can see that:

mixin .!(args) ident;
mixin ..ident ident;
mixin ..ident;
mixin .;

are valid statements from the syntaxic point of view.

My question is: Is this is Ok ?

If not, than, the correct syntax might be:

Mixin : "mixin" ( "."  Ident | ["typeof" "(" Expr ")" "." ] Ident ) [ "!"
"(" Args ")" ]
                ( "." Ident [ "!" "(" Args ")" ] )... [Ident] ";"

giving with the 4 preceding statements:

mixin .ident!(args) ident;
mixin .ident.ident ident;
mixin .ident.ident;
mixin .ident;

If not, what is the full syntax of Mixin ?


----- 

Here is the syntax of mixin, translated in pseudo EBNF,
from the source code:

Mixin : "mixin" ( "." | ["typeof" "(" Expr ")" "." ] Ident ) [ "!" "(" Args
")" ]
                ( "." Ident [ "!" "(" Args ")" ] )... [Ident] ";"

Here is the source of parseMixin:

Dsymbol *Parser::parseMixin()
{
  nextToken();    // to skip over "mixin"

  if (token.value == TOKdot)
  {
    id = Id::empty;
  }
  else
  {
    if (token.value == TOKtypeof)
    { nextToken();
      check(TOKlparen);
      exp = parseExpression();
      check(TOKrparen);
      check(TOKdot);
    }
    if (token.value != TOKidentifier)
    {
      error("identifier expected, not %s", token.toChars());
      goto Lerr;
    }
  }
  while (1)
  {
    nextToken();
    if (token.value == TOKnot)
    {
      parseTemplateArgumentList() // "(" Args ")"
      nextToken();
    }
    if (token.value != TOKdot)
      break;
    nextToken();
    if (token.value != TOKidentifier)
    {   error("identifier expected following '.' instead of '%s'",
token.toChars());
          break;
    }
  }
  if (token.value == TOKidentifier)
  {
    nextToken();
  }

  if (token.value != TOKsemicolon)
    error("';' expected after mixin");
  nextToken();
}

-- 
Aug 24 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
 Mixin : "mixin" ( "." | ["typeof" "(" Expr ")" "." ] Ident ) [ "!" "(" Args
")" ]
                 ( "." Ident [ "!" "(" Args ")" ] )... [Ident] ";"


This is what i basically wrote in my gramamr as:

<mixin> -> mixin . <MixinPart2>
<mixin> -> mixin typeof ( <Expression> ) . idn <MixinPart2>
<mixin> -> mixin idn <MixinPart2>
    <MixinPart2> -> ! <TemplateArgumentList> . idn <MixinPart2>
    <MixinPart2> -> ! <TemplateArgumentList> <MixinEnd>
    <MixinPart2> -> . idn <MixinPart2>
    <MixinPart2> -> <MixinEnd>
        <MixinEnd> -> idn ;
        <MixinEnd> -> ;

And as far as i can tell, that is the right syntax (and i have tested it
quite a lot!)


"Sammy" <not there.com> wrote in message
news:cgf3b8$n5r$1 digitaldaemon.com...
 I am working on the Grammar of D ( yes, another ).

 I first began to work with the html version of the grammar
 published on the DM site.

 Then i had a look into parse.c to see how it was programmed.

 As i saw some differences i decided to work only with
 the parse.c source code.

 I am almost done with it. But i have some questions.

 Here is the syntax of mixin from the D spec:

 TemplateMixin:
   mixin TemplateIdentifier ;
   mixin TemplateIdentifier MixinIdentifier ;
   mixin TemplateIdentifier !( TemplateArgumentList ) ;
   mixin TemplateIdentifier !( TemplateArgumentList ) MixinIdentifier ;

  MixinIdentifier:
   Identifier

 This syntax, is much simpler than what is expressed in parse.c

 Here is an EBNF expression that closely match the parseMixin() function:

 Mixin : "mixin" ( "." | ["typeof" "(" Expr ")" "." ] Ident ) [ "!" "("
Args
 ")" ]
                 ( "." Ident [ "!" "(" Args ")" ] )... [Ident] ";"

 From that, you can see that:

 mixin .!(args) ident;
 mixin ..ident ident;
 mixin ..ident;
 mixin .;

 are valid statements from the syntaxic point of view.

 My question is: Is this is Ok ?

 If not, than, the correct syntax might be:

 Mixin : "mixin" ( "."  Ident | ["typeof" "(" Expr ")" "." ] Ident ) [ "!"
 "(" Args ")" ]
                 ( "." Ident [ "!" "(" Args ")" ] )... [Ident] ";"

 giving with the 4 preceding statements:

 mixin .ident!(args) ident;
 mixin .ident.ident ident;
 mixin .ident.ident;
 mixin .ident;

 If not, what is the full syntax of Mixin ?


 -----

 Here is the syntax of mixin, translated in pseudo EBNF,
 from the source code:

 Mixin : "mixin" ( "." | ["typeof" "(" Expr ")" "." ] Ident ) [ "!" "("
Args
 ")" ]
                 ( "." Ident [ "!" "(" Args ")" ] )... [Ident] ";"

 Here is the source of parseMixin:

 Dsymbol *Parser::parseMixin()
 {
   nextToken();    // to skip over "mixin"

   if (token.value == TOKdot)
   {
     id = Id::empty;
   }
   else
   {
     if (token.value == TOKtypeof)
     { nextToken();
       check(TOKlparen);
       exp = parseExpression();
       check(TOKrparen);
       check(TOKdot);
     }
     if (token.value != TOKidentifier)
     {
       error("identifier expected, not %s", token.toChars());
       goto Lerr;
     }
   }
   while (1)
   {
     nextToken();
     if (token.value == TOKnot)
     {
       parseTemplateArgumentList() // "(" Args ")"
       nextToken();
     }
     if (token.value != TOKdot)
       break;
     nextToken();
     if (token.value != TOKidentifier)
     {   error("identifier expected following '.' instead of '%s'",
 token.toChars());
           break;
     }
   }
   if (token.value == TOKidentifier)
   {
     nextToken();
   }

   if (token.value != TOKsemicolon)
     error("';' expected after mixin");
   nextToken();
 }

 --
Aug 24 2004
parent reply "Sammy" <not there.com> writes:
"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:cgf6fd$o53$1 digitaldaemon.com...
 This is what i basically wrote in my gramamr as:

 (1) <mixin> -> mixin . <MixinPart2>
 (2) <mixin> -> mixin typeof ( <Expression> ) . idn <MixinPart2>
 (3) <mixin> -> mixin idn <MixinPart2>
 (4) <MixinPart2> -> ! <TemplateArgumentList> . idn <MixinPart2>
 (5) <MixinPart2> -> ! <TemplateArgumentList> <MixinEnd>
 (6) <MixinPart2> -> . idn <MixinPart2>
 (7) <MixinPart2> -> <MixinEnd>
 (8) <MixinEnd> -> idn ;
 (9) <MixinEnd> -> ;
If i read it right, this grammar can derives the following sentences: mixin . . idn ; ( rules 1,6,9 ) mixin . ! <TAL> ; ( rules: 1,5, 9 ) mixin . ; ( rules: 1,7,9 ) Which does not seems Ok. Of course, these will be eliminated somewhere, in the subsequents phases of the parsing, but not by the syntactic one. Now, this rule, does not permit the above. A dot must always be followed by an ident "mixin" ( "." Ident | ["typeof" "(" Expr ")" "." ] Ident ) [ "!"" (" Args ")" ] ( "." Ident [ "!" "(" Args ")" ] )... [Ident] ";" NB: ( ) means 1 of. [ ] means 0 or 1. ( )... means 1 or more. | indicates alternatives.
 And as far as i can tell, that is the right syntax
May be. But it needs more explanations.
Aug 24 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Sammy" <not there.com> wrote in message
news:cgfcak$qia$1 digitaldaemon.com...
 "Ivan Senji" <ivan.senji public.srce.hr> wrote in message
 news:cgf6fd$o53$1 digitaldaemon.com...
 This is what i basically wrote in my gramamr as:

 (1) <mixin> -> mixin . <MixinPart2>
 (2) <mixin> -> mixin typeof ( <Expression> ) . idn <MixinPart2>
 (3) <mixin> -> mixin idn <MixinPart2>
 (4) <MixinPart2> -> ! <TemplateArgumentList> . idn <MixinPart2>
 (5) <MixinPart2> -> ! <TemplateArgumentList> <MixinEnd>
 (6) <MixinPart2> -> . idn <MixinPart2>
 (7) <MixinPart2> -> <MixinEnd>
 (8) <MixinEnd> -> idn ;
 (9) <MixinEnd> -> ;
If i read it right, this grammar can derives the following sentences: mixin . . idn ; ( rules 1,6,9 ) mixin . ! <TAL> ; ( rules: 1,5, 9 ) mixin . ; ( rules: 1,7,9 ) Which does not seems Ok. Of course, these will be eliminated somewhere, in the subsequents phases of the parsing, but not by the syntactic one. Now, this rule, does not permit the above. A dot must always be followed by an ident
Yes you are right :) i changed the first rule to: (1) <mixin> -> mixin . idn <MixinPart2> and i think it should be ok now, although your EBNF rule inpires me to try to rewrite it in a simpler way!
 "mixin" ( "."  Ident | ["typeof" "(" Expr ")" "." ] Ident )
       [ "!"" (" Args ")" ]
       ( "." Ident [ "!" "(" Args ")" ] )... [Ident] ";"

 NB:
     ( ) means 1 of. [ ] means 0 or 1. ( )... means 1 or more.
     | indicates alternatives.
I figured this out but what does NB mean? And if ()... means 1 or more then your rule cannot generate mixin idn ; // :)
 And as far as i can tell, that is the right syntax
May be. But it needs more explanations.
Aug 24 2004
parent "Sammy" <not there.com> writes:
 Yes you are right :) i changed the first rule to:
 (1) <mixin> -> mixin . idn <MixinPart2>

 and i think it should be ok now, although your
 EBNF rule inpires me to try to rewrite it in a
 simpler way!

 "mixin" ( "."  Ident | ["typeof" "(" Expr ")" "." ] Ident )
       [ "!"" (" Args ")" ]
       ( "." Ident [ "!" "(" Args ")" ] )... [Ident] ";"

 NB:
     ( ) means 1 of. [ ] means 0 or 1. ( )... means 1 or more.
     | indicates alternatives.
I figured this out but what does NB mean?
NB -> Nota Bene a Latin phrase (or its abbreviation) used to indicate that special attention should be paid to something http://www.hyperdictionary.com/search.aspx?define=nota+bene
 And if ()...  means 1 or more
 then your rule cannot generate
 mixin idn ; // :)
Yes, you are right. Corrected expression: "mixin" ( "." Ident | ["typeof" "(" Expr ")" "." ] Ident ) [ "!"" (" Args ")" ] [ "." Ident [ "!" "(" Args ")" ] ]... [Ident] ";" And then, after simplification: "mixin" [ "." | "typeof" "(" Expr ")" "." ] ( ident [ "!" "(" Args ")" ] ) / "." [ident] ";" Have to check this one again.
Aug 24 2004