Changeset 3595 for proto


Ignore:
Timestamp:
Dec 24, 2013, 7:21:38 AM (6 years ago)
Author:
cameron
Message:

Various corrections and cleanups

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/RE/doc/CanonicalRE.hs

    r3594 r3595  
    1212import Data.Char
    1313
    14 data RepLimit = UpperBound Int | Unbounded deriving Show
     14data RepLimit = UpperBound Int | UnBounded deriving Show
     15upper_bound_dec :: RepLimit -> RepLimit
    1516upper_bound_dec (UpperBound j) = UpperBound (j - 1)
    16 upper_bound_dec Unbounded = Unbounded
     17upper_bound_dec UnBounded = UnBounded
    1718
    1819data RE = CC String | Seq [RE] | Alt [RE] | Rep RE Int RepLimit |
    1920          Start | End  -- lookbehind/lookahead assertions for newline
    20 -- Rep r i j represents bounded repetition, with lower bound i and
     21-- Rep r i j represents UpperBound repetition, with lower bound i and
    2122-- upper bound j.   We use the convention that any negative value of
    22 -- j represents unbounded repetition.
     23-- j represents UnBounded repetition.
    2324
    2425canonicalize :: Proto.RE -> RE
     
    3233canonicalize (Proto.Alt rs) = Alt (map canonicalize rs)
    3334canonicalize (Proto.Opt r) = Rep (canonicalize r) 0 (UpperBound 1)
    34 canonicalize (Proto.Kstar r) = Rep (canonicalize r) 0 Unbounded
    35 canonicalize (Proto.Kplus r) = Rep (canonicalize r) 1 Unbounded
    36 canonicalize (Proto.OrMore i r) = Rep (canonicalize r) i Unbounded
     35canonicalize (Proto.Kstar r) = Rep (canonicalize r) 0 UnBounded
     36canonicalize (Proto.Kplus r) = Rep (canonicalize r) 1 UnBounded
     37canonicalize (Proto.OrMore i r) = Rep (canonicalize r) i UnBounded
    3738canonicalize (Proto.Bounded i j r) = Rep (canonicalize r) i (UpperBound j)
    3839
     
    9596-- is the next one in sequence.
    9697
    97 mre2pablo_helper(CC(c), m) =
     98re2pablo_helper(CC(c), m) =
    9899   (m + 1, [SetMarker (m + 1, Advance(And(Marker(m), CharClass(c))))])
    99100
     
    121122re2pablo_helper(Seq (r1:rs), m) = (m2, s1 ++ s2)
    122123  where
    123    (m1, s1) = mre2pablo_helper(r1, m)
    124    (m2, s2) = mre2pablo_helper(Seq rs, m1)
     124   (m1, s1) = re2pablo_helper(r1, m)
     125   (m2, s2) = re2pablo_helper(Seq rs, m1)
    125126
    126127-- Alt[r] has a single alternative r to match, just match
     
    132133-- does.
    133134
    134 re2pablo_helper(Alt (r1:rs), m) = (m2 + 1, s1 ++ s2 ++ [SetMarker (m2 + 1, Or(Marker m1, Marker m2))])
     135re2pablo_helper(Alt (r1:rs), m) = (m2 + 1, save ++ s1 ++ s2 ++ final)
    135136  where
    136    (m1, s1) = mre2pablo_helper(r1, m)
    137    (m2, s2) = mre2pablo_helper(Alt rs, m1)
     137   (m1, s1) = re2pablo_helper(r1, m)
     138   (m2, s2) = re2pablo_helper(Alt rs, m1+1)
     139   save = [SetMarker(m1 + 1, Marker(m))]
     140   final = [SetMarker(m2 + 1, Or(Marker(m1), Marker(m2)))]
    138141
    139 re2pablo_helper(Rep (CC c) 0 Unbounded, m) =
     142re2pablo_helper(Rep (CC c) 0 UnBounded, m) =
    140143   (m + 1, [SetMarker(m + 1, MatchStar(Marker(m), CharClass(c)))])
    141144
    142 re2pablo_helper(Rep r 0 ub, m)
    143   | ub < 0 = (m1, [SetMarker(m + 1, Marker m), While (Marker (m+1), s1 ++ [SetMarker(m + 1, And(Marker m1 ,Not(Marker (m+1))))] )])
    144   | otherwise = (m + 1, s1 ++ s2 ++ [SetMarker (m + 1, Or(Marker m1, Marker m2))])
     145re2pablo_helper(Rep r 0 UnBounded, m) =
     146  (m1+1, save ++ [While (Marker(m+1), s1 ++ loopfinal)])
    145147  where
    146    (m1, s1) = mre2pablo_helper(r, m+1)
    147    (m2, s2) = mre2pablo_helper(Rep r 0 (upper_bound_dec ub), m1)
     148   (m1, s1) = re2pablo_helper(r, m+1)
     149   save = [SetMarker(m+1, Marker(m)), SetMarker(m1+1, Marker(m))]
     150   loopfinal = [SetMarker(m+1, And(Marker(m1), Not(Marker(m1+1)))), SetMarker(m1+1, Or(Marker(m1+1), Marker(m1)))]
     151   
     152re2pablo_helper(Rep r 0 (UpperBound 0), m) = (m, [])
    148153
    149 re2pablo_helper(Rep r lb ub, m) =  (m2, s1 ++ s2)
     154re2pablo_helper(Rep r 0 (UpperBound ub), m) =
     155  (m2 + 1, s1 ++ s2 ++ [SetMarker (m2 + 1, Or(Marker m1, Marker m2))])
    150156  where
    151    (m1, s1) = mre2pablo_helper(r, m)
    152    (m2, s2) = mre2pablo_helper(Rep r (lb - 1) (upper_bound_dec ub), m1)
     157   (m1, s1) = re2pablo_helper(r, m)
     158   (m2, s2) = re2pablo_helper(Rep r 0 (UpperBound(ub-1)), m1)
    153159
     160re2pablo_helper(Rep r lb (UpperBound ub), m) =  (m2, s1 ++ s2)
     161  where
     162   (m1, s1) = re2pablo_helper(r, m)
     163   (m2, s2) = re2pablo_helper(Rep r (lb-1) (UpperBound(ub-1)), m1)
     164
Note: See TracChangeset for help on using the changeset viewer.