source: proto/RE/Haskell/REcompile.hs @ 3826

Last change on this file since 3826 was 3826, checked in by cameron, 5 years ago

Update for SparseCharSet?

File size: 7.2 KB
Line 
1-- Module REcompile is a research compiler for prototyping
2-- algorithms and ideas in regular expression processing using
3-- parallel bit streams and MatchStar.
4-- It uses a canonical representation for regular expressions
5-- that uses a small number of alternative forms (e.g., combining
6-- all forms of repetition (Kleene star, Kleene plus, ?, {m,n}) into
7-- a single Rep structure. 
8
9-- Robert D. Cameron, 2014
10
11module REcompile (compile, searchRE, searchRegExp) where
12       
13import Data.Char
14import SparseCharSet
15import CanonicalRE
16import REparse
17import RunPablo
18
19--
20-- The Symbol Environment keeps track of the symbols generated
21-- during compilation, so that new unique symbols can be
22-- generated (gensym) as needed.   All symbols are formed from
23-- a string prefix and a numeric suffix.
24--
25-- For example, if the symbols "marker0", "marker1", "condition0"
26-- have been generated and no others have, the environment is
27-- [("marker", 1), ("condition", 0)]
28--
29type Env = [(String, Int)]
30
31gensym :: (Env, String) -> (Env, String)
32gensym ([], s) = ([(s, 0)], s ++ show 0)
33gensym ((prefix1, count1):env, s) 
34  | prefix1 == s = ((s, count1+1):env, s ++ show(count1+1))
35  | otherwise    = ((prefix1, count1):newenv, newsym)
36  where (newenv, newsym) = gensym(env, s)
37
38
39--
40-- To keep track of the current context for code generation during
41-- compilation, we define a CodeGenState that represents the output
42-- at each compilation step.   This state consists of an environment
43-- defining the symbols used so far, the sequence of generated PabloS
44-- statements generated so far and the name of the variable that
45-- holds the match result for the overall expression so far.
46--
47type CodeGenState = (Env, [PabloS], String)
48
49--
50-- In compiling to regular expression form, we use a helper function
51-- that takes a regular expression and a code generation state, and
52-- returns the code generation state.   The main compile routine
53-- initializes the process by setting an initial marker stream
54-- of all ones.
55--
56--
57compile :: RE -> CodeGenState
58re2pablo_helper :: (RE, CodeGenState) -> CodeGenState
59
60-- We also define helper functions for the working through
61-- the RE lists in Seq and Alt structures, as well as the
62-- iteration required for Rep structures.
63seq_helper :: ([RE], CodeGenState) -> CodeGenState
64alt_helper :: ([RE], CodeGenState) -> CodeGenState
65rep_helper :: (RE, Int, RepLimit, CodeGenState) -> CodeGenState
66
67eol_CC = CharClass([CharRange(ord '\n', ord '\n')])
68
69compile(re) = re2pablo_helper(re, (env, [Assign(marker, All(1))], marker))
70  where 
71    (env, marker) = gensym([], "start_marker")
72
73-- To match a character class, use bitwise and of the marker and
74-- character class, then advance 1. 
75
76re2pablo_helper(CC(c), (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
77   where 
78     (newenv, newsym) = gensym(env, "marker")
79     new_stmt = Assign (newsym, Advance(And(Var(last_marker), CharClass(c))))
80
81-- To match "^" we must be at the start of line, i.e., one past
82-- a newline character or at the beginning of file.  We use the
83-- trick of advancing the negated newline stream to determine these
84-- positions.
85--
86re2pablo_helper(Start, (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
87   where 
88     (newenv, newsym) = gensym(env, "start_of_line_marker")
89     new_stmt = Assign (newsym, And(Var(last_marker), Not(Advance(Not(eol_CC)))))
90
91-- To match "$" we must have reached end of line.
92--
93re2pablo_helper(End, (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
94   where 
95     (newenv, newsym) = gensym(env, "end_of_line_marker")
96     new_stmt = Assign (newsym, And(Var(last_marker), eol_CC))
97
98-- For the structured types (Seq, Alt, Rep), just call the specific helper.
99--
100re2pablo_helper(Seq rs, cg_state) = seq_helper(rs, cg_state)
101re2pablo_helper(Alt rs, cg_state) = alt_helper(rs, cg_state)
102re2pablo_helper(Rep (r, lb, ub), cg_state) = rep_helper(r, lb, ub, cg_state)
103
104
105-- Seq [] is the empty regexp which matches the empty string. 
106-- We just leave the current marker unchanged, with no new statements
107-- needed to compute it.
108--
109seq_helper([], cg_state) = cg_state
110-- Seq (r1:rs): generate code to match r1, the first subexpression,
111-- then to match the rest, concatenating the statements.
112seq_helper((r1:rs), cg_state) = seq_helper(rs, re2pablo_helper(r1, cg_state))
113
114-- Alt[r] has a single alternative r to match, just match
115-- it with no other possibility.
116alt_helper([r], cg_state) = re2pablo_helper(r, cg_state)
117
118-- For completeness, we define Alt[] as the regular expression that
119-- always fails (since no alternatives will match).
120alt_helper([], (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
121   where 
122     (newenv, newsym) = gensym(env, "always_fail_marker")
123     new_stmt = Assign (newsym, All(0))
124
125-- Alt (r1:rs): we have r1 and some other alternatives, the
126-- match succeeds if either r1 matches or any of the others
127-- does.
128alt_helper(r1:rs, (env, stmts, last_marker)) = (newenv, new_stmts, newsym)
129  where 
130   (e1, s1, alt1_marker) = re2pablo_helper(r1, (env, stmts, last_marker))
131   (e2, s2, alt2_marker) = alt_helper(rs, (e1, s1, last_marker))
132   (newenv, newsym) = gensym(e2, "alt_marker")
133   new_stmts = s2 ++ [Assign (newsym, Or(Var(alt1_marker), Var(alt2_marker)))]
134
135-- For Kleene Star character class repetition, use MatchStar
136rep_helper(CC c, 0, Unbounded, (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
137   where 
138     (newenv, newsym) = gensym(env, "marker")
139     new_stmt = Assign (newsym, MatchStar(Var(last_marker), CharClass(c))) 
140
141-- For general Kleene Star, we need a while loop.
142rep_helper(r, 0, Unbounded, (env, stmts, last_marker)) = (newenv, stmts ++ while_init ++ [loop_stmt], while_accum)
143  where
144    (e1, while_test) = gensym(env, "while_test")
145    (e2, while_accum) = gensym(e1, "while_accum")
146    (newenv, loop_body_stmts, loop_result) = re2pablo_helper(r, (e2, [], while_test))
147    while_init = [Assign(while_test, Var(last_marker)), Assign(while_accum, Var(last_marker))]
148    repeat = [Assign(while_test, And(Var(loop_result), Not(Var(while_accum)))),
149              Assign(while_accum, Or(Var(while_accum), Var(loop_result)))]
150    loop_stmt = While(Var(while_test), loop_body_stmts ++ repeat)
151
152rep_helper(r, lb, Unbounded, cg_state) = rep_helper(r, lb-1, Unbounded, cg1_state)
153  where 
154   cg1_state = re2pablo_helper(r, cg_state)
155
156-- Now Bounded Repetition: use multiple copies
157   
158rep_helper(r, 0, UpperBound 0, cg_state) = cg_state
159
160rep_helper(r, 0, UpperBound ub, (env, stmts, last_marker)) = (newenv, new_stmts, newsym)
161  where 
162   (e1, s1, rep1_marker) = re2pablo_helper(r, (env, stmts, last_marker))
163   (e2, s2, rep2plus_marker) = rep_helper(r, 0, UpperBound(ub-1), (e1, s1, rep1_marker))
164   (newenv, newsym) = gensym(e2, "alt_marker")
165   new_stmts = s2 ++ [Assign (newsym, Or(Var(last_marker), Var(rep2plus_marker)))]
166
167rep_helper(r, lb, UpperBound ub, cg_state) = rep_helper(r, lb-1, UpperBound(ub-1), cg1_state)
168  where 
169   cg1_state = re2pablo_helper(r, cg_state)
170
171---------------
172
173searchRE :: (RE, String) -> PabloVal
174searchRE(regexp, str) = evalPabloS(compiled, marker, str)
175  where (e, compiled, marker) = compile(regexp)
176
177searchRegExp :: (String, String) -> Maybe PabloVal
178searchRegExp(regexp, str) =
179  case parseRE(regexp) of
180    ParseSuccess r  -> Just (searchRE(r, str))
181    _ -> Nothing
182
Note: See TracBrowser for help on using the repository browser.