evaluation_order - Twinside/Webrexp GitHub Wiki

Home | Tutorial | Grammar

Evaluation order

Right now, two evaluation orders exists, the depth first evaluation order, and the breadth first evaluation order. Understanding them is important if you want to use the webrexp to output some well ordered text.

For the explanation, we will use the following document as "file.xml" :

    <?xml version="1.0" encoding="utf-8" ?>
    <Items>
        <Item>
            <Name>Foo</Name>
            <Price>15</Price>
            <Description>A foo</Description>
        </Item>
        <Item>
            <Name>Bar</Name>
            <Price>7</Price>
            <Description>A place</Description>
        </Item>
        <Item>
            <Name>Forloi</Name>
            <Price>0</Price>
            <Description>some descr</Description>
        </Item>
    </Items>

And we will apply the following webrexp :

    "file.xml" >> item ( name ["Name: "; . ; "\n"]
                       ; description ["Description: "; .; "\n"] )

Common evaluation order

First, we're gonna examine the common part of evaluation.

Evaluation common to BFS and DFS

  1. We begin the execution with an empty data.
  2. Then a string is encountered in the expression, it then become the new data.
  3. The dereference operator load the element pointed by the data in the pipeline.
  4. Last, we search the tag item in the document, and we find three of them.

Depth first evaluation

We now are at a beginning of a branch in depth first evaluation with 3 elements to evaluate.

Evaluation for DFS

  1. The first element is taken into the beginning of the branch, searching for a name, and finding one.
  2. Then the element is displayed on screen.
  3. The evaluator backtrack to the beginning of the branch, re-take the first element and put it in the second part of it.
  4. A description is searched with one result.
  5. The description content is displayed on screen.

Finally the same scenario happen with the second and third item. The DFS evaluator when multiple item are found, will pick one element and evaluate it as much as it could. Then it will backtrack to evaluate the left elements.

At the end of the evaluation we're left with the following information on screen :

    Name: Foo
    Description: A Foo
    Name: Bar
    Description: A place
    Name: Forloi
    Description: some descr

Breadth first evaluation

We now are at a beginning of a branch in breadth first evaluation with 3 elements to evaluate.

Evaluation for BFS

  1. The evaluator take the three elements and search the element name in each one of them, leaving 3 name elements in the pipeline.
  2. The action dump all the name content, each element in the pipeline will be dumped in turn.
  3. As we're in a branch, the evaluator go back to the beginning, take the 3 element and put them in the second branch. Descriptions are searched and 3 are found.
  4. Every description are outputted at the same time.

At the end of the evaluation we're left with the following information on screen :

    Name: Foo
    Name: Bar
    Name: Forloi
    Description: A Foo
    Description: A place
    Description: some descr
⚠️ **GitHub.com Fallback** ⚠️