-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParser.hs
More file actions
90 lines (73 loc) · 2.53 KB
/
Copy pathParser.hs
File metadata and controls
90 lines (73 loc) · 2.53 KB
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
module Parser where
import KnuthBendixCompletion.Datatypes
import Text.ParserCombinators.Parsec
translate :: String -> Term
translate ('(':text) = (translate.takeBrackets) ('(':text)
translate text =
if (length.betterWords) text == 1
then Var text
else Func funcName (map translate funcArgs) where funcName:funcArgs = betterWords text
takeBrackets :: [Char] -> [Char]
takeBrackets text = tb text 0 where
tb :: [Char] -> Int -> [Char]
tb [] _ = []
tb ('(':xs) 0 = tb xs 1
tb ('(':xs) n = '(':tb xs (n+1)
tb (')':xs) 1 = tb xs 0
tb (')':xs) n = ')':tb xs (n-1)
tb (x:xs) n = x:tb xs n
betterWords :: [Char] -> [[Char]]
betterWords text = bw text [] 0 where
bw :: [Char] -> [Char] -> Int -> [[Char]]
bw (' ':xs) tmpWord 0 = tmpWord:bw xs [] 0
bw ('(':xs) tmpWord 0 = bw xs tmpWord 1
bw ('(':xs) tmpWord n = bw xs (tmpWord ++ ['(']) (n+1)
bw (x:xs) tmpWord 0 = bw xs (tmpWord ++ [x]) 0
bw (')':xs) tmpWord 1 = bw xs tmpWord 0
bw (')':xs) tmpWord n = bw xs (tmpWord ++ [')']) (n-1)
bw (x:xs) tmpWord n = bw xs (tmpWord ++ [x]) n
bw [] tmpWord n = [tmpWord]
parseAxiom :: String -> Either ParseError Axiom
parseAxiom input = parse axiomParser "(unknown)" input
parseReductionRule :: String -> Either ParseError ReductionRule
parseReductionRule input = parse reductionRuleParser "(unknown)" input
parseTerm :: String -> Either ParseError Term
parseTerm input = parse termParser "(unknown)" input
convertError :: Either ParseError a -> Either String a
convertError (Left e) = (Left $ show e)
convertError (Right a) = (Right a)
axiomParser :: GenParser Char st Axiom
axiomParser = do
lhs <- termParser
string "<->"
rhs <- termParser
return $ Axiom (lhs,rhs)
reductionRuleParser :: GenParser Char st ReductionRule
reductionRuleParser = do
parsedRule <- termParser
string "->"
parsedResult <- termParser
return $ ReductionRule {rule=parsedRule,result=parsedResult}
termParser :: GenParser Char st Term
termParser = do
result <- try (funcParser) <|> varParser
return $ result
varParser :: GenParser Char st Term
varParser = do
name <- nameParser
return $ Var name
funcParser :: GenParser Char st Term
funcParser = do
name <- nameParser
char '('
args <- argsParser
char ')'
return $ Func name args
argsParser :: GenParser Char st [Term]
argsParser = do
args <- sepBy termParser (char ',')
return $ args
nameParser :: GenParser Char st String
nameParser = do
result <- many1 (noneOf ",()\n")
return $ result