git @ Cat's Eye Technologies Fountain / 204075b
Flatten Seq's during parsing. Chris Pressey 1 year, 5 months ago
3 changed file(s) with 13 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
2222 Multi-character terminals.
2323
2424 Goal ::= "foo" "bar";
25 ===> Grammar [("Goal",[],Alt [Seq [Seq [Terminal 'f',Terminal 'o',Terminal 'o'],Seq [Terminal 'b',Terminal 'a',Terminal 'r']]])]
25 ===> Grammar [("Goal",[],Alt [Seq [Terminal 'f',Terminal 'o',Terminal 'o',Terminal 'b',Terminal 'a',Terminal 'r']])]
2626
2727 Alternation and recursion.
2828
4747
4848 Goal ::= "Hi" Sp<a> "there" Sp<b> "world" "!";
4949 Sp<n> ::= <. n = 0 .> { " " <. n += 1 .> } <. n > 0 .>;
50 ===> Grammar [("Goal",[],Alt [Seq [Seq [Terminal 'H',Terminal 'i'],NonTerminal "Sp" [Var "a"],Seq [Terminal 't',Terminal 'h',Terminal 'e',Terminal 'r',Terminal 'e'],NonTerminal "Sp" [Var "b"],Seq [Terminal 'w',Terminal 'o',Terminal 'r',Terminal 'l',Terminal 'd'],Terminal '!']]),("Sp",[Var "n"],Alt [Seq [Constraint (UnifyConst (Var "n") 0),Loop (Alt [Seq [Terminal ' ',Constraint (Inc (Var "n") (CInt 1))]]) [],Constraint (GreaterThan (Var "n") (CInt 0))]])]
50 ===> Grammar [("Goal",[],Alt [Seq [Terminal 'H',Terminal 'i',NonTerminal "Sp" [Var "a"],Terminal 't',Terminal 'h',Terminal 'e',Terminal 'r',Terminal 'e',NonTerminal "Sp" [Var "b"],Terminal 'w',Terminal 'o',Terminal 'r',Terminal 'l',Terminal 'd',Terminal '!']]),("Sp",[Var "n"],Alt [Seq [Constraint (UnifyConst (Var "n") 0),Loop (Alt [Seq [Terminal ' ',Constraint (Inc (Var "n") (CInt 1))]]) [],Constraint (GreaterThan (Var "n") (CInt 0))]])]
5151
5252 Comments.
5353
6161 //
6262 // There are many ways to place comments.
6363 "//";
64 ===> Grammar [("Goal",[],Alt [Seq [Seq [Terminal 'f',Terminal 'o',Terminal 'o']]]),("A",[],Alt [Seq [Terminal 'f',Terminal 'o',Seq [Terminal '/',Terminal '/']]])]
64 ===> Grammar [("Goal",[],Alt [Seq [Terminal 'f',Terminal 'o',Terminal 'o']]),("A",[],Alt [Seq [Terminal 'f',Terminal 'o',Terminal '/',Terminal '/']])]
6565
6666 Misplaced semicolon is a syntax error.
6767
3434
3535 expr1 = do
3636 es <- many1 term
37 return $ Seq es
37 return $ Seq $ flattenseq es
38
39 flattenseq [] = []
40 flattenseq (s:ss) = case s of
41 -- Not just flatten Seq but also remove 1-element Alt's.
42 -- Note that xs will always be flat already
43 Seq xs -> xs ++ (flattenseq ss)
44 Alt [x] -> (flattenseq [x]) ++ (flattenseq ss)
45 _ -> (s:flattenseq ss)
3846
3947 term = (try parenExpr) <|> (try loopExpr) <|> (try constraintExpr) <|> (try terminal) <|> nonterminal
4048
2323 (Loop expr' constraints):(preprocessSeq rest')
2424 preprocessSeq (expr:rest) =
2525 (preprocessExpr expr):(preprocessSeq rest)
26 preprocessExpr (Alt [expr]) = Seq [preprocessExpr expr]
26 preprocessExpr (Alt [expr]) = Seq [preprocessExpr expr] -- FIXME: flatten Alts in a more thorough way
2727 preprocessExpr (Alt exprs) = Alt (map preprocessExpr exprs)
2828 preprocessExpr (Loop expr _) = error "Cannot preprocess Loop that is not in Seq"
2929 preprocessExpr other = other