Quantcast

parbioled will cache the rules?

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

parbioled will cache the rules?

Freewind
Please see the code in gist:

https://gist.github.com/1090116

And the output is:

================== output ==================

************ myrule: 2
############## count: 2, str: a
Starting new parsing run
all/myrule/'a', matched, cursor at 1:2 after "a"
..(1)../myrule/'a', matched, cursor at 1:3 after "aa"
..(1)../myrule, matched, cursor at 1:3 after "aa"
all/myrule/'a', matched, cursor at 1:4 after "aaa"
..(1)../myrule/'a', matched, cursor at 1:5 after "aaaa"
..(1)../myrule, matched, cursor at 1:5 after "aaaa"
all/myrule/'a', matched, cursor at 1:6 after "aaaaa"
..(1)../myrule/'a', matched, cursor at 1:7 after "aaaaaa"
..(1)../myrule, matched, cursor at 1:7 after "aaaaaa"
all/myrule/'a', matched, cursor at 1:8 after "aaaaaaa"
..(1)../myrule/'a', matched, cursor at 1:9 after "aaaaaaaa"
..(1)../myrule, matched, cursor at 1:9 after "aaaaaaaa"
all/myrule/'a', matched, cursor at 1:10 after "aaaaaaaaa"
..(1)../myrule/'a', matched, cursor at 1:11 after "aaaaaaaaaa"
..(1)../myrule, matched, cursor at 1:11 after "aaaaaaaaaa"
all/myrule/'a', matched, cursor at 1:12 after "aaaaaaaaaaa"
..(1)../myrule/'a', failed, cursor at 1:12 after "aaaaaaaaaaa"
..(1)../myrule, failed, cursor at 1:12 after "aaaaaaaaaaa"
all, matched, cursor at 1:11 after "aaaaaaaaaa"
None

========================================

You can see, "myrule" and "drule" can invoked only once, so the rules should be cached. I think it's because of performance, and it should be.

But what should I do if I want the rules are dynamic?

(
I want to parse a haml file, a tag may have children tags, and they are nested by the indentation spaces. My code is like:

    def MultiLineHtmlTag = rule {
        var indent = new Indent()
        InlineSpaces ~% (indent.set(_)) ~
            zeroOrMore(HtmlTagHead ~ optional(InlineSpaces1 ~ HtmlTagAttributes), separator = " > ") ~ NewLine ~
            oneOrMore(&(nextIndent(indent.level + 1)) ~ HtmlTag) ~~> MultiLineHtmlTagNode
    }

You can see I get the indentation first, and then check if the next line has proper indentations. But I found it doesn't work, since the "MultiLineHtmlTag" rule are generated only once, and then cached.
)

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

Re: parbioled will cache the rules?

mathias
Administrator
It is important to understand, that parboiled rule creation and rule execution are two different phases!

Your rules are created _before_ any input is actually being consumed.
Therefore your rules methods should do nothing but create a proper rule structure.

If you'd like to have code run during the parsing phase you need to embed it as parser actions.

HTH,

Mathias

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

On 18.07.2011, at 19:39, Freewind [via parboiled users] wrote:

> Please see the code in gist:
>
> https://gist.github.com/1090116
>
> And the output is:
>
> ================== output ==================
>
> ************ myrule: 2
> ############## count: 2, str: a
> Starting new parsing run
> all/myrule/'a', matched, cursor at 1:2 after "a"
> ..(1)../myrule/'a', matched, cursor at 1:3 after "aa"
> ..(1)../myrule, matched, cursor at 1:3 after "aa"
> all/myrule/'a', matched, cursor at 1:4 after "aaa"
> ..(1)../myrule/'a', matched, cursor at 1:5 after "aaaa"
> ..(1)../myrule, matched, cursor at 1:5 after "aaaa"
> all/myrule/'a', matched, cursor at 1:6 after "aaaaa"
> ..(1)../myrule/'a', matched, cursor at 1:7 after "aaaaaa"
> ..(1)../myrule, matched, cursor at 1:7 after "aaaaaa"
> all/myrule/'a', matched, cursor at 1:8 after "aaaaaaa"
> ..(1)../myrule/'a', matched, cursor at 1:9 after "aaaaaaaa"
> ..(1)../myrule, matched, cursor at 1:9 after "aaaaaaaa"
> all/myrule/'a', matched, cursor at 1:10 after "aaaaaaaaa"
> ..(1)../myrule/'a', matched, cursor at 1:11 after "aaaaaaaaaa"
> ..(1)../myrule, matched, cursor at 1:11 after "aaaaaaaaaa"
> all/myrule/'a', matched, cursor at 1:12 after "aaaaaaaaaaa"
> ..(1)../myrule/'a', failed, cursor at 1:12 after "aaaaaaaaaaa"
> ..(1)../myrule, failed, cursor at 1:12 after "aaaaaaaaaaa"
> all, matched, cursor at 1:11 after "aaaaaaaaaa"
> None
>
> ========================================
>
> You can see, "myrule" and "drule" can invoked only once, so the rules should be cached. I think it's because of performance, and it should be.
>
> But what should I do if I want the rules are dynamic?
>
> (
> I want to parse a haml file, a tag may have children tags, and they are nested by the indentation spaces. My code is like:
>
>     def MultiLineHtmlTag = rule {
>         var indent = new Indent()
>         InlineSpaces ~% (indent.set(_)) ~
>             zeroOrMore(HtmlTagHead ~ optional(InlineSpaces1 ~ HtmlTagAttributes), separator = " > ") ~ NewLine ~
>             oneOrMore(&(nextIndent(indent.level + 1)) ~ HtmlTag) ~~> MultiLineHtmlTagNode
>     }
>
> You can see I get the indentation first, and then check if the next line has proper indentations. But I found it doesn't work, since the "MultiLineHtmlTag" rule are generated only once, and then cached.
> )
>
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/parbioled-will-cache-the-rules-tp3179972p3179972.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.

Loading...