Best of both worlds : ignore unknown characters but only on top state

This commit is contained in:
Tissevert 2019-05-06 22:09:04 +02:00
parent 174912bb5a
commit ed5ee22a49
2 changed files with 5 additions and 2 deletions

View File

@ -3,6 +3,7 @@
module Graph (
Graph(..)
, Vertex(..)
, Zipper(..)
, editLabel
, follow
, open

View File

@ -7,7 +7,7 @@ module Transducer (
import Stream (Stream(..), merge)
import qualified Stream (empty)
import Graph (Graph(..), Vertex(..), editLabel, follow, open, rewind, singleton, weave)
import Graph (Graph(..), Vertex(..), Zipper(..), editLabel, follow, open, rewind, singleton, weave)
type Transducer input output = Graph input [output]
@ -24,7 +24,9 @@ fromList = foldl add empty
run :: (Ord input, Eq output) => Transducer input output -> Stream input -> Stream output
run transducer (Stream inputs) = foldl (\(Stream outputs) (input, stream) ->
case follow transducer input of
Nothing -> Stream.empty
Nothing -> case context transducer of
Top -> run (rewind transducer) stream
_ -> Stream.empty
Just newState@(Graph {focus}) ->
Stream ((emit stream <$> label focus) ++ outputs) `merge` run newState stream
) Stream.empty inputs