Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6972763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:00:46+00:00 2026-05-27T17:00:46+00:00

I’m using the Whittle gem to parse a template language and wanted to match

  • 0

I’m using the Whittle gem to parse a template language and wanted to match anything that is not contained in a rule. I am well aware of other template engines out there but this is more of an academic exercise than a production case.

The problem I encounter is that the parser ignores the priority of :id above :raw and still awaits for a :raw tag after the {{.

How to tell the parses that it’s not allowed to apply the :raw rule inside an expression and only apply the :spc rule inside of an expression ?

Parser code

class Parser < Whittle::Parser
    # Skip whitespaces (should not apply in :raw)
    rule(:spc => /\s+/).skip!

    # Various delimiters
    rule("{{") ^ 4
    rule("}}") ^ 4
    rule("{%") ^ 4
    rule("%}") ^ 4
    rule("|") ^ 4
    rule("end") ^ 4

    # Defines an id (very large match)
    rule(:id => /[a-zA-Z_.$<>=!:]+(\((\w+|\s+|,|")+\))?/) ^ 2

    # inline tag
    rule(:inline) do |r|
        r["{{", :inline_head, "}}"].as { |_,id,_| Tag::Inline.new(id) }
    end
    # inline tag contents
    # allows "|" chaining
    rule(:inline_head) do |r|
        r[:inline_head, "|", :id].as { |head, _, id| head << id }
        r[:id].as { |id| [id] }
        r[].as { [] }
    end

    # block tag
    rule(:block) do |r|
        r["{%", :block_head, "%}", :all, "{%", "end", "%}"].as { |_,head,_,tags,_,_,_|
            Tag::Block.new(head, tags)
        }
    end
    # block tag heading
    # separates all the keywords
    rule(:block_head) do |r|
        r[:block_head, :id].as { |head, id| head << id }
        #r[:id].as { |id| [id] }
        r[].as { [] }
    end

    # one rule to match them all
    rule(:all) do |r|
        r[:all,:inline].as { |all, inline| all << inline }
        r[:all, :block].as { |all, block| all << block }
        r[:all, :raw].as { |all, raw| all << raw }
        r[].as { [] }
    end

    # the everything but tags rule
    rule(:raw => /[^\{\}%]+/).as { |text| Tag::Raw.new(text) } ^ 1

    # starting rule
    start(:all)
end

And the input text would be and the output is an abstract syntax tree represented by objects (they are simply hash like objects for now).

<html>
    <head>
        <title>{{ title|capitalize }}</title>
    </head>
    <body>
        <div class="news">
            {% for news in articles %}
                {{ news.title }}
                {{ news.body | limit(100) }}
                {{ tags | join(",", name) }}
            {% end %}
        </div>
    </body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:00:47+00:00Added an answer on May 27, 2026 at 5:00 pm

    I don’t believe the operator precedence support plays a role here. Operator precedences only come into play when resolving ambiguities in expressions like foo = 6 + 7, where the expression could either be interpreted as (foo = 6) + 7 or foo = (6 + 7). Giving non-operators a precedence doesn’t really serve any purpose.

    Perhaps it’s not clear what the parser actually does. It basically loops repeated, matching all terminal rules against your input string. For the ones it finds, it takes the longest one and tries to find a rule in the current state that it will fit into. So the parser will always find your whitespace and discard it, since that is the first rule in your grammar.

    I think you actually don’t want to skip whitespace, since it is significant in your grammar. You want to include it in your rules that allow it; which will make your grammar more verbose, but is (currently) unavoidable.

    So :raw becomes something like the following, swallowing up all whitespace and non-syntax tokens into a single string:

    rule(:raw => /[^\s\{\}%]+/)
    
    rule(:text) do |r|
      r[:text, :raw].as { |text, raw| text << raw }
      r[:text, :spc].as { |text, spc| text << spc }
      r[:spc]
      r[:raw]
    end
    

    Then in your :all rule, turn that text into part of your AST (you could actually do this in the above rule too, but I don’t know anything about your class definitions).

    rule(:all) do |r|
      # ... snip ...
      r[:all, :text].as { |all, text| all << Tag::Raw.new(text) }
      # ... snip ...
    end
    

    I’ve been thinking about how to provide the ability to match different tokens within different states, but I’m wary of writing a clone of lex/flex, which I think would be too confusing, so I’m trying to come up with an approach that uses blocks to nest rules inside each other to convey how states relate to each other; though it’s not simple to create an easy to comprehend DSL that does this 😉 I also want to provide an optional DSL to hide the algorithm used for repetition; maybe providing a sort of PEG layer that transforms into a LR parser. This is still a (very) young project 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Seemingly simple, but I cannot find anything relevant on the web. What is the
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.