distributors-grammar
Copyright(C) 2026 - Eitan Chatav
LicenseBSD-style (see the file LICENSE)
MaintainerEitan Chatav <eitan.chatav@gmail.com>
Stabilityprovisional
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Control.Lens.Grammar.Machine

Description

 
Synopsis

Matching

class Matching word pattern | pattern -> word where Source #

Does a word match a pattern?

Methods

(=~) :: word -> pattern -> Bool infix 2 Source #

Instances

Instances details
Matching String RegBnf Source # 
Instance details

Defined in Control.Lens.Grammar

Methods

(=~) :: String -> RegBnf -> Bool Source #

Matching String RegString Source # 
Instance details

Defined in Control.Lens.Grammar

Methods

(=~) :: String -> RegString -> Bool Source #

Matching s k => Matching s (Grammor k a b) Source # 
Instance details

Defined in Data.Profunctor.Grammar

Methods

(=~) :: s -> Grammor k a b -> Bool Source #

AsEmpty s => Matching s (Parsor s [] a b) Source # 
Instance details

Defined in Data.Profunctor.Grammar

Methods

(=~) :: s -> Parsor s [] a b -> Bool Source #

Matching s (APrism s t a b) Source # 
Instance details

Defined in Control.Lens.Grammar.Machine

Methods

(=~) :: s -> APrism s t a b -> Bool Source #

Categorized token => Matching [token] (Bnf (RegEx token)) Source # 
Instance details

Defined in Control.Lens.Grammar.Machine

Methods

(=~) :: [token] -> Bnf (RegEx token) -> Bool Source #

Categorized token => Matching [token] (Transducer token) Source # 
Instance details

Defined in Control.Lens.Grammar.Machine

Methods

(=~) :: [token] -> Transducer token -> Bool Source #

Categorized token => Matching [token] (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.Machine

Methods

(=~) :: [token] -> RegEx token -> Bool Source #

Transducer

transducer :: Bnf (RegEx token) -> Transducer token Source #

Compile a RegExtended Bnf into a Transducer, using a combination of Thompson's algorithm for regular expressions and Earley's algorithm for context-free grammars. See Jim & Mandelbaum, Efficient Earley Parsing with Regular Right-hand Sides, and McIlroy, Enumerating the strings of regular languages.

A transducer is a form of finite state machine that can be run in various ways like =~, expectNext, languageSample, parseForest & unreachableRules.

parseForest Source #

Arguments

:: Categorized token 
=> Transducer token 
-> [token]

string

-> ([Tree (String, Int, Int, [token])], [token])

parse forest & remaining unparsed tokens

The parse forest of a string of tokens.

languageSample Source #

Arguments

:: (TokenAlgebra token (f token), Applicative f) 
=> Transducer token

transducer

-> f [[token]] 

languageSample lazily produces all words in a language from shortest to longest. However since TokenClasses can resolve to infinite sets of tokens, and the relevant case of Char tokens while not infinite is huge, it samples tokens in an Applicative TokenAlgebra.

expectNext Source #

Arguments

:: Categorized token 
=> Transducer token 
-> [token]

prefix

-> TokenClass token 

What token is expected next? The scanner frontier, expectNext returns the TokenClass that can be scanned next after the given input prefix. A falseP result means the current chart has no scanner transitions, i.e. the prefix is a dead end for recognition.

expectNext scans its whole prefix each time it's run; to ask after each token of a growing prefix, as a decoder does, scan a Chart a token at a time, by scanChart, & ask chartExpects.

unreachableRules :: Transducer token -> Set String Source #

Rule names that can never be entered from the start expression — dead productions. A non-empty result is a grammar-hygiene warning: those rules can be deleted without changing the recognized language.

data Transducer token Source #

A Transducer is a tuple

T = (Σ, Δ, Q, I ⊆ Q, F ∈ Q, transition ⊆ Q × (Σ ∪ ∆) × Q, output ⊆ Q × ∆)

Constructors

Transducer 

Fields

Instances

Instances details
Categorized token => Matching [token] (Transducer token) Source # 
Instance details

Defined in Control.Lens.Grammar.Machine

Methods

(=~) :: [token] -> Transducer token -> Bool Source #

Chart

data Chart token Source #

The Earley chart of a prefix of tokens scanned by a Transducer: the sets E_0, …, E_n of Earley items, a state & the position it started at, which the prefix x_1 … x_n reaches. See Earley, An efficient context-free parsing algorithm.

A Chart is a prefix's state of recognition, scanned incrementally, a token at a time, by scanChart, which costs as much as closing one set of items, rather than rescanning the prefix. Charts persist, so one chart can be scanned by many different tokens, as a decoder does to mask a vocabulary of pieces by a grammar.

startChart :: Transducer token -> Chart token Source #

The Chart of the empty prefix.

scanChart :: Categorized token => token -> Chart token -> Maybe (Chart token) Source #

Scan a token into a Chart, or fail when the prefix with it is a dead end, which no word of the language starts with, provided that each of the grammar's rules matches some word.

scanChartMany :: Categorized token => [token] -> Chart token -> Maybe (Chart token) Source #

Scan tokens into a Chart, in order, or fail at a dead end.

data Scan Source #

The scanner transitions out of a Chart's last set of items which a token passes. Tokens with equal Scans scan a chart alike, so a chart need be scanned once for each Scan, by scanChartBy, rather than once for each token, as allowedPieces does. A Scan is of the chart it's taken of, & no other.

Instances

Instances details
Show Scan Source # 
Instance details

Defined in Control.Lens.Grammar.Machine

Methods

showsPrec :: Int -> Scan -> ShowS #

show :: Scan -> String #

showList :: [Scan] -> ShowS #

Eq Scan Source # 
Instance details

Defined in Control.Lens.Grammar.Machine

Methods

(==) :: Scan -> Scan -> Bool #

(/=) :: Scan -> Scan -> Bool #

Ord Scan Source # 
Instance details

Defined in Control.Lens.Grammar.Machine

Methods

compare :: Scan -> Scan -> Ordering #

(<) :: Scan -> Scan -> Bool #

(<=) :: Scan -> Scan -> Bool #

(>) :: Scan -> Scan -> Bool #

(>=) :: Scan -> Scan -> Bool #

max :: Scan -> Scan -> Scan #

min :: Scan -> Scan -> Scan #

chartScan :: Categorized token => token -> Chart token -> Scan Source #

The Scan of a token, of a Chart.

nullScan :: Scan -> Bool Source #

Does a Scan pass no transitions, so that its token is a dead end?

scanChartBy :: Scan -> Chart token -> Maybe (Chart token) Source #

Scan a Chart by a Scan of it, as scanChart scans it by any token of that Scan. The scanned chart's closure is lazy, so that it's only closed when it's scanned in turn, or asked of.

chartExpects :: Categorized token => Chart token -> TokenClass token Source #

The TokenClass of tokens which scanChart scans, the disjunction of the classes of the scanner transitions out of the chart's last set.

chartAccepts :: Chart token -> Bool Source #

Is a Chart's prefix a word of the language?

chartLength :: Chart token -> Int Source #

The number of tokens a Chart has scanned.