Changeset 3901 for proto/RE


Ignore:
Timestamp:
Jun 27, 2014, 3:16:08 AM (5 years ago)
Author:
cameron
Message:

Avoid duplicating the repeated expression, if it can't be simplified

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/RE/Haskell/Nullable.hs

    r3888 r3901  
    4646removeNullablePrefix (Alt as) = Alt (map removeNullablePrefix as)
    4747removeNullablePrefix (Rep(r, lb, ub))
    48    | lb == 0        = Seq []
    49    | isNullable(r)  = Seq []
    50    | otherwise      = Seq [removeNullablePrefix(r), Rep(r, lb-1, lb-1)]
     48   | lb == 0               = Seq []
     49   | isNullable(r)         = Seq []
     50   | hasNullablePrefix(r)  = mkSeq[removeNullablePrefix(r), mkRep(r, lb-1, lb-1)]
     51   | otherwise             = mkRep(r, lb, lb)
    5152-- default: do nothing
    5253removeNullablePrefix r = r
     
    6263removeNullableSuffix (Alt as) = Alt (map removeNullableSuffix as)
    6364removeNullableSuffix (Rep(r, lb, ub))
    64    | lb == 0        = Seq []
    65    | isNullable(r)  = Seq []
    66    | otherwise      = Seq [Rep(r, lb-1, lb-1), removeNullableSuffix(r)]
     65   | lb == 0               = Seq []
     66   | isNullable(r)         = Seq []
     67   | hasNullableSuffix(r)  = mkSeq[mkRep(r, lb-1, lb-1), removeNullableSuffix(r)]
     68   | otherwise             = mkRep(r, lb, lb)
    6769-- default: do nothing
    6870removeNullableSuffix r = r
     
    7375  | isNullableSeq(more)       = [removeNullableSuffix(a)]
    7476  | otherwise                 = a:removeNullableSeqSuffix(more)
     77
     78
     79hasNullablePrefix :: RE -> Bool
     80hasNullablePrefix (Seq (e:es))
     81  | isNullable(e)   = True
     82  | otherwise       = hasNullablePrefix(e)
     83hasNullablePrefix (Alt []) = False
     84hasNullablePrefix (Alt (a:as))
     85  | hasNullablePrefix(a)   = True
     86  | otherwise              = hasNullablePrefix (Alt as)
     87hasNullablePrefix (Rep(r, lb, ub)) = hasNullablePrefix(r)
     88hasNullablePrefix r = False
     89
     90hasNullableSuffix :: RE -> Bool
     91hasNullableSuffix (Seq [e])
     92  | isNullable(e)   = True
     93  | otherwise       = hasNullableSuffix(e)
     94hasNullableSuffix (Seq (e:es)) = hasNullableSuffix(Seq es)
     95hasNullableSuffix (Alt []) = False
     96hasNullableSuffix (Alt (a:as))
     97  | hasNullableSuffix(a)   = True
     98  | otherwise              = hasNullableSuffix (Alt as)
     99hasNullableSuffix (Rep(r, lb, ub)) = hasNullableSuffix(r)
     100hasNullableSuffix r = False
Note: See TracChangeset for help on using the changeset viewer.