Lua comments

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

Lua comments

fschmidt
Administrator
I am trying to implement Lua comments.  They look like this:

--[===[ abc ... xyz ]===]

The number of '=' chars is zero or more and must match.  I tried this:

        Rule Comment() {
                return Sequence(
                        "--[",
                        ZeroOrMore('='),
                        push("]"+match()+"]"),
                        '[',
                        ZeroOrMore(
                                TestNot((String)peek()),
                                ANY
                        ),
                        (String)pop()
                );
        }

Which gave me:

org.parboiled.errors.GrammarException: Illegal rule definition: Unwrapped action expression!

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

Re: Lua comments

fschmidt
Administrator
I found a solution, not sure if it's optimal.

        Rule Comment() {
                Var<Integer> n = new Var<Integer>();
                return Sequence(
                        "--[",
                        ZeroOrMore('='),
                        setN(n),
                        '[',
                        ZeroOrMore(
                                TestNot(CommentEnd(n)),
                                ANY
                        ),
                        CommentEnd(n)
                );
        }

        Rule CommentEnd(Var<Integer> n) {
                return Sequence( ']', ZeroOrMore('='), eqN(n), ']' );
        }

        boolean setN(Var<Integer> n) {
                return n.set(matchLength());
        }

        boolean eqN(Var<Integer> n) {
                return n.get()==matchLength();
        }
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Lua comments

mathias
Administrator
Yes, something like this is the right approach.
However, you shouldn't have to create the boolean methods `setN` and `eqN`.
You can simply write the `n.set(matchLength())` expression directly into your rule definition.

Cheers,
Mathias

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

On 26.11.2012, at 10:17, "fschmidt [via parboiled users]" <[hidden email]> wrote:

> I found a solution, not sure if it's optimal.
>
>         Rule Comment() {
>                 Var<Integer> n = new Var<Integer>();
>                 return Sequence(
>                         "--[",
>                         ZeroOrMore('='),
>                         setN(n),
>                         '[',
>                         ZeroOrMore(
>                                 TestNot(CommentEnd(n)),
>                                 ANY
>                         ),
>                         CommentEnd(n)
>                 );
>         }
>
>         Rule CommentEnd(Var<Integer> n) {
>                 return Sequence( ']', ZeroOrMore('='), eqN(n), ']' );
>         }
>
>         boolean setN(Var<Integer> n) {
>                 return n.set(matchLength());
>         }
>
>         boolean eqN(Var<Integer> n) {
>                 return n.get()==matchLength();
>         }
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Lua-comments-tp4024105p4024106.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Loading...