t-regex-0.1.0.0: Matchers and grammars using tree regular expressions

Safe HaskellNone
LanguageHaskell2010

Data.Regex.Rules

Contents

Description

Attribute grammars with regular expression matching.

Synopsis

Basic blocks

type Action c f inh syn = Fix f -> inh -> Map c syn -> (Bool, Map c inh, syn) Source

Actions create new inherited attributes for their children, and synthesized attribute for its own node, from the synthesized attributes of children and the inheritance from its parent. Additionally, actions may include an explicit backtrack.

type Rule c f inh syn = (Regex c f, Action c f inh syn) Source

A rule comprises the regular expression to match and the action to execute if successful.

type Grammar c f inh syn = [Rule c f inh syn] Source

A grammar is simply a list of rules.

eval :: (Ord c, Matchable f, Monoid syn) => Grammar c f inh syn -> inh -> Fix f -> syn Source

Evaluate an attribute grammar over a certain term.

Nice syntax for defining rules

rule :: RuleBuilder f inh syn fn r => fn -> r Source

Converts a rule description into an actual Rule. Its use must follow this pattern:

  • A block of lambda-bound variables will introduce the capture names,
  • A tree regular expression to match should capture using the previous names,
  • After ->>> or ->>, the state calculation should proceed.
rule $ \c1 c2 ->
  regex ... c1 <<- ... c2 <<- ... ->> do
    at c2 . inh .= ...          -- Set inherited for children
    c1Syn <- use (at c1 . syn)  -- Get synthesized from children
    this . syn  .= ...          -- Set upwards synthesized attributes

Combinators

check :: Bool -> State (ActionState Integer inh syn) () Source

Makes the attribute calculation fail if the condition is false. This function can be used to add extra conditions over whether a certain rule should be applied (a bit like guards).

(->>>) :: Monoid syn => (forall k. Regex' k Integer f) -> (Fix f -> State (ActionState Integer inh syn) ()) -> [Integer] -> Rule Integer f inh syn Source

Separates matching and attribute calculation on a rule. The action should take as extra parameter the node which was matched.

(->>) :: Monoid syn => (forall k. Regex' k Integer f) -> State (ActionState Integer inh syn) () -> [Integer] -> Rule Integer f inh syn Source

Separates matching and attribute calculation on a rule.

Special lenses

this :: Functor f => ((inh, syn) -> f (inh, syn)) -> ActionState c inh syn -> f (ActionState c inh syn) Source

Lens for the attributes of the current node. To be used in composition with inh or syn.

at :: (Ord c, Functor f) => c -> ((inh, syn) -> f (inh, syn)) -> ActionState c inh syn -> f (ActionState c inh syn) Source

Lens the attributes of a child node. To be used in composition with inh or syn.

inh :: Functor f => (inh -> f inh) -> (inh, syn) -> f (inh, syn) Source

Lens for the inherited attributes of a node. Use only as getter with this and as setter with at.

syn :: Functor f => (syn -> f syn) -> (inh, syn) -> f (inh, syn) Source

Lens the inherited synthesized attributes of a node. Use only as setter with this and as getter with at.