-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathLexer_CLIF.hs
166 lines (128 loc) · 4.36 KB
/
Lexer_CLIF.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
{- |
Module : $Header$
Description : Parser of common logic interchange format
Copyright : (c) Karl Luc, DFKI Bremen 2010
License : GPLv2 or higher
Maintainer : kluc@informatik.uni-bremen.de
Stability : provisional
Portability : portable
Parser of common logic interchange format
-}
{-
Ref. Common Logic ISO/IEC IS 24707:2007(E)
-}
module CommonLogic.Lexer_CLIF where
import CommonLogic.AS_CommonLogic
import Common.Id as Id
import Common.Lexer as Lexer
import Common.Parsec
import Common.Keywords
import Text.ParserCombinators.Parsec as Parsec
import Data.Char (ord)
name :: CharParser st String
name = do
x <- identifier
return $ (tokStr x)
quotedstring :: CharParser st String
quotedstring = do
char '\''
s <- (many $ (satisfy clLetters2) <|> (oneOf whitec)
<|> char '(' <|> char ')' <|> char '\"')
<?> "quotedstring: word"
char '\''
return $ s
enclosedname :: CharParser st String
enclosedname = do
char '\"'
s <- (many $ (satisfy clLetters2) <|> (oneOf whitec)
<|> char '(' <|> char ')' <|> char '\'')
<?> "word"
char '\"' <?> "\""
return s
-- | parser for parens
parens :: CharParser st a -> CharParser st a
parens p = do
spaces
oParenT >> p << cParenT
-- | parser for ignoring parentheses
-- why do i need that?
par :: CharParser st a -> CharParser st a
par p = do
try oParenT
x <- p
cParenT
return x
<|> do
x <- p
return x
-- Parser Keywords
andKey :: CharParser st Id.Token
andKey = Lexer.pToken $ string andS
notKey :: CharParser st Id.Token
notKey = Lexer.pToken $ string notS
orKey :: CharParser st Id.Token
orKey = Lexer.pToken $ string orS
ifKey :: CharParser st Id.Token
ifKey = (Lexer.pToken $ string ifS)
iffKey :: CharParser st Id.Token
iffKey = (Lexer.pToken $ string iffS)
forallKey :: CharParser st Id.Token
forallKey = Lexer.pToken $ string forallS
existsKey :: CharParser st Id.Token
existsKey = Lexer.pToken $ string existsS
-- cl :: CharParser st a -> CharParser st a
-- cl p = string "cl-" >> p
-- cl keys
clTextKey :: CharParser st Id.Token
clTextKey = Lexer.pToken $ try (string "cl-text") <|> string "cl:text"
clModuleKey :: CharParser st Id.Token
clModuleKey = Lexer.pToken $ try (string "cl-module") <|> string "cl:module"
clImportsKey :: CharParser st Id.Token
clImportsKey = Lexer.pToken $ try (string "cl-imports") <|> string "cl:imports"
clExcludesKey :: CharParser st Id.Token
clExcludesKey = Lexer.pToken
$ try (string "cl-excludes") <|> string "cl:excludes"
clCommentKey :: CharParser st Id.Token
clCommentKey = Lexer.pToken $ try (string "cl-comment") <|> string "cl:comment"
clRolesetKey :: CharParser st Id.Token
clRolesetKey = Lexer.pToken $ string "cl-roleset" <|> string "roleset:"
seqmark :: CharParser st Id.Token
seqmark = Lexer.pToken $ reserved reservedelement2 $ scanSeqMark
identifier :: CharParser st Id.Token
identifier = Lexer.pToken $ reserved reservedelement $ scanClWord
scanSeqMark :: CharParser st String
scanSeqMark = do
sq <- string "..."
w <- many clLetter <?> "sequence marker"
return $ sq ++ w
scanClWord :: CharParser st String
scanClWord = quotedstring <|> enclosedname <|> (many1 clLetter <?> "words")
clLetters :: Char -> Bool
clLetters ch = let c = ord ch in
if c >= 33 && c <= 126 then c <= 38 && c /= 34
|| c >= 42 && c /= 64 && c /= 92
else False
clLetters2 :: Char -> Bool
clLetters2 ch = let c = ord ch in
if c >= 32 && c <= 126 then c <= 38 && c /= 34
|| c >= 42 && c /= 64 && c /= 92
else False
-- a..z, A..z, 0..9, ~!#$%^&*_+{}|:<>?`-=[];,.
clLetter :: CharParser st Char
clLetter = satisfy clLetters <?> "cl letter"
reservedelement :: [String]
reservedelement = ["=", "and", "or", "iff", "if", "forall", "exists", "not"
, "...", "cl:text", "cl:imports", "cl:excludes", "cl:module"
, "cl:comment", "roleset:"] ++ reservedcl
reservedcl :: [String]
reservedcl = ["cl-text", "cl-imports", "cl-exlcudes", "cl-module"
, "cl-comment", "cl-roleset"]
-- reserved elements for sequence marker
reservedelement2 :: [String]
reservedelement2 = ["=", "and", "or", "iff", "if", "forall", "exists", "not"
, "cl:text", "cl:imports", "cl:excludes", "cl:module"
, "cl:comment", "roleset:"]
whitec :: String
whitec = "\n\r\t\v\f "
white :: CharParser st String
white = many1 $ oneOf whitec