git @ Cat's Eye Technologies Fountain / 33f23d1
"applyGenConstraint" ignores Lookahead constraints. Chris Pressey 1 year, 2 months ago
3 changed file(s) with 28 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
3636 genTerminal :: Char -> GenState -> GenState
3737 genTerminal c state@Generating{ text=text } = state{ text=(c:text) }
3838
39 --
40 -- Alt choices heed preconditions to prevent unnecessary backtracking.
41 --
42 getPreCondition :: Expr -> Constraint Value
43 getPreCondition (Seq (x:_)) = getPreCondition x
44 getPreCondition (Constraint c) = c
45 getPreCondition _ = Identity
46
47 getApplicableChoices store choices =
39 getApplicableChoices state choices =
4840 let
4941 preConditionedChoices = map (\x -> (getPreCondition x, x)) choices
50 isApplicableChoice (c, _) = can $ applyConstraint c store
42 isApplicableChoice (c, _) = can $ applyGenConstraint c state
5143 in
5244 filter (isApplicableChoice) preConditionedChoices
45
46 applyGenConstraint :: Constraint Value -> Store Value -> Maybe (Store Value)
47 applyGenConstraint (Lookahead _) store = Just store
48 applyGenConstraint other store = applyConstraint other store
5349
5450 scramble :: [a] -> GenState -> ([a], GenState)
5551 scramble choices state@Generating{ prng=prng } =
118114 Nothing -> genLoop st' e
119115 checkLimit [] st = Just st
120116 checkLimit (c:cs) st =
121 case applyConstraint c st of
117 case applyGenConstraint c st of
122118 Nothing -> Nothing
123119 Just st' -> checkLimit cs st'
124120
138134 Left x
139135
140136 gen _g st@Generating{ store=store } (Constraint cstr) =
141 case applyConstraint cstr store of
137 case applyGenConstraint cstr store of
142138 Just store' ->
143139 -- TODO st{ trace=(("OK " ++ depictConstraint cstr):trace), store=store' }
144140 Right $ st{ store=store' }
99 Grammar(Grammar),
1010 depictExpr, depictExprs, depictProduction, depictGrammar, depictVars,
1111 startSymbol, productionExpr, productionParams, getFormals,
12 getPreCondition
1213 ) where
1314
1415 import Data.List (intercalate)
9394 if nt == (ntname prod) then params prod else getFormals nt (Grammar rest)
9495 getFormals nt (Grammar []) = error ("Production '" ++ nt ++ "' not found")
9596
97 --
98 -- Alt choices heed preconditions to prevent unnecessary backtracking.
99 --
100 getPreCondition :: Expr -> Constraint Value
101 getPreCondition (Seq (x:_)) = getPreCondition x
102 getPreCondition (Constraint c) = c
103 getPreCondition (Terminal c) = Lookahead s where s = [c]
104 getPreCondition _ = Identity
3030 expectTerminal _tc (Parsing [] _a) =
3131 Left "end of string"
3232
33 --
34 -- Alt choices heed preconditions to prevent unnecessary backtracking.
35 --
36 getPreCondition :: Expr -> Constraint Value
37 getPreCondition (Seq (x:_)) = getPreCondition x
38 getPreCondition (Constraint c) = c
39 getPreCondition (Terminal c) = Lookahead s where s = [c]
40 getPreCondition _ = Identity
41
4233 getApplicableChoices state choices =
4334 let
4435 preConditionedChoices = map (\x -> (getPreCondition x, x)) choices
4536 isApplicableChoice (c, _) = can $ applyParseConstraint c state
4637 in
4738 filter (isApplicableChoice) preConditionedChoices
39
40 applyParseConstraint :: Constraint Value -> ParseState -> ParseResult
41 applyParseConstraint (Lookahead s) state@(Parsing (c:_) _) =
42 if s == [c] then (Right state) else (Left "lookahead failed")
43 applyParseConstraint (Lookahead _) _ = Left "lookahead failed"
44 applyParseConstraint other (Parsing s store) =
45 case applyConstraint other store of
46 Just store' ->
47 Right $ Parsing s store'
48 Nothing ->
49 Left "constraint failed"
4850
4951 --
5052 -- Parser
107109
108110 parse _g state (Constraint cstr) = applyParseConstraint cstr state
109111
110
111 applyParseConstraint :: Constraint Value -> ParseState -> ParseResult
112 applyParseConstraint (Lookahead s) state@(Parsing (c:_) _) =
113 if s == [c] then (Right state) else (Left "lookahead failed")
114 applyParseConstraint (Lookahead _) _ = Left "lookahead failed"
115 applyParseConstraint other (Parsing s store) =
116 case applyConstraint other store of
117 Just store' ->
118 Right $ Parsing s store'
119 Nothing ->
120 Left "constraint failed"
121
122112 --
123113 -- Usage interface
124114 --