Changeset 3606 for proto


Ignore:
Timestamp:
Jan 11, 2014, 10:39:10 AM (6 years ago)
Author:
cameron
Message:

Use individual helper functions for Seq, Alt, RE

File:
1 edited

Legend:

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

    r3600 r3606  
    7676re2pablo_helper :: (RE, CodeGenState) -> CodeGenState
    7777
     78-- We also define helper functions for the working through
     79-- the RE lists in Seq and Alt structures, as well as the
     80-- iteration required for Rep structures.
     81seq_helper :: ([RE], CodeGenState) -> CodeGenState
     82alt_helper :: ([RE], CodeGenState) -> CodeGenState
     83rep_helper :: (RE, Int, RepLimit, CodeGenState) -> CodeGenState
    7884
    7985compile(re) = re2pablo_helper(re, (env, [Assign(marker, All(1))], marker))
     
    106112     new_stmt = Assign (newsym, And(Var(last_marker), CharClass("\n")))
    107113
     114-- For the structured types (Seq, Alt, Rep), just call the specific helper.
     115--
     116re2pablo_helper(Seq rs, cg_state) = seq_helper(rs, cg_state)
     117re2pablo_helper(Alt rs, cg_state) = alt_helper(rs, cg_state)
     118re2pablo_helper(Rep (r, lb, ub), cg_state) = rep_helper(r, lb, ub, cg_state)
     119
     120
    108121-- Seq [] is the empty regexp which matches the empty string. 
    109122-- We just leave the current marker unchanged, with no new statements
    110123-- needed to compute it.
    111124--
    112 re2pablo_helper(Seq [], cg_state) = cg_state
    113 
     125seq_helper([], cg_state) = cg_state
    114126-- Seq (r1:rs): generate code to match r1, the first subexpression,
    115127-- then to match the rest, concatenating the statements.
    116 re2pablo_helper(Seq (r1:rs), cg_state) = re2pablo_helper(Seq rs, re2pablo_helper(r1, cg_state))
    117  
     128seq_helper((r1:rs), cg_state) = seq_helper(rs, re2pablo_helper(r1, cg_state))
     129
    118130-- Alt[r] has a single alternative r to match, just match
    119131-- it with no other possibility.
    120 re2pablo_helper(Alt [r], cg_state) = re2pablo_helper(r, cg_state)
     132alt_helper([r], cg_state) = re2pablo_helper(r, cg_state)
    121133
    122134-- For completeness, we define Alt[] as the regular expression that
    123135-- always fails (since no alternatives will match).
    124 re2pablo_helper(Alt [], (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
     136alt_helper([], (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
    125137   where
    126138     (newenv, newsym) = gensym(env, "always_fail_marker")
     
    130142-- match succeeds if either r1 matches or any of the others
    131143-- does.
    132 re2pablo_helper(Alt (r1:rs), (env, stmts, last_marker)) = (newenv, new_stmts, newsym)
     144alt_helper(r1:rs, (env, stmts, last_marker)) = (newenv, new_stmts, newsym)
    133145  where
    134146   (e1, s1, alt1_marker) = re2pablo_helper(r1, (env, stmts, last_marker))
    135    (e2, s2, alt2_marker) = re2pablo_helper(Alt rs, (e1, s1, last_marker))
     147   (e2, s2, alt2_marker) = alt_helper(rs, (e1, s1, last_marker))
    136148   (newenv, newsym) = gensym(e2, "alt_marker")
    137149   new_stmts = s2 ++ [Assign (newsym, Or(Var(alt1_marker), Var(alt2_marker)))]
    138150
    139 -- Repetition Matching Rules
    140 --
    141151-- For Kleene Star character class repetition, use MatchStar
    142 re2pablo_helper(Rep (CC c, 0, Unbounded), (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
     152rep_helper(CC c, 0, Unbounded, (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
    143153   where
    144154     (newenv, newsym) = gensym(env, "marker")
     
    146156
    147157-- For general Kleene Star, we need a while loop.
    148 re2pablo_helper(Rep (r, 0, Unbounded), (env, stmts, last_marker)) = (newenv, stmts ++ while_init ++ [loop_stmt], while_accum)
     158rep_helper(r, 0, Unbounded, (env, stmts, last_marker)) = (newenv, stmts ++ while_init ++ [loop_stmt], while_accum)
    149159  where
    150160    (e1, while_test) = gensym(env, "while_test")
     
    156166    loop_stmt = While(Var(while_test), loop_body_stmts ++ repeat)
    157167
    158 re2pablo_helper(Rep (r, lb, Unbounded), cg_state) = re2pablo_helper(Rep (r, lb-1, Unbounded), cg1_state)
     168rep_helper(r, lb, Unbounded, cg_state) = rep_helper(r, lb-1, Unbounded, cg1_state)
    159169  where
    160170   cg1_state = re2pablo_helper(r, cg_state)
     
    162172-- Now Bounded Repetition: use multiple copies
    163173   
    164 re2pablo_helper(Rep (r, 0, UpperBound 0), cg_state) = cg_state
     174rep_helper(r, 0, UpperBound 0, cg_state) = cg_state
    165175
    166 re2pablo_helper(Rep (r, 0, UpperBound ub), (env, stmts, last_marker)) = (newenv, new_stmts, newsym)
     176rep_helper(r, 0, UpperBound ub, (env, stmts, last_marker)) = (newenv, new_stmts, newsym)
    167177  where
    168178   (e1, s1, rep1_marker) = re2pablo_helper(r, (env, stmts, last_marker))
    169    (e2, s2, rep2plus_marker) = re2pablo_helper(Rep (r, 0, UpperBound(ub-1)), (e1, s1, rep1_marker))
     179   (e2, s2, rep2plus_marker) = rep_helper(r, 0, UpperBound(ub-1), (e1, s1, rep1_marker))
    170180   (newenv, newsym) = gensym(e2, "alt_marker")
    171181   new_stmts = s2 ++ [Assign (newsym, Or(Var(last_marker), Var(rep2plus_marker)))]
    172182
    173 re2pablo_helper(Rep (r, lb, UpperBound ub), cg_state) = re2pablo_helper(Rep (r, lb-1, UpperBound(ub-1)), cg1_state)
     183rep_helper(r, lb, UpperBound ub, cg_state) = rep_helper(r, lb-1, UpperBound(ub-1), cg1_state)
    174184  where
    175185   cg1_state = re2pablo_helper(r, cg_state)
Note: See TracChangeset for help on using the changeset viewer.