git @ Cat's Eye Technologies Fountain / 6145b0a
Start moving from locals to params. Chris Pressey 1 year, 10 months ago
5 changed file(s) with 33 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
1515 grammar describe the concrete structure of tokens.
1616
1717 Grammar ::= {Production}.
18 Production ::= NonTerminal "::=" {"local" Variable ":"} {Expr0}.
18 Production ::= NonTerminal [Formals] "::=" {Expr0}.
1919 Expr0 ::= Expr1 {"|" Expr1}.
2020 Expr1 ::= Term {Term}.
2121 Term ::= "{" Expr0 "}"
2222 | "(" Expr0 ")"
2323 | "<." Constraint ".>"
2424 | Terminal
25 | NonTerminal.
25 | NonTerminal [Actuals].
26 Formals ::= "<" Variable {"," Variable} ">".
27 Actuals ::= "<" VarExpr {"," VarExpr} ">".
28 VarExpr ::= Variable | IntLit.
2629 Constraint ::= Variable Constrainer.
2730 Constrainer ::= "=" (Variable | IntLit)
2831 | "+=" IntLit
2932 | "-=" IntLit
3033 | ">" IntLit
3134 | "<" IntLit.
32 NonTerminal ::= <<upper>><<alphanumeric>>*
33 Terminal ::= <<">><<any>>+<<">>
35 NonTerminal ::= <<upper>><<alphanumeric>>*.
36 Terminal ::= <<">><<any except ">>+<<">>.
3437
3538 The Tests
3639 ---------
120123 <=== aaabbccc
121124 ???> Failure
122125
123 ### Parsing with local variables
126 ### Parsing with parameters
124127
125 Goal ::= "Hi" Sp "there" Sp "world" "!";
126 Sp ::= local n: <. n = 0 .> { " " <. n += 1 .> } <. n > 0 .>;
128 Goal ::= "Hi" Sp<a> "there" Sp<b> "world" "!";
129 Sp<n> ::= <. n = 0 .> { " " <. n += 1 .> } <. n > 0 .>;
127130 <=== Hi there world!
128131 ===> Success
129132
130 Goal ::= "Hi" Sp "there" Sp "world" "!";
131 Sp ::= local n: <. n = 0 .> { " " <. n += 1 .> } <. n > 0 .>;
133 Goal ::= "Hi" Sp<a> "there" Sp<b> "world" "!";
134 Sp<n> ::= <. n = 0 .> { " " <. n += 1 .> } <. n > 0 .>;
132135 <=== Hi there world!
133136 ===> Success
134137
135 Goal ::= "Hi" Sp "there" Sp "world" "!";
136 Sp ::= local n: <. n = 0 .> { " " <. n += 1 .> } <. n > 0 .>;
138 Goal ::= "Hi" Sp<a> "there" Sp<b> "world" "!";
139 Sp<n> ::= <. n = 0 .> { " " <. n += 1 .> } <. n > 0 .>;
137140 <=== Hi thereworld!
138141 ???> Failure
139142
4343 ;
4444 ===> Grammar [("Goal",[],Alt [Seq [Constraint (UnifyConst (Var "a") 0),Loop (Alt [Seq [Terminal 'a',Constraint (Inc (Var "a") 1)]]) [],Constraint (UnifyVar (Var "a") (Var "n")),Constraint (UnifyConst (Var "b") 0),Loop (Alt [Seq [Terminal 'b',Constraint (Inc (Var "b") 1)]]) [],Constraint (UnifyVar (Var "b") (Var "n")),Constraint (UnifyConst (Var "c") 0),Loop (Alt [Seq [Terminal 'c',Constraint (Inc (Var "c") 1)]]) [],Constraint (UnifyVar (Var "c") (Var "n"))]])]
4545
46 Locals and multiple productions.
46 Parameters and multiple productions.
4747
48 Goal ::= "Hi" Sp "there" Sp "!";
49 Sp ::= local n: { " " <. n += 1 .> } <. n > 0 .>;
50 ===> Grammar [("Goal",[],Alt [Seq [Seq [Terminal 'H',Terminal 'i'],NonTerminal "Sp",Seq [Terminal 't',Terminal 'h',Terminal 'e',Terminal 'r',Terminal 'e'],NonTerminal "Sp",Terminal '!']]),("Sp",[Var "n"],Alt [Seq [Loop (Alt [Seq [Terminal ' ',Constraint (Inc (Var "n") 1)]]) [],Constraint (GreaterThan (Var "n") 0)]])]
48 Goal ::= "Hi" Sp<a> "there" Sp<b> "world" "!";
49 Sp<n> ::= <. n = 0 .> { " " <. n += 1 .> } <. n > 0 .>;
50 ===> Grammar [("Goal",[],Alt [Seq [Seq [Terminal 'H',Terminal 'i'],NonTerminal "Sp",Seq [Terminal 't',Terminal 'h',Terminal 'e',Terminal 'r',Terminal 'e'],NonTerminal "Sp",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") 1)]]) [],Constraint (GreaterThan (Var "n") 0)]])]
5151
5252 ### Preprocessing
5353
2424 startSymbol (Grammar ((term, _, _) : _)) = term
2525 startSymbol (Grammar []) = error "No productions in grammar"
2626
27 production nt (Grammar ((term, locals, expr) : rest)) =
27 production nt (Grammar ((term, formals, expr) : rest)) =
2828 if term == nt then expr else production nt (Grammar rest)
2929 production nt (Grammar []) = error ("Production '" ++ nt ++ "' not found")
1212
1313 prod = do
1414 nt <- capWord
15 p <- option [] formals
1516 keyword "::="
16 l <- many local
1717 e <- expr0
1818 keyword ";"
19 return (nt, l, e)
19 return (nt, p, e)
2020
21 local = do
22 keyword "local"
23 v <- variable
24 keyword ":"
21 formals = do
22 keyword "<"
23 v <- sepBy (variable) (keyword ",")
24 keyword ">"
2525 return v
2626
2727 expr0 = do
102102
103103 nonterminal = do
104104 s <- capWord
105 a <- option [] actuals
105106 return $ NonTerminal s
107
108 actuals = do
109 keyword "<"
110 v <- sepBy (variable) (keyword ",")
111 keyword ">"
112 return v
106113
107114 --
108115 -- Low level: Concrete things
88 preprocessGrammar :: Grammar -> Grammar
99 preprocessGrammar (Grammar productions) =
1010 let
11 productions' = map (\(term, locals, expr) -> (term, locals, preprocessExpr expr)) productions
11 productions' = map (\(term, formals, expr) -> (term, formals, preprocessExpr expr)) productions
1212 in
1313 Grammar productions'
1414