derp-0.1.2: Derivative Parsing

Text.Derp

Contents

Synopsis

Data Types

data Parser a Source

Represents both a formal context-free language and the reduction of a member of that language to a value of type a.

Instances

Show (Parser a) 

data Token Source

The input type for parsing. For example the parser:

 (ter "x") 

will parse:

 (Token "x" "foo") 

into:

 (eps "foo")

Constructors

Token 

Instances

Parser construction

(<|>) :: ResultType a => Parser a -> Parser a -> Parser aSource

Alternation.

(<~>) :: (ResultType a, ResultType b) => Parser a -> Parser b -> Parser (a, b)Source

Concatenation.

(==>) :: (ResultType a, ResultType b) => Parser a -> (a -> b) -> Parser bSource

Reduction.

(==>|) :: (ResultType a, ResultType b) => Parser a -> (Set a -> Set b) -> Parser bSource

Set generalized version of ==>.

nul :: ResultType a => Parser a -> Parser aSource

Null-parse extraction.

pzip :: (ResultType a, ResultType b) => Parser a -> Context a b -> Parser bSource

One-hole-context focus.

ter :: String -> Parser StringSource

Terminal.

eps :: ResultType a => a -> Parser aSource

Epsilon/empty-string.

epsM :: ResultType a => Set a -> Parser aSource

Set generalized version of eps.

emp :: ResultType a => Parser aSource

The empty language.

Parser computation steps

derive :: Parser a -> Token -> Parser aSource

The main derivative function.

compact :: Parser a -> Parser aSource

The optimization step of the algorithm.

parseNull :: ResultType a => Parser a -> Set aSource

Extract the parse-null set of a parser.

Full parsing and result extraction

defaultCompactSteps :: IntSource

The number of compact steps that usually keeps a parser constant in size while parsing.

compactNum :: Int -> Parser a -> Parser aSource

A specified number of compactions.

deriveStepNum :: Int -> Parser a -> Token -> Parser aSource

Derivation followed by specified number of compactions.

runParseNum :: ResultType a => Int -> Parser a -> [Token] -> Set aSource

Parse using a specified number of intermediate compactions.

deriveStep :: Parser a -> Token -> Parser aSource

Derivation followed by the default number of compactions.

runParse :: ResultType a => Parser a -> [Token] -> Set aSource

Parse using the default number of intermediate compactions. This is the main parsing function. Examples:

 let e =     ter "num"
         <|> e <~> ter "+" <~> e ==> (\(x1,(o,x2)) -> "(" ++ x1 ++ o ++ x2 ++ ")")
 in runParse e [Token "num" "1", Token "+" "+", Token "num" 3", Token "+" "+", Token "num" "5"]

evaluates to:

 Set.fromList ["((1+3)+5)", "(1+(3+5))"]
 let e =     ter "num" ==> read 
         <|> e <~> ter "+" <~> e ==> (\(x1,(_,x2)) -> x1 + x2)
 in runParse e [Token "num" "1", Token "+" "+", Token "num" 3", Token "+" "+", Token "num" "5"]

evaluates to:

 Set.fromList [9]

Demos