Skip to content

Commit

Permalink
Improve state graph parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
bugarela committed Jul 21, 2022
1 parent ed1118a commit 48f0374
Show file tree
Hide file tree
Showing 4 changed files with 3,856 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Math.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ atom =

number :: P.Parser Value
number = do
n <- numberLiteral
return (Lit n)

numberLiteral :: P.Parser Lit
numberLiteral = do
digits <- P.many1 P.digit
let n = foldl (\x d -> 10 * x + toInteger (digitToInt d)) 0 digits
ws
return (Lit (Num n))
return (Num n)
28 changes: 26 additions & 2 deletions Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ parseTla a = do f <- readFile a
return (left show e)

parseState s = parse action ("Error parsing " ++ s ++ ":") s
parseValue s = parse value ("Error parsing " ++ s ++ ":") s

parseTrace t = parse trace ("Error parsing " ++ t ++ ":") t

Expand Down Expand Up @@ -323,7 +324,7 @@ value = do try $ do string "Cardinality("
<|>
do try $ do {e <- arithmeticExpression; ws; return (e)}
<|>
do {literal;}
do {l <- lit; return (Lit l)}

set = do try $ do {s1 <- atomSet; string "\\cup"; ws; s2 <- set; ws; return (Union s1 s2)}
<|>
Expand Down Expand Up @@ -371,6 +372,12 @@ record = do try $ do char '['
char ']'
ws
return (Record ms)
<|>
do try $ do char '('
ms <- recEntry `sepBy` try (string "@@ ")
char ')'
ws
return (Record ms)
<|>
do char '['
i <- identifier
Expand All @@ -388,6 +395,23 @@ record = do try $ do char '['
ws
return (Except i [(Ref k, v)])

literal = do try $ do {char '\"'; cs <- many1 (noneOf reserved); char '\"'; ws; return (Lit (Str cs))}
recEntry = do try $ do l <- lit
ws
string ":>"
ws
a <- lit
ws
return (Key l, (Lit a))

lit = do try $ do stringLiteral
<|>
do try $ do numberLiteral
<|>
do try $ do {string "TRUE"; return (Boolean True)}
<|>
do try $ do {string "FALSE"; return (Boolean False)}

stringLiteral = do try $ do {char '\"'; cs <- many1 (noneOf reserved); char '\"'; ws; return (Str cs)}
literal = do try $ do {s <- stringLiteral; return (Lit s)}

arithmeticExpression = Math.build
9 changes: 8 additions & 1 deletion StateGraphParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Data.List
import GHC.Generics
import System.Environment

import Head as H
import Elixir
import Helpers
import Parser
Expand Down Expand Up @@ -83,9 +84,15 @@ findNode ns n =
toMap :: Node -> Either String String
toMap Node {nodeId = _, label = l} =
case parseState (unescape l) of
Right a -> Right (initialState [] a)
Right a -> Right (initialState [] (toValue a))
Left e -> Left (show e)

toValue :: H.Action -> H.Value
toValue (H.ActionAnd as) = H.And (map toValue as)
toValue (H.Condition v) = v
toValue (H.ActionCall i ps) = H.ConditionCall i ps
toValue a = error("Not a value: " ++ show a)

unescape :: String -> String
unescape [] = []
unescape [s] = [s]
Expand Down
Loading

0 comments on commit 48f0374

Please sign in to comment.