Spacing, Identifier and Comments

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Spacing, Identifier and Comments

taharqa
Hi,

I started by taking elements from JavaParser to create a basic dsl
parser brace based.
Very similar to java. It'll look like this.

context lifeInsur {
   /**
   * comment is handled by Spacing rule
   */
   aggregate contract  {

      entity Contract
      {
//       id  ContractNo contractNumber,

      }
   }
}

for now, I took building blocks from JavaParser Identifier, Spacing
Terminal and Keywords.
I follow spacing recommandation from parboiled documentation
https://github.com/sirthias/parboiled/wiki/Handling-Whitespace

my problem is the following

GIVEN the DSL
- - - - - - - - - - - - 8< snip start - - - - - - - - - - - - - - - - - - - -
context lifeInsur {
   aggregate product
   {
       entity Product /* is Nameable */
       {
//          id ProductId productId,
       }
   }
}
- - - - - - - - - - - - 8< snip end - - - - - - - - - - - - - - - - - - - -

AND the Rule for entity

- - - - - - - - - - - - 8< snip start - - - - - - - - - - - - - - - - - - - -
    public Rule Entity()
    {
        return Sequence(ENTITY,
                         Identifier() ,
                         ACTION( builder.addEntityIdentifier () ),
                         EntityBody()  );
    }
- - - - - - - - - - - - 8< snip end - - - - - - - - - - - - - - - - - - - -

where
ENTITY is the keyword "entity"
and builder.addEntityIdentifier () basicaly does the following
   String entityName = context.getMatch();

I get entityName equals to "Product /* is Nameable */" because
Spacing() in JavaParser include comments

How can I safely get "Product " knowing I want to keep comment
handling in spacing because it is very handy.

Thank you

Epo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Spacing, Identifier and Comments

mathias
Administrator
Epo,

I think the easiest would be to split the `Identifier` rule into two:

    Rule Identifier() {
        return Sequence(IdentifierBase(), push(match()), Spacing());
    }

    Rule IdentifierBase() {
        return Sequence(TestNot(Keyword()), Letter(), ZeroOrMore(LetterOrDigit()));
    }

This way the `Identifier` rule pushes the identifier string onto the value stack, from where you can `pop` it later to turn it into some higher-level representation.

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 22.08.2012, at 18:42, taharqa [via parboiled users] wrote:

> Hi,
>
> I started by taking elements from JavaParser to create a basic dsl
> parser brace based.
> Very similar to java. It'll look like this.
>
> context lifeInsur {
>    /**
>    * comment is handled by Spacing rule
>    */
>    aggregate contract  {
>
>       entity Contract
>       {
> //       id  ContractNo contractNumber,
>
>       }
>    }
> }
>
> for now, I took building blocks from JavaParser Identifier, Spacing
> Terminal and Keywords.
> I follow spacing recommandation from parboiled documentation
> https://github.com/sirthias/parboiled/wiki/Handling-Whitespace
>
> my problem is the following
>
> GIVEN the DSL
> - - - - - - - - - - - - 8< snip start - - - - - - - - - - - - - - - - - - - -
> context lifeInsur {
>    aggregate product
>    {
>        entity Product /* is Nameable */
>        {
> //          id ProductId productId,
>        }
>    }
> }
> - - - - - - - - - - - - 8< snip end - - - - - - - - - - - - - - - - - - - -
>
> AND the Rule for entity
>
> - - - - - - - - - - - - 8< snip start - - - - - - - - - - - - - - - - - - - -
>     public Rule Entity()
>     {
>         return Sequence(ENTITY,
>                          Identifier() ,
>                          ACTION( builder.addEntityIdentifier () ),
>                          EntityBody()  );
>     }
> - - - - - - - - - - - - 8< snip end - - - - - - - - - - - - - - - - - - - -
>
> where
> ENTITY is the keyword "entity"
> and builder.addEntityIdentifier () basicaly does the following
>    String entityName = context.getMatch();
>
> I get entityName equals to "Product /* is Nameable */" because
> Spacing() in JavaParser include comments
>
> How can I safely get "Product " knowing I want to keep comment
> handling in spacing because it is very handy.
>
> Thank you
>
> Epo
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Spacing-Identifier-and-Comments-tp4024053.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Spacing, Identifier and Comments

taharqa
Thanks Mathias,

I'll try this and let you know !

Epo
Loading...