Verified Commit 4da6dec8 authored by Camil Staps's avatar Camil Staps 🙂

Move toString to separate module

parent ddf3aab8
......@@ -4,7 +4,7 @@ CLMFLAGS:=\
-I $$CLEAN_HOME/lib/Generics\
-nt
DEPMODS:=Regex
DEPMODS:=Regex Regex/Print
DEPS:=$(addsuffix .dcl,$(DEPMODS)) $(addsuffix .icl,$(DEPMODS))
EXE:=test
......
definition module Regex
from StdOverloaded import class toString
import Regex.Print
:: Regex
= Literal [Char]
......@@ -17,6 +17,4 @@ from StdOverloaded import class toString
:: Greediness :== Bool
:: Position :== Int
instance toString Regex
match :: Regex [Char] -> [(Position, [Char])]
implementation module Regex
import _SystemArray
import StdBool
import StdString
from StdFunc import flip, id
from Data.Func import $
import Data.List
/*** Printing ***/
print :: Bool Regex -> String
print ps (Literal cs) = parens (ps && length cs > 1) cs
print ps (CharacterClass [c]) = {c}
print ps (CharacterClass cs) = "[" <+ cs <+ "]"
print ps (Optional r) = print True r <+ "?"
print ps (Concat r1 r2) = parens ps $ r1 <+ r2
print ps (Any rgxs) = "(?:" <+ foldl (\x s -> x +++ "|" +++ s) r rs <+ ")"
where [r:rs] = map (print True) rgxs
print ps (Many gr r) = parens ps $ print True r <+ "*" <+ if gr "" "?"
print ps (Some gr r) = parens ps $ print True r <+ "+" <+ if gr "" "?"
print ps StartOfString = "^"
print ps EndOfString = "$"
print ps Empty = ""
instance toString Regex where toString r = print False r
parens :: Bool a -> String | toString a
parens True s = "(?:" <+ s <+ ")"
parens False s = toString s
(<+) infixr 5 :: a b -> String | toString a & toString b
(<+) x y = toString x +++ toString y
/*** Matching ***/
:: MatchStatus
= { skipped :: [Char]
, matched :: [Char]
......
definition module Regex.Print
from StdOverloaded import class toString
from Regex import :: Regex
instance toString Regex
implementation module Regex.Print
import _SystemArray
import StdBool
import StdString
from Data.Func import $
import Data.List
import Regex
print :: Bool Regex -> String
print ps (Literal cs) = parens (ps && length cs > 1) cs
print ps (CharacterClass [c]) = {c}
print ps (CharacterClass cs) = "[" <+ cs <+ "]"
print ps (Optional r) = print True r <+ "?"
print ps (Concat r1 r2) = parens ps $ r1 <+ r2
print ps (Any rgxs) = "(?:" <+ foldl (\x s -> x +++ "|" +++ s) r rs <+ ")"
where [r:rs] = map (print True) rgxs
print ps (Many gr r) = parens ps $ print True r <+ "*" <+ if gr "" "?"
print ps (Some gr r) = parens ps $ print True r <+ "+" <+ if gr "" "?"
print ps StartOfString = "^"
print ps EndOfString = "$"
print ps Empty = ""
instance toString Regex where toString r = print False r
parens :: Bool a -> String | toString a
parens True s = "(?:" <+ s <+ ")"
parens False s = toString s
(<+) infixr 5 :: a b -> String | toString a & toString b
(<+) x y = toString x +++ toString y
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment