-- -- file: Script.lhs -- -- created: 29/1/97 by Andrew Rock. -- -- purpose: This module defines and parses entre scripts. -- > module Script (offside, Def(Binds, Blanks, Evals), Script, scriptP) where > import Parser > import Lexer > import Expr ========================= Applying the offside rule ========================= This function modifies the output of the Lexer lexerL by inserting ";" tokens before the first lexeme that occurs anywhere not to the right of the preceeding "=" or ":=". > offside :: [((Tag, Lexeme), Pos)] -> [((Tag, Lexeme), Pos)] > offside > = offside' (-1,-1) > where > offside' :: Pos -> [((Tag, Lexeme), Pos)] -> [((Tag, Lexeme), Pos)] > offside' _ [] > = [] > offside' _ ((("symbol","="), p) : ls) > = (("symbol","="), p) : offside' p ls > offside' _ ((("symbol",":="), p) : ls) > = (("symbol",":="), p) : offside' p ls > offside' (l,c) (((tag,lexeme),(l',c')) : ls) > | c' <= c > = ((";",";"),(l',c')) : ((tag,lexeme),(l',c')) : offside' (l',c') ls > | otherwise > = ((tag,lexeme),(l',c')) : offside' (l,c) ls =========================================== Data type used to represent a parsed script =========================================== > data Def = Binds String Expr -- bind String to Expr > | Evals String Expr -- bind String to evaluated Expr > | Blanks -- only whitespace or comments > type Script = [Def] ================== Parser for scripts ================== script ::= script ; def | def script ::= def script' script' ::= ; def script' | epsilon > scriptP :: Parser Script > scriptP = (nofail . total) (defP <*> script'P) @> (\(d,s) -> d : s) > where > script'P :: Parser Script > script'P = literalP ";" ";" *> defP <*> script'P > @> (\(d,s) -> d : s) > <|> succeedA [] > defP = nameP <*> (many nameP) <*> literalP "symbol" "=" > *> nofail' "expression expected" exprP > @> (\ (Name n, (args, e)) -> Binds n (makeFunction args e)) > <|> nameP <*> literalP "symbol" ":=" > *> nofail' "expression expected" exprP > @> (\(Name n,e) -> Evals n e) > <|> succeedA Blanks