Ver código fonte

start parsing more complicated expressions

Getty Ritter 3 anos atrás
pai
commit
8140d0ac42
7 arquivos alterados com 167 adições e 39 exclusões
  1. 49 22
      src/grammar.lalrpop
  2. 1 1
      src/main.rs
  3. 30 8
      tests/assn.parsed
  4. 15 4
      tests/atom_lit.parsed
  5. 2 0
      tests/exprs.matzo
  6. 55 0
      tests/exprs.parsed
  7. 15 4
      tests/num_lit.parsed

+ 49 - 22
src/grammar.lalrpop

@@ -5,6 +5,10 @@ grammar;
 match {
     "<",
     ">",
+    "(",
+    ")",
+    "{",
+    "}",
     "|",
     ":",
     ",",
@@ -23,40 +27,63 @@ match {
 use crate::ast::*;
 
 pub Stmts: Vec<Stmt> = {
-  <mut stmts:(<Stmt> ";")*> <stmt:Stmt?> => match stmt {
-    None => stmts,
-    Some(stmt) => {
-      stmts.push(stmt);
-      stmts
-    }
-  },
+    <mut stmts:(<Stmt> ";")*> <stmt:Stmt?> => match stmt {
+        None => stmts,
+        Some(stmt) => {
+            stmts.push(stmt);
+            stmts
+        }
+    },
 };
 
 pub Stmt: Stmt = {
-  "puts" <Expr> => Stmt::Puts(<>),
-  <Name> ":=" <Expr> => Stmt::Assn(<>),
+    "puts" <Expr> => Stmt::Puts(<>),
+    <Name> ":=" <Expr> => Stmt::Assn(<>),
 };
 
 pub Name: String = {
-  r"[a-z][A-Za-z0-9_-]*" => <>.to_owned(),
+    r"[a-z][A-Za-z0-9_-]*" => <>.to_owned(),
 };
 
 pub Expr: Expr = {
-  <Literal> => Expr::Lit(<>),
-  <Name> => Expr::Var(<>),
-  "<" <mut es:(<Expr> ",")*> <e:Expr> ">" => {
-    es.push(e);
-    Expr::Tup(es)
-  },
-  "let" <name:Name> ":=" <e1:Expr> "in" <e2:Expr> =>
-    Expr::Let(name, Box::new(e1), Box::new(e2)),
+    <mut ts:(<Choice> "|")*> <t:Choice> => {
+        ts.push(t);
+        Expr::Chc(ts)
+    }
+};
+
+pub Choice: Choice = {
+    <weight:Num> ":" <value:Term> => Choice {
+        weight: Some(weight),
+        value
+    },
+    <value:Term> => Choice {
+        weight: None,
+        value
+    }
+};
+
+pub Term: Expr = {
+    (<Leaf>)* => Expr::Cat(<>),
+};
+
+pub Leaf: Expr = {
+    <Literal> => Expr::Lit(<>),
+    <Name> => Expr::Var(<>),
+    "<" <mut es:(<Expr> ",")*> <e:Expr> ">" => {
+        es.push(e);
+        Expr::Tup(es)
+    },
+    "let" <name:Name> ":=" <e1:Expr> "{" <e2:Expr> "}" =>
+        Expr::Let(name, Box::new(e1), Box::new(e2)),
+    "(" <e:Expr> ")" => e,
 };
 
 pub Num: i64 = {
-  r"[0-9]+" => i64::from_str(<>).unwrap(),
+    r"[0-9]+" => i64::from_str(<>).unwrap(),
 };
 
 pub Literal: Literal = {
-  <Num> => Literal::Num(<>),
-  r"[A-Z][A-Za-z0-9_-]*" => Literal::Atom(<>.to_owned()),
-};
+    <Num> => Literal::Num(<>),
+    r"[A-Z][A-Za-z0-9_-]*" => Literal::Atom(<>.to_owned()),
+};

+ 1 - 1
src/main.rs

@@ -1,3 +1,3 @@
 fn main() {
-    println!("{:?}", matzo::grammar::StmtsParser::new().parse("puts 55"));
+    println!("{:?}", matzo::grammar::StmtsParser::new().parse("puts Foo Bar | Baz"));
 }

+ 30 - 8
tests/assn.parsed

@@ -1,18 +1,40 @@
 [
     Assn(
         "x",
-        Lit(
-            Num(
-                5,
-            ),
+        Chc(
+            [
+                Choice {
+                    weight: None,
+                    value: Cat(
+                        [
+                            Lit(
+                                Num(
+                                    5,
+                                ),
+                            ),
+                        ],
+                    ),
+                },
+            ],
         ),
     ),
     Assn(
         "y",
-        Lit(
-            Atom(
-                "This",
-            ),
+        Chc(
+            [
+                Choice {
+                    weight: None,
+                    value: Cat(
+                        [
+                            Lit(
+                                Atom(
+                                    "This",
+                                ),
+                            ),
+                        ],
+                    ),
+                },
+            ],
         ),
     ),
 ]

+ 15 - 4
tests/atom_lit.parsed

@@ -1,9 +1,20 @@
 [
     Puts(
-        Lit(
-            Atom(
-                "Foo",
-            ),
+        Chc(
+            [
+                Choice {
+                    weight: None,
+                    value: Cat(
+                        [
+                            Lit(
+                                Atom(
+                                    "Foo",
+                                ),
+                            ),
+                        ],
+                    ),
+                },
+            ],
         ),
     ),
 ]

+ 2 - 0
tests/exprs.matzo

@@ -0,0 +1,2 @@
+puts This That;
+puts This | That;

+ 55 - 0
tests/exprs.parsed

@@ -0,0 +1,55 @@
+[
+    Puts(
+        Chc(
+            [
+                Choice {
+                    weight: None,
+                    value: Cat(
+                        [
+                            Lit(
+                                Atom(
+                                    "This",
+                                ),
+                            ),
+                            Lit(
+                                Atom(
+                                    "That",
+                                ),
+                            ),
+                        ],
+                    ),
+                },
+            ],
+        ),
+    ),
+    Puts(
+        Chc(
+            [
+                Choice {
+                    weight: None,
+                    value: Cat(
+                        [
+                            Lit(
+                                Atom(
+                                    "This",
+                                ),
+                            ),
+                        ],
+                    ),
+                },
+                Choice {
+                    weight: None,
+                    value: Cat(
+                        [
+                            Lit(
+                                Atom(
+                                    "That",
+                                ),
+                            ),
+                        ],
+                    ),
+                },
+            ],
+        ),
+    ),
+]

+ 15 - 4
tests/num_lit.parsed

@@ -1,9 +1,20 @@
 [
     Puts(
-        Lit(
-            Num(
-                55,
-            ),
+        Chc(
+            [
+                Choice {
+                    weight: None,
+                    value: Cat(
+                        [
+                            Lit(
+                                Num(
+                                    55,
+                                ),
+                            ),
+                        ],
+                    ),
+                },
+            ],
         ),
     ),
 ]