Repository navigation
Expand file tree
/
Copy pathParser.hs
More file actions
70 lines (59 loc) · 2.64 KB
/
Copy pathParser.hs
File metadata and controls
70 lines (59 loc) · 2.64 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
module DobadoBots.Interpreter.Parser(
parseScript
) where
import DobadoBots.Interpreter.Data (Cond(..), ActionToken(..),
SensorToken(..), LogicExpr(..),
CmpInteger(..), Collider(..))
import Data.Text (unpack, Text(..))
import Text.ParserCombinators.Parsec (CharParser, ParseError, (<|>),
string, spaces, parse, many1,
digit, newline, char, try)
scriptFile :: CharParser () Cond
scriptFile = Token <$> actionParser
<|> conditionParser
actionParser :: CharParser () ActionToken
actionParser = MoveForward <$ string "moveForward"
<|> try (TurnLeft <$ string "turnLeft")
<|> try (TurnRight <$ string "turnRight")
<|> FaceObjective <$ string "faceObjective"
<|> ChangeObjective <$ string "changeObjective"
sensorParser :: CharParser () SensorToken
sensorParser = try (LaserDistance <$ string "laserDistance")
<|> ObjectiveDistance <$ string "objectiveDistance"
<|> try (LaserScan <$ string "laserScan")
cmpIntegerParser :: CharParser () (CmpInteger SensorToken)
cmpIntegerParser = op '=' Eq
<|> op '<' Sup
<|> op '>' Inf
where
op c cons = try (cons <$>
(spaces *> sensorParser)
<*
(spaces *> char c)
<*>
(spaces *> (read <$> many1 digit)))
colliderParser :: CharParser () Collider
colliderParser = try (Obstacle <$ string "obstacle")
<|> try (Objective <$ string "objective")
<|> try (Wall <$ string "wall")
<|> try (Robot <$ string "robot")
cmpCollider :: CharParser () LogicExpr
cmpCollider = CmpCollider <$>
(spaces *> sensorParser)
<*>
(spaces *> string "=" *> spaces *> colliderParser)
logicExprParser :: CharParser () LogicExpr
logicExprParser = try cmpCollider
<|> try (CmpLogicInt <$> cmpIntegerParser)
conditionParser :: CharParser () Cond
conditionParser = Cond <$>
((spaces *> string "IF") *> logicExprParser <* newline)
<*>
(spaces *> parseNested <* newline)
<*>
((spaces *> string "ELSE") *> (newline *> (spaces *> parseNested)))
where
parseNested = Token <$> actionParser
<|> conditionParser
parseScript :: Text -> Either ParseError Cond
parseScript script = parse scriptFile "Error while parsing script: " $ unpack script