Skip to content

Commit

Permalink
isa-parser: add parsing for case-of
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZoeyChen committed Jan 14, 2020
1 parent b480da4 commit 6721484
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions isa-parser/Isabelle/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,18 @@ type ParserM a = Parsec String () a
--
-- FIXME: Don't duplicate the names here and elsewhere. Define as one constant in one place.
--

reservedWords :: [String]
reservedWords = [
"and", "apply", "apply_end", "assumes", "begin", "by", "chapter", "class", "consts",
"abbreviation", "and", "apply", "apply_end", "assumes", "begin", "by", "chapter", "class", "consts",
"datatype", "definition", "defs", "domintros", "done", "end", "fixes", "for", "fun",
"function", "imports", "instance", "instantiation", "is", "keywords", "lemma", "lemmas",
"no_translations", "open", "overloaded", "primrec", "record ", "section", "sequential", "sorry",
"subsection", "subsubsection", "termination", "text", "theorems", "theory", "translations",
"type_synonym", "typedecl", "unchecked", "uses", "where" ]

reservedWordsInner = ["case", "of", "if", "then", "else"]

---------------------------------------------------------------
-- Utility functions and combinators
-------------------------------------------------------------
Expand Down Expand Up @@ -123,6 +126,7 @@ parensL p = do { stringL "("; r <- p; stringL ")"; return r }
quotedL :: ParserM a -> ParserM a
quotedL p = do { stringL "\""; r <- p; stringL "\""; return r }


--------------------------------------------------------------------------------
-- Primitive parsers
--------------------------------------------------------------------------------
Expand Down Expand Up @@ -180,7 +184,13 @@ identS :: ParserM String
identS = letterS <++> manyP quasiletterS

identL :: ParserM String
identL = lexeme identS
identL = lexeme . try $
do { s <- identS
; if s `elem` reservedWordsInner
then unexpected ("'" ++ s ++ "' is a reserved word")
else return s }



antiquoteS :: ParserM String
antiquoteS = char '$' >> lookAhead anyChar >>= \case
Expand Down Expand Up @@ -342,9 +352,19 @@ sigL = do

abbreviationL :: ParserM (L Abbrev)
abbreviationL = do
mbSig <- mb sigL
t <- quotedTermL
return (Abbrev mbSig t)
reserved "abbreviation"
res <- alt1 <||> alt2
return res

where
alt1 = do
t <- quotedTermL
return (Abbrev Nothing t)
alt2 = do
sig <- sigL
reserved "where"
t <- quotedTermL
return $ Abbrev (Just sig) t

lemmaL :: ParserM (L Lemma)
lemmaL = do
Expand Down Expand Up @@ -503,6 +523,7 @@ methodWithArgsL = do
return $ n ++ mbColon
argL = lexeme argS


quotedTermL :: ParserM Term
quotedTermL = quotedL termL

Expand Down Expand Up @@ -560,7 +581,7 @@ antiquoteTermL = AntiTerm <$> antiquoteL
-- unary operators. The quantifier plus the identifiers that follow
-- it are considered as a unary operator. (.e.g. "\<exists>x y z." is
-- considered to be a unary operator which is applied to a term.)
--
--
termL :: ParserM Term
termL = buildExpressionParser table restL
where
Expand All @@ -583,9 +604,22 @@ termL = buildExpressionParser table restL
; return (TermUnOp u) }))


restL = antiquoteTermL <||> parensTermL <||> constTermL <||> (TermIdent <$> innerIdentL)
restL = antiquoteTermL <||> parensTermL <||> constTermL <||> (TermIdent <$> innerIdentL) <||> caseOfTermL
parensTermL = parensL termL

caseOfTermL :: ParserM Term
caseOfTermL = do { stringL "case"
; term <- termL
; stringL "of"
; alts <- sepBy1 (try altTermL) (stringL "|")
; return $ CaseOf term alts }

altTermL :: ParserM (Term, Term)
altTermL = do { term1 <- termL
; stringL "\\<Rightarrow>"
; term2 <- termL
; return (term1, term2) }

innerIdentL :: ParserM Ident
innerIdentL = (Id <$> identL) <||> wildcardL <||> parensL typedIdentL

Expand Down Expand Up @@ -644,7 +678,7 @@ multiParamDatatypeL = do
tyVarL :: ParserM Type
tyVarL = do
char '\''
v <- identL -- FIXME: Could be wrong
v <- lexeme (letterS <++> manyP quasiletterS)
return $ TyVar v

stripTyVar = map (\t -> let (TyVar v) = t in v)
Expand Down

0 comments on commit 6721484

Please sign in to comment.