git @ Cat's Eye Technologies Fountain / 1074ffb
Another checkpoint, towards lookahead constraints. Chris Pressey 1 year, 5 months ago
4 changed file(s) with 26 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
1717 | LessThan Variable CExpr
1818 | LessThanOrEqual Variable CExpr
1919 | Both Constraint Constraint
20 | Lookahead String
2021 deriving (Show, Ord, Eq)
2122
2223 depictVar (Var s) = s
3334 depictConstraint (LessThan v e) = (depictVar v) ++ " < " ++ (depictCExpr e)
3435 depictConstraint (LessThanOrEqual v e) = (depictVar v) ++ " <= " ++ (depictCExpr e)
3536 depictConstraint (Both c1 c2) = "&& " ++ (depictConstraint c1) ++ ", " ++ (depictConstraint c2)
37 depictConstraint (Lookahead s) = "token is " ++ (show s)
3434 gen g state@(Generating _str store) (Alt False choices) =
3535 case missingPreConditions choices of
3636 missing@(_:_) ->
37 error ("No pre-condition present on these Alt choices: " ++ (show missing))
37 error ("No pre-condition present on these Alt choices: " ++ (depictExprs missing))
3838 [] ->
3939 let
4040 preConditionedChoices = map (\x -> (getPreCondition x, x)) choices
4848 -- we ignore the constraint here because it will be found and applied when we descend into e
4949 genAlt st [(_, e)] = gen g st e
5050 genAlt _st other =
51 error ("Multiple pre-conditions are satisfied in Alt: " ++ (show other))
51 error ("Multiple pre-conditions are satisfied in Alt: " ++ (depictExprs (map (snd) other)))
5252
5353 gen _g _state (Alt True _choices) = error "Backtracking alternations during generation not yet implemented"
5454
123123 Just $ update (\i -> Just (i - delta)) v st
124124 Nothing ->
125125 Nothing
126 applyConstraint (GreaterThan v e) st = applyRelConstraint (>) v e st
127 applyConstraint (GreaterThanOrEqual v e) st = applyRelConstraint (>=) v e st
128 applyConstraint (LessThan v e) st = applyRelConstraint (<) v e st
129 applyConstraint (LessThanOrEqual v e) st = applyRelConstraint (<=) v e st
130126 applyConstraint (Both c1 c2) st =
131127 case applyConstraint c1 st of
132128 Just st' ->
133129 applyConstraint c2 st'
134130 Nothing ->
135131 Nothing
132 applyConstraint (Lookahead _) _st =
133 -- This only applies in parsing; during generation, there is nothing to look ahead TO!
134 Nothing
135 applyConstraint (GreaterThan v e) st = applyRelConstraint (>) v e st
136 applyConstraint (GreaterThanOrEqual v e) st = applyRelConstraint (>=) v e st
137 applyConstraint (LessThan v e) st = applyRelConstraint (<) v e st
138 applyConstraint (LessThanOrEqual v e) st = applyRelConstraint (<=) v e st
136139
137140 applyRelConstraint op v e st =
138141 case (fetch v st, ceval e st) of
22 Expr(Seq, Alt, Loop, Terminal, NonTerminal, Constraint),
33 Production(Production, ntname, params, backtrackable, constituents),
44 Grammar(Grammar),
5 depictExpr, depictProduction, depictGrammar, depictVars,
5 depictExpr, depictExprs, depictProduction, depictGrammar, depictVars,
66 startSymbol, production, getFormals,
77 getPreCondition, missingPreConditions
88 ) where
5151 depictExpr (NonTerminal name vars) = name ++ depictVars vars
5252 depictExpr (Constraint c) = "<. " ++ (depictConstraint c) ++ " .>"
5353
54 depictExprs exprs = (intercalate ", " (map (depictExpr) exprs))
55
5456 depictVars [] = ""
5557 depictVars vars = "<" ++ (intercalate ", " (map (depictVar) vars)) ++ ">"
5658
4040 st' -> st'
4141
4242 -- Hello, Mrs Non-Backtracking Alternation!
43 parse g state@(Parsing _str store) expr@(Alt False choices) =
43 parse g state@(Parsing _str store) (Alt False choices) =
4444 case missingPreConditions choices of
4545 missing@(_:_) ->
46 error ("No pre-condition present on these Alt choices: " ++ (show missing) ++ " of this: " ++ (show expr))
46 error ("No pre-condition present on these Alt choices: " ++ (depictExprs missing))
4747 [] ->
4848 let
4949 preConditionedChoices = map (\x -> (getPreCondition x, x)) choices
5757 -- we ignore the constraint here because it will be found and applied when we descend into e
5858 parseAlt st [(_, e)] = parse g st e
5959 parseAlt _st other =
60 error ("Multiple pre-conditions are satisfied in Alt: " ++ (show other))
60 error ("Multiple pre-conditions are satisfied in Alt: " ++ (depictExprs (map (snd) other)))
6161
6262 parse g state (Loop l _) = parseLoop state l where
6363 parseLoop st e =
120120 Just $ update (\i -> Just (i - delta)) v st
121121 Nothing ->
122122 Nothing
123 applyConstraint (GreaterThan v e) st = applyRelConstraint (>) v e st
124 applyConstraint (GreaterThanOrEqual v e) st = applyRelConstraint (>=) v e st
125 applyConstraint (LessThan v e) st = applyRelConstraint (<) v e st
126 applyConstraint (LessThanOrEqual v e) st = applyRelConstraint (<=) v e st
127123 applyConstraint (Both c1 c2) st =
128124 case applyConstraint c1 st of
129125 Just st' ->
130126 applyConstraint c2 st'
131127 Nothing ->
132128 Nothing
129 applyConstraint (Lookahead _s) _st =
130 -- FIXME we need access to state, not just store!
131 -- state@(Parsing (c:cs) _store)
132 -- if s == [c] then Just state else Nothing
133 Nothing
134 applyConstraint (GreaterThan v e) st = applyRelConstraint (>) v e st
135 applyConstraint (GreaterThanOrEqual v e) st = applyRelConstraint (>=) v e st
136 applyConstraint (LessThan v e) st = applyRelConstraint (<) v e st
137 applyConstraint (LessThanOrEqual v e) st = applyRelConstraint (<=) v e st
133138
134139 applyRelConstraint op v e st =
135140 case (fetch v st, ceval e st) of